<?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 Hossein Ameri on Medium]]></title>
        <description><![CDATA[Stories by Hossein Ameri on Medium]]></description>
        <link>https://medium.com/@hossein.ameri?source=rss-8973931d5fd1------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*A31riqfviGZTgThG7gOL8w.jpeg</url>
            <title>Stories by Hossein Ameri on Medium</title>
            <link>https://medium.com/@hossein.ameri?source=rss-8973931d5fd1------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 03 Dec 2026 07:56:58 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@hossein.ameri/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[How to Create and Publish an Android Library in Jitpack]]></title>
            <link>https://medium.com/@hossein.ameri/how-to-create-and-publish-an-android-library-in-jitpack-18ea7bd5946b?source=rss-8973931d5fd1------2</link>
            <guid isPermaLink="false">https://medium.com/p/18ea7bd5946b</guid>
            <category><![CDATA[application]]></category>
            <category><![CDATA[google]]></category>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[development]]></category>
            <dc:creator><![CDATA[Hossein Ameri]]></dc:creator>
            <pubDate>Sun, 08 Sep 2024 06:27:55 GMT</pubDate>
            <atom:updated>2024-09-08T06:27:55.683Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/744/1*bOo9Jnmkwnb_EbO38V_WVg.jpeg" /></figure><h3><strong>Introduction</strong></h3><p><strong>What is Jitpack?</strong></p><ul><li>JitPack is a novel package repository for JVM and Android projects. It builds Git projects on demand and provides you with ready-to-use artifacts (jar, aar).</li><li>Easily integrate open-source libraries into your projects.</li></ul><p><strong>Why Publish on Jitpack?</strong></p><ul><li>No need to set up a custom Maven repository.</li><li>Automatic integration with GitHub.</li></ul><h3><strong>Step 1 : Create Your Android Project</strong></h3><ol><li>Open Android Studio.</li><li>Create a new project and Create a module for your Android library.</li><li>Add your module library code and resources.</li></ol><h3><strong>Step 2 : Configuring Gradle for Publishing</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/634/1*ISTUwPPHm_2e4nIcNHjQZw.png" /></figure><h3><strong>Step 3 : Setting Up Jitpack Configuration</strong></h3><ul><li>Creating jitpack.yml for Environment Project</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/523/1*FQMkVfK_-XHtJSD4VEOf2w.png" /></figure><h3><strong>Step 4 : Create Public Repository in Github</strong></h3><ul><li>Push your project to a GitHub repository.</li><li>Tag a release version on GitHub.</li></ul><h3><strong>Step 5 : Configure Jitpack</strong></h3><ol><li>Sign in to Jitpack.io</li><li>Find your GitHub repository in Jitpack and generate the build.</li><li>Jitpack will provide you with a Maven/Gradle dependency link.</li></ol><h3><strong>Step 6 : Publish Your Library</strong></h3><ol><li>Verify that your library builds correctly on Jitpack.</li><li>Add the provided Jitpack link to your project’s dependencies.</li><li>Your library is now live and ready to be used by others.</li></ol><h3><strong>Try the Sample Library</strong></h3><p>Explore the GitHub Repository</p><p>I’ve published a sample Android library using Jitpack.</p><p>You can find the complete code and instructions for integration in my GitHub repository.</p><p><strong>Get Started:</strong></p><p>Visit <a href="https://github.com/hossein69ameri/SamplePublishLibrary">https://github.com/hossein69ameri/SamplePublishLibrary</a></p><p>Clone the project and explore the integration steps.</p><p>Feel free to fork or contribute!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=18ea7bd5946b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Mastering LiveData in Android : Best Practices and Implementation Guide]]></title>
            <link>https://medium.com/@hossein.ameri/mastering-livedata-in-android-using-kotlin-best-practices-and-implementation-guide-7c339b0ecdc5?source=rss-8973931d5fd1------2</link>
            <guid isPermaLink="false">https://medium.com/p/7c339b0ecdc5</guid>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[application]]></category>
            <category><![CDATA[coding]]></category>
            <dc:creator><![CDATA[Hossein Ameri]]></dc:creator>
            <pubDate>Fri, 10 Nov 2023 09:27:08 GMT</pubDate>
            <atom:updated>2023-11-10T09:27:50.040Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LC3XvppMT2qvqzHEXKUHfA.jpeg" /></figure><p>In the realm of Android app development, keeping data and UI components in sync is crucial for a seamless user experience. LiveData, a lifecycle-aware data holder class, has emerged as a powerful tool for managing data updates in a way that’s both efficient and responsive. Leveraging LiveData in conjunction with Kotlin can significantly enhance the robustness of an Android application.</p><h3><strong>Understanding LiveData in Android</strong></h3><p>LiveData, a part of the Android Architecture Components, is designed to observe changes in data and react accordingly, making it an ideal choice for managing UI-related data. Its integration with Kotlin, a concise and expressive programming language, provides a clean and efficient way to handle data updates and UI synchronization.</p><h3><strong>Implementing LiveData in Android with Kotlin</strong></h3><p><strong>1. Integration and Setup</strong></p><p>To get started, integrate the necessary dependencies in your project. Utilize the `androidx.lifecycle` library, which includes the LiveData component. Ensure that your project-level `build.gradle` file includes the necessary dependencies:</p><pre>implementation &quot;androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version&quot;</pre><p><strong>2. Creating LiveData Objects</strong></p><p>Define LiveData objects in your ViewModel classes to hold the data that needs to be observed. Utilize Kotlin’s `MutableLiveData` to create instances of LiveData and expose them as immutable LiveData objects.</p><pre>val liveDataObject: LiveData&lt;DataType&gt; = MutableLiveData()</pre><p><strong>3. Observing LiveData</strong></p><p>In your UI components, such as Activities or Fragments, observe changes in LiveData objects using the `observe()` function. Update the UI when the observed data changes.</p><pre>viewModel.liveDataObject.observe(viewLifecycleOwner) { newData -&gt;<br> // Update UI with new data<br>}</pre><p><strong>4. Utilizing Transformation Functions</strong></p><p>LiveData provides transformation functions like `map` and `switchMap` that enable the transformation of data within LiveData objects, ensuring that the observed data meets the UI requirements without direct manipulation.</p><pre>val transformedData: LiveData&lt;TransformedType&gt; = Transformations.map(originalLiveData) { originalData -&gt;<br> // Transform original data to meet UI requirements<br>}</pre><h3>Best Practices for Using LiveData with Kotlin</h3><p><strong>1. Lifecycle Awareness</strong></p><p>LiveData is lifecycle-aware, which means it automatically respects the lifecycle state of components (activities, fragments, etc.). Ensure that LiveData is observed within the appropriate lifecycle owner to prevent potential memory leaks.</p><p><strong>2. Encapsulation and Immutability</strong></p><p>Maintain the principle of encapsulation by exposing LiveData as immutable objects. Use `MutableLiveData` within the ViewModel to update the LiveData objects and expose them as immutable LiveData to the UI components.</p><p><strong>3. Proper Error Handling</strong></p><p>Implement error handling within LiveData to gracefully manage error scenarios. Utilize transformations or custom LiveData wrappers to handle errors in a structured manner.</p><p><strong>4. Testing LiveData</strong></p><p>Write unit tests to validate LiveData behavior, ensuring that the data updates and transformations occur as expected. Utilize libraries like Mockito and LiveData testing libraries for comprehensive testing.</p><h3><strong>Conclusion</strong></h3><p>LiveData, when employed in Android development with Kotlin, offers an elegant solution for managing and synchronizing data changes with UI components. Its lifecycle awareness, immutability, and seamless integration with Kotlin make it a powerful asset for crafting responsive and maintainable Android applications. By following best practices and employing LiveData effectively, developers can ensure a robust and user-friendly app experience.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7c339b0ecdc5" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Mastering Kotlin: A Collection of Essential Functions Every Developer Should Know with Examples —…]]></title>
            <link>https://medium.com/@hossein.ameri/mastering-kotlin-a-collection-of-essential-functions-every-developer-should-know-with-examples-67e76d8a31b7?source=rss-8973931d5fd1------2</link>
            <guid isPermaLink="false">https://medium.com/p/67e76d8a31b7</guid>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[kotlin-beginners]]></category>
            <category><![CDATA[application]]></category>
            <category><![CDATA[kotlin]]></category>
            <dc:creator><![CDATA[Hossein Ameri]]></dc:creator>
            <pubDate>Thu, 02 Nov 2023 06:35:34 GMT</pubDate>
            <atom:updated>2023-11-02T06:35:34.474Z</atom:updated>
            <content:encoded><![CDATA[<h3>Mastering Kotlin: A Collection of Essential Functions Every Developer Should Know with Examples — — -Last Part-String Representation</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8AC0095aULqn92fllca7rg.png" /></figure><p>If you are generating user-visible text out of Kotlin collections or trying to log an internal state you will need to represent collections as strings. I really liked when I learned that Kotlin standard library has a function just for this reason:</p><h3>.joinToString()</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aGKTwggbzratFzhY1UkAlg.png" /></figure><p>If don’t pass any parameters to <strong>.joinToString()</strong>, string representations of each items will be concatenated in a string and separated by commas with spaces by default.</p><h3>Customizing the separator, prefix and postfix strings</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FjeKxJkzX5pg2gf_QwDgLg.png" /></figure><p>However, if you need to change the separator strings or add some more useful information to the start or to the end you can pass some arguments for <strong>separator</strong>, <strong>prefix</strong> and <strong>postfix</strong> parameters.</p><h3>The limit and truncated arguments</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9OXMpOI6T1IxtDZU_ogABg.png" /></figure><p><strong>.joinToString()</strong> also support limiting and truncating the list. If you have a long list and don’t want to print everything and make a mess, you can limit how many elements you want in the resulting string and what string to use in the place of an ellipsis.</p><p>Finally, it also supports passing a transform lambda if you also want to define how each item will be converted into a string.</p><p>That was it! Now you have learned how to transform Kotlin collections using a handful of convenience functions.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=67e76d8a31b7" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Mastering Kotlin: A Collection of Essential Functions Every Developer Should Know with Examples —…]]></title>
            <link>https://medium.com/@hossein.ameri/mastering-kotlin-a-collection-of-essential-functions-every-developer-should-know-with-examples-c46e414bd683?source=rss-8973931d5fd1------2</link>
            <guid isPermaLink="false">https://medium.com/p/c46e414bd683</guid>
            <category><![CDATA[proandroiddev]]></category>
            <category><![CDATA[androiddev]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[android-app-development]]></category>
            <dc:creator><![CDATA[Hossein Ameri]]></dc:creator>
            <pubDate>Thu, 26 Oct 2023 13:59:05 GMT</pubDate>
            <atom:updated>2023-10-26T13:59:05.357Z</atom:updated>
            <content:encoded><![CDATA[<h3>Mastering Kotlin: A Collection of Essential Functions Every Developer Should Know with Examples — — -Part 4 -Flattening</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JQeYWYjFCKP6dWC63GwkXA.png" /></figure><p>Flattening in general is removing the nestedness in a collection. Consider you are given a list of lists. If you want to remove all child lists and get their elements into one single-level list, you need to flatten that list.</p><p>Let’s begin with :</p><h3>.flatten()</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MAoAszM7F3-4rCjhk2gzbw.png" /></figure><p>This is the most simple way of flattening a list of lists into just list. Just call <strong>.flatten()</strong> on the receiver collection to have a flat list.</p><h3>.flatMap()</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*d4MdBGX8kQj55kCS-2aPzg.png" /></figure><p>Or, if you want to apply some kind of transformation whilst flattening, you can use <strong>.flatMap()</strong>.</p><p>Please note that <strong>.flatMap()</strong> is just a shorter form of <strong>.map().flatten()</strong> chain.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9WA-Evtgjtn-UPU5LHKCdA.png" /></figure><p>Kotlin’s standard library provides a rich collection of functions that can greatly simplify your code and make it more readable. By mastering these essential functions, you’ll become a more efficient and expressive Kotlin developer. Whether you’re working with collections, handling errors, or simplifying null checks, Kotlin has you covered with powerful tools at your disposal. So go ahead, explore these functions, and take your Kotlin programming to the next level!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c46e414bd683" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Mastering Kotlin: A Collection of Essential Functions Every Developer Should Know with Examples —…]]></title>
            <link>https://medium.com/@hossein.ameri/mastering-kotlin-a-collection-of-essential-functions-every-developer-should-know-with-examples-bbbc0760d35f?source=rss-8973931d5fd1------2</link>
            <guid isPermaLink="false">https://medium.com/p/bbbc0760d35f</guid>
            <category><![CDATA[androiddev]]></category>
            <category><![CDATA[development]]></category>
            <category><![CDATA[application]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[kotlin]]></category>
            <dc:creator><![CDATA[Hossein Ameri]]></dc:creator>
            <pubDate>Thu, 19 Oct 2023 10:42:18 GMT</pubDate>
            <atom:updated>2023-10-19T10:42:18.670Z</atom:updated>
            <content:encoded><![CDATA[<h3>Mastering Kotlin: A Collection of Essential Functions Every Developer Should Know with Examples — — -Part 3 -Association</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*D15ysc5dfYp0BowKF56BDA.png" /></figure><p>Association is when you want to traverse a <strong>List</strong> and convert it into a <strong>Map</strong> by associating each item with one of its properties. Understanding association operations is easier to show than tell, so let’s take a look at what sort of functions we have in the standard library:</p><h3>.associateWith()</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oZv0TPXbpsdjBzXXw5D50Q.png" /></figure><p>The <strong>.associateWith()</strong> function, structures the items of the List as keys of a <strong>Map</strong>. The values for each key are computed by running the given lambda on the item. Please note that maps cannot have duplicate keys, so if the receiver collection has repetitive items, only the last one will remain in the map.</p><h3>.associateBy()</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_265sq44rRdWBH3HEORDzw.png" /></figure><p>Kotlin also has <strong>.associateBy()</strong>, which functions almost the same. The difference is items of the receiver collections are not used as keys but values.</p><p>That means if the lambda returns the same values for different items, only the last one will be present in the resulting map.</p><h3>.associate()</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WpQuX4xjfbSbYOXPLJJ9vQ.png" /></figure><p>The last associating function in the standard library is <strong>.associate()</strong>.</p><p>Using this function, you can define the logic of building the map. You need to pass a lambda that returns <strong>Pair</strong>s and a <strong>Map</strong> will be built for you using these <strong>Pair</strong>s.</p><p>Please note that this function has some performance implications since it generates <strong>Pair</strong>s as an intermediate step.</p><p>Kotlin’s standard library provides a rich collection of functions that can greatly simplify your code and make it more readable. By mastering these essential functions, you’ll become a more efficient and expressive Kotlin developer. Whether you’re working with collections, handling errors, or simplifying null checks, Kotlin has you covered with powerful tools at your disposal. So go ahead, explore these functions, and take your Kotlin programming to the next level!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bbbc0760d35f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Mastering Kotlin: A Collection of Essential Functions Every Developer Should Know with Examples —…]]></title>
            <link>https://medium.com/@hossein.ameri/mastering-kotlin-a-collection-of-essential-functions-every-developer-should-know-with-examples-04af165fafad?source=rss-8973931d5fd1------2</link>
            <guid isPermaLink="false">https://medium.com/p/04af165fafad</guid>
            <category><![CDATA[androiddev]]></category>
            <category><![CDATA[proandroiddev]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[app-development]]></category>
            <dc:creator><![CDATA[Hossein Ameri]]></dc:creator>
            <pubDate>Fri, 06 Oct 2023 07:43:59 GMT</pubDate>
            <atom:updated>2023-10-06T07:43:59.201Z</atom:updated>
            <content:encoded><![CDATA[<h3>Mastering Kotlin: A Collection of Essential Functions Every Developer Should Know with Examples — — -Part 2 -Zipping</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QWFvLYB9dvV-GeygqiFfIQ.jpeg" /></figure><h3><strong>2. Zipping</strong></h3><p>When you have two lists of the same size and you want to merge each item from the first list with the corresponding item coming from the second list you want zipping.</p><p>Kotlin standard library has convenient functions to zip and unzip collections including <strong>List</strong>s, <strong>Array</strong>s, and <strong>Sequence</strong>s.</p><p>Let’s take a look at what we have:</p><h3>.zip()</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0sh97EkIYUMsipfx5ud36w.png" /></figure><p>You can use <strong>.zip()</strong> without any transformation to create a list of pairs. The pairs have their first item from the receiver list and the second from the argument. If you want to combine two different data sources into one and if the sources has the same number of items, the function <strong>.zip()</strong> is really useful.</p><h3>.zip() with transform lambda</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-yD2cMa5TMYd2h3IjMMAFA.png" /></figure><p>If you don’t need pairs but you want to define your own zipping logic, good news: <strong>.zip()</strong> can also take a transform lambda as an argument. So you don’t need to chain a <strong>.zip()</strong> command with a <strong>.map()</strong>. Instead, pass a lambda and combine two items coming from two data sources in a way you would prefer.</p><h3>.unzip()</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gdqqY5Y8cFGoq4hNF_thNg.png" /></figure><p>Another great function in Kotlin is the <strong>.unzip()</strong>. The name gives it all away: it does the opposite of <strong>.zip()</strong>. If you have a list of pairs and want to create two lists that contain the firsts and seconds of each item, then just <strong>.unzip()</strong> it!</p><p>Kotlin’s standard library provides a rich collection of functions that can greatly simplify your code and make it more readable. By mastering these essential functions, you’ll become a more efficient and expressive Kotlin developer. Whether you’re working with collections, handling errors, or simplifying null checks, Kotlin has you covered with powerful tools at your disposal. So go ahead, explore these functions, and take your Kotlin programming to the next level!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=04af165fafad" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Mastering Kotlin: A Collection of Essential Functions Every Developer Should Know with Examples —…]]></title>
            <link>https://medium.com/@hossein.ameri/mastering-kotlin-a-collection-of-essential-functions-every-developer-should-know-with-examples-fe683a1fab26?source=rss-8973931d5fd1------2</link>
            <guid isPermaLink="false">https://medium.com/p/fe683a1fab26</guid>
            <category><![CDATA[androiddev]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[app-development]]></category>
            <category><![CDATA[proandroiddev]]></category>
            <dc:creator><![CDATA[Hossein Ameri]]></dc:creator>
            <pubDate>Fri, 29 Sep 2023 08:30:08 GMT</pubDate>
            <atom:updated>2023-09-29T08:30:08.840Z</atom:updated>
            <content:encoded><![CDATA[<h3>Mastering Kotlin: A Collection of Essential Functions Every Developer Should Know with Examples — — -Part 1 -Mapping</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Q3uNLkmAYeItjCKLs8qbiw.png" /></figure><p>Knowing about the standard library functions in Kotlin saves a lot of time while working on complex data structures. Whether you are mapping the API response model into your business model or you are sorting, filtering, and manipulating data; knowing these operation on Kotlin collections come really handy.</p><p>In this post, we are gonna go through some collection transformation operations that I use frequently.</p><h3>1. Mapping</h3><p>Mapping means to modify each item in a collection by applying a transformation lambda which results in the creation of another collection.</p><p>If you are working on lists of data that you need to transform from one model into another or if you want to convert the API response model into a domain layer model you should use mapping operations in Kotlin.</p><h3>.map()</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iYuL2wuapksFlGeZMUfXrQ.png" /></figure><p>Using the <strong>.map()</strong> function is the most common way to convert a collection of items into another. It applies the given transform lambda to each of the items of the receiver collection and yields them into a newly created list.</p><h3>.mapIndexed()</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VVjzetRj9fyG2e-TCFJmLw.png" /></figure><p>If you also need the index of each item when transforming them, you can use the <strong>.mapIndexed()</strong> function. It will feed the index of each item to the transform lambda so that you can utilize the order of the items.</p><h3>.mapNotNull()</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vnAL2Kl_W7r-azCR3OatDQ.png" /></figure><p>Another convenient mapping function is <strong>.mapNotNull()</strong>. You can use it if you want to filter out null items after the conversion.</p><p>For example, if you have a list of objects which may or may not have text property, you can use <strong>.mapNotNull()</strong> to get a list of texts without any nulls being in it.</p><p>Kotlin’s standard library also has <strong>.mapIndexedNotNull()</strong> method which combines <strong>.mapIndexed()</strong> and <strong>.mapNotNull()</strong>.</p><p>Also you can find <strong>.mapKeys()</strong> and <strong>.mapValues()</strong> methods which are defined on <strong>Map</strong>s.</p><p>Kotlin’s standard library provides a rich collection of functions that can greatly simplify your code and make it more readable. By mastering these essential functions, you’ll become a more efficient and expressive Kotlin developer. Whether you’re working with collections, handling errors, or simplifying null checks, Kotlin has you covered with powerful tools at your disposal. So go ahead, explore these functions, and take your Kotlin programming to the next level!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fe683a1fab26" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Exploring The EventBus Pattern With Kotlin Flow on Android]]></title>
            <link>https://medium.com/@hossein.ameri/exploring-the-eventbus-pattern-with-kotlin-flow-on-android-cb17c00aadda?source=rss-8973931d5fd1------2</link>
            <guid isPermaLink="false">https://medium.com/p/cb17c00aadda</guid>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[application]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[android-app-development]]></category>
            <dc:creator><![CDATA[Hossein Ameri]]></dc:creator>
            <pubDate>Thu, 14 Sep 2023 10:26:56 GMT</pubDate>
            <atom:updated>2023-09-14T10:26:56.933Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/516/1*ulLw2I_6v49IHauyGQ0UeQ.png" /></figure><h3><strong>Introduction</strong></h3><p>In the world of modern software development, building responsive and maintainable applications is paramount. One of the key challenges in achieving this goal is efficiently handling communication between different parts of an application. Enter the EventBus pattern, a powerful mechanism for simplifying inter-component communication. In this article, we will explore how to implement the EventBus pattern using Kotlin Flow, a versatile library for asynchronous programming, to create more responsive and loosely coupled Android applications.</p><h3>Understanding the EventBus Pattern</h3><p>The EventBus pattern is a design pattern that facilitates communication between different parts of a software system. It promotes the decoupling of components, making the code more maintainable, and enhancing the overall flexibility of an application.</p><p>In its essence, an event bus acts as a central communication channel that allows components to publish and subscribe to events. When an event is published, it can be consumed by multiple subscribers, enabling a one-to-many communication model.</p><h3>Why Kotlin Flow?</h3><p>Kotlin Flow is a modern and powerful asynchronous programming library that provides a simple and intuitive way to work with asynchronous data streams. It is a perfect fit for implementing the EventBus pattern, as it allows us to seamlessly handle the flow of events in a reactive and non-blocking manner.</p><h3>Implementing the EventBus with Kotlin Flow</h3><p>1.Create the EventBus Object</p><pre><br><br>Object EventBus {<br><br>    private val _events = MutableSharedFlow&lt;&quot;data&quot;&gt;()<br>    val events = _events.asSharedFlow()<br><br>    suspend fun publish(event: String) {<br>        _events.emit(event)<br>    }<br>}</pre><p>In this Object Class, we use a MutableSharedFlow to represent the stream of events. The publish function emits an event to the flow.</p><p>2.Publish Events</p><pre>class MyViewModel : ViewModel() {<br><br>    init {<br>        viewModelScope.launch {<br>            EventBus.publish(&quot;Hello&quot;)<br>        }<br>    }<br>}</pre><p>3.Subscribe to Events</p><pre>    private fun bindSubscribe() {<br>        lifecycleScope.launch {<br>            EventBus.events.collectLatest { string -&gt;<br>                binding.textView.text = string<br>            }<br>        }<br>    }</pre><h3>Benefits of Using Kotlin Flow with EventBus</h3><p><strong>Asynchronous and Non-blocking</strong>: Kotlin Flow allows you to handle events asynchronously and without blocking the main thread, ensuring a responsive user interface.</p><p><strong>Loose Coupling</strong>: The EventBus pattern promotes loose coupling between components, making your codebase more maintainable and adaptable to changes.</p><p><strong>Scalability</strong>: You can easily scale your EventBus implementation to handle various types of events and components in your application.</p><p><strong>Testability</strong>: Events can be easily unit-tested, ensuring the reliability of your event-driven code.</p><h3>Conclusion</h3><p>In this article, we explored how to implement the EventBus pattern using Kotlin Flow. Leveraging the power of asynchronous programming with Kotlin Flow, you can create more responsive and loosely coupled Android applications. This pattern simplifies communication between components, leading to cleaner, more maintainable code.</p><p>By adopting the EventBus pattern and Kotlin Flow in your projects, you can improve code organization, reduce complexity, and create more robust and scalable applications. So, go ahead and start implementing EventBus in your Kotlin projects, and unlock the benefits of reactive, event-driven programming.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cb17c00aadda" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>