<?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 Maryam Dashtirahmatabadi on Medium]]></title>
        <description><![CDATA[Stories by Maryam Dashtirahmatabadi on Medium]]></description>
        <link>https://medium.com/@maryam-dashtirahmatabadi?source=rss-8998a5aff141------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*rCG47so6UpIsdjNq13WCQw.jpeg</url>
            <title>Stories by Maryam Dashtirahmatabadi on Medium</title>
            <link>https://medium.com/@maryam-dashtirahmatabadi?source=rss-8998a5aff141------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 11:53:17 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@maryam-dashtirahmatabadi/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[Everything About the Kotlin Nullability]]></title>
            <link>https://maryam-dashtirahmatabadi.medium.com/everything-about-the-kotlin-nullability-77b88454130f?source=rss-8998a5aff141------2</link>
            <guid isPermaLink="false">https://medium.com/p/77b88454130f</guid>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[nullability]]></category>
            <dc:creator><![CDATA[Maryam Dashtirahmatabadi]]></dc:creator>
            <pubDate>Tue, 14 Dec 2021 17:37:17 GMT</pubDate>
            <atom:updated>2022-01-02T11:59:05.275Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YlRNvHWdnQSVsmGrkaWgSA.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@mreichelt?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Marc Reichelt</a> on <a href="https://unsplash.com/s/photos/kotlin?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>With the help of the Kotlin nullability feature, we can better handle the NullPointerException because the compiler can detect many of those problems in compile-time which leads to fewer errors in runtime. So let&#39;s see how Kotlin works like that! 🥳</p><p>The below snippet is a simple function in Java. If we try to pass a null argument to that function, it will throw a NullPointerException. So we need to check the passed value before using that.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/aff9bd020e6811785633e9db533a8a1e/href">https://medium.com/media/aff9bd020e6811785633e9db533a8a1e/href</a></iframe><p>Now let’s rewrite that function in Kotlin. If we want to pass a null argument to that, we have to add a question mark (?) after the type name so in Kotlin all regular types are by default not null unless we mark them as nullable.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bb5571a17cc2e5d66c4175ff105a856b/href">https://medium.com/media/bb5571a17cc2e5d66c4175ff105a856b/href</a></iframe><p>But what happens if we try to call the above function? we will receive the below error:</p><pre><strong>Only safe (?.) or non-null asserted (!!.) calls are allowed on a nullable receiver of type String?</strong></pre><p>And the solution is to add a check for null:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d7cd4509276af8c9c19b5edfabbfe199/href">https://medium.com/media/d7cd4509276af8c9c19b5edfabbfe199/href</a></iframe><p>The above code snippet is one of the possibilities, but Kotlin has other ways which are more concise. Let’s go through them!</p><h4>The Safe Call Operator (?.) :</h4><p>What happens when we use this operator? 🧐</p><p>If the value on which we are trying to call a method, is not null, the method will be executed otherwise if it is null, the method will not be called and instead of that, a null value will be used. The previous example is written with this operator below:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2b658a700fa876c64d373a87ac0bf1d7/href">https://medium.com/media/2b658a700fa876c64d373a87ac0bf1d7/href</a></iframe><p>Be careful that we can not assign the return type of the above example which can be null to a not null type such as String because of that reason, I have changed the return type to the String? otherwise, the below error will happen:</p><pre><strong>Type mismatch: inferred type is String? but String was expected</strong></pre><p>One of the best cases of using this operator is when we need to check several nullabilities on different values so we can get rid of those checks and just create a chain just like the below snippet:</p><pre><strong>val result: String? = value1?.value2?.value3</strong></pre><h4>The Elvis Operator (?:) :</h4><p>There are some situations that we want to return a default value instead of null which can be handled with this operator easily. The Elvis operator takes two values. The first one which is before the ?: sign will be used whenever it is not null otherwise the second value will be considered as a result.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6b38faf458980ea852c04748c074e733/href">https://medium.com/media/6b38faf458980ea852c04748c074e733/href</a></iframe><h4>The Not-null Assertion Operator (!!) :</h4><p>When we want to use this operator, a value of a nullable type must be written before a double exclamation mark. If the value is null, an exception will be thrown at runtime otherwise the value itself will be used so we have to be very careful with this approach as we do not want to throw an explicit exception and maybe catch it later! 🙄</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a8028f7d1e71fbb177a9d19fddb005ce/href">https://medium.com/media/a8028f7d1e71fbb177a9d19fddb005ce/href</a></iframe><h4>The “let” Function :</h4><p>If we want to handle a nullable argument that should be passed to a function that expects a non-null parameter, we can use the let function via a safe call operator. If the value of that nullable type is not null, the let function will execute a lambda otherwise nothing will happen. Here is an example:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4acf768cf428899ba29093d0d60c4329/href">https://medium.com/media/4acf768cf428899ba29093d0d60c4329/href</a></iframe><h4>Late Initialized Properties (“lateinit” Modifier) :</h4><p>Kotlin usually needs to initialize all properties in the constructor, and if a property has a non-null type, you have to provide a non-null initializer value. But what happens if you can not provide that value? (for example when the property will be initialized through the dependency injection, or in the setUp() function of a test class). Then, you have to use a nullable type instead, and for every access to that property, you need to use either a null check or the !! operator which is not a good approach. The best solution is to declare that property as late initialized with the help of the lateinit modifier. Look at the below code sample of one of my test classes:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5064d7d4d3ee279c52dd5e414abb0e0b/href">https://medium.com/media/5064d7d4d3ee279c52dd5e414abb0e0b/href</a></iframe><h4>The Nullability of Type Parameters :</h4><p>Let’s start this topic with an example!</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5a5410ef3ed7afba9173ba13c1af3e8c/href">https://medium.com/media/5a5410ef3ed7afba9173ba13c1af3e8c/href</a></iframe><p>If we try to run the above snippet code, we will receive the below error:</p><pre><strong>&#39;&#39;lateinit&#39;&#39; modifier is not allowed on nullable properties</strong></pre><p>As we have already known that the lateinit modifier must always be non-null, but the T generic type consists of all types even the null, so the above error happens. The main reason is that the T extends from the Any? type (which is nullable). That means class Test&lt;T&gt; { … } is equal to class Test&lt;T: Any?&gt; { … }. The solution here is to specify a non-null upper bound for the generic type such as class Test&lt;T: Any&gt; { … }.</p><h4>Nullable Extensions :</h4><p>There is a very good definition of this topic in the <strong>Kotlin in Action</strong> book:</p><blockquote>Defining extension functions for nullable types is one more powerful way to deal with null values. Rather than ensuring that a variable can’t be null before a method call, you can allow the calls with null as a receiver, and deal with null in the function. This is only possible for extension functions; regular member calls are dispatched through the object instance and therefore can never be performed when the instance is null.</blockquote><p>So let’s write an extension function on a nullable type:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3675fa425f63c5f55ca20ffad88f41a7/href">https://medium.com/media/3675fa425f63c5f55ca20ffad88f41a7/href</a></iframe><p>Now you can call the showErrorMessage function on a nullable type without any check for the possible null values:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c742aed998b8b7ffa6935c087a48d5ed/href">https://medium.com/media/c742aed998b8b7ffa6935c087a48d5ed/href</a></iframe><h4>Nullable Functions :</h4><p>Kotlin provides a feature that a function can take other functions as its arguments so we can store them inside variables. If that function that will be used as an argument is nullable, we have to write a null check for that since there is a possibility of a null pointer exception. There are some solutions for that as the below code sample:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/77def8c9b958c96ecd362f2e6fa35e39/href">https://medium.com/media/77def8c9b958c96ecd362f2e6fa35e39/href</a></iframe><p>That was my short article about one of the most favorite topics in Kotlin. I hope that you liked it, also feel free to write your comments and thoughts here! 🤩</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=77b88454130f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Migrate Dagger-Android to Dagger]]></title>
            <link>https://maryam-dashtirahmatabadi.medium.com/migrate-dagger-android-to-dagger-25a95b5fb189?source=rss-8998a5aff141------2</link>
            <guid isPermaLink="false">https://medium.com/p/25a95b5fb189</guid>
            <category><![CDATA[dagger-2]]></category>
            <category><![CDATA[dependency-injection]]></category>
            <category><![CDATA[android]]></category>
            <dc:creator><![CDATA[Maryam Dashtirahmatabadi]]></dc:creator>
            <pubDate>Thu, 09 Sep 2021 16:27:20 GMT</pubDate>
            <atom:updated>2021-09-09T16:33:54.548Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9tQW1OcsjKmyk7VLaOleFg.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@kylejglenn?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Kyle Glenn</a> on <a href="https://unsplash.com/s/photos/migrate?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>As I started refactoring one of my Android projects, I came up with the dependency injection part which used Dagger-Android. Since it’s been recommended not to use it anymore (I’m not going to the details here!) I replaced it with the pure Dagger.</p><p>Here I’ll show it with very simple steps! 🤩</p><p>First of all, you need to change the Dagger dependencies. You can get rid of those android ones like the below snippet:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5ff89c3caab701755aec8bd744777379/href">https://medium.com/media/5ff89c3caab701755aec8bd744777379/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0ee4abcd3758b70409dbda50c11b2b22/href">https://medium.com/media/0ee4abcd3758b70409dbda50c11b2b22/href</a></iframe><p>Now let’s change your base application class which is called App inside my project. In the Dagger-Android we used the DaggerApplication() but for Dagger, there is no use for that anymore and instead of that extend your base application class from the Application()and for now, just initialize your AppComponent inside that. So change your base application class as below:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/642af607f97d26904fe0bba3ce828c94/href">https://medium.com/media/642af607f97d26904fe0bba3ce828c94/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/47d09235efaed454a869ae65fe6796c1/href">https://medium.com/media/47d09235efaed454a869ae65fe6796c1/href</a></iframe><p>Now let’s change the AppComponent class. For now, remove the AndroidInjectionModule and the ActivityBuildersModule, also there is no need to extend this interface from the AndroidInjector.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6b4e7d41ae3ba83c3cf9b9b87e22dccc/href">https://medium.com/media/6b4e7d41ae3ba83c3cf9b9b87e22dccc/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/439d3c3135e5fcc3b477008318e4d6b2/href">https://medium.com/media/439d3c3135e5fcc3b477008318e4d6b2/href</a></iframe><p>With Dagger-Android I had general classes for ViewModelKey and ViewModelFactory but inside the Dagger version because we are going to have the subcomponents we need to separate them for each component e.i. for the main component these classes will be named as MainViewModelKey and MainViewModelFactory. I also scope the factory class with its custom scope which is the MainScope. In this step, you can delete any FragmentsModule (or FragmentsBuilderModule) as these classes will not be used in Dagger anymore. Below you can see that I had the MainFragmentsModule which was responsible for binding the fragments inside the MainActivity :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0108de5b8efcc332a48acb1b793f21af/href">https://medium.com/media/0108de5b8efcc332a48acb1b793f21af/href</a></iframe><p>Now let’s build the subcomponents of the AppComponent as the below snippet:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d9c0664601f65a99b54f4d05704aa9b1/href">https://medium.com/media/d9c0664601f65a99b54f4d05704aa9b1/href</a></iframe><p>Notice that the MainViewModelModule class is now responsible for binding the MainViewModelFactory and all fragments’ ViewModel inside that activity. As I have two fragments for my MainActivity this module for the current subcomponent looks like the below snippet:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/258828bcc300c6ea4bc811c0730ef2b3/href">https://medium.com/media/258828bcc300c6ea4bc811c0730ef2b3/href</a></iframe><p>Here we can delete all the custom modules for the fragments such as my HomeModule which was responsible for providing its ViewModel or binding its repository and other dependencies. Instead of that class, inside the MainModule we are going to provide them:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/53221aac59242a809425b24de0039346/href">https://medium.com/media/53221aac59242a809425b24de0039346/href</a></iframe><p>Now you need to associate the subcomponents to the AppComponent with the help of a module class as the below:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/31406f70b394c480759c763057aef2b0/href">https://medium.com/media/31406f70b394c480759c763057aef2b0/href</a></iframe><p>And then just inside the AppComponent module’s list, add the SubComponentsModule . As we want to create those subcomponents and have access to them later inside other classes, we are going to add them inside the AppComponent too. So the final look of our AppComponent will be like the below snippet:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f7b1bef80669a8b0829cf72ed6f9c2f4/href">https://medium.com/media/f7b1bef80669a8b0829cf72ed6f9c2f4/href</a></iframe><p>As I have an abstract activity which is the superclass for my activities, I’m going to create a function inside that with the responsibility of adding all of this super activity’s children to the dagger graph. So my AbstractActivity will look like as below:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fa37f17642b21b1c02a6207d9c9bc1a3/href">https://medium.com/media/fa37f17642b21b1c02a6207d9c9bc1a3/href</a></iframe><p>Then the inject() function will be overwritten inside all activities. For that purpose, we need a reference of that subcomponent which here is the MainComponent and we’re gonna provide it inside our base application class. So my App class will look like as below:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b70f32a3514b285bac899699d9ccb9ce/href">https://medium.com/media/b70f32a3514b285bac899699d9ccb9ce/href</a></iframe><p>Now we are able to inject the MainActivity to the dagger graph with the help of the previous changes:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/96a3009147108a0cfac673824a1462e0/href">https://medium.com/media/96a3009147108a0cfac673824a1462e0/href</a></iframe><p>I’m gonna use the field injection for the fragment dependencies. So I need to add inject() function for each fragment inside its related subcomponent and then call that inject() method inside the onAttach() of fragments. So my MainComponent will be changed as:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d765f3d285525b5d1a9d3d04e18ef116/href">https://medium.com/media/d765f3d285525b5d1a9d3d04e18ef116/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0b936e0c9e48b110f257aef001f1ec02/href">https://medium.com/media/0b936e0c9e48b110f257aef001f1ec02/href</a></iframe><p>That was the full migration from Dagger-Android to the pure Dagger! 😎</p><p>You can use other libraries for dependency injection in Android applications such as Hilt or Koin as well. In the upcoming articles, I’m gonna share my experience using them.</p><p>Feel free to leave comments below! 🙌🏼</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=25a95b5fb189" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to Use Context Correctly in Android]]></title>
            <link>https://maryam-dashtirahmatabadi.medium.com/how-to-use-context-correctly-in-android-93e775cf27c5?source=rss-8998a5aff141------2</link>
            <guid isPermaLink="false">https://medium.com/p/93e775cf27c5</guid>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[context]]></category>
            <category><![CDATA[android]]></category>
            <dc:creator><![CDATA[Maryam Dashtirahmatabadi]]></dc:creator>
            <pubDate>Mon, 28 Dec 2020 17:38:14 GMT</pubDate>
            <atom:updated>2020-12-28T17:38:14.181Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3MeZ82ZGV0WZH43skjs6iw.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@redaquamedia?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Denny Müller</a> on <a href="https://unsplash.com/s/photos/android?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>The Context in Android is one of the most important objects and actually is the Context of the current state of the application with major responsibilities such as:</p><ul><li>Getting information regarding the activity and application.</li><li>Getting access to the database, application-specific resources (strings, drawable, …) and classes, filesystems, and shared preferences.</li><li>Up-calls for application-level operations such as launching activities, broadcasting, and receiving intents.</li></ul><p>As you can see, almost everything in Android needs access to Context, so if we use it wrong, it can lead to memory leaks. Mainly there are two types of Context:</p><ul><li><strong>Application Context:</strong> This Context is bound to the life cycle of an application. That means as long as the application has not been killed, it is available. Chiefly it is a singleton instance that can be accessed from your application class via getApplicationContext(). The application Context can be used where you need a Context whose lifecycle is separate from the current Context. The main point about application Context is that it is not UI related Context. So, if we want to start an activity by using an intent or showing a toast or anything else related to the UI, even If we are fetching something that should be themed, we shouldn’t use the application Context. Notice that holding a reference to activity in a long-living object or thread can cause a memory leak. In this case, also use the application Context. Below is the list of application Context functionalities:</li></ul><blockquote><em>1. Load resource values,<br>2. Start a service,<br>3. Bind to a service,<br>4. Send a broadcast,<br>5. Register broadcastReceiver.</em></blockquote><ul><li><strong>Activity Context: </strong>As it is obvious this Context is bound to the life cycle of activity and is accessible as long as activity lives via getContext(). We should use the activity Context when we want to call the Context from only the current running activity and for operations related to the UI such as:</li></ul><blockquote><em>1. Load resource values,<br>2. Layout inflation,<br>3. Start an activity,<br>4. Show a dialog or a toast<br>5. Start a service,<br>6. Bind to a service,<br>7. Send a broadcast,<br>8. Register broadcastReceiver.</em></blockquote><p>Other than above mentioned getApplicationContext() and getContext(), you might have seen getBaseContext() and this as the Context in Android. getBaseContext() is the method of ContextWrapper which is:</p><blockquote>Proxying implementation of Context that simply delegates all of its calls to another Context. Can be subclassed to modify behavior without changing the original Context.</blockquote><p>With getBaseContext() we can fetch the existing Context inside the ContextWrapper class.</p><p>Also this<em> </em>refers to the instance of the class and can be used whenever the Context is needed inside an activity. Below are a few use cases that require a Context object using Java and Kotlin:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/36886dbd5a26f9bd93b3d139c90919ab/href">https://medium.com/media/36886dbd5a26f9bd93b3d139c90919ab/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a65c5eb1e9e42739ae846d03ea7c425c/href">https://medium.com/media/a65c5eb1e9e42739ae846d03ea7c425c/href</a></iframe><h4>References:</h4><ul><li><a href="https://www.geeksforgeeks.org/what-is-context-in-android/">What is Context in Android? - GeeksforGeeks</a></li><li><a href="https://medium.com/@banmarkovic/what-is-context-in-android-and-which-one-should-you-use-e1a8c6529652">What is Context in Android and which one should you use?</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=93e775cf27c5" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Why We Should Use Custom Listeners?]]></title>
            <link>https://maryam-dashtirahmatabadi.medium.com/why-we-should-use-custom-listeners-1d912dc30adf?source=rss-8998a5aff141------2</link>
            <guid isPermaLink="false">https://medium.com/p/1d912dc30adf</guid>
            <category><![CDATA[listener]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[java]]></category>
            <dc:creator><![CDATA[Maryam Dashtirahmatabadi]]></dc:creator>
            <pubDate>Mon, 02 Nov 2020 08:33:13 GMT</pubDate>
            <atom:updated>2020-11-03T06:07:40.465Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AZPqGlTffr4E0hhR-swVkw.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@cookiethepom?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Cookie the Pom</a> on <a href="https://unsplash.com/s/photos/pc?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>If you are getting data by using a web service, you should wait until the data is successfully fetched (or even if it’s not successful) then take your next action. How would you implement it? Maybe by using an OnDataLoadedListener ?🧐.</p><p>The custom listener (listener/observer pattern) is used to emit events to a single parent for the purpose of communicating information asynchronously in order to run the code when an event occurs which helps us to move complicated logic out from our code. This Java pattern can be used in so many cases such as the communication from a fragment to activities, between two fragments through activity, between an adapter and an activity, Etc in Android. There are already several installed listeners within the Android and the very familiar one is when we want to attach a click event to a view which will be looking like the below snippet code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b0d0b826888e3c834107e511481f4031/href">https://medium.com/media/b0d0b826888e3c834107e511481f4031/href</a></iframe><h3>Implement a Custom Listener in Android</h3><p>The most important key in custom listener implementation is the <a href="https://en.wikipedia.org/wiki/Interface_(Java)"><strong>interface</strong></a>.</p><blockquote>Interfaces are declared using the interface keyword, and may only contain method signature and constant declarations.</blockquote><p>The interface can be defined individually in a separate file in the communication between the two classes, or it can be placed in one of those classes. This class is where we want to call the specific event there, and in our example, it will be an adapter. So let’s define an interface with methods we need in the listener as the following:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5cd45b5bb41ef6ab8d5413eeb88acaf8/href">https://medium.com/media/5cd45b5bb41ef6ab8d5413eeb88acaf8/href</a></iframe><p>As you can see after creating an interface and its method which is called onPhraseClick(int position), I have created a listener instance that will be valued in the setter method of that listener(3rd step). At the last point here I have called the interface method inside the onClick(View v) method of one of my views and pass the position of the current item inside the adapter. When that item is clicked, the next activity will be created and this part will be handled in a related activity to that adapter as below:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f2c88a6aaec9ed376b0ff788fe800a1c/href">https://medium.com/media/f2c88a6aaec9ed376b0ff788fe800a1c/href</a></iframe><p>By implementing OnPhraseClickListener in our activity, its method will be overridden there(2nd step) and then we can access the passed parameters. Last but not the least, set that listener to the adapter instance.</p><p>Notice that there are a lot of situations that you might need to use Custom Listeners not only in Android but also when you code Java natively and I have just shown a very general sample in Android.</p><p>Do not hesitate to share your ideas 🤪.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1d912dc30adf" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to Implement the Parcelable in Android?]]></title>
            <link>https://maryam-dashtirahmatabadi.medium.com/how-to-implement-the-parcelable-in-android-112ac6828b0?source=rss-8998a5aff141------2</link>
            <guid isPermaLink="false">https://medium.com/p/112ac6828b0</guid>
            <category><![CDATA[parcelable]]></category>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[java]]></category>
            <dc:creator><![CDATA[Maryam Dashtirahmatabadi]]></dc:creator>
            <pubDate>Tue, 13 Oct 2020 07:09:40 GMT</pubDate>
            <atom:updated>2020-10-14T16:13:37.129Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hyHNm84_z3LwZ9zsCNHCQA.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@matthewhenry?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Matthew Henry</a> on <a href="https://unsplash.com/s/photos/city?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>This article is about the <a href="https://developer.android.com/reference/android/os/Parcelable">Parcelable</a> interface in Android. I will start with a short definition of it and then implement it using Java and Kotlin. In the end, I will show you how to add it as a plugin in the android studio. So let’s start with this article 👏🏻 …</p><h3>What is Parcelable?</h3><p>With the help of Parcelable, we will be able to pass object references between components of the application such as activities using the intent. However, there is another approach called the <a href="https://developer.android.com/reference/java/io/Serializable">Serialization</a> which will be enabled when a class<strong> </strong>implements the Serializable interface, but it uses reflection and allocates more temporary objects leading to a periodic garbage collection. Hence, the Serialization process is slower than the Parcelable.</p><h3>Parcelable Implementation Using Java</h3><p>At the first step, your model class which in my case is a Task class (with 3 attributes) has to implement Parcelable, but then you will get some warnings and must implement the suggested methods which are describeContents():int and writeToParcel(parcel:Parcel,i:int):void.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3b8b33a971a03076e8434d1a42f67279/href">https://medium.com/media/3b8b33a971a03076e8434d1a42f67279/href</a></iframe><p>Then still you will get another warning which says:</p><blockquote>Your model class implements Parcelable, but does not provide a CREATOR field.</blockquote><p>You should click on the suggested solution which is:</p><blockquote>Add Parcelable Implementation.</blockquote><p>Then it will add firstly, a protected constructor which takes a Parcelable as a parameter and secondly, a static final field called Creator which has our model class as its type and uses that new constructor to create our model class object(Task).</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c4d721ab5629667c78878eb94f04efc0/href">https://medium.com/media/c4d721ab5629667c78878eb94f04efc0/href</a></iframe><p>Now I want to pass a Task object from an activity called MainActivity to another one, ResultActivity using intent as below:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e311fd59778635be925191374f73d8f2/href">https://medium.com/media/e311fd59778635be925191374f73d8f2/href</a></iframe><p>Then inside ResultActivity, we can access the passed object:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a4132f20f01bdabf442a2d4f710571e2/href">https://medium.com/media/a4132f20f01bdabf442a2d4f710571e2/href</a></iframe><h3>Parcelable Implementation Using Kotlin</h3><p>If you are using Kotlin, there is a standard way just as Parcelable implementation using Java which will be looking like the below snippet code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a112f83829981cadc6b98c4a855c2f56/href">https://medium.com/media/a112f83829981cadc6b98c4a855c2f56/href</a></iframe><p>But, there is another way which creates less boilerplate code called <strong>Parcelize </strong>that has been introduced in the Kotlin 1.1.4 release. Firstly add the following snippet to your application build.gradle file:</p><pre>android {<br>    ...<br>    androidExtensions {<br>        experimental = <strong>true<br>    </strong>}<br>}</pre><p>Then add the @Parcelize annotation and Parcelable supertype to your model class:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f55e4caf77fefb3db6336941f95c4533/href">https://medium.com/media/f55e4caf77fefb3db6336941f95c4533/href</a></iframe><p>Then we are going to pass a Task object from MainActivity to ResultActivity:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/60c2855b04877b1b828af00786f9d2f4/href">https://medium.com/media/60c2855b04877b1b828af00786f9d2f4/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9e3275e92f5621ecfaca17778270ce49/href">https://medium.com/media/9e3275e92f5621ecfaca17778270ce49/href</a></iframe><p>And we are done 🤩. Notice that all the required code for the Parcelable implementation is generated by the Annotation Processor and it will get updated automatically without the need to change the model class each time.</p><h3>Parcelable Implementation Using Plugins</h3><p>There are plugins such as <a href="https://github.com/mcharmas/android-parcelable-intellij-plugin">android-parcelable-intellij-plugin</a> that can be imported into Android Studio which helps us to generate the related codes for creating Parcelable. Follow the below path inside Android Studio and then install it: File ➛ Settings ➛ Plugins ➛ Marketplace:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*djjJCGIfNW0GPm3GZ_yUFA.gif" /></figure><p>Then by pressing the alt + insert keys (in Windows OS) inside your model classes, you can find and select the plugin resulting in generating all the relevant codes for Parcelable implementation:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*lVPZMJ7GeNC3LclnHO2iXg.png" /></figure><p>And that was all for this article 😊. I hope it was useful for you. Do not hesitate to share your ideas.</p><h4>References:</h4><ul><li><a href="https://guides.codepath.com/android/using-parcelable">Using Parcelable | CodePath Android Cliffnotes</a></li><li><a href="https://joaoalves.dev/posts/kotlin-playground/parcelable-in-kotlin-here-comes-parcelize/">Parcelable in Kotlin? Here comes Parcelize</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=112ac6828b0" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Custom Toast in Android ]]></title>
            <link>https://maryam-dashtirahmatabadi.medium.com/custom-toast-in-android-c0cffb6d6a97?source=rss-8998a5aff141------2</link>
            <guid isPermaLink="false">https://medium.com/p/c0cffb6d6a97</guid>
            <category><![CDATA[toast]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[java]]></category>
            <dc:creator><![CDATA[Maryam Dashtirahmatabadi]]></dc:creator>
            <pubDate>Wed, 07 Oct 2020 15:11:58 GMT</pubDate>
            <atom:updated>2020-10-07T15:11:58.841Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RdBJR_3EqkXBaQce7NGFVg.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@medbadrc?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Med Badr Chemmaoui</a> on <a href="https://unsplash.com/s/photos/design?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>In this article, I will show you how to create a custom Toast that can have a custom layout. Then we end up with some libraries that help us o create different custom Toast messages like success, error, info Toasts, and Etc.</p><h3>Create a Custom Text for Toast:</h3><p>The Toast class uses a TextView object for showing a message so that if we have access to it, we will be able to change the text as we want. For getting that TextView object, I use the getView() method of Toast class and as it is a non-static method, we can call it by a Toast object as below:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c68b74376165997f5d51b4a666b1ce4e/href">https://medium.com/media/c68b74376165997f5d51b4a666b1ce4e/href</a></iframe><p>Now by calling methods for the TextView, we can make it custom:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f82efeb80ab31dce934fb8e98b12c8a7/href">https://medium.com/media/f82efeb80ab31dce934fb8e98b12c8a7/href</a></iframe><p>And our custom Toast will be looking like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/300/1*zpQtk443iW7OqwpZwSiyIg.gif" /></figure><p>We can also change the view’s background to red color and the position of the Toast as the below snippet code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9cc87d9997fce77fd0f944eb5c2795fb/href">https://medium.com/media/9cc87d9997fce77fd0f944eb5c2795fb/href</a></iframe><p>And the custom Toast will be changing like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/300/1*10K5hbDJu3UAx2wfs7np4A.gif" /></figure><h3>Create a Custom Layout for Toast:</h3><p>For this approach, we need to design our custom layout for Toast as per below:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/304241847ce8f041c20aea34f3a919d2/href">https://medium.com/media/304241847ce8f041c20aea34f3a919d2/href</a></iframe><p>And then we are just going to inflate the layout of our whole container views which will be set as the view for that custom Toast:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/76b6f6442c3676c47ab1e97636c82462/href">https://medium.com/media/76b6f6442c3676c47ab1e97636c82462/href</a></iframe><p>Let’s see how does it look like 🤩:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/300/1*E3P1GUR_NLoqusnl8BKsuQ.gif" /></figure><p>Now let’s have a look at the libraries which help us to have a custom Toast as well:</p><p>On top of those dependencies, it is the <a href="https://github.com/GrenderG/Toasty"><strong>Toasty</strong></a><strong> </strong>library which is very easy to use and supports the +14 API level.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*P1zQF-UWjhtyA8bBqgvv6g.png" /><figcaption>Image Source: <a href="https://github.com/GrenderG/Toasty">https://github.com/GrenderG/Toasty</a></figcaption></figure><p>And the second one which I recommend is the <a href="https://github.com/Muddz/StyleableToast"><strong>StyleableToast</strong> </a>library. With this one, you can style your toasts either by code or with a style in styles.xml and it supports +16 API level.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*a9-n7GKb21CWkefVQPGdVA.png" /><figcaption>Image Source: <a href="https://github.com/Muddz/StyleableToast">https://github.com/Muddz/StyleableToast</a></figcaption></figure><p>I hope this article was useful to create a custom Toast. Feel free to comment and share it with others 🙌🏽.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c0cffb6d6a97" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Android Data Binding and MVVM Architecture Design]]></title>
            <link>https://maryam-dashtirahmatabadi.medium.com/android-data-binding-and-mvvm-architecture-design-208fcd4f0fb?source=rss-8998a5aff141------2</link>
            <guid isPermaLink="false">https://medium.com/p/208fcd4f0fb</guid>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[mvvm-databinding]]></category>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[data-binding]]></category>
            <dc:creator><![CDATA[Maryam Dashtirahmatabadi]]></dc:creator>
            <pubDate>Tue, 15 Sep 2020 16:07:46 GMT</pubDate>
            <atom:updated>2021-11-10T17:15:21.087Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NzRuuCAFKox0qZfs1p9XjQ.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@nosoylasonia?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Juan Gomez</a> on <a href="https://unsplash.com/?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>You might have heard of Data Binding. It is a library as part of the <a href="https://developer.android.com/jetpack">Android Jetpack</a> that binds UI components to data sources in the XML layout and minimizes the required code for the application’s logic. It has the ability of automatic UI update whenever data is changed and you can say goodbye to the very familiar findViewById🤗 … so more than code readability it helps to reduce null pointer exceptions and memory leaks, but consider that the errors will be caught in the compile-time. Although the layout expression language gives you a lot of power, it is best to avoid nesting complex logic inside views. Complex expressions will make your layout harder to read and maintain.</p><p>Now let’s get into coding 👩‍💻 …</p><h3>Prerequisite</h3><p><a href="https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93viewmodel">Model-view-viewmodel (MVVM) architecture design</a></p><h3>Setup Instructions</h3><p>First of all, in this sample project, the user enters a valid id which is between 1 and 5000, and then, as a result, a Photo and its title inside the activity will be shown (with a network call).</p><p>Add the following snippet inside the app’s build.gradle file:</p><pre><strong>apply plugin: &#39;kotlin-android&#39;<br>apply plugin: &#39;kotlin-kapt&#39;</strong></pre><pre><strong>android {<br>    ...<br>    dataBinding {<br>        enabled = true<br>    }<br>}</strong></pre><pre><strong>dependencies {<br>    ...<br>    //the compiler version must be the same as the gradle version<br>    kapt &quot;com.android.databinding:compiler:$gradle_version&quot;<br>}</strong></pre><p>If you are using Kotlin, you have to add Data Binding’s compiler as a dependency like the above code or you can enable it inside gradle.properties file of the project properties section as shown below:</p><pre><strong>android.databinding.enableV2=true</strong></pre><p>The next step to enable Data Binding is to wrap your whole XML layout with a &lt;layout&gt; tag as a parent. Then if you build the project, it will generate all the needed classes, which enables Data Binding. From now, all your changes related to Data Binding will be effective and there is no need to rebuild the project for each change. For binding UI components to data sources add a &lt;data&gt; tag inside the &lt;layout&gt; tag as per below:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dd97c3158e00c00ba022df8f08abcb1d/href">https://medium.com/media/dd97c3158e00c00ba022df8f08abcb1d/href</a></iframe><p>Layout variables are used to write layout expressions. Layout expressions are placed in the value of element attributes and they use the @{} syntax. Here are some examples:</p><pre><strong>android:text=&quot;@{photoModel.title</strong><strong>}&quot;<br>android:onClick=&quot;@{photoViewModel::onGetPhotoButtonClick}&quot;<br></strong><strong>android:visibility=&quot;@{user.age &gt; 18 ? View.INVISIBLE: View.VISIBLE}&quot;<br>android:onClick=&quot;@{() -&gt; photoViewModel.onGetPhotoButtonClick()}&quot;</strong></pre><p>I have defined two variables inside the &lt;data&gt; tag. One with the name of photoModel and type of my current <strong>Model </strong>class inside the project which is a data class, called Photo as the below snippet:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0f0c2d24c466fb22204edba717f9fc2e/href">https://medium.com/media/0f0c2d24c466fb22204edba717f9fc2e/href</a></iframe><p>And the other variable is photoViewModel with the type of my <strong>ViewModel</strong> class,PhotoViewModel.</p><p>The @={} syntax is used for <strong>Two-way Data Binding</strong> in which the layout and the object can both send data to each other and I have used it for the &lt;EditText&gt; due to getting the input automatically and for that, you need to create a ViewModel class for your activity or fragment.</p><blockquote>The ViewModel helps in keeping the view state, modify the model as the result of actions on the view, and trigger events to reflect the changes into the view.</blockquote><p>Inside the ViewModel class, I needed to get the Id from UI, I defined an Id variable so that by pressing a button the process could start. So, I have defined the onGetPhotoButtonClick() function. Whatever value is entered in that edit text, it will be assigned automatically to the photoViewModel.id value which is var id: Int = 0 inside the below class:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7cdeedd6b258a5ff30d6c9418d50912e/href">https://medium.com/media/7cdeedd6b258a5ff30d6c9418d50912e/href</a></iframe><p>As our architecture pattern says the View Model class will interact with the <strong>Repository</strong> class to send the Id to the server for getting the photo and if the response is successful we will get our photo model. For this purpose, I have defined the PhotoListener class:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/aee8cf20c3ee2391ec705aafe24b88f7/href">https://medium.com/media/aee8cf20c3ee2391ec705aafe24b88f7/href</a></iframe><p>For inflating the activity’s layout we’re going to follow the below new structure:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d665bb9daf1a903032db0e158130d2f1/href">https://medium.com/media/d665bb9daf1a903032db0e158130d2f1/href</a></iframe><p>As you can see the binding variable name (ActivityPhotoBinding) is a combination of the name of the XML layout (<em>activity_photo) </em>without underline, with capitalized letters, and the Binding Keyword at the end. The purpose of this binding variable is to set those layout variables which are declared in the &lt;data&gt; block and one of our variables will be photoViewModel . After creating the instance of photoViewModel set its value inside the activity (it’s going to be as our binding view model and it will bind the data with the UI) as shown in the following code snippet:</p><pre><strong>val </strong>photoViewModel = ViewModelProvider(<strong>this</strong>, ViewModelProvider.NewInstanceFactory()).get(PhotoViewModel::<strong>class</strong>.<em>java</em>)<br> <br><strong>photoBinding</strong>.<em>photoViewModel </em>= photoViewModel<br>                               ...</pre><p>As I have overridden the methods of PhotoListener inside the activity, when the response is successful the below function is called and then we can set the given Photo from the server to the photoModel ‘s value:</p><pre><strong>override fun </strong>onSuccess(photoResponse: LiveData&lt;Photo&gt;) {<br>    photoResponse.observe(<strong>this</strong>, <em>Observer </em><strong>{<br>            </strong>progress_bar.<em>hide</em>()<br>            <strong>if </strong>(<strong>it </strong>!= <strong>null</strong>) {<br>                <strong>photoBinding</strong>.<em>photoModel </em>= <strong>it<br>                </strong><em>showToast</em>(getString(R.string.<em>success</em>))<br>            } <strong>else </strong>{<br>                <em>showToast</em>(getString(R.string.<em>error</em>))<br>            }<br>        <strong>}</strong>)<br>}</pre><p>When you’re using Data Binding, setting values into your views with a normal approach is like the way we set the &lt;Button&gt; or &lt;EditText&gt;, but sometimes you want to do something special in your setting methods. For example, we have an &lt;ImageView&gt; and we want to define its resource, we can do it by defining it via a @BindingAdapter in our code for it:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/78415b8f02757b1672bb7d41c7c76185/href">https://medium.com/media/78415b8f02757b1672bb7d41c7c76185/href</a></iframe><p>And then inside your XML layout for that &lt;ImageView&gt; add the below snippet:</p><pre><strong>app:imageResource=&quot;@{photoModel.thumbnailUrl}&quot;</strong></pre><p>That was a simple example of using Data Binding with MVVM architecture design 🤩 … You can find the whole code at the below repository:</p><p><a href="https://github.com/Maryam-T/Data-Binding">GitHub - Maryam-T/Data-Binding: Data Binding and MVVM architecture design</a></p><h4>References:</h4><ul><li><a href="https://developer.android.com/topic/libraries/data-binding">https://developer.android.com/topic/libraries/data-binding</a></li><li><a href="https://github.com/probelalkhan/android-mvvm-architecture">https://github.com/probelalkhan/android-mvvm-architecture</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=208fcd4f0fb" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[A Guide on How to Write a Clean Code]]></title>
            <link>https://medium.com/swlh/a-guide-on-how-to-write-a-clean-code-776ce278755f?source=rss-8998a5aff141------2</link>
            <guid isPermaLink="false">https://medium.com/p/776ce278755f</guid>
            <category><![CDATA[java]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[clean-code]]></category>
            <dc:creator><![CDATA[Maryam Dashtirahmatabadi]]></dc:creator>
            <pubDate>Wed, 02 Sep 2020 18:17:57 GMT</pubDate>
            <atom:updated>2020-10-08T15:52:27.140Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*-0m6Suz_WMBP7R5AUTkRBg.png" /><figcaption>Image Source: <a href="https://www.wlion.com/blog/5-reasons-you-should-be-writing-clean-code/">https://www.wlion.com/blog/5-reasons-you-should-be-writing-clean-code/</a></figcaption></figure><p>In this article, I’m going to talk about writing clean code in general and then end up with some examples. As an Android developer having a clean code has always been challenging for me which needs lots of effort and that’s just coding and coding 💻 …</p><h3>Rules We Should Follow</h3><ol><li>Ignore duplication (imply <a href="https://en.wikipedia.org/wiki/Don%27t_repeat_yourself"><strong>DRY </strong></a>principle-Don’t Repeat Yourself)</li><li>Minimize entities, classes, and functions (avoid repetition)</li><li>It should be readable and simple</li><li>Be testable as it makes your code flexible and maintainable</li><li>Follow <a href="https://en.wikipedia.org/wiki/SOLID"><strong>SOLID</strong></a><strong> </strong>principles</li></ol><blockquote><strong>S = </strong><a href="https://en.wikipedia.org/wiki/Single-responsibility_principle"><strong>Single-responsibility principle</strong></a><strong>:</strong> A class should only have one purpose.</blockquote><blockquote><strong>O = </strong><a href="https://en.wikipedia.org/wiki/Open%E2%80%93closed_principle"><strong>Open-closed principle</strong></a><strong>: </strong>A class should be open for extension, but closed for modification.</blockquote><blockquote><strong>L = </strong><a href="https://en.wikipedia.org/wiki/Behavioral_subtyping"><strong>Liskov substitution principle</strong></a><strong>: </strong>Abstraction should be able to provide all needs of child class.</blockquote><blockquote><strong>I = </strong><a href="https://en.wikipedia.org/wiki/Interface_segregation_principle"><strong>Interface segregation principle</strong></a><strong>: </strong>Small interface is better than big one.</blockquote><blockquote><strong>D = </strong><a href="https://en.wikipedia.org/wiki/Dependency_inversion_principle"><strong>Dependency Inversion Principle</strong></a><strong>:</strong> A class should depend on abstraction, not implementation.</blockquote><p>6. Be careful with dependencies</p><blockquote>As many as possible try to have one-directional dependency. When dependency goes in multiple directions, things get much more complicated which makes hard to update and change.</blockquote><p>7. Don’t hardcode</p><blockquote>Define constant or use variables instead of hardcoding the values which will not only help readability but also make it easy to change if it is being used at multiple places.</blockquote><p>Now let’s start writing Clean Code 🚀 …</p><h3>Valid Names</h3><p>Naming is one of the hardest parts of programming and may take time to choose, but it is a strong way to convey your code’s intent to other developers who read them in the future which must show the purpose of that class, function, or variable and even tell what is going to do. Don’t append prefixes or type information. I always have a checklist in my mind as listed below which assures me that a name has been chosen well:</p><ul><li>Are the variables named according to the convention (camelCase, PascalCase, etc)?.</li><li>Does the name have an appropriate length to ensure that no one will be confused by it?.</li><li>Are the name of variables clear about what they hold?.</li><li>Are the names meaningful, searchable, and easy to pronounce?.</li></ul><p><strong>Class names </strong>should not only be names (not verbs) but also should have the PascalCase convention. On the other hand, <strong>method names </strong>should be verbs or phrase verbs and follow the camelCase convention. The same rules apply to <strong>variable names</strong>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/574e46647d27bcdfeaf481c473aba4a4/href">https://medium.com/media/574e46647d27bcdfeaf481c473aba4a4/href</a></iframe><h3>Functions and Methods</h3><p>Following <strong>S</strong> from <strong>SOLID</strong> principles, let functions and methods perform only one task which should be small. If the function arguments are too many, you should review your code and maybe pack them into an object or divide that task between some other functions. Prefer possible exceptions to return error codes and extract error handling try catch into their own function.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/850e2477dfb85dca41de027795157ca7/href">https://medium.com/media/850e2477dfb85dca41de027795157ca7/href</a></iframe><h3>Comments</h3><p>We should use comments only when it is necessary, not to explain bad code. Writing lengthy comments will not help us in changing our code into a clean one. If the code is bad, we should solve it by improving the code, not by adding instructions on how to use it, but it doesn’t mean that you shouldn’t use comments at all, sometimes it is important such as dealing with third party APIs where you need to explain some behavior.</p><h3>Code Appearance</h3><p>It might look unimportant at first glance, but it is of high importance to write your code with a well-organized format which makes it readable such as:</p><ul><li>Do not write everything in a single line. Give proper whitespace, indentation, or line breaks in your code.</li></ul><blockquote>Indentation styles assist in identifying control flow and blocks of code. In some programming languages, indentation is used to delimit logical blocks of code; correct indentation in these cases is more than a matter of style. In other languages, indentation and white space do not affect function, although logical and consistent indentation makes code more readable. (<a href="https://en.wikipedia.org/wiki/Programming_style">Inspired by WIKIPEDIA</a>)</blockquote><ul><li>Instance variables should be declared at the top of the class.</li><li>If functions are calling each other (dependent functions) they should be close while putting the caller at first.</li><li>It is often helpful to align similar elements vertically, to make typo-generated bugs more obvious.</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4343a0b7da3024c0139dba0fd996f8b9/href">https://medium.com/media/4343a0b7da3024c0139dba0fd996f8b9/href</a></iframe><h3>Tests</h3><p>The importance of Test code is equal to producing the code and of course, while writing tests you might find some bugs. However, it can sometimes be quite difficult to write a good test for a particular piece of code which is a result of a badly designed and untestable code. Testing is almost like coding for the second time so follow all the previous rules for writing a good test as well which should be as per below :</p><ul><li>One assert and single concept per test</li><li>Easy to write and readable (simplicity)</li><li>Fast</li><li>Independent</li></ul><p>To conclude, I hope this article could be helpful to write a better code although it is important to keep practicing and learning in this field 😃 …</p><h4><strong>References</strong>:</h4><ul><li><a href="https://medium.com/tunaiku-tech/s-o-l-i-d-principles-simplified-explanation-example-f7268ca75758">https://medium.com/tunaiku-tech/s-o-l-i-d-principles-simplified-explanation-example-f7268ca75758</a></li><li><a href="https://blog.gojekengineering.com/the-basics-of-writing-clean-code-39ed770b626d">https://blog.gojekengineering.com/the-basics-of-writing-clean-code-39ed770b626d</a></li><li><a href="https://medium.com/mindorks/how-to-write-clean-code-lessons-learnt-from-the-clean-code-robert-c-martin-9ffc7aef870c">https://medium.com/mindorks/how-to-write-clean-code-lessons-learnt-from-the-clean-code-robert-c-martin-9ffc7aef870c</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=776ce278755f" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swlh/a-guide-on-how-to-write-a-clean-code-776ce278755f">A Guide on How to Write a Clean Code</a> was originally published in <a href="https://medium.com/swlh">The Startup</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>