<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Islam Rustamov on Medium]]></title>
        <description><![CDATA[Stories by Islam Rustamov on Medium]]></description>
        <link>https://medium.com/@islamrustamov?source=rss-89fb6d7e8486------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*1JhshXQNbOAxGh17o5jXjQ.png</url>
            <title>Stories by Islam Rustamov on Medium</title>
            <link>https://medium.com/@islamrustamov?source=rss-89fb6d7e8486------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 02:29:24 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@islamrustamov/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Solari/Flip animation effect in React Native]]></title>
            <link>https://medium.com/@islamrustamov/solari-flip-animation-effect-in-react-native-1641b29888e9?source=rss-89fb6d7e8486------2</link>
            <guid isPermaLink="false">https://medium.com/p/1641b29888e9</guid>
            <category><![CDATA[react-native]]></category>
            <category><![CDATA[react-native-reanimated]]></category>
            <category><![CDATA[animation]]></category>
            <dc:creator><![CDATA[Islam Rustamov]]></dc:creator>
            <pubDate>Fri, 23 Oct 2026 12:25:00 GMT</pubDate>
            <atom:updated>2026-10-23T12:25:00.440Z</atom:updated>
            <content:encoded><![CDATA[<p>Recently on my job I’ve got a task to implement component with this animation:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/400/1*W4BkLe5wSdAJtvq7hQneBw.gif" /><figcaption>le fortinaiti</figcaption></figure><p>Naturally I started googling for ready-to-use solutions and sadly didn’t find any. So I decided to implement this thing myself.</p><blockquote>NOTE: I do not think that my solution is the bestest solution out there, so if you have other ideas on the implementation — feel free to share.</blockquote><h4>Understanding the animation concept</h4><p>Achieving this animation in Figma is somewhat easy, but doing something like that with code — not really. We need to understand how to trick our code into imitating this animation, so after some time I came up with this approach:</p><ol><li>We render 4 pictures. Two on top, two on bottom. Two in front, two in the back;</li><li>We don’t render full images, we render only halves of these images;</li><li>We “flip” the top picture and hide it once it reached the center of the component;</li><li>After that we show the bottom picture and “flip” it to achieve this animation that looks like it’s one picture being flipped.</li></ol><h4>The code</h4><p>Yea yea, lot’s of cool words, just show me the code.</p><p>We will start with the parent container of the flipping animation component that is going to hold some logic, that is going to be passed down to the main component with animation:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6121a07011bc7e4adc2ecbffe6956ac2/href">https://medium.com/media/6121a07011bc7e4adc2ecbffe6956ac2/href</a></iframe><p>Now onto the <strong>SolariImage</strong> component itself with main animation logic:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3f6f4b1ca02b1cbdecf5092407400194/href">https://medium.com/media/3f6f4b1ca02b1cbdecf5092407400194/href</a></iframe><p>That is basically it. I didn’t find this animation eating up FPS or RAM or anything, so it’s pretty much optimized (I think?):</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/486/1*eftMfqGwO6IWQkgw6Ci1BQ.png" /></figure><p>Thanks for reading and feel free to share your ideas about other approaches of implemetation of this animation.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1641b29888e9" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How I became a somewhat open source contributor and how You can do that too]]></title>
            <link>https://medium.com/@islamrustamov/how-i-became-a-somewhat-open-source-contributor-and-how-you-can-do-that-too-09ecba7dee93?source=rss-89fb6d7e8486------2</link>
            <guid isPermaLink="false">https://medium.com/p/09ecba7dee93</guid>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[open-source-contribution]]></category>
            <category><![CDATA[open-source]]></category>
            <dc:creator><![CDATA[Islam Rustamov]]></dc:creator>
            <pubDate>Wed, 07 Jan 2026 20:41:38 GMT</pubDate>
            <atom:updated>2026-01-07T20:41:38.847Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*b2GG3tGJXIpHzUXRry5E_Q.jpeg" /><figcaption>Thanks to <a href="https://unsplash.com/@5tep5">Alexander Popov</a> for this picture: <a href="https://unsplash.com/@5tep5">https://unsplash.com/@5tep5</a></figcaption></figure><p>For a long period of time I thought that being an open source contributor is for “those” type of programmers who are “sick” with programming and “programming is their life” and they are “waaaay too invested” into it and I thought that I am not going to achieve that level in a long period of time.</p><p>Gladly I was wrong about that.</p><h3>My very first useful contribution</h3><p>Once upon a time I encountered a problem on my job where our mobile application was re-downloading an <strong>HLS</strong> video every time we were watching it on Android. It is not a nice thing to download those <strong>.ts</strong> segments again and again over the same video so I was tasked with finding a solution.</p><p>The most obvious solution is — caching. BUT! It’s a <strong>React Native</strong> application and to watch the video we’ve been using <a href="https://github.com/TheWidlarzGroup/react-native-video">react-native-video</a> package which didn’t support caching for <strong>HLS</strong> videos (it does support caching of other types of videos, but no <strong>HLS</strong>).</p><p>The first thing that might come to a mind of a not-so-great developer is to tell the manager “uuuh the library that we use doesn’t really support the caching so it’s unsolvable”.</p><h4>How I solved the problem</h4><p>I — am a man with little-to-no knowledge of native <strong>Android</strong> development, <strong>Java</strong> and <strong>Kotlin</strong>.</p><p>I — have no idea what to do.</p><p>So I decided to google 2 things:</p><ol><li>How to cache <strong>HLS</strong> videos on <strong>Android</strong> (specifically with <strong>ExoPlayer</strong>);</li><li>How and where to write code in the library so it will support caching of <strong>HLS</strong> videos.</li></ol><p>That’s it. These 2 things were enough for me to find a solution. Specifically, I found out that you can use <strong>SimpleCache</strong> to cache <strong>HLS</strong> videos on <strong>ExoPlayer</strong> AND I also found the next part in the library’s code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6b797b421dca7ba18131093efd209ac5/href">https://medium.com/media/6b797b421dca7ba18131093efd209ac5/href</a></iframe><p>What this part of code does — it sets up a part of the <strong>Media Source</strong> object which is later getting passed further into the player. And after some googling I found out that this is the exact place where I need to set up caching, because using <strong>mediaDataSourceFactory</strong> isn’t going to work.</p><p>So what I did was next:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5ea3e5d681db67cbb9f34676b927e436/href">https://medium.com/media/5ea3e5d681db67cbb9f34676b927e436/href</a></iframe><p>Now, I do whatever that is with <strong>SimpleCache</strong> and if I want the <strong>HLS</strong> video to be cached (<strong>useCache</strong>) then I pass the correct <strong>dataSourceFactory</strong>.</p><p>This will lead to <strong>HLS</strong> videos getting cached on <strong>Android</strong>.</p><h4>And after that</h4><p>I thought to myself “I can actually now take this code, fork original library and create a PR with this code into original library”. And that’s what I did. And after some time:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KpdrARn6qEIUddBYPcXjrQ.png" /><figcaption>big success</figcaption></figure><p>This event has boosted my desire to do open source contributions, because with that you are not just improving your skills as developer, but also create a stronger portfolio and getting a chance to become a part of a new release of any library out there.</p><p>After my very first contribution that was released in a new version of a library I had plenty of other experiences creating fixes, new features and issues for lots of other libraries.</p><h3>Second example</h3><p>At some point I stumbled across this library <a href="https://github.com/software-mansion/react-native-enriched">react-native-enriched</a> and I instantly got interested in it since it is made by <strong>Software Mansion</strong> and I also created a <strong>React Native</strong> rich text input myself.</p><p>So, since I had some knowledge about creating native text input components in <strong>React Native</strong> I instantly thought to myself that maybe I can be a good contributor to this library.</p><p>I browsed through issues and found this issue:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*g3lbOaHXjJr3V7H64YEznw.png" /></figure><p>This issue asked for a really simple feature to be implemented and I instantly got the desire to implement it.</p><p>Not so long after that, I did open a PR:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ReJDQkqwiStbO9NebukhjA.png" /></figure><p>And then:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fRheg3J7a-SwZJblmvfiVw.png" /></figure><p>Boom, that’s it. Yet another contribution made.</p><h3>What I learnt from one year of open source contributions and how to do it yourself</h3><h4>Approach 1</h4><p>You need to know that finding a thing to contribute is never an easy and quick task. When you visit the github of a library that you use — you might see lots of issues that are too complicated for you, since you don’t know the insides of the library. So the first thing to do is to:</p><blockquote>Look for the easiest issues to take</blockquote><p>Don’t pick up the biggest feature requests/bugs. You might be overwhelmed by the complexity of it and in the end you lose your motivation and put away the desire to contribute for some time (well, if you are capable of doing complicated contributions then you are welcome to do so).</p><p>So, for me, the steps are usually next:</p><ol><li>Browse across several libraries that I am interested in;</li><li>Browse their issues;</li><li>Search for the easiest issues/issues that I’m confident I will be able to deal with;</li><li>Do what’s being asked in the issue.</li></ol><p>This approach might take some time since you will not always find the issues that you want to work with.</p><h4>Approach 2</h4><p>The other way that really helped me a lot to contribute to different libraries is next one:</p><blockquote>On my project that I work on there were a lot of cases when libraries that we used were having bugs or were lacking some functionality. So what we did — we were patching those libraries. BUT! What if you won’t just patch the library, but also open a PR to that same library you just patched and ask the maintainers to take a look at it?</blockquote><p>If you are an experienced <strong>JS</strong> developer you have most likely stumbled upon patching. Try to take a look at your current patches on the project you work on— maybe some of them can be contributed to the libraries. They won’t necessarily be accepted, but at least you tried, which is also a good thing for your self development as a programmer.</p><p>So, the steps are next:</p><ol><li>Take a look at your project’s patches;</li><li>Check what patches can be contributed (since patches can often be hacks that you are too embarrassed to contribute);</li><li>Contribute them.</li></ol><p>This approach can be used only on projects that actually has patches, but I believe that any experienced <strong>JS</strong> developer, who worked on a project that sees the light of production stage, has some patches in their projects.</p><p>You also need to know that finding bugs in libraries and creating issues for these bugs also considered a contribution. Requesting useful features is also a form of open source contribution.</p><p>That’s pretty much it. I believe that if every developer tried to contribute stuff to open source — the world would be a better place.</p><p>Sources:</p><ul><li><a href="https://github.com/software-mansion/react-native-enriched">GitHub - software-mansion/react-native-enriched: Rich Text Editor for React Native</a></li><li><a href="https://github.com/TheWidlarzGroup/react-native-video">GitHub - TheWidlarzGroup/react-native-video: A component for react-native</a></li><li><a href="https://developer.android.com/reference/androidx/media3/datasource/cache/SimpleCache">SimpleCache | API reference | Android Developers</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=09ecba7dee93" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How React Native improved from 2023 to 2025? Animation stress testing (and a little bit of Flutter)]]></title>
            <link>https://medium.com/@islamrustamov/how-react-native-improved-from-2023-to-2025-animation-stress-testing-and-a-little-bit-of-flutter-edd44297b815?source=rss-89fb6d7e8486------2</link>
            <guid isPermaLink="false">https://medium.com/p/edd44297b815</guid>
            <category><![CDATA[react-native]]></category>
            <category><![CDATA[skia]]></category>
            <category><![CDATA[react-native-reanimated]]></category>
            <category><![CDATA[flutter]]></category>
            <category><![CDATA[performance]]></category>
            <dc:creator><![CDATA[Islam Rustamov]]></dc:creator>
            <pubDate>Mon, 30 Dec 2024 18:59:05 GMT</pubDate>
            <atom:updated>2024-12-30T19:01:12.830Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xIybZWG5MOMPuw3Y4KP5VQ.jpeg" /><figcaption>Thanks to <a href="https://unsplash.com/@5tep5">Alexander Popov</a> for this picture: <a href="https://unsplash.com/@5tep5">https://unsplash.com/@5tep5</a></figcaption></figure><p>In 2023 I participated in a conference called HolyJS as a speaker, talking about such a topic as <strong>“Insides of React Native”</strong> where I also showed an interesting results of one research.</p><p>People usually compare Flutter to React Native in <em>“le animationes FPS speed who is better faster stronger more more FPS”</em> and, honestly, you don’t really need to compare these two technologies in FPS. Flutter IS going to perform better, because it uses a graphic engine inside of it, whereas React Native actually renders real native components. Of course graphic engine is going to outperfom real native components, it’s not called “graphic engine” for nothing.</p><p>But in 2023 I asked myself whether it’s possible to achieve the same FPS in React Native and I will show you some results here. You can watch my speech if you want at but it’s in Russian language:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2Fd7WKMnhv_9E%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3Dd7WKMnhv_9E&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2Fd7WKMnhv_9E%2Fhqdefault.jpg&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/1513260b82390eee22118bae3d31155b/href">https://medium.com/media/1513260b82390eee22118bae3d31155b/href</a></iframe><h3>The 2023 comparison</h3><blockquote>Note: I did lose the testing environment data and code samples, so believing me in these results is totally on you, but later in this article I will provide code samples and testing environment for new tests</blockquote><p>We are stress-testing (if I understand what this phrase means correctly) the ability to animate lots of squares on the screen. We are using Flutter’s animation stuff (don’t remember which ones (probably Tweens with AnimationController), but don’t worry, we will do this testing again a bit later in this article with more information of what was being used) and react-native-reanimated in RN.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1bTID9aKd3uFsCDm5BpPdg.png" /><figcaption>Example of what was being tested</figcaption></figure><p>The results for 100 animated elements are the same:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NM0MsvgkopXBq46cY806JQ.png" /><figcaption>left is RN, right is Flutter</figcaption></figure><p>The results for 500 animated elements was almost the same, the FPS was dropping from time to time but that’s not a big deal.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*f4VuefxWqO9MLWu5IBNMYw.png" /><figcaption>left is RN, right is Flutter</figcaption></figure><p>The results for 1500 animated elements kind of showed us that Flutter is outperforming React Native.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*eOx205Za2jMay7i4-7fxcw.png" /><figcaption>left is RN, right is Flutter</figcaption></figure><p>But now, ask yourself a question — what can we do in order to achieve better FPS in React Native?</p><p>Actually, the answer is pretty simple. If Flutter is using a graphic engine, let’s also use a graphic engine in React Native!</p><p>Gladly, we have Skia for React Native too and if we animate rotating objects through Skia (I mean render them through Skia but react-native-reanimated is still responsible for rotation animation) we will get the next result:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UIoHs96znMX0aLavqM8zCw.png" /><figcaption>left is RN, right is Flutter</figcaption></figure><p>1500 elements — React Native managed to outperform Flutter.</p><p>Buuuuuuuut React Native needed 2 additional libraries to achieve that. Is that a bad thing? It’s totally up to you to decide. This brings some disadvantages like bigger size of the app but this is not what we are testing here. Our main goal is the FPS.</p><p>I do not exactly remember if I actually switched to release mode to test this thing so I will try to definitely test the very same thing again in release mode this time.</p><h3>The new test</h3><blockquote>Giant spoiler: I am not going to test Flutter in release mode, why — read further</blockquote><p>Now, in 2024 (almost 2025) Flutter dropped new graphic engine and React Native decided to switch to new architecture completely. It’s kind of interesting — can React Native still outperfom Flutter with additional libraries?</p><p>So, onto the testing environment!</p><p><em>Testing machine: MacBook Pro Apple M1 Pro 32GB RAM</em></p><p><em>iOS Simulator: iPhone 15 Pro, iOS 17.4</em></p><p><em>Mode: Release</em></p><p><em>React Native: 0.76.5 with new Arch</em></p><p><em>React Native Reanimated: 3.16.6</em></p><p><em>React Native Skia: 1.7.7</em></p><p>We are basically going to draw lots of rotating squares on the screen. This is how it’s going to look like:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/850/1*hZzImSyi70QIAB18xNeR4Q.png" /></figure><p>Let’s start with React Native and reanimated. Here is the gist:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/befab772cb0d88ce8e8a851b525a3c06/href">https://medium.com/media/befab772cb0d88ce8e8a851b525a3c06/href</a></iframe><p>If you are too lazy to read the code, here is what’s happening:</p><ol><li>We create one shared value for rotation of all elements;</li><li>We change the <strong>SharedValue’s</strong> value with workletized functions <strong>withRepeat</strong> and <strong>withTiming</strong> for rotation;</li><li>We position squares randomly on the screen;</li><li>We render as many squares as we need.</li></ol><p>So let’s check out the tests.</p><h4><strong>500 rotating squares:</strong></h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/452/1*7W9bG89HPE6akLA2NwkpOg.png" /></figure><h4><strong>1500 rotating squares:</strong></h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/446/1*_gJer6DzMNe3YvXC_CkzmA.png" /></figure><h4><strong>3000 rotating squares:</strong></h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/448/1*smMTxWbjjyzXAtSmf7PIIQ.png" /></figure><p>That’s it. We can see that React Native cannot really handle 3000 rotating squares on the screen (let’s say that anything below 30 FPS is bad).</p><p>Now, let’s take a look at the same thing but with Skia.</p><p>The code snippet is next:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a3f6ccaa18209641db12a5a838b45d29/href">https://medium.com/media/a3f6ccaa18209641db12a5a838b45d29/href</a></iframe><p>What’s happening here:</p><ol><li>We draw canvas with Skia;</li><li>We draw rectangles on canvas with Skia;</li><li>We compute the rotation value through <strong>useDerivedValue</strong> hook and shared value with reanimated.</li></ol><p>Now let’s take a look at results.</p><h4><strong>3000 rotating squares:</strong></h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/444/1*kaKQwFYx3bHj-68auksfXA.png" /></figure><p>Uhm. What? How does it yield 60 FPS? I am looking at the screen right now and they are actually rotating pretty smooth (I am honestly quite shocked by myself to see this, last year it wasn’t show this result).</p><p>Let’s try to break it by increasing amount of squares.</p><h4>10000 rotating squares:</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/444/1*87PTDCjVos7jfDKb5_omjg.png" /></figure><p>Now that’s more like it. We see that the app is eating lot’s of memory and the FPS dropped. We finally broke it.</p><p><strong>BUT!</strong> The result is actually improved! In 2023 I’ve been getting 37 FPS for 1500 elements and in 2024 I can get 60 FPS with 3000 elements.</p><p>So, we can see that React Native with additional libraries is showing a pretty good result in imaginary situation of thousands of rotating squares on the screen, but what about Flutter? Let’s see.</p><blockquote>Okay, I wanted to run the same tests for Flutter but I just found out that for some reason I cannot run release builds on emulators in Flutter because “le emulators are not the same as real devices pick a real device and run release build on a real device”, what? How is that an issue? Why wouldn’t I be able to test release build on an emulator? Why not a single Flutter-propagandistic article, which is trash talking React Native, mentioned this issue? For God’s sake really? It now wants me to CREATE APPLE DEV ACCOUNT (WHICH IS NOT FREE) TO RUN IN RELEASE MODE! It also requires in iPhone for that (gladly I have it). This is actually pretty bad and basically stops me from full comparison.</blockquote><h3>Results</h3><p>Here’s a table showing FPS results:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Nhdgw3WbDQQRRh7_znN18Q.png" /><figcaption>empty cells were not worthy of testing since getting lower than 30 FPS is considered a bad result</figcaption></figure><p>We can see that after a year and a half (I did first testing at summer in 2023 and second testing in December 2024) the performance of React Native with Skia and Reanimated has increased a lot.</p><p>For example, in 2023 when I tested RN + Skia + Reanimated on 1500 elements I had 38 FPS, but now I get 60 FPS on 3000 elements!</p><p>I wish I could test the same thing on Flutter in Release mode, but yea, I don’t think that testing in Debug mode is worth it since the difference between release and debug can be significant.</p><p>Actually you know what, let’s actually see, it’s kind of interesting too.</p><h4>3000 elements in RN + Skia + Reanimated debug mode</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/450/1*qbJbR30wByh_O5bMDbvwRQ.png" /></figure><h4>3000 elements in Flutter (v3.27.1) debug mode</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/238/1*aCLGA63eGis9YOjLYo6Y9w.png" /></figure><p>This is actually interesting. Flutter did improve it’s rendering system. It wouldn’t yield a better result for 1500 elements in 2023. And now it’s handling 3000 elements.</p><p>So what does it mean for us? It definitely means that both React Native and Flutter improve itself with time. At least in terms of imaginary/non-realistic/fantastic tasks like rendering thousands of squares.</p><p>The main question that I wanted to answer for myself — is it possible for RN to achieve the same FPS as in Flutter — was answered. Even though I didn’t check out the optimizing techniques for Flutter animations, maybe I can improve Flutter’s animations too and achieve even better results.</p><p>By the way here is the code snippet for Flutter that I used to test (I’m not going to embed it so just click the link): <a href="https://gist.github.com/IslamRustamov/a5a4bbe70f2c36cbc0bd7de9424d0296">https://gist.github.com/IslamRustamov/a5a4bbe70f2c36cbc0bd7de9424d0296</a></p><p>I highly ask you to not consider my computations 100% correct because there are several factors that might affect the outcoming result. If you think that something was wrong or not clear — please write in the comments or contact me at <em>islamrustamov@rambler.ru.</em></p><p>Sources:</p><ul><li><a href="https://shopify.github.io/react-native-skia/">React Native Skia | React Native Skia</a></li><li><a href="https://docs.swmansion.com/react-native-reanimated/">React Native Reanimated</a></li><li><a href="https://flutter.dev/">Flutter - Build apps for any screen</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=edd44297b815" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Custom screen transition based on user touch in React Native]]></title>
            <link>https://medium.com/@islamrustamov/custom-screen-transition-based-on-user-touch-in-react-native-98f1c4c831ee?source=rss-89fb6d7e8486------2</link>
            <guid isPermaLink="false">https://medium.com/p/98f1c4c831ee</guid>
            <category><![CDATA[react-navigation]]></category>
            <category><![CDATA[reels-on-instagram]]></category>
            <category><![CDATA[react-native-animation]]></category>
            <category><![CDATA[react-native]]></category>
            <category><![CDATA[rn-screen-transition]]></category>
            <dc:creator><![CDATA[Islam Rustamov]]></dc:creator>
            <pubDate>Tue, 26 Nov 2024 13:03:51 GMT</pubDate>
            <atom:updated>2024-11-26T13:17:35.317Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*sRt_xvVlITEJxMrE" /><figcaption>Thanks to <a href="https://unsplash.com/@5tep5">Alexander Popov</a> for this picture: <a href="https://unsplash.com/@5tep5">https://unsplash.com/@5tep5</a></figcaption></figure><p>I’ve been out for over a year but now I’m back.</p><p>Recently I’ve been investigating a topic of “Instagram-like” screen transition when you press on item in the list and then get to watch reels (like in Instagram).</p><p>Check out this simple thing that I’ve done:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*cdEb0bhI3x-_8h9QToXALg.gif" /><figcaption>le fort at nite characters</figcaption></figure><p>There are several approaches to do this.</p><h4>React Navigation + React Native Reanimated + Shared Element</h4><p>This approach looks good and basically does what needs to be done but the problem here is that this shared transition is NOT production ready. So using this approach might create a lot of bugs that you probably won’t be able to fix since they are under the hood of react navigation/react native reanimated (but you will be more than welcome to fix those issues and contribute them to open source).</p><h4>react-native-shared-element</h4><p>Also a good approach, also does what needs to be done. But. There is always a but. That lib is not actively maintained/perished in the ocean of the forgotten NPM libraries. But, if you are lucky enough — this library is going to be a great solution for you to implement Instagram’s reels-like transition.</p><p>And now to the solution that I’m going to share with you.</p><h4>React Navigation’s cardStyleInterpolator</h4><p>It turns out (it’s not news) that you can customize transitions between two screens in React Navigation. There are plenty of predefined transitions, but we are going to do what I’ve shown in the GIF above.</p><p>So, what’s exactly happening?</p><ol><li>We press on the list item;</li><li>We save the X and Y of our touch;</li><li>We start the transition to a new screen from the X and Y from step 2;</li><li>We scroll to a pressed element from step 1.</li></ol><p>How can we do all of that? Let’s break it down to code snippets.</p><h4>Store</h4><p>Note: I am using Zustand for state management since it takes like 5 seconds to setup.</p><p>Here is how our store looks like:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/700099efc152343acb878f54e16c0fc9/href">https://medium.com/media/700099efc152343acb878f54e16c0fc9/href</a></iframe><p>We have one store that stores images and the other that stores the last touch. Take some time to read this code to understand it.</p><h4>MainScreen</h4><p>Here is the main screen, where we render our list of images and process our touches:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5ed749cd94064e07880aa455e2c5338e/href">https://medium.com/media/5ed749cd94064e07880aa455e2c5338e/href</a></iframe><p>Take some time to read what’s happening in this code. Basically, the next things:</p><ol><li>We render the <strong>Flatlist</strong> with images from images store;</li><li>We handle the press on the list item, where we also store the X and Y of our touch (<strong>pageX</strong> and <strong>pageY</strong> are representing exactly the X and Y of touch on the whole screen. There are also other coordinates that can be passed in this event, we don’t need them);</li><li>We also pass index of the item that we pressed on, it’s needed for autoscroll;</li><li>We are navigating to the next screen. Note that we save the coordinates FIRST and navigate only after that. It’s important.</li></ol><h4>ReelsScreen</h4><p>Now the reels screen itself:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0a7fa225f4e8acdd473df7e756865bd5/href">https://medium.com/media/0a7fa225f4e8acdd473df7e756865bd5/href</a></iframe><p>What’s happening here:</p><ol><li>We render the list of images, but with different styles and vertically, not horizontally;</li><li>We also need to specify <strong>getItemLayout</strong>, because our list needs to autoscroll and to do that — it needs to know exactly the sizes of the list elements;</li><li>In <strong>useEffect</strong> we do the autoscrolling thing.</li></ol><p>That’s it.</p><h4>Navigation</h4><p>Now this is the main part. Take a look at the code below:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b2497b4cf2dea57d1113f56aa28e01cf/href">https://medium.com/media/b2497b4cf2dea57d1113f56aa28e01cf/href</a></iframe><p>Take your time to read it and understand what’s going on.</p><p>What’s happening in this screen is this:</p><ol><li>We define the navigator;</li><li>We create the <strong>screenOptions</strong> prop;</li><li>We created a separate function called <strong>cardStyleInterpolator</strong>, where we pass our <strong>touchX</strong> and <strong>touchY</strong>, that we got from the main screen;</li></ol><p>Now let’s pause here for a sec and break down what’s happening in <strong>cardStyleInterpolator</strong>. There are a lot of things that we can do with it, you can check out the docs if you want to, but what we are interested in is that this function returns the <strong>cardStyle</strong> object. It’s a regular object that has not-so-regular stuff inside of it. You see, this <strong>cardStyleInterpolator</strong> is having such an arguments as <strong>current</strong> and <strong>next</strong> which are actually Animated nodes. If you are more familiar with Reanimated — these are something like shared values. I mean, they change from one value to another and allow us to implement different animations with this.</p><p>Now, if we take a look in the <strong>transform</strong> sub-object — we will see that it has 3 fields:</p><ol><li><strong>translateX;</strong></li><li><strong>translateY;</strong></li><li><strong>scale.</strong></li></ol><p>What they do is actually pretty simple, they start the new screen’s transition from the X and Y position of our touch AND scale up the screen. So it creates an animation of you screen appearing from the position of your touch.</p><p>And that’s prerry much it.</p><p>Some things that you need to take into consideration:</p><ol><li>You still need to create a closing animation of the screen going back the same way it was opened. It can be done by using the <strong>next</strong> argument or by using <strong>inverted</strong> property of <strong>Animated</strong> from <strong>react-native</strong>;</li><li>If you are going to use videos instead of images (how I did) you might encounter a problem — where your new screen is going to “flicker”: it will appear blank at first and only after that it might show the video. It’s happening because it can’t show the video instantly on new screen since it takes some time to inititalize the video player and load the video (that’s why shared transition is better, since it avoids this issue);</li></ol><p>So yea, if you thing that something was done wrong or could be improved — write in the comments or hit me up at <em>islamrustamov@rambler.ru</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=98f1c4c831ee" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Building animations with RN Reanimated v3: FlatList with animated items and auto centering]]></title>
            <link>https://medium.com/@islamrustamov/building-animations-with-rn-reanimated-v3-flatlist-with-animated-items-and-auto-centering-cb72cadf53ae?source=rss-89fb6d7e8486------2</link>
            <guid isPermaLink="false">https://medium.com/p/cb72cadf53ae</guid>
            <category><![CDATA[reanimated]]></category>
            <category><![CDATA[react-native]]></category>
            <category><![CDATA[animation]]></category>
            <category><![CDATA[react-native-reanimated]]></category>
            <category><![CDATA[flatlist]]></category>
            <dc:creator><![CDATA[Islam Rustamov]]></dc:creator>
            <pubDate>Sat, 10 Jun 2023 19:50:16 GMT</pubDate>
            <atom:updated>2023-06-10T19:50:16.269Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rVKRZkSpa0aLDEyxHX8sBA.jpeg" /><figcaption>Thanks to <a href="https://unsplash.com/@5tep5">Alexander Popov</a> for this picture: <a href="https://unsplash.com/@5tep5">https://unsplash.com/@5tep5</a></figcaption></figure><p>In this story I am going to share with you how to create a simple scrollable list of items, which are animating when they are in the middle with an effect of them being “selected”. This is how it is going to look like:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/322/1*IzsKpxYcvhlERpNyFoEgSQ.gif" /></figure><p>We will start with items of the list:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a5e532029d9d26b5f44aee2741aee31a/href">https://medium.com/media/a5e532029d9d26b5f44aee2741aee31a/href</a></iframe><p>Code for this one is pretty simple, but let’s anyway take a look at the most interesting parts (I am not going to use <em>StyleSheet</em> so it’s easier for you to understand what’s happening with inline styles).</p><ol><li>We use hook called <em>useAnimatedStyle </em>which returns us animated styles, specifically <em>width</em> and <em>height</em>. Our component receives <em>isCenter</em> prop and hook <em>useAnimatedStyle</em> depends on it, it means that these styles are going to be recalculated once this <em>isCenter</em> changes;</li><li>With ternary operators I choose what sizes I want for my item. Do use <em>withSpring</em> or analogues to achieve nice effects;</li><li>And <em>View</em> must be animated, of course.</li></ol><p>Now to the list:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1e85fcf9387286dd6e919394c1b8638e/href">https://medium.com/media/1e85fcf9387286dd6e919394c1b8638e/href</a></iframe><p>Let’s see what’s going on here:</p><p>We need to know the height of our screen. Why — because we need to fill the list with empty space above and below. For this I created a component called <em>ListSpacer</em> with <em>height / 2</em>. Then I’m going to pass this <em>ListSpace</em> to <em>ListHeaderComponent</em> and <em>ListFooterComponent</em> of <em>FlatList</em>.</p><p>We need to know exact size (in our case — height) of each item. It’s going to be 70. Why 70? In previous code you could see that the height is actually 60. We need 70 because our items have a gap between them with height of 10 — it’s the height of <em>ItemSeparator</em> component that’s going to be passed into <em>ItemSeparatorComponent</em> of <em>FlatList</em>. That’s why we need to add additional 10 units to the height.</p><p>Now in the main component itself we need to store selected index in the state (you can also try to store it in shared value and see if it’s going to work).</p><p>We need to define a function that is going to be invoked once scrolling has ended. <em>FlatList</em> has a special prop called <em>onMomentumScrollEnd</em> which returns data about scroll event. In function <em>momentumScrollEndHandler</em> we are going to do this computation:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8ac0dfd6ba32c8029fc3ddfbdfbfaadd/href">https://medium.com/media/8ac0dfd6ba32c8029fc3ddfbdfbfaadd/href</a></iframe><p>What’s happening? Before we continue, let’s take a look at the prop called snapToInterval. It allows us to break our list into parts. What parts? We want our list to be broken into parts of 70 units height. Look at the image below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wvAe38sXrTZxdJIrWIeigw.png" /></figure><p>(<em>start</em> and <em>end</em> define the Y value)</p><p>With this prop, if I scroll to the Y of 200, for example, my list will automatically snap to the 3rd element, so it will be in the middle (~175). Without this prop it will not happen. It will stay at Y of 200.</p><p>Now back to the <em>momentumScrollEndHandler</em> function:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8ac0dfd6ba32c8029fc3ddfbdfbfaadd/href">https://medium.com/media/8ac0dfd6ba32c8029fc3ddfbdfbfaadd/href</a></iframe><p>When the scroll finished, let’s say, with Y equals to 170, the value of <em>e.nativeEvent.contentOffset.y</em> isn’t going to be 170, it’s going to be 140 (please, don’t get confused with what I said, it will snap to the middle ~175, but the value returned is going to be 140). And since we know the size of each item in the list, we can divide this Y by <em>blockSize</em> and it will return us the index of the element that we scrolled to. I do also some <em>Math.round</em> for safety.</p><p>And that’s pretty much it.</p><p>I will continue creating new stories, where I create interesting/simple/advanced animations in future.</p><p>If you have any questions, comments or think that something could be implemented better — feel free to comment here or reach me out at <em>islamrustamov@rambler.ru</em>.</p><p><em>Sources:</em></p><p><a href="https://www.npmjs.com/package/react-native-reanimated">react-native-reanimated</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cb72cadf53ae" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Obj-C Turbo Module vs C++ Turbo Module vs Flutter Platform Channel]]></title>
            <link>https://medium.com/@islamrustamov/obj-c-turbo-module-vs-c-turbo-module-vs-flutter-platform-channel-e610a344ec92?source=rss-89fb6d7e8486------2</link>
            <guid isPermaLink="false">https://medium.com/p/e610a344ec92</guid>
            <category><![CDATA[react-native]]></category>
            <category><![CDATA[flutter-platform-channel]]></category>
            <category><![CDATA[flutter]]></category>
            <category><![CDATA[turbo-module]]></category>
            <category><![CDATA[performance]]></category>
            <dc:creator><![CDATA[Islam Rustamov]]></dc:creator>
            <pubDate>Thu, 08 Jun 2023 15:37:08 GMT</pubDate>
            <atom:updated>2023-06-08T15:37:08.891Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/0*UbsRoJ0eeFb3-lpr" /><figcaption>Thanks to <a href="https://unsplash.com/@5tep5">Alexander Popov</a> for this picture: <a href="https://unsplash.com/@5tep5">https://unsplash.com/@5tep5</a></figcaption></figure><blockquote><strong>Off topic</strong>: a lot of people have a wrong idea of React Native’s performance. People will straight up tell you that RN is a bad technology, without any understanding of how it works (starting from 0.69.0+, or at all), how to write performant code, how to solve tasks with good quality, which leads to unjustified blaming of the technology. When something isn’t working right — try to think, maybe you are doing something wrong, instead of blaming the framework/tool you are using.</blockquote><p>In this story I’m going to show you some computations I did with React Native and Flutter. Specifically how does JS/Dart interact with native code. I wanted to see what’s actually going to work faster:</p><ol><li>React Native’s Objective-C turbo module;</li><li>React Native’s C++ turbo module;</li><li>Flutter’s platform channel (with Swift).</li></ol><p>Tests are run on MacBook Pro with 2,6 GHz 6-Core Intel Core i7 and 16 GB 2400 MHz DDR4. For testing device I used an emulated iPhone 14 with iOS 16.2.</p><p>The task I chose for testing is a simple bubble sorting algorithm, below I will show you how solutions were implemented.</p><p>For C++ module:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1b024586d35ee77ce57d637037c01c41/href">https://medium.com/media/1b024586d35ee77ce57d637037c01c41/href</a></iframe><p>For Objective-C module:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7a380b0bd79e07d69c113f61535c5584/href">https://medium.com/media/7a380b0bd79e07d69c113f61535c5584/href</a></iframe><p>For Swift:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/72180a7d6ca173327f9015ab34a9f033/href">https://medium.com/media/72180a7d6ca173327f9015ab34a9f033/href</a></iframe><p>So, implementations are (almost) same for all cases.</p><p>How did I test:</p><ol><li>Run sorting function 1000 times with the array of numbers in decreasing order;</li><li>Get a timestamp before and after each sorting function execution;</li><li>Get time of execution from step 2 for each iteration;</li><li>Sum all of the times of executions;</li><li>Divide sum by 1000.</li></ol><p>How did it look in the code for React Native:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4f8806a73b298076ac02b914330b0bad/href">https://medium.com/media/4f8806a73b298076ac02b914330b0bad/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a2d212bb4963a7be238c9b46d6035652/href">https://medium.com/media/a2d212bb4963a7be238c9b46d6035652/href</a></iframe><p>How did it look in the code for Flutter:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3736a5d230e1f6dcb5b2ac84ad679d53/href">https://medium.com/media/3736a5d230e1f6dcb5b2ac84ad679d53/href</a></iframe><p><em>list10 </em>is an array with 10 numbers. I also used <em>list100</em> and <em>list200</em>.</p><p>These are the results I got:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qhFA0LpKbwlW-QQtwHPHfA.png" /></figure><p>On the first column you can see the sizes of the arrays. On other columns you can see results in milliseconds.</p><p>As a bonus I also tried to run this thing with an array of 10000 elements, to see how fast it’s going to work with big arrays. But I did only 1 run instead of 1000, because I didn’t feel like waiting for 9 hours for the code to complete. Objective-C run for 8 seconds, C++ for 0.89 seconds, Swift for 34 seconds.</p><p>In graph it looks like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*lDtUfOW2KK0kGnUR0qMf_w.png" /></figure><p>We can see that actually C++ is showing the best results (lower = better).</p><p>So, what does this mean? It definitely doesn’t mean that React Native is overall faster than Flutter. It only means that if you could write a C++ code for some complex computation in RN’s turbo module or Swift code invocation with platform channel in Flutter — C++ would be way faster.</p><p>Also please bear in mind that these results might be different on different machines, real devices instead of emulators, programming languages and <em>performance</em> probably doesn’t work the same way as <em>stopwatch</em>.</p><p>If you think that computations are not done correctly, please inform me about it so I could recompute them to share more accurate data with you.</p><p>You can reach me at <em>islamrustamov@rambler.ru </em>or in the comments<em>.</em></p><p><em>Sources:</em></p><ul><li><a href="https://docs.flutter.dev/platform-integration/platform-channels#example">Writing custom platform-specific code</a></li><li><a href="https://reactnative.dev/docs/next/the-new-architecture/cxx-cxxturbomodules">C++ Turbo Native Modules · React Native</a></li><li><a href="https://reactnative.dev/docs/next/the-new-architecture/pillars-turbomodules">Turbo Native Modules · React Native</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e610a344ec92" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[One of the many React Native’s TextInput problem on Android]]></title>
            <link>https://medium.com/@islamrustamov/one-of-the-many-react-natives-textinput-problem-on-android-e4c9915acbc6?source=rss-89fb6d7e8486------2</link>
            <guid isPermaLink="false">https://medium.com/p/e4c9915acbc6</guid>
            <category><![CDATA[java]]></category>
            <category><![CDATA[react-native-modules]]></category>
            <category><![CDATA[react-native]]></category>
            <category><![CDATA[keyboard]]></category>
            <category><![CDATA[android]]></category>
            <dc:creator><![CDATA[Islam Rustamov]]></dc:creator>
            <pubDate>Fri, 17 Feb 2023 10:18:36 GMT</pubDate>
            <atom:updated>2023-02-17T10:18:49.449Z</atom:updated>
            <content:encoded><![CDATA[<h3>One of the many React Native’s TextInput problems on Android</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/0*AQUdedZJfEN6TQ3f" /><figcaption>Thanks to <a href="https://unsplash.com/@5tep5">Alexander Popov</a> for this picture: <a href="https://unsplash.com/@5tep5">https://unsplash.com/@5tep5</a></figcaption></figure><p>Sometimes I feel like React Native is the greatest tool for cross platform development, but then I face such simple problems that I start to hate it. This is a small example of the problem that I’ve been facing for all of my career as RN developer and its’ solution.</p><h4>The case</h4><p>Take a look at the next example:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/450/1*GUQEU3BvIxLvGnFZGpafsQ.png" /><figcaption>Example of the screen</figcaption></figure><p>Let’s say that what we have in the red square is the part of the screen to which you have to scroll if you want to see it.</p><p>Whole component is wrapped in <strong>ScrollView</strong>, and inside of it there are plenty of <strong>TextInputs</strong>. When you open a keyboard — you might encounter a plenty of issues: jumping screen, screen is not fully scrollable and etc.</p><p>You might already tell me to use <strong>KeyboardAvoidingView</strong>, <strong>KeyboardAwareScrollView</strong> and plenty of other solutions that might help me out. But, since React Native has a lot of problems with Android’s <strong>TextInput</strong>s inside of <strong>ScrollView</strong>s, these suggestions might not help.</p><p>One more thing that could help you out is to change the <strong>windowSoftInputMode</strong> in the <strong>AndroidManifest.xml</strong>, but here comes another problem — changing this value from <strong>adjustPan</strong>/<strong>adjustResize</strong> to <strong>adjustResize</strong>/<strong>adjustPan</strong> might break some other screens that you have in your app.</p><p>But what if you want to have <strong>adjustResize</strong> in one screen and <strong>adjustPan</strong> on other screens?</p><p>Changing <strong>adjustPan</strong> to <strong>adjustResize</strong> only in this screen is exactly what I needed to solve my problem. Because I don’t want to rewrite all of the other screens to work well with <strong>adjustResize</strong>.</p><p>How can you achieve that? Let’s take a look at the already existing solutions:</p><ol><li><a href="https://www.npmjs.com/package/react-native-android-keyboard-adjust">https://www.npmjs.com/package/react-native-android-keyboard-adjust</a> — this library is basically based on the implementation of the native Java module that’s exposing methods to change <strong>windowSoftInputMode</strong>. But the problem is that last time it was updated 4 years ago. If you try to install this package you are probably going to end up with a lot of building errors (that’s what happened to me at least, when I tried it);</li><li><a href="https://www.npmjs.com/package/rn-android-keyboard-adjust">https://www.npmjs.com/package/rn-android-keyboard-adjust</a> — this library is based on the first one, but at least it’s supported as of 17 Feb 2023. I don’t know if you are going to face same building problems as with the first package, simply because I didn’t try to work with this one.</li></ol><p>Now you might think “I will just install that one new library that is currently supported and that’s it”. But do you really need to do that? Do you really need to add a new dependency in your project which adds more kBs/Bs to your application with a lot of unnecessary methods? I don’t think so. There is a much better way to do that.</p><p>What you need to do as a React Native developer — practice new skills. In this case — practice to create your own native modules.</p><p>Let’s start with creating a package:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2fb927e2f40dd38ddb62417999ef3061/href">https://medium.com/media/2fb927e2f40dd38ddb62417999ef3061/href</a></iframe><p>This code is pretty simple, basically a copy-paste from the official documentation with just some renaming. Packages in Java is like a place where you group your related classes/modules together.</p><p>You see that there is a line:</p><pre>modules.add(new AdjusterModule(reactContext));</pre><p><strong>AdjusterModule</strong> is the next thing we are going to create. It’s going to have the main code that does what we want it to do.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7fddbc20495f4a6595a4a416eacb04c5/href">https://medium.com/media/7fddbc20495f4a6595a4a416eacb04c5/href</a></iframe><p>You see that in our module we have 2 methods: <strong>setAdjustResize</strong> and <strong>setAdjustPan</strong>. These methods allow us to change <strong>windowSoftInputMode </strong>without rebuilding our project and changes to <strong>AndroidManifest.xml</strong>.</p><p>Let’s understand what’s written there by checking out one of them.</p><p>First we get the activity with <strong>getCurrentActivity</strong> method. This method can return null, so we do an <strong>if</strong> check.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bpeOxodcY0LY0-A8HcJqQg.png" /></figure><p>What’s an activity? It’s a specific for Android thing, basically a bunch of code that’s being executed (if we simplify things — our application that is launched on Android is an activity).</p><p>So after we got our application’s activity — we launch code in the UI thread to change <strong>windowSoftInputMode</strong>. What’s that method <strong>runOnUiThread</strong>? It might take a lot of time to explain this, so better <a href="https://medium.com/@yossisegev/understanding-activity-runonuithread-e102d388fe93">check out this story</a> to get a better understanding of why we need to use this thing in this particular case.</p><p>And then you can see that we are just calling <strong>setSoftInputMode</strong>. I don’t think that there is an explanation needed for this.</p><p>And after you created you module with package, added it in your MainApplication.java:</p><pre>@Override<br>protected List&lt;ReactPackage&gt; getPackages() {<br>  @SuppressWarnings(&quot;UnnecessaryLocalVariable&quot;)<br>  List&lt;ReactPackage&gt; packages = new PackageList(this).getPackages();<br>  packages.add(new AdjusterPackage()); // here we added our new package<br>  return packages;<br>}</pre><p>We can use our methods in the screen we want like this:</p><pre>import { NativeModules } from &#39;react-native&#39;;<br><br>const { AdjusterModule } = NativeModules;<br><br>//...<br><br>useEffect(() =&gt; {<br>  if (Platform.OS === &#39;android&#39;) {<br>    AdjusterModule.setAdjustResize();<br><br>    return () =&gt; AdjusterModule.setAdjustPan();<br>  }<br>}, []);</pre><p>We initially set the desirable <strong>windowSoftInputMode</strong> and then change it back after our screen is dismounted.</p><p>Don’t be afraid to take some code from the existing native modules and create your own modules to solve your tasks. The more you know about native development, the better React Native developer you become.</p><p>If you have any questions, comments or think that something I wrote isn’t quite right — I’m open for a discussion in the comments or you can reach out at <em>islamrustamov@rambler.ru</em>.</p><p><em>Sources</em>:</p><ul><li><a href="https://www.npmjs.com/package/rn-android-keyboard-adjust">rn-android-keyboard-adjust</a></li><li><a href="https://github.com/zubricky/react-native-android-keyboard-adjust">GitHub - zubricky/react-native-android-keyboard-adjust: Change the input mode for the Android keyboard in a React Native app</a></li><li><a href="https://developer.android.com/reference/android/app/Activity">Activity | Android Developers</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e4c9915acbc6" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Building animations with RN Reanimated v2: Animated header in ScrollView]]></title>
            <link>https://medium.com/@islamrustamov/building-animations-with-rn-reanimated-v2-animated-header-in-scrollview-d8814e0df40f?source=rss-89fb6d7e8486------2</link>
            <guid isPermaLink="false">https://medium.com/p/d8814e0df40f</guid>
            <category><![CDATA[scrollview]]></category>
            <category><![CDATA[react-native-reanimated]]></category>
            <category><![CDATA[react-native-scrollview]]></category>
            <category><![CDATA[animation]]></category>
            <category><![CDATA[react-native]]></category>
            <dc:creator><![CDATA[Islam Rustamov]]></dc:creator>
            <pubDate>Thu, 29 Sep 2022 07:15:55 GMT</pubDate>
            <atom:updated>2022-09-29T07:15:55.329Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WqkKD3gwZm7xNgs5y7QG2w.jpeg" /><figcaption>Thanks to <a href="https://unsplash.com/@sepoys">Rohit Tandon</a> for this picture: <a href="https://unsplash.com/@sepoys">https://unsplash.com/@sepoys</a></figcaption></figure><p>This is the second episode in the series of animations with react native reanimated. In this story I’m going to show you how to create a simple animated header with <strong>ScrollView</strong>. Here is how it looks:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/277/1*h80T-s2lo3cFY1n95BNwAg.gif" /></figure><p>The beginning of the component looks like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/28168293fb33c85716feff71e27275f3/href">https://medium.com/media/28168293fb33c85716feff71e27275f3/href</a></iframe><ol><li>We create a <strong>useSharedValue</strong> <strong>scrollY</strong> which is looking at how much we scrolled the list. There is a hook — <strong>useAnimatedScrollHandler</strong> which is returning a function, that we are going to pass to the <strong>ScrollView</strong>’s<strong> onScroll </strong>(I will show that part a bit later);</li><li>Next we create animated styles for the header part. I want to change the height of the header and decrease the margin, because I want to make my header smaller when I scroll down;</li><li>Also we have a styling for the text in the header. As you can see, I want to make the font of the text smaller and I also want it to slide nicely to the top left corner.</li></ol><p>If all of this things like <strong>interpolate</strong> and <strong>useAnimatedStyles</strong> are confusing to you, check out my previous story, where I explain these things with pictures — <a href="https://medium.com/@islamrustamov/building-animations-with-rn-reanimated-v2-swipeable-button-8115075135ed">https://medium.com/@islamrustamov/building-animations-with-rn-reanimated-v2-swipeable-button-8115075135ed</a>.</p><p>The rest of the components looks like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8b27dea0414b97c6d0d6b728d85bb1d5/href">https://medium.com/media/8b27dea0414b97c6d0d6b728d85bb1d5/href</a></iframe><p>You see that I passed <strong>scrollHandler</strong> to the <strong>ScrollView</strong>. Also <strong>ScrollView</strong> component is taken from the <strong>Animated</strong>. It is needed to properly control how much you scrolled. If you use regular state and will try to update it in <strong>ScrollView</strong>’s <strong>onScroll</strong> — it isn’t going to work.</p><p>And that’s pretty much it, here is the whole code snippet:<br><a href="https://gist.github.com/IslamRustamov1/ef53d2aaaa4168b99770918e52bbe3b3">https://gist.github.com/IslamRustamov1/ef53d2aaaa4168b99770918e52bbe3b3</a></p><p>I will continue creating new stories, where I create interesting animations in future.</p><p>If you have any questions, comments or think that something could be implemented better — feel free to comment here or reach me out at <em>islamrustamov@rambler.ru</em>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d8814e0df40f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Building animations with RN Reanimated v2: Swipeable button]]></title>
            <link>https://medium.com/@islamrustamov/building-animations-with-rn-reanimated-v2-swipeable-button-8115075135ed?source=rss-89fb6d7e8486------2</link>
            <guid isPermaLink="false">https://medium.com/p/8115075135ed</guid>
            <category><![CDATA[gestures]]></category>
            <category><![CDATA[swiping]]></category>
            <category><![CDATA[react-native]]></category>
            <category><![CDATA[react-native-animation]]></category>
            <category><![CDATA[reanimated]]></category>
            <dc:creator><![CDATA[Islam Rustamov]]></dc:creator>
            <pubDate>Fri, 16 Sep 2022 16:07:53 GMT</pubDate>
            <atom:updated>2022-09-16T16:07:53.438Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Mx-L9249Dx0J8QhHv1PyoA.jpeg" /><figcaption>Thanks to <a href="https://unsplash.com/@prometheusdesign">Prometheus Design</a> for this picture: <a href="https://unsplash.com/@prometheusdesign">https://unsplash.com/@prometheusdesign</a></figcaption></figure><p>Animations in React native were always complicated for me. Maybe because I didn’t want to spend a lot of time learning how to work with them, when I could find a solution for my task and copy/paste it. But recently I had a task which wasn’t really googleable, so I had to spend some time investigating into gesture handler and reanimated. And I found it rather easy to work with.</p><p>In this story I’m going to share an example of the component that I’ve implemented and hopefully it will help you to build your own animated components with ease.</p><p>What we are going to build is a swipeable button that looks like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*9P448Tihmxn-hh_-BFKjlA.gif" /><figcaption>Swipeable button</figcaption></figure><p>So, the main goals of this component are:</p><ol><li>Have some fancy animation (as you can see in “Swipe me for some action” text);</li><li>Execute desirable action, once you finish swiping;</li><li>Return the button to the initial position once our action is over.</li></ol><p>What we are going to need to create this component:</p><ol><li><a href="https://www.npmjs.com/package/react-native-gesture-handler">react-native-gesture-handler</a>;</li><li><a href="https://www.npmjs.com/package/react-native-reanimated">react-native-reanimated</a> (please, pay attention to the version, <strong>v1</strong> isn’t going to fit, since it doesn’t have all of the required methods, only <strong>v2</strong> or <strong>v3</strong> are suitable as of <strong>16.09.2022</strong>).</li></ol><p>Let’s start with some basic setup, we have a parent component that looks somewhat like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/929dd6e9a4262dec9b6d8bec82c40770/href">https://medium.com/media/929dd6e9a4262dec9b6d8bec82c40770/href</a></iframe><p>As you can see, we have some simple logic here, we want to make some request once we swiped the button, so we pass <strong>onSwipe </strong>prop to the <strong>SwipeableButton</strong> component. We also pass <strong>isLoading</strong> prop, so that our button knows when it needs to show loader.</p><p>Now, the real code begins, I’m going to show you the pieces of the component bit by bit, so that it won’t be overwhelming for you. I will share the whole code snippet in the end, so don’t worry.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ab961c82b6ef541210486ad631dc0913/href">https://medium.com/media/ab961c82b6ef541210486ad631dc0913/href</a></iframe><p>What is happening here:</p><ol><li>We start with importing all of the required things. We will go over almost all of them later;</li><li>Variable <strong>BUTTON_WIDTH</strong> speaks for itself, you can adjust button’s width however it suits you;</li><li>Variable <strong>SWIPE_RANGE </strong>is responsible for how long you can swipe the button. These two variables (<strong>SWIPE_RANGE </strong>and<strong> BUTTON_WIDTH</strong>) can but customized however you want, so those values (48 and 74) are the values that I picked myself to achieve desired result;</li><li>I specify that my component can accept only two props as you can see in the <strong>type</strong>;</li><li>And I also create only one important value for the animation — <strong>X</strong>. It will be responsible for different animations — transforming and opacity. Please check out what’s a shared value in the <a href="https://docs.swmansion.com/react-native-reanimated/docs/fundamentals/shared-values/#carrying-data">official documentation</a>, getting an understanding of the shared memory, worklets and how react native reanimated works overall is a good thing.</li></ol><p>Now let’s go further into the component:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e256f766bc97040026ddb573041f3f73/href">https://medium.com/media/e256f766bc97040026ddb573041f3f73/href</a></iframe><p>We have a <strong>useEffect</strong> that’s getting invoked everytime our <strong>isLoading</strong> prop changes.</p><p>What’s happening inside of it? I check, if loading was completed (if action is over) I simply invoke <strong>withSpring(0)</strong> and assign it to <strong>X.value</strong>.</p><p>What does it mean? <strong>X</strong> variable is basically an object with a field <strong>value</strong>, this value can change from 0 to &lt;any value&gt; (by that I mean 0, 0.01, 0.02, 0.03 and so on for example) so that our animation is smooth. When I invoke that code on the line 4 in the snippet above — I tell my <strong>X</strong> to change its’ value from &lt;any value&gt; to 0 smoothly.</p><p>For example, if you imagine some object (let’s call it <strong>SQUARE</strong>) on the <strong>x</strong> axis with <strong>x=10</strong> (or <strong>SQUARE.value === 10</strong>), and then you call:</p><pre>SQUARE.value = 0;</pre><p>Then it will immediately move our object to the <strong>x=0</strong>, without any animation. Maybe it will be easier to understand with picture:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zzKHNRtBJl9-m2yP6b7zmg.png" /></figure><p>So, to solve this, we will do this:</p><pre>SQUARE.value = withSpring(0);</pre><p>This will make our object go from<strong> x=10</strong> to <strong>x=0</strong> smoothly, because values of the SQUARE are not going to jump from 10 to 0, they will be changing somewhat like this: 10, 9, 8, 7, … , 0.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*12Kpc3fiqgK3OsXa2RBUjg.png" /></figure><p>So, now let’s take a look at this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/25bcf78729cd86fdb43748ae97e9a183/href">https://medium.com/media/25bcf78729cd86fdb43748ae97e9a183/href</a></iframe><p>Look at that hook — <strong>useAnimatedGestureHandler</strong>. This hook is really useful and I recommend you to get to know it better.</p><p>This hook allows us to pass different functions inside of it, which are getting executed at different points of the animation. For now, we use only two:<br>1. <strong>onActive</strong> — getting executed as long as gesture is in progress (swiping);</p><p>2. <strong>onEnd</strong> — getting executed once gesture is over (you lifted your finger from the button).</p><p>In <strong>onActive</strong> event we get the event argument with <strong>translationX</strong> value. We then check whether this value in required boundaries or not and if everything is fine — we update our <strong>X</strong> variable. That condition forbids us from swiping to the left outside of the button (though it will work without <strong>newValue &gt;= 0</strong>, I put it there just to be sure) and from swiping it way too far to the right (<strong>newValue &lt;= SWIPE_RANGE</strong>).</p><p>In <strong>onEnd</strong> event I check two things:</p><ol><li>If I didn’t swipe long enough, I want my button to return to the initial position;</li><li>If I swiped long enough, I want to execute my action.</li></ol><p>Now take a look at the <strong>else</strong> block. I invoke <strong>onSwipe</strong> method through such a thing as <strong>runOnJS</strong>. Since react native reanimated works with worklets/different threads, we need to help it to understand on what threads and how we need to launch our functions. If you are willing to get a better understanding of how it works, please head to the <a href="https://docs.swmansion.com/react-native-reanimated/docs/api/miscellaneous/runOnJS/">official documentation</a>. Basically, we cannot call onSwipe itself, because it will be synchronously called in a different thread, which will throw an error (because <strong>onSwipe</strong> is inside of the <strong>useAnimatedGestureHandler</strong>, and this hook puts everything on a different thread). <strong>runOnJS</strong> helps to launch <strong>onSwipe</strong> asynchronously, which is going to solve our problem.</p><p>Just so that you understand the carrying of <strong>runOnJS</strong>, here is the example — if we have a function like this:</p><pre>const addition = (a, b) =&gt; a + b</pre><p>then</p><pre>runOnJS(addition)(1, 2);</pre><p>is<strong> </strong>(almost) same as</p><pre>addition(1, 2);</pre><p>Now to another slightly complicated part:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0c407a78cd28daf3c9477dc7a2674b32/href">https://medium.com/media/0c407a78cd28daf3c9477dc7a2674b32/href</a></iframe><p><strong>useAnimatedStyle</strong> hook is another hook that you should get to know, since it’s really useful to build animations with ease. Basically, you create a stylesheet with animations.</p><p>Right now I have 2 animated styles: <strong>swipeButton </strong>and<strong> swipeText</strong>. Let’s take a look at them.</p><p><strong>swipeButton</strong> has a <strong>transform</strong> field, so that I can implement the translation on the x axis. Look at the interpolate function:</p><pre>translateX: interpolate(<br>  X.value,<br>  [20, BUTTON_WIDTH],<br>  [0, BUTTON_WIDTH],<br>  Extrapolation.<em>CLAMP</em>,<br>),</pre><p><strong>interpolate</strong> accepts a couple of arguments:</p><ol><li>The animated value that it needs to look at;</li><li>The array of input value;</li><li>The array of output values;</li><li>And extrapolation type.</li></ol><p>Yeah, but what does those arguments mean?</p><p>Let’s once again return to example with the <strong>SQUARE</strong> object on the x axis. When we press on our imaginary <strong>SQUARE</strong> and swipe it to the right, its’ <strong>value </strong>will<strong> </strong>change (for example) from 0 to 1, to 2, to 3, to 4 and so on. Let’s also say that we have an input array [2, 4] and an output array [20, 40]. We want to build an animation, where the height of our <strong>SQUARE</strong> changes from 20 to 40 once we swiped starting from <strong>x=2</strong> to <strong>x=4</strong>. So, when we are swiping our <strong>SQUARE</strong> to the right and once it reached the <strong>x=2</strong> it will start changing the height smoothly from 20 to 40 until it reaches <strong>x=4</strong>.</p><p>Maybe it will be easier to understand once you see a picture.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*l3EWQ676Qf_RNA3Qdx5jTg.png" /></figure><p>As for extrapolation, there are different types of extrapolations, for this case I chose <strong>CLAMP</strong> — what it does is that it doesn’t allow my values to be greater or less than the values that I specified in the output range.</p><p>Now, the <strong>swipeText</strong>—it has an animation where it changes its’ opacity and also translated slightly to the right. Take a look at the GIF I attached at the top to understand how this is going to look like.</p><p>And now, finally, the end of the component:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5139d99d43ed99aea8c4fcd4f6ca20bb/href">https://medium.com/media/5139d99d43ed99aea8c4fcd4f6ca20bb/href</a></iframe><p>We have such a great component here as <strong>PanGestureHandler</strong>. It is taken from the <strong>react-native-gesture-handler</strong>. It allows us to control, what parts of the UI will respond to the gestures. We pass a <strong>enabled</strong> prop there to disable gestures while the action is in progress, and also we pass there <strong>onGestureEvent</strong> prop, that you already know what’s doing. We wrapped the blue button with this <strong>PanGestureHandler</strong> so that this blue button could respond to our gestures (in our case — swipe gesture).</p><p>The code above is pretty simple, so I don’t think there is any point in explanations.</p><p>And that’s pretty much it, here is the whole code snippet:<br><a href="https://gist.github.com/IslamRustamov/b35eff8cb951ffbadede44eb7a2a6d2c">https://gist.github.com/IslamRustamov/b35eff8cb951ffbadede44eb7a2a6d2c</a></p><p>I will continue creating new stories, where I create interesting animations in future.</p><p>If you have any questions, comments or think that something could be implemented better — feel free to comment here or reach me out at <em>islamrustamov@rambler.ru</em>.</p><p><em>Sources:</em></p><ul><li><a href="https://www.npmjs.com/package/react-native-reanimated">react-native-reanimated</a></li><li><a href="https://www.npmjs.com/package/react-native-gesture-handler">react-native-gesture-handler</a></li><li><a href="https://www.codedaily.io/courses/React-Native-Reanimated-Fundamentals/Create-a-Draggable-Opacity-Changing-Circle-with-Reanimated-in-React-Native">React Native Reanimated Fundamentals</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8115075135ed" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Yet another example of camera implementation in React Native]]></title>
            <link>https://medium.com/@islamrustamov/yet-another-example-of-camera-implementation-in-react-native-85337090e07c?source=rss-89fb6d7e8486------2</link>
            <guid isPermaLink="false">https://medium.com/p/85337090e07c</guid>
            <category><![CDATA[react-native-camera]]></category>
            <category><![CDATA[react-native]]></category>
            <category><![CDATA[react-native-hook]]></category>
            <category><![CDATA[typescript]]></category>
            <category><![CDATA[custom-hook]]></category>
            <dc:creator><![CDATA[Islam Rustamov]]></dc:creator>
            <pubDate>Sat, 18 Jun 2022 10:49:09 GMT</pubDate>
            <atom:updated>2022-06-19T19:56:49.025Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5q_VqTKgZTtZ1l-IVTKjYw.jpeg" /><figcaption>Thanks <a href="https://unsplash.com/@prometheusdesign">Prometheus Design</a> for this picture: <a href="https://unsplash.com/@prometheusdesign">https://unsplash.com/@prometheusdesign</a></figcaption></figure><p>A while ago I had an interesting task, where I had to implement a somewhat WhatsApp-like camera. This is how it’s going to look like in the end:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FZHOFPZiH-7A&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DZHOFPZiH-7A&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/5326409a65feaf286c9a4ae028d3106c/href">https://medium.com/media/5326409a65feaf286c9a4ae028d3106c/href</a></iframe><p>I decided to share this tutorial with the others, because it was the first time I implemented something more or less complex (not really complex) with RN camera, so that you might copy/paste some of the ideas to your project or learn something new.</p><p>Let’s take a look at some basic features, that we are going to need:</p><ol><li>We can turn switch the flash mode;</li><li>We can switch between front and back camera;</li><li>We can tap once to make a photo;</li><li>We can press and hold the recording button to make a video;</li><li>Once we hold the recording button, we want to have a timer of allowed video recording duration.</li></ol><p>Our basic navigation is going to look like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a2d70c03c1b409d3b3381797d7840b53/href">https://medium.com/media/a2d70c03c1b409d3b3381797d7840b53/href</a></iframe><p>The Home screen is going to have a simple button that takes us to the camera screen:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a4928a052c0d0a18a46671891c764c33/href">https://medium.com/media/a4928a052c0d0a18a46671891c764c33/href</a></iframe><p>The camera screen itself is going to have some more code in it, but be not afraid, we are going to discuss every part of it, so don’t spend much time reading code snippet below. The screen is going to look like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a84ba7a25a04b3d0afc8c988c36a2ec3/href">https://medium.com/media/a84ba7a25a04b3d0afc8c988c36a2ec3/href</a></iframe><p>Let’s brake it into pieces. The first part is header:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4ec72dfe7acc6af683b99a462899a6fc/href">https://medium.com/media/4ec72dfe7acc6af683b99a462899a6fc/href</a></iframe><ol><li>In the header we have a close button that takes us to the previous screen;</li><li>After that we conditionally render timer. The <strong>recording</strong> variable is taken from our hook <strong>useCamera</strong>, we are going to take a look at it later. If <strong>recording</strong> is true, then we show how many seconds have passed;</li><li>Right after that we have a button to enable/disable torch. We conditionally render this part, because torch is available only on the back camera.</li></ol><p>The next part is the body (and the footer) of our screen:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e38172615749ac59b6dfcc2c04d66b00/href">https://medium.com/media/e38172615749ac59b6dfcc2c04d66b00/href</a></iframe><ol><li>We have a button to start recording/take a picture (depending on the action you make — long press, press);</li><li>And also we have a camera switch button, because we also want to make selfies, probably;</li><li>In the footer there is nothing special, just a black rectangle with some guiding text.</li></ol><p>Now, let’s take a look at some real code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d36bd5598a1394e9d1a6107b4023789a/href">https://medium.com/media/d36bd5598a1394e9d1a6107b4023789a/href</a></iframe><p>I have created 2 services which help me to work with the camera: flashModeService and CameraService. Let’s see what they do.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b85ea8eea566c9216e57c3e927185cee/href">https://medium.com/media/b85ea8eea566c9216e57c3e927185cee/href</a></iframe><p>A simple class that encapsulates conditional logic inside of it, so that we don’t write this inside of the component. Depending on the current flash mode, we decide whether we want to turn torch off or on.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/435eed7baa161151ee48c9911b45fd57/href">https://medium.com/media/435eed7baa161151ee48c9911b45fd57/href</a></iframe><p>A service that allows us to understand whether the current camera is the back or front camera (isBackCamera) and also returns us a new camera type, depending on the current camera type (getNewCameraType).</p><p>Also, you can see that instead of a regular ref, I am using a callback ref. This is needed, because initially ref is not passed correctly to the <strong>useCamera</strong> hook, which will not allow us to make photos/videos. Because:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*t7EuKzP5PhH7I4CvCvmxog.png" /><figcaption><a href="https://reactjs.org/docs/hooks-faq.html#how-can-i-measure-a-dom-node">https://reactjs.org/docs/hooks-faq.html#how-can-i-measure-a-dom-node</a></figcaption></figure><p>You can try to use a regular ref and see how your camera isn’t going to work.</p><p>Simple callback ref implementation:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2df923ab944d25f59b7841c805736271/href">https://medium.com/media/2df923ab944d25f59b7841c805736271/href</a></iframe><p>Now, we are going to take a look at the <strong>biggest</strong> <strong>brain</strong> of our camera — the <strong>useCamera</strong> hook.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8551e43f423969deb79b0c5206e93756/href">https://medium.com/media/8551e43f423969deb79b0c5206e93756/href</a></iframe><p>Before we dive into this hook, let me share a <strong>TimerService</strong> code. It’s a simple service to work with (you won’t believe it) — timer.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9b6addfd5973c601e768bbfd03825357/href">https://medium.com/media/9b6addfd5973c601e768bbfd03825357/href</a></iframe><p>So, we just store a timer id, and then we can start timer with a certain callback (<strong>startTimer</strong>), or we can clear this timer (<strong>stopTimer</strong>).</p><p>Now, let’s discuss useCamera bit by bit:</p><ol><li>Hook accepts ref — we need a ref to a camera so that we can call the methods we need (like image capturing or starting record);</li><li><strong>recording</strong> state — we need to know whether our app is recording or not;</li><li><strong>seconds</strong> state — we need to know for how long we’ve been recording;</li><li><strong>cameraActionProxy</strong> — this is a helpful method that allows me to check whether the ref was set or not. I don’t want to write conditional statement in every function that uses ref (an if statement that checks whether ref is set or not), so with this proxy I reduce amount of duplicated code;</li><li><strong>takePicture</strong> — the name of the method speaks for itself. When we tap the recording button, we call this method to take a picture;</li><li><strong>startRecordingVideo</strong> — when we perform a onLongPress action, we launch our timer, tell our component that the recording is in progress, and launch <strong>camera.recordAsync</strong> method, which starts the recording;</li><li><strong>stopRecordingVideo</strong> — basically it does what it says. We are stopping the timer, defaulting our recording state, timer state and finishing recording. Also we check whether the recording was in progress, because this method is called on onPressOut event, which is also getting called on a regular tap;</li><li><strong>countdown</strong> — it’s a callback that we pass to the <strong>startTimer</strong> method, which counts the seconds. Once we reached 0 seconds, we want to stop this timer and stop video recording.</li></ol><p>And that’s pretty much it. In future I would love to implement 2 native modules which return me an array of URLs — which define the paths to the images from the gallery, so that I can also show images on the camera screen and pick them.</p><p>If you have any questions, comments or think that something could be implemented better — feel free to comment here or reach me out at <em>islamrustamov@rambler.ru</em>.</p><p><em>Sources:</em></p><ul><li><a href="https://react-native-camera.github.io/react-native-camera/docs/rncamera">RNCamera · React Native Camera</a></li><li><a href="https://reactnative.dev/docs/getting-started">Introduction · React Native</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=85337090e07c" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>