<?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 MR3Y on Medium]]></title>
        <description><![CDATA[Stories by MR3Y on Medium]]></description>
        <link>https://medium.com/@mr3y?source=rss-a2571725a67f------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*MLJc7oyRqmfP6wU4MfBhgA.jpeg</url>
            <title>Stories by MR3Y on Medium</title>
            <link>https://medium.com/@mr3y?source=rss-a2571725a67f------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 12:25:32 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@mr3y/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[What is missing in compose now?]]></title>
            <link>https://mr3y.medium.com/what-is-missing-in-compose-now-bf96f4ca2423?source=rss-a2571725a67f------2</link>
            <guid isPermaLink="false">https://medium.com/p/bf96f4ca2423</guid>
            <category><![CDATA[jetpack-compose]]></category>
            <category><![CDATA[declarative-ui]]></category>
            <category><![CDATA[android]]></category>
            <dc:creator><![CDATA[MR3Y]]></dc:creator>
            <pubDate>Fri, 11 Jun 2021 14:34:15 GMT</pubDate>
            <atom:updated>2021-06-11T14:34:15.523Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ObiwHm6g1U_vcINsjcZHiw.jpeg" /><figcaption>header by @barkiple from Unsplash</figcaption></figure><p>Compose is Production ready? Okay, Compose <a href="https://android-developers.googleblog.com/2021/05/whats-new-for-android-developers-at.html">will hit 1.0 stable in July</a>? Great. I’m absolutely happy with the hard work which made compose almost ready for production But I think there are some areas that need more improvement to be fully production-ready for everyday use</p><blockquote>Note: my feedback is based on latest compose release at the time of writing which is 1.0-beta08</blockquote><h3>Debugging:</h3><p>the Issues related to Debugging are mostly around tooling, Live Preview is somewhat buggy, sometimes it doesn’t work, sometimes it is so laggy, sometimes it displays an ambiguous error message and Once I hit the refresh button, it renders without any problems. I know it is going to evolve over time with the next Arctic Fox updates but for now Live Preview isn’t a good option for complex components.</p><p>Another issue I’ve faced in debugging some compose code is the silent errors. Take a look at the next example:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/611/1*_CIFj0LpotY3aXCxTCWkJg.png" /></figure><p>What should that code display? Can you guess?</p><p>The answer is simply:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/331/1*vIHeLCQ8OgezKjtMNp4AZA.png" /></figure><p>The inner Box is completely invisible, can you figure out why? because you haven’t declared a value for the alpha channel when you had declared teal700 color, To fix this we can simply add the alpha channel value:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/849/1*qPSAY9ZnxzrWtNDtAAIFKg.png" /></figure><p>You might catch that quickly but remember this example is simple enough to not be realistic, In a more real-world scenario you may have hundreds of colors in a single file &amp; you are more likely to access the color from something like MaterialTheme.colors.primary , also, you may have deeply nested components that each parent could affect the appearance of its children with a customized theme.</p><p>All of those factors in addition to that there is no error propagation mechanism could waste your time debugging this ridiculous issue.</p><p>One of the proposed solutions to this issue is to throw an exception at the render time with a stacktrace that can help you track down the root of this issue.</p><p>Another less aggressive solution is to report a lint warning when you forget to provide an alpha value.</p><p>Thankfully, this issue got addressed by compose team <a href="https://android-review.googlesource.com/c/platform/frameworks/support/+/1650950">at this commit</a> and the latter solution has been the preferred way to deal with this issue.</p><h3>Testing:</h3><p>compose team wrote an <a href="https://developer.android.com/jetpack/compose/testing">excellent documentaion</a> about testing composable functions. it contains the basics you need to get started, but once you have more complex components you can realize that things can get out of your hands easily, you’ll find yourself writing some additional boilerplate plus relying on some implementation details to write your UI tests which collides with the idea of making your UI black-box tested, Moreover it makes your test suite fragile &amp; tightly coupled to the implementation.</p><p>Another problem with testing is <em>not technically a problem but a feature request</em> which is to be able to run some compose tests locally, this is not possible so far, though there is a promise to make that happen soon:</p><h3>Jose Alcérreca on Twitter: &quot;Soon (in a couple of Compose releases) you&#39;ll be able to run certain Compose tests locally 🥳using Robolectric. You can see it in action here:https://t.co/xAXNlTiouT🧵1/3 / Twitter&quot;</h3><p>Soon (in a couple of Compose releases) you&#39;ll be able to run certain Compose tests locally 🥳using Robolectric. You can see it in action here:https://t.co/xAXNlTiouT🧵1/3</p><h3>Coupled with fixed kotlin version:</h3><p>Right now compose comes with a restriction that prevents you from using a higher major kotlin version than the version compose compiler uses. This was annoying, because, until 1.0-beta07 release compose was depending on 1.4.32 kotlin version, So, If you intended to use kotlin 1.5 in your project you get blocked and you will have to wait for compose compiler to update to 1.5 version. This restriction has been relaxed quite a bit with 1.0-beta08 release because now compose compiler has opted for kotlin 1.5 but the main idea still exists.</p><h3>Conclusion:</h3><p>Those are some of my reflections on the current state of compose, some of them if not all them are likely to be obsolete as long as compose is evolving rapidly.</p><p>Last but not least, Some people noticed that there are some performance regressions in compose compared to the traditional View system, but genuinely I haven’t investigated that yet.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bf96f4ca2423" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[You’ve 2+ JDKs installed on your machine? Ok, but be careful]]></title>
            <link>https://mr3y.medium.com/youve-2-jdks-installed-on-your-machine-ok-but-be-careful-a6c6937ad26b?source=rss-a2571725a67f------2</link>
            <guid isPermaLink="false">https://medium.com/p/a6c6937ad26b</guid>
            <category><![CDATA[jdk]]></category>
            <category><![CDATA[windows-registry]]></category>
            <category><![CDATA[jre]]></category>
            <category><![CDATA[java-home]]></category>
            <dc:creator><![CDATA[MR3Y]]></dc:creator>
            <pubDate>Fri, 04 Jun 2021 21:59:07 GMT</pubDate>
            <atom:updated>2021-06-04T21:59:07.784Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FtNlKa6Gyjm_DuLVTMcpfA.jpeg" /><figcaption>header by <a href="https://unsplash.com/@virgilcayasa">@virgilcayasa</a> from Unsplash</figcaption></figure><p>I’m writing this article to share my story about an issue on my machine I’ve spent about 2 days solving it.</p><blockquote><strong><em>Tl;DR</em></strong></blockquote><blockquote>if you have more than 1 JDK installed on your windows machine, make sure to configure the one which is widely used by your projects the default on <strong>Windows Registry </strong>not just the PATH or JAVA_HOME environment variables, and configure the other JDKs on demand per each project/IDE.</blockquote><p>On my windows machine, I have two JDKs installed or <em>technically speaking one JDK &amp; one JRE, they are JDK 13.0.1 &amp; JRE 1.8.0_271 </em>respectively. I’m using JDK 13 99% of the time, it is the default one on the PATH environment variable, almost all of the projects on my machine have been built with it, so that, sometimes I forget that I have JRE 1.8 installed! everything is fine so far.</p><blockquote>Note: You might ask why I just don’t simply get rid of JRE 1.8? You’re right but that will lead us to another headache which is beyond the scope of this article</blockquote><p>The problem appeared when I decided to download <a href="https://github.com/gradle/gradle-profiler">gradle-profiler</a> tool in order to benchmark gradle build speed of a Project. I had downloaded the latest release and then I ran gradlew installDist from the command line within the dir we’ve just installed, it gave me the following error 💥💥:</p><pre>Execution failed for task &#39;:client-protocol:compileJava&#39;.<br>&gt; Could not find tools.jar. Please check that C:\Program Files\Java\jre1.8.0_271 contains a valid JDK installation.</pre><p>Why would gradle-profiler want to use JRE 1.8 path? Why it just don’t use JDK 13 like every other single project on my machine?</p><p>Luckily we know where this exception is thrown. In a sub-project :client-protocol ‘s compileJava task which is basically provided by <a href="https://docs.gradle.org/current/userguide/java_plugin.html#java_plugin">java gradle core plugin</a>.</p><p>typing the error message in the search box of github within <a href="https://github.com/gradle/gradle">gradle</a> repository would navigate us to a class called <a href="https://github.com/gradle/gradle/blob/8173acdb88b1fdaf7a7cda9e583800628ad145fb/subprojects/language-java/src/main/java/org/gradle/api/internal/tasks/compile/JdkTools.java#L76">JdkTools</a> where an IllegalStateException is thrown with a message that matches our error message, the interesting part in that is that JdkTools class uses a function called jvm.getJavaHome to know about the java home on your machine, So, what does this method actually do? Let’s dig into <a href="https://github.com/gradle/gradle/blob/8173acdb88b1fdaf7a7cda9e583800628ad145fb/subprojects/base-services/src/main/java/org/gradle/internal/jvm/Jvm.java">Jvm</a> class to find out.</p><p>We can see that <a href="https://github.com/gradle/gradle/blob/8173acdb88b1fdaf7a7cda9e583800628ad145fb/subprojects/base-services/src/main/java/org/gradle/internal/jvm/Jvm.java#L256">getJavaHome()</a> returns a private <a href="https://github.com/gradle/gradle/blob/8173acdb88b1fdaf7a7cda9e583800628ad145fb/subprojects/base-services/src/main/java/org/gradle/internal/jvm/Jvm.java#L41">javaHome</a> field which got initialized <a href="https://github.com/gradle/gradle/blob/8173acdb88b1fdaf7a7cda9e583800628ad145fb/subprojects/base-services/src/main/java/org/gradle/internal/jvm/Jvm.java#L85">on creating the constructor of </a><a href="https://github.com/gradle/gradle/blob/8173acdb88b1fdaf7a7cda9e583800628ad145fb/subprojects/base-services/src/main/java/org/gradle/internal/jvm/Jvm.java#L85">Jvm instance</a> with the value of System.getProperty(&quot;java.home&quot;)</p><p>Copying System.getProperty(&quot;java.home&quot;) &amp; running it locally on my machine would print:</p><pre>C:\Program Files\Java\jre1.8.0_271</pre><p>As you can see, here is the core of the problem. why does “java.home” key is pointing to JRE 1.8 path? how can JRE 1.8 path be discovered when I have JDK 13 configured by default? Just to make sure I’ve nothing wrong up to this point I ran javac -version &amp; java -version. both gave me the correct JDK 13 which is on my PATH .</p><p>Also, I’ve checked <a href="https://docs.oracle.com/javase/tutorial/essential/environment/sysprop.html">System Properties Oracle official doc</a> and we can see that “java.home” is:</p><blockquote>Installation directory for Java Runtime Environment (JRE)</blockquote><p>So, according to the documentation, it should point to JAVA_HOME\bin path on my machine, but Obviously, this is not the case here.</p><p>I started to make use of traditional solutions such as:</p><ul><li>override “java.home” location explicitly from the command line by using -Djava.home=&quot;C:\\Program Files\\Java\\jdk-13.0.1\\bin&quot;</li><li>running the given task from an IDE like Intellij or Eclipse and adding -Djava.home=&quot;C:\\Program Files\\Java\\jdk-13.0.1\\bin&quot; to VM parameters within Run Configurations</li><li>override “java.home” location <em>programmatically </em>by using<em> </em><em>System.setProperty(&quot;java.home&quot;, &quot;JRE_13_path&quot;)</em></li><li>Download the missing tools.jar manually and add it the JRE 1.8 path</li></ul><p>Unfortunately, all of those solutions &amp; other naive solutions didn’t make any difference. I started to burn out &amp; giving up but I’ve remembered one last thing I could try which is <a href="https://en.wikipedia.org/wiki/Windows_Registry"><em>Windows Registry</em></a><em>.</em></p><p>I know that some apps take advantage of Windows Registry to store some preferences in a form of key/value. And here is where I have discovered that Java Runtime Environment is being set to JRE 1.8 path, I’ve changed it to JDK 13 path, restarted my machine and finally everything worked like a charm!</p><h3>Conclusion:</h3><p>I don’t know but I wasn’t aware that windows registry could be the root of the problem. I’ve searched online &amp; got many solutions but none of them was talking about the windows registry. even the tutorials that help you install java on your machine, they only talk about adding java to your PATH after installation. Anyway, I just wanted to document my story debugging this issue to make sure I’m not going to fall in that mistake twice. Also, to give a reference for anyone who might get stuck by a similar problem.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a6c6937ad26b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[The argument of declaring constants]]></title>
            <link>https://mr3y.medium.com/the-argument-of-declaring-constants-82d79bae659a?source=rss-a2571725a67f------2</link>
            <guid isPermaLink="false">https://medium.com/p/82d79bae659a</guid>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[performance]]></category>
            <category><![CDATA[android-runtime]]></category>
            <category><![CDATA[kotlin]]></category>
            <dc:creator><![CDATA[MR3Y]]></dc:creator>
            <pubDate>Thu, 21 Jan 2021 23:47:12 GMT</pubDate>
            <atom:updated>2021-06-05T23:56:49.124Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cNmAzUXSdjSpEtTOEzL3PQ.jpeg" /><figcaption>header by <a href="http://twitter.com/alevisionco">@alevisionco</a> from Unsplash</figcaption></figure><h3>Introduction</h3><p>Every piece of code in any codebase has to work with some values, those values come in many forms/syntax, Some of them are <strong><em>variables </em></strong>that vary over time and some others are just <strong><em>constants</em></strong>. the latter isn’t something new in programming languages, it has been addressed by early programming languages designers and Hence they provided first-class support for declaring constants.</p><p>In Kotlin we have 3 main different options to declare a constant. One of those options is keeping the constants shared in one object, this is simple enough and doesn’t have any bad consequences for usability, maintainability or performance but this is only suitable for global constants, So in order to create some locally scoped constants, we have to go with the other 2 options. those 2 options created some confusion for us as developers on When to use option <strong>X</strong> over option <strong>Y</strong>, so I’m gonna talk about those 2 options and a third option which is creating constants with just a val without const modifier from usability and performance perspectives.</p><h4>1. First Option: Declare Constants as a top-level values in the same file</h4><p>As kotlin is a functional programming language unlike Java, we can declare top-level functions, values directly without needing for a class to wrap them, So we do some thing like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a840a8dbeee252d25d9423d9c30b05c7/href">https://medium.com/media/a840a8dbeee252d25d9423d9c30b05c7/href</a></iframe><p>But anyway this is gonna be converted to Java bytecode when compiled, So how Kotlin tell Java about top-level constants because Java doesn’t have such a concept, If we look at the decompiled bytecode, we will see:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6d9d2eccb20ff4f3b0ad098ae087c120/href">https://medium.com/media/6d9d2eccb20ff4f3b0ad098ae087c120/href</a></iframe><p>Kotlin will create a class with the name of our file for us and all of the constants will be marked as static , So when invoking a constant from those no object allocation will happen or unnessacary object creation, Thus we see that this option has no overhead at runtime, but wait before you judge we still don’t know about other options, Also some people(including me in some situations) will not prefer this option because declaring constant as a top-level sometimes doesn’t provide a strong indication (compared to other options) that it is related to class/functions defined in the same source file.</p><h4>2. Second Option: Declare constants in a companion object</h4><p>This the most common way and the first thing that comes to your mind when you need to declare a constant, We do that like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e375d42c65e539f8626617eff462d36f/href">https://medium.com/media/e375d42c65e539f8626617eff462d36f/href</a></iframe><p>if We look at the decompiled bytecode this time:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d4684b73234c158f6c3118492b55b06f/href">https://medium.com/media/d4684b73234c158f6c3118492b55b06f/href</a></iframe><p>Similar to what we saw before but this time with a new class called Companion created for us and also a new instance of it created automatically, in comparison with the previous approach we can see the previous one outperforms this approach from a performance perspective because of the new unnecessary object allocated But on the other hand this approach represent the constants in a more structured and semantic way compared to the previous one.</p><blockquote><strong>NOTE</strong>: Using R8 with optimization enabled can eliminate/remove this unnecessary object allocation and move the field/method to outer class (i.e Wrapper ) plus making it a static instead of member/virtual property. This Process is called <em>Staticization.</em></blockquote><p>If you haven’t heard of this term before? check <a href="https://jakewharton.com/r8-optimization-staticization/">R8 Optimization: Staticization by Jake Wharton</a></p><p>We can now easily choose what is more convenient For us, So are we done yet? No, there is third option we haven’t covered yet.</p><h4>3. Third Option: Declaring constant with just val without const</h4><p>We all know that val is the equivalent of final in java so it can be used for declaring constants as well but without using const modifier, actually this will be the only option if we have to declare something that is not primitive or String, but can we compare that option to other options in terms of performance, Let’s see:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/69d1e6fee5aea70e6d537008c1f89d5b/href">https://medium.com/media/69d1e6fee5aea70e6d537008c1f89d5b/href</a></iframe><p>If we look at the decompiled bytecode:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2caadfb494aaca92093272b3c29059a7/href">https://medium.com/media/2caadfb494aaca92093272b3c29059a7/href</a></iframe><p>As you can see, nothing fancy is happening here. it is merely identical to the decompiled bytecode of 2 previous approaches. But let’s make a small change: we will change the access modifier of the field from private to public</p><p>will it differ from the previous? Let’s see:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cac90aa35a3c9679709589a842bc2c04/href">https://medium.com/media/cac90aa35a3c9679709589a842bc2c04/href</a></iframe><p>And the decompiled bytecode:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d6d5b461cf34d7b89c9db9ee5b72c84b/href">https://medium.com/media/d6d5b461cf34d7b89c9db9ee5b72c84b/href</a></iframe><p>similar to the previous, but this time with a public getFoo() generated for us and changing our public field to private one, So we can access the field through its getter method, and that may annoy some of you who still think accessing getter method is more expensive than accessing the field directly.</p><p>Fortunately, Android Runtime is smart enough to inline trivial getter method like the one we have above, So you don’t get any performance hit. <a href="https://medium.com/androiddevelopers/busting-android-performance-myths-d85ae1d87f22">see this article for details</a>.</p><p>Moreover, not just the ART that is smart, R8/Proguard also could inline(Assuming optimization is enabled) that method for you as well. So if you don’t believe yet in ART optimization, you will have R8 do it for you.</p><p>So, what to do now?</p><p>Basically, I have shown you the different options and shared my humble opinion with you. Personally, I prefer approach #2. But in the end, choose whichever convenient for your situation/use case.</p><h4><strong>Still not Convinced/Hungry? I got more References for you:</strong></h4><p>1- <a href="https://bladecoder.medium.com/exploring-kotlins-hidden-costs-part-1-fbb9935d9b62">Great series by Christophe beyls</a></p><p>2- <a href="https://stackoverflow.com/questions/44038721/constants-in-kotlin-whats-a-recommended-way-to-create-them">This StackOverflow post</a></p><p>3- <a href="https://jakewharton.com/androids-java-8-support/">A series by Jake Wharton on R8, D8 Optimizations</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=82d79bae659a" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Use jetpack compose correctly!]]></title>
            <link>https://mr3y.medium.com/use-jetpack-compose-correctly-86b3f53e5975?source=rss-a2571725a67f------2</link>
            <guid isPermaLink="false">https://medium.com/p/86b3f53e5975</guid>
            <category><![CDATA[android]]></category>
            <category><![CDATA[jetpack-compose]]></category>
            <category><![CDATA[kotlin]]></category>
            <dc:creator><![CDATA[MR3Y]]></dc:creator>
            <pubDate>Sun, 27 Sep 2020 01:53:20 GMT</pubDate>
            <atom:updated>2021-06-04T22:10:37.374Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*40Qq7ACohuIJgcPbyItw1Q.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@gabebarletta?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Gabriel Barletta</a> on <a href="https://unsplash.com/s/photos/compose?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>It is not a surprise anymore what the world of building UIs on mobile screens will be in the upcoming years, finally, we can get rid of all XML pain and rely on the reactive programming paradigm to not only build beautiful UI but also keep our code and architecture clean.</p><p>It’s been a month now since releasing the alpha version of Jetpack Compose, And it is pretty obvious that compose had been welcomed from the wider android community, Personally, I’m very excited about it and I play with it regularly to discover its new capabilities So, here in this article I’m gonna share some best practices and tips I found them very helpful when getting to work with compose. Let’s Start :)</p><h3>1- Making custom states:</h3><p>if you’ve ever worked with compose chances are you used state in many places you need scrollState? compose replies: we’ve got remeberScrollState for you, swipeState? no problem, we have also rememberSwipeableState For you. And state doesn’t just stop here for examples Colors class implemented like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5f750fd41a2ada7deecc289e9f7c7a67/href">https://medium.com/media/5f750fd41a2ada7deecc289e9f7c7a67/href</a></iframe><p>So, if I want to save and remember a state to make some decisions based on this state(e.g button is enabled when text isn’t empty) I can do it in fairly simple way like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8e35fa925c56615b5739042aeb0358b5/href">https://medium.com/media/8e35fa925c56615b5739042aeb0358b5/href</a></iframe><p>We can take it one step further and implement our own rememberTextState</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3e27128ef3d0370a5b92a8ca75c60791/href">https://medium.com/media/3e27128ef3d0370a5b92a8ca75c60791/href</a></iframe><p>With that we keep “state” of our components in manageable maintainable way .</p><h3>2- Stateful/Stateless Components:</h3><p>continue speaking of states we can make our composable functions code much more simple and keep our architecture clean if we’ve leveraged stateless/stateful technique.</p><p>briefly, by applying this to any composable fun we’ll end up with two functions:</p><ol><li>one that is responsible for managing/controlling state of components displayed on screen but doesn’t actually display them</li><li>the second function that is responsible for displaying components on screen but doesn’t need to worry about state</li></ol><p>Seems ambiguous in theory, let’s take an example From some compose code I wrote for <a href="https://github.com/android/sunflower">SunFlower</a> app:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/33532fc8131872f3c05c9e3c40646375/href">https://medium.com/media/33532fc8131872f3c05c9e3c40646375/href</a></iframe><p>You get it!! the second composable(stateless) doesn’t know anything about state of our plants it just displays them, this also make stateless PlantListScreen more reusable in other places of codebase</p><p><strong>But wait!!!</strong> every best practice is good when you apply it correctly, Don’t throw stateless/stateful components everywhere in your code you’ll end up duplicating a lot of code leading to annoying boilerplate. Only apply this pattern with top-level composables(e.g SearchScreen which displays the entire search Screen) but it seems inconvenient to do that with things like buttons or Texts.</p><h3>3- Using layoutId to find or mark certain items:</h3><p>Have you missed “android:id=…”, findViewById and all that stuff related to ids in XML/view system? (I think no one would miss findViewById we have ViewBinding 😁😁)</p><p>Well, compose has a handy fun called Modifier.layoutId() it is very handy tip when you want to tell compose: hey, you remember the item with that id, GIVE IT TO ME</p><p>let’s take an example, consider if I want to make a custom layout and I want to measure and layout an item in different way than others, code will look something like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a6f88f9f31f069595e91958f267ebbb8/href">https://medium.com/media/a6f88f9f31f069595e91958f267ebbb8/href</a></iframe><p>So, as you saw I can distinguish between many items and treat each one solely</p><h3>4- Make reusable Composables generic as much as possible:</h3><p>At first look, You may say this is trivial and intuitive thing to know, who can mistake in something like that? but I bet you’ll get amazed just like me when you look at how it is implemented in <a href="https://github.com/android/compose-samples/tree/master/Rally">Rally</a> (One of the official compose samples)</p><p>In this sample there are two screens(Accounts, Bills) have much in common in their structure, see GIF:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/1*fxlKWwFQ9gU0bHU_8Si4Lw.gif" /><figcaption>rally.gif</figcaption></figure><p>As you see, we need an agnostic shared composable able to display two screens, and here’s where StatementBody come to play, its implementation is fairly simple just like that:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/adc8bf6d37badadff1c2faf3b567ab0f/href">https://medium.com/media/adc8bf6d37badadff1c2faf3b567ab0f/href</a></iframe><p>And so Accounts and Bills screens can be implemented like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3579ba620ca4d966af80f36cfd9dba6f/href">https://medium.com/media/3579ba620ca4d966af80f36cfd9dba6f/href</a></iframe><p>Pretty neat!, also sample has beautiful animations I recommend you to <a href="https://github.com/android/compose-samples/tree/master/Rally">check it out</a>.</p><h3>5- the power of Ambients</h3><blockquote>If I have to choose something compose can do it like a magic definitely it will be Ambients</blockquote><p>Briefly if you don’t know what ambients is?</p><ul><li>it is a way to flow data down the tree to lower composables without leaking private implementation details. You’ll understand more with an example</li></ul><p>We have ton of built-in Ambients like ContextAmbient , DensityAmbient …etc, And that is because implementing your custom ambient is quite easy, let’s take an example:</p><p>say if we want in some composable function to know current configuration of screen like screenWidth to make some calculations based on that, how it will look like in the code? (Note: compose has already a built-in ConfigurationAmbient but let’s make simple one to understand the idea)</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5f47c2698f90920d7e4abfbaab2c546e/href">https://medium.com/media/5f47c2698f90920d7e4abfbaab2c546e/href</a></iframe><p>You see!! with just three or two lines of code we made our custom ambient that any composable can call it and tell it: hey, what is the current configuration of you because I really need to know the current width available</p><p>We can extend it more with <a href="https://en.wikipedia.org/wiki/Proxy_pattern">ProxyPattern</a> to make this configuration part of our theme, like:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8c80fdec96cebefc7d108e2a3c0ddcd6/href">https://medium.com/media/8c80fdec96cebefc7d108e2a3c0ddcd6/href</a></iframe><p>With that our AppTheme becomes a single source of truth for any unified shared Info.</p><p>This is the end of this article I hope you have now better understanding of how to make complex composables without sacrificing on things like architecture and if you want to deep dive into compose you may already know who to follow! it is him <a href="https://twitter.com/intelligibabble?lang=ar">Leland Richardson</a>. 👏 👏</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=86b3f53e5975" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[definitely, there is a difference between cancelling Job and cancelling scope.]]></title>
            <link>https://mr3y.medium.com/definitely-there-is-a-difference-between-cancelling-job-and-cancelling-scope-cfa0f3a6ca73?source=rss-a2571725a67f------2</link>
            <guid isPermaLink="false">https://medium.com/p/cfa0f3a6ca73</guid>
            <dc:creator><![CDATA[MR3Y]]></dc:creator>
            <pubDate>Wed, 16 Sep 2020 22:27:32 GMT</pubDate>
            <atom:updated>2020-09-16T22:27:32.810Z</atom:updated>
            <content:encoded><![CDATA[<p>definitely, there is a difference between cancelling Job and cancelling scope.</p><p>You can think of a job as the `child` of scope, So a scope can have children(i.e..jobs) but Job has only one parent. That being said cancelling a job won&#39;t cancel its siblings unlike cancelling scope which cancels children beneath it</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cfa0f3a6ca73" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>