<?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 Ashesh Bharadwaj on Medium]]></title>
        <description><![CDATA[Stories by Ashesh Bharadwaj on Medium]]></description>
        <link>https://medium.com/@asheshb?source=rss-a08949e258d7------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*LhA4JsltBgBAgw07LHsNzQ.jpeg</url>
            <title>Stories by Ashesh Bharadwaj on Medium</title>
            <link>https://medium.com/@asheshb?source=rss-a08949e258d7------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 16 Nov 2026 20:38:43 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@asheshb/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[Android: “Shortcut” Kotlin Coroutines for Asynchronous task]]></title>
            <link>https://medium.com/@asheshb/android-shortcut-kotlin-coroutines-for-asynchronous-task-ae7d75c142b0?source=rss-a08949e258d7------2</link>
            <guid isPermaLink="false">https://medium.com/p/ae7d75c142b0</guid>
            <category><![CDATA[androiddev]]></category>
            <category><![CDATA[asynchronous]]></category>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[kotlin-coroutines]]></category>
            <category><![CDATA[kotlin]]></category>
            <dc:creator><![CDATA[Ashesh Bharadwaj]]></dc:creator>
            <pubDate>Fri, 13 Mar 2020 06:01:20 GMT</pubDate>
            <atom:updated>2020-03-13T06:01:20.903Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*j4-zFI9YJ83oKG9cdrAUbg.jpeg" /><figcaption>Photo by Simon Mumenthaler on Unsplash</figcaption></figure><p>There are many good articles to learn Kotlin Coroutines, this article is not one of them. The purpose of this article is to show you how to offload a task from Main Thread using Coroutines without getting into m̶u̶c̶h any details. One thing I ask is a promise that you will learn Coroutines at your earliest :)</p><p>The examples below is for ViewModel class because it’s a good place to start asynchronous task as it has a more stable lifecycle than Activity/Fragment. In case you want to offload a task from Main thread in Activity/Fragment refer to the <em>Tip 2</em> at the bottom.</p><p>You will be required to add the following dependencies in build.gradle(Module) file which most likely you already would have.</p><pre>implementation &#39;androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0&#39;<br>implementation &#39;androidx.lifecycle:lifecycle-extensions:2.2.0&#39;</pre><p><strong>[OPTION 1]</strong> Let’s say you just want to run some code off Main thread.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6485b09fcd5f4c42d1dfcc1ec8147f11/href">https://medium.com/media/6485b09fcd5f4c42d1dfcc1ec8147f11/href</a></iframe><p><strong>Line 3</strong>: <em>viewModelScope.launch </em>is one of the ways we start a coroutine. The lambda contains all the code which will be executed in this coroutine.</p><p><em>viewModelScope </em>ties coroutine execution with the lifecycle scope of <em>MainViewModel</em> class object so that if this ViewModel object gets destroyed then the code inside the coroutine stops execution. This is to prevent the memory leak.</p><p>By default <em>viewModelScope.launch </em>executes the code in Main thread but that’s not we want here so we pass <em>Dispatchers.IO </em>as an argument <em>to launch()</em> function which tells the coroutine to execute the code off Main thread. There are mainly 3 types of thread we can execute the code on:</p><ol><li><strong><em>Dispatchers.IO</em></strong><em> </em>For Network/Database/File related tasks</li><li><strong><em>Disptachers.Default</em></strong> For CPU intensive task (algo etc.)</li><li><strong>Dispatchers.Main</strong> is used to run the code on Main thread. This is what <em>viewModelScope.launch </em>uses if no argument provided.</li></ol><p>Depending on what type of task we are running we can choose one of them.</p><p>THAT’S ALL! if we just want to run some task off the Main thread.</p><p><strong>[OPTION 2] </strong>Life is not that simple so if we need more let’s explore a few other possibilities under minimalist approach only.</p><p>Let’s say your task involves some of the code to be run on the Main thread and some off Main thread:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e7595a51e4c0693ff4336047aeadcb32/href">https://medium.com/media/e7595a51e4c0693ff4336047aeadcb32/href</a></iframe><p><strong>Line 3</strong>: Here we are calling <em>viewModelScope.launch </em>without any parameters which will make the lambda execute on Main thread.</p><p><strong>Line 6</strong>: Now, if we want some code to run off Main thread then we can call <em>withContext(Dispatchers.IO). </em>Any code executed inside its lambda will be off Main thread.</p><p><strong>Line 10</strong>: Here we are back to the Main thread and we can use the <em>result </em>variable on the Main thread.</p><p><strong>Line 11</strong>: Mind it, the code will be executed sequentially. <strong>Line 11</strong> will be executed only after <strong>Line 8 </strong>code block is done executing, that too without blocking Main thread.</p><p>THAT’S ALL if we need to mix Main and off Main thread code execution.</p><p><strong>[OPTION 3] </strong>In the previous option we executed all the code in <em>launch </em>lambda code block but we might have code organized inside various functions. Let’s see how we can achieve “option 2&quot; but instead calling other functions.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/922d749c979c00939d7b167a2f1ffd27/href">https://medium.com/media/922d749c979c00939d7b167a2f1ffd27/href</a></iframe><p><strong>Line 4 and 5: </strong>In the above example instead of putting all the code in <em>viewModelScope.launch </em>code block we are calling two functions <em>fetchData() </em>and <em>parseData()</em></p><p><strong>Line 11</strong>: These functions are just like any function except they have <em>suspend </em>modifier in the function declaration. This <em>suspend m</em>odifier does the magic of executing asynchronous code as if the code is being executed sequentially.</p><p>THAT’S ALL if we need to organize our code in various functions and use coroutines.</p><p><strong>Tip 1</strong>: If you want to see in which thread your code is executing. Here is a log call which will show it to you in <em>Logcat</em> window on Android Studio:</p><pre>Log.d(&quot;TAG&quot;, &quot;Code running in: ${Thread.currentThread().<em>name</em>}&quot;)</pre><p><strong>Tip 2</strong>: For offloading a task in Activity/Fragment just replace <em>viewModelScope </em>with <em>lifecycleScope</em> in the above code and you are good to go<em> . </em>You may require to add the following dependency in <em>build.gradle</em>:</p><pre>implementation &quot;androidx.lifecycle:lifecycle-runtime-ktx:$lifecycle_version&quot;</pre><p><strong>NOTE</strong>: Coroutines are a lot more than this. Here we have learnt the very basic implementation. Please go ahead ahead and learn Coroutines in detail.</p><p>Thanks for reading this article. You can connect with me on <a href="https://www.linkedin.com/in/asheshb">LinkedIn</a> and <a href="https://twitter.com/asheshbt">Twitter</a></p><blockquote><strong>If you like this article, please recommend it by hitting the clap icon as many times you wish 👏 Let’s enable each other with the power of knowledge.</strong></blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ae7d75c142b0" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Sample App — Android app architecture by example Part 5/5]]></title>
            <link>https://medium.com/@asheshb/sample-app-android-app-architecture-by-example-part-5-5-a57ec04a93d9?source=rss-a08949e258d7------2</link>
            <guid isPermaLink="false">https://medium.com/p/a57ec04a93d9</guid>
            <category><![CDATA[software-architecture]]></category>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[architecture-components]]></category>
            <category><![CDATA[clean-architecture]]></category>
            <category><![CDATA[androiddev]]></category>
            <dc:creator><![CDATA[Ashesh Bharadwaj]]></dc:creator>
            <pubDate>Wed, 03 Jul 2019 04:50:01 GMT</pubDate>
            <atom:updated>2019-07-03T05:00:49.620Z</atom:updated>
            <content:encoded><![CDATA[<h3>Sample App — Android app architecture by example Part 5/5</h3><blockquote>Doesn’t matter how meticulously we plan, there is always going to be a deviation when implementing it.</blockquote><p>This is the final article of a five-article series on Android app architecture:</p><ol><li><a href="https://medium.com/@asheshb/what-is-architecture-android-app-architecture-by-example-part-1-7ffa2cbfc0df">What is Architecture?</a></li><li><a href="https://medium.com/@asheshb/programming-principles-android-app-architecture-by-example-part-2-5-41d87eaf30bf">Programming principles</a></li><li><a href="https://blog.usejournal.com/layered-architecture-android-app-architecture-by-example-part-3-5-1ed5c644a152">Layered Architecture</a></li><li><a href="https://medium.com/@asheshb/clean-architecture-android-app-architecture-by-example-part-4-5-360c792d3fa7">Clean Architecture</a></li><li>Sample app (you are here)</li></ol><p>The sample is a movie app which fetches movie data from the <a href="https://www.themoviedb.org/documentation/api">TMDb API</a>. There are 2 screens in the app. The main screen shows the list of movie posters and clicking on a poster take us to the movie detail screen.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*t40H91FnN0dn0attyOjYag.png" /><figcaption>The sample movie app</figcaption></figure><p>The code is written in Kotlin (if you are using Java, don’t worry. It wouldn’t be that difficult to follow). The app uses <a href="https://developer.android.com/jetpack">Jetpack</a> Architecture Components: ViewModel, LiveData, Room, Paging and Navigation. Retrofit is used to fetch the data and Dagger 2 is used for dependency injection.</p><p>As discussed earlier, <a href="https://blog.usejournal.com/layered-architecture-android-app-architecture-by-example-part-3-5-1ed5c644a152">layered architecture</a> is an easy form of separation and a good start for the beginners. we can separate the code of an app based on the following layers:</p><ol><li><strong>User interface (UI)</strong>: Classes which interact with system UI events and display the results to the user.</li><li><strong>Domain</strong>: The classes which contain logic exclusive to our app. For example, calculating the discount based on the quantity ordered by a user.</li><li><strong>Data</strong>: Classes which interact with the app database as well as system services.</li></ol><p>We have two ways to implement the layers. First is through the module so that instead of default app module, we can create 3 modules ui , domain and data.</p><p>The second way is to use packages so that under the default app module we create three packages (ui, domain and data) and put the respective classes in there.</p><p>Modules provide a clear and strict separation but could be slightly difficult if one is used to working with a single app module. In the <a href="https://github.com/asheshb/AACPagingInfiniteScrollingWithNetworkSample">sample app</a>, I have used package as it’s simple to implement yet provide a good degree of separation.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/702/1*9rkfoz_pYbHfyNe44UwQJg.png" /><figcaption>ui, domain and data packages</figcaption></figure><p>According to the previous article <a href="https://medium.com/@asheshb/clean-architecture-android-app-architecture-by-example-part-4-5-360c792d3fa7">Clean Architecture</a>, we need to take care of the flow of dependency and how high-level classes should interact with low-level classes. Keeping these things in mind let’s look at the app code. We will discuss the implementation for movie list screen.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/283/1*PWjDISZY1CqXydKgmH3PWg.png" /><figcaption>movielist package in ui layer</figcaption></figure><p>In the ui layer I have further created a movielist package to keep together the classes related to the movie list screen. This is a further separation based on feature.</p><p>Our task here is to fetch movies from the database and show it in <em>RecyclerView</em>. The process to fetch movies from the database and show it in the ui span across all 3 layers. Here is how we can make the call based on Clean Architecture principles:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/365/1*XlyUj8OBaccB9m-9grGRDg.png" /><figcaption>Movie list screen: Class data flow</figcaption></figure><p><strong><em>1. </em></strong><a href="https://github.com/asheshb/AACPagingInfiniteScrollingWithNetworkSample/blob/dagger_dependency_injection/app/src/main/java/com/bitwindow/aacpaginginfinitescrollingwithnetworksample/ui/movielist/MovieListFragment.kt"><strong><em>MovieListFragment</em></strong></a><strong><em> -&gt; </em></strong><a href="https://github.com/asheshb/AACPagingInfiniteScrollingWithNetworkSample/blob/dagger_dependency_injection/app/src/main/java/com/bitwindow/aacpaginginfinitescrollingwithnetworksample/ui/movielist/MovieListViewModel.kt"><strong><em>MovieListViewModel</em></strong></a><em>:</em></p><pre>// <a href="https://github.com/asheshb/AACPagingInfiniteScrollingWithNetworkSample/blob/dagger_dependency_injection/app/src/main/java/com/bitwindow/aacpaginginfinitescrollingwithnetworksample/ui/movielist/MovieListFragment.kt">MovieListFragment.kt</a></pre><pre>movieListViewModel.movies.observe(<em>viewLifecycleOwner</em>, <em>Observer </em><strong>{ </strong>list <strong>-&gt;<br>    </strong>movieListAdapter.submitList(list)<br><strong>}</strong>)</pre><p>In <a href="https://github.com/asheshb/AACPagingInfiniteScrollingWithNetworkSample/blob/dagger_dependency_injection/app/src/main/java/com/bitwindow/aacpaginginfinitescrollingwithnetworksample/ui/movielist/MovieListFragment.kt"><em>MovieListFragment</em></a><em> </em>class we observe the <em>movies </em>variable (a <em>PagedList&lt;MoviePoster&gt;</em>) of <a href="https://github.com/asheshb/AACPagingInfiniteScrollingWithNetworkSample/blob/dagger_dependency_injection/app/src/main/java/com/bitwindow/aacpaginginfinitescrollingwithnetworksample/ui/movielist/MovieListViewModel.kt"><em>MovieListViewModel</em></a> class and submit any changes to the adapter to show the list of movies.</p><p><strong>2.</strong> <a href="https://github.com/asheshb/AACPagingInfiniteScrollingWithNetworkSample/blob/dagger_dependency_injection/app/src/main/java/com/bitwindow/aacpaginginfinitescrollingwithnetworksample/ui/movielist/MovieListViewModel.kt"><strong><em>MovieListViewModel</em></strong></a><strong><em> -&gt; </em></strong><a href="https://github.com/asheshb/AACPagingInfiniteScrollingWithNetworkSample/blob/dagger_dependency_injection/app/src/main/java/com/bitwindow/aacpaginginfinitescrollingwithnetworksample/domain/movielist/MovieListUseCase.kt"><strong><em>MovieListUseCase</em></strong></a><em>:</em></p><pre>// <a href="https://github.com/asheshb/AACPagingInfiniteScrollingWithNetworkSample/blob/dagger_dependency_injection/app/src/main/java/com/bitwindow/aacpaginginfinitescrollingwithnetworksample/ui/movielist/MovieListViewModel.kt"><em>MovieListViewModel.kt</em></a></pre><pre>val movies = movieListUseCase.getMovies()</pre><p>In <a href="https://github.com/asheshb/AACPagingInfiniteScrollingWithNetworkSample/blob/dagger_dependency_injection/app/src/main/java/com/bitwindow/aacpaginginfinitescrollingwithnetworksample/ui/movielist/MovieListViewModel.kt"><em>MovieListViewModel</em></a><em> </em>class we initialize <em>movies </em>variable by calling <em>getMovies() </em>function of <a href="https://github.com/asheshb/AACPagingInfiniteScrollingWithNetworkSample/blob/dagger_dependency_injection/app/src/main/java/com/bitwindow/aacpaginginfinitescrollingwithnetworksample/domain/movielist/MovieListUseCase.kt"><em>MovieListUseCase</em></a> class.</p><p>Usually, we would directly call a database class and get the movies but according to the <a href="https://medium.com/@asheshb/clean-architecture-android-app-architecture-by-example-part-4-5-360c792d3fa7">Clean Architecture</a> flow, we should go through the domain layer instead of directly interacting with the data layer<em>.</em></p><p>Also, in <a href="https://github.com/asheshb/AACPagingInfiniteScrollingWithNetworkSample/blob/dagger_dependency_injection/app/src/main/java/com/bitwindow/aacpaginginfinitescrollingwithnetworksample/ui/movielist/MovieListViewModel.kt"><em>MovieListViewModel</em></a><em> </em>class we are directly depending on an instance of a concrete class <a href="https://github.com/asheshb/AACPagingInfiniteScrollingWithNetworkSample/blob/dagger_dependency_injection/app/src/main/java/com/bitwindow/aacpaginginfinitescrollingwithnetworksample/domain/movielist/MovieListUseCase.kt"><em>MovieListUseCase</em></a><em> </em>that too from another layer<em>. </em>As such we should use interfaces when interacting across the boundaries but here as the<em> </em><a href="https://github.com/asheshb/AACPagingInfiniteScrollingWithNetworkSample/blob/dagger_dependency_injection/app/src/main/java/com/bitwindow/aacpaginginfinitescrollingwithnetworksample/ui/movielist/MovieListViewModel.kt"><em>MovieListViewModel</em></a><em> </em>is a lower class, I took the liberty to make it directly depend on a higher class<em> </em><a href="https://github.com/asheshb/AACPagingInfiniteScrollingWithNetworkSample/blob/dagger_dependency_injection/app/src/main/java/com/bitwindow/aacpaginginfinitescrollingwithnetworksample/domain/movielist/MovieListUseCase.kt"><em>MovieListUseCase</em></a><em>. </em>In <a href="https://medium.com/@asheshb/clean-architecture-android-app-architecture-by-example-part-4-5-360c792d3fa7">part 4 Clean Architecture</a> you can read more about high/low-level classes.</p><p><strong>3.</strong> <a href="https://github.com/asheshb/AACPagingInfiniteScrollingWithNetworkSample/blob/dagger_dependency_injection/app/src/main/java/com/bitwindow/aacpaginginfinitescrollingwithnetworksample/domain/movielist/MovieListUseCase.kt"><strong><em>MovieListUseCase</em></strong></a><strong><em> -&gt; </em></strong><a href="https://github.com/asheshb/AACPagingInfiniteScrollingWithNetworkSample/blob/dagger_dependency_injection/app/src/main/java/com/bitwindow/aacpaginginfinitescrollingwithnetworksample/domain/movielist/MovieListRepository.kt"><strong><em>MovieListRepository</em></strong></a><em>:</em></p><pre><em>// </em><a href="https://github.com/asheshb/AACPagingInfiniteScrollingWithNetworkSample/blob/dagger_dependency_injection/app/src/main/java/com/bitwindow/aacpaginginfinitescrollingwithnetworksample/domain/movielist/MovieListUseCase.kt"><em>MovieListUseCase.kt</em></a></pre><pre>fun getMovies(): LiveData&lt;PagedList&lt;MoviePoster&gt;&gt; {<br>    return repository.getMovies()<br>}</pre><p><a href="https://github.com/asheshb/AACPagingInfiniteScrollingWithNetworkSample/blob/dagger_dependency_injection/app/src/main/java/com/bitwindow/aacpaginginfinitescrollingwithnetworksample/domain/movielist/MovieListUseCase.kt"><em>MovieListUseCase</em></a> class is in the domain package and it handles actions for movie list screen. This class acts as an intermediary between ui and data layer classes. The advantage is, in this layer, we can implement logic to manipulate the data flow both ways. Typically use case classes map one to one to action performed by user but here the <a href="https://github.com/asheshb/AACPagingInfiniteScrollingWithNetworkSample/blob/dagger_dependency_injection/app/src/main/java/com/bitwindow/aacpaginginfinitescrollingwithnetworksample/domain/movielist/MovieListUseCase.kt"><em>MovieListUseCase</em></a><em> </em>handles<em> </em>all the actions performed on movie list screen. This is a compromise I have made to reduce the number of thin classes but which leads to the coupling of actions.</p><p><a href="https://github.com/asheshb/AACPagingInfiniteScrollingWithNetworkSample/blob/dagger_dependency_injection/app/src/main/java/com/bitwindow/aacpaginginfinitescrollingwithnetworksample/domain/movielist/MovieListUseCase.kt"><em>MovieListUseCase</em></a> class <em>getMovies() </em>function makes a call to a repository class residing in the data layer. This repository class again works as an intermediary between domain layer classes and data source. This way domain layer class doesn’t need to know from where the data is coming through.</p><p><strong>4.</strong> <a href="https://github.com/asheshb/AACPagingInfiniteScrollingWithNetworkSample/blob/dagger_dependency_injection/app/src/main/java/com/bitwindow/aacpaginginfinitescrollingwithnetworksample/domain/movielist/MovieListRepository.kt"><strong><em>MovieListRepository</em></strong></a><strong><em> -&gt; </em></strong><a href="https://github.com/asheshb/AACPagingInfiniteScrollingWithNetworkSample/blob/dagger_dependency_injection/app/src/main/java/com/bitwindow/aacpaginginfinitescrollingwithnetworksample/data/repository/MovieDetailRepositoryImpl.kt"><strong><em>MovieListRepositryImpl</em></strong></a><em>: </em>We use the <a href="https://medium.com/@asheshb/programming-principles-android-app-architecture-by-example-part-2-5-41d87eaf30bf">Dependency Inversion Principle (DIP)</a> by defining a <a href="https://github.com/asheshb/AACPagingInfiniteScrollingWithNetworkSample/blob/dagger_dependency_injection/app/src/main/java/com/bitwindow/aacpaginginfinitescrollingwithnetworksample/domain/movielist/MovieListRepository.kt"><em>MovieListRepository</em></a><em> </em>interface inthe domain layer and making the <a href="https://github.com/asheshb/AACPagingInfiniteScrollingWithNetworkSample/blob/dagger_dependency_injection/app/src/main/java/com/bitwindow/aacpaginginfinitescrollingwithnetworksample/data/repository/MovieDetailRepositoryImpl.kt"><em>MovieListRepositryImpl</em></a> class in the data layer to implement it. This way <a href="https://github.com/asheshb/AACPagingInfiniteScrollingWithNetworkSample/blob/dagger_dependency_injection/app/src/main/java/com/bitwindow/aacpaginginfinitescrollingwithnetworksample/domain/movielist/MovieListUseCase.kt"><em>MovieListUseCase</em></a><em> </em>doesn’t directly depend on a concrete class in data layer.</p><p><strong>5.</strong> <a href="https://github.com/asheshb/AACPagingInfiniteScrollingWithNetworkSample/blob/dagger_dependency_injection/app/src/main/java/com/bitwindow/aacpaginginfinitescrollingwithnetworksample/data/repository/MovieDetailRepositoryImpl.kt"><strong><em>MovieListRepositryImpl</em></strong></a><strong><em> -&gt; </em></strong><a href="https://github.com/asheshb/AACPagingInfiniteScrollingWithNetworkSample/blob/dagger_dependency_injection/app/src/main/java/com/bitwindow/aacpaginginfinitescrollingwithnetworksample/data/repository/LocalData.kt"><strong><em>LocalData</em></strong></a><em>:</em></p><pre>// <a href="https://github.com/asheshb/AACPagingInfiniteScrollingWithNetworkSample/blob/dagger_dependency_injection/app/src/main/java/com/bitwindow/aacpaginginfinitescrollingwithnetworksample/data/repository/MovieDetailRepositoryImpl.kt"><em>MovieListRepositryImpl.kt</em></a></pre><pre>override<br>fun getMovies(): LiveData&lt;PagedList&lt;MoviePoster&gt;&gt; {<br>    return localData.getMovies()<br>}</pre><p><a href="https://github.com/asheshb/AACPagingInfiniteScrollingWithNetworkSample/blob/dagger_dependency_injection/app/src/main/java/com/bitwindow/aacpaginginfinitescrollingwithnetworksample/data/repository/MovieDetailRepositoryImpl.kt"><em>MovieListRepositryImpl</em></a><em> calls getMovies() </em>function of <a href="https://github.com/asheshb/AACPagingInfiniteScrollingWithNetworkSample/blob/dagger_dependency_injection/app/src/main/java/com/bitwindow/aacpaginginfinitescrollingwithnetworksample/data/repository/LocalData.kt"><em>LocalData</em></a><em> </em>class. The advantage of using repository class can be seen here as we can replace local data class with any other data class and it wouldn’t affect classes calling the repository class.</p><p><strong>6.</strong> <a href="https://github.com/asheshb/AACPagingInfiniteScrollingWithNetworkSample/blob/dagger_dependency_injection/app/src/main/java/com/bitwindow/aacpaginginfinitescrollingwithnetworksample/data/repository/LocalData.kt"><strong><em>LocalData</em></strong></a><strong><em> -&gt; </em></strong><a href="https://github.com/asheshb/AACPagingInfiniteScrollingWithNetworkSample/blob/dagger_dependency_injection/app/src/main/java/com/bitwindow/aacpaginginfinitescrollingwithnetworksample/data/database/LocalDataImpl.kt"><strong><em>LocalDataImpl</em></strong></a><em>: </em>Here again we are using interface <a href="https://github.com/asheshb/AACPagingInfiniteScrollingWithNetworkSample/blob/dagger_dependency_injection/app/src/main/java/com/bitwindow/aacpaginginfinitescrollingwithnetworksample/data/repository/LocalData.kt"><em>LocalData</em></a><em> </em>which is implemented by <a href="https://github.com/asheshb/AACPagingInfiniteScrollingWithNetworkSample/blob/dagger_dependency_injection/app/src/main/java/com/bitwindow/aacpaginginfinitescrollingwithnetworksample/data/database/LocalDataImpl.kt">LocalDataImpl</a> class in database<em> package. </em>This way in future if we want to change the local data source let’s say from SQLite to some other local database we can do so without affecting the repository class.</p><p><strong>7.</strong> <a href="https://github.com/asheshb/AACPagingInfiniteScrollingWithNetworkSample/blob/dagger_dependency_injection/app/src/main/java/com/bitwindow/aacpaginginfinitescrollingwithnetworksample/data/database/LocalDataImpl.kt"><strong>LocalDataImpl</strong></a><strong> -&gt; </strong><a href="https://github.com/asheshb/AACPagingInfiniteScrollingWithNetworkSample/blob/dagger_dependency_injection/app/src/main/java/com/bitwindow/aacpaginginfinitescrollingwithnetworksample/data/database/MovieDao.kt"><strong>MovieDao</strong></a>:</p><pre>// <a href="https://github.com/asheshb/AACPagingInfiniteScrollingWithNetworkSample/blob/dagger_dependency_injection/app/src/main/java/com/bitwindow/aacpaginginfinitescrollingwithnetworksample/data/database/LocalDataImpl.kt">LocalDataImpl.kt</a></pre><pre>override fun getMovies(): LiveData&lt;PagedList&lt;MoviePoster&gt;&gt; {<br>    val dataSourceFactory = movieDao.getDataSourcefactory()<br>    return LivePagedListBuilder(dataSourceFactory,<br>        MovieBoundaryCallback.DATABASE_PAGE_SIZE)<br>        .setBoundaryCallback(boundaryCallback)<br>        .build()<br>}</pre><p>All the heavy work is done here and this is the last point in data flow. The <em>LiveData</em> is returned from here which is observed by <a href="https://github.com/asheshb/AACPagingInfiniteScrollingWithNetworkSample/blob/dagger_dependency_injection/app/src/main/java/com/bitwindow/aacpaginginfinitescrollingwithnetworksample/ui/movielist/MovieListFragment.kt"><em>MovieListFragment</em></a>.</p><p>Here we can see how the data flows across the classes:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*aJIhyYTunKlgbjbxu14GZQ.png" /><figcaption>Data flow across classes</figcaption></figure><p>This route may look tedious but at each stage, we have an opportunity to manipulate the data flow. This leaves a provision for any future changes.</p><p>We use the <a href="https://github.com/asheshb/AACPagingInfiniteScrollingWithNetworkSample/blob/dagger_dependency_injection/app/src/main/java/com/bitwindow/aacpaginginfinitescrollingwithnetworksample/domain/entity/MoviePoster.kt"><em>MoviePoster</em></a> class to pass data. This class is free of any dependency (except framework class). We have put this class in the domain layer. This way domain layer classes don’t have to depend on any outside class and also classes from other layers can freely depend on <a href="https://github.com/asheshb/AACPagingInfiniteScrollingWithNetworkSample/blob/dagger_dependency_injection/app/src/main/java/com/bitwindow/aacpaginginfinitescrollingwithnetworksample/domain/entity/MoviePoster.kt">MoviePoster</a> class(being a higher level class). It is also possible to use separate data classes for each layer to provide more flexibility and customization. This would require mapping / converting of <em>MoviePoster </em>class to suitable data classes in each layer.</p><p>In Clean Architecture, Domain layer classes should be free of any outer dependency but we shouldn’t get obsessed with that because we have to utilize framework also. For example:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/974/1*S7GlX3WxyIAmLBf2698Mfg.png" /><figcaption>It’s very difficult to have a dependency-free domain class</figcaption></figure><p>At last, I want to say, when designing or implementing an architecture we need to remember two things:</p><ol><li><strong>There are always compromises</strong>: Most likely things wouldn’t go the way we planned. Be prepared to adapt and don’t be rigid.</li><li><strong>There are many ways to do the same thing</strong>: Finding a perfect solution may not be worth it so our goal should be to follow the guidelines as closely as possible.</li></ol><p>Here you can read my other article about architecture <a href="https://proandroiddev.com/10-things-every-android-app-developer-should-know-about-architecture-75ffc37df556">10 things every Android app developer should know about Architecture</a></p><p>Thanks for reading this article. You can connect with me on <a href="https://www.linkedin.com/in/asheshb">LinkedIn</a> and <a href="https://twitter.com/asheshbt">Twitter</a></p><blockquote><strong><em>If you like this article, please recommend it by hitting the clap icon as many times you wish 👏 Let’s enable each other with the power of knowledge.</em></strong></blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a57ec04a93d9" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Clean Architecture — Android app architecture by example Part 4/5]]></title>
            <link>https://medium.com/@asheshb/clean-architecture-android-app-architecture-by-example-part-4-5-360c792d3fa7?source=rss-a08949e258d7------2</link>
            <guid isPermaLink="false">https://medium.com/p/360c792d3fa7</guid>
            <category><![CDATA[android-developers]]></category>
            <category><![CDATA[software-architecture]]></category>
            <category><![CDATA[architecture-components]]></category>
            <category><![CDATA[clean-architecture]]></category>
            <category><![CDATA[android-app-development]]></category>
            <dc:creator><![CDATA[Ashesh Bharadwaj]]></dc:creator>
            <pubDate>Sat, 04 May 2019 05:03:52 GMT</pubDate>
            <atom:updated>2019-07-03T07:05:29.334Z</atom:updated>
            <content:encoded><![CDATA[<h3>Clean Architecture — Android app architecture by example Part 4/5</h3><p>This is the fourth article of a five-article series on Android app architecture:</p><ol><li><a href="https://medium.com/@asheshb/what-is-architecture-android-app-architecture-by-example-part-1-7ffa2cbfc0df">What is Architecture?</a></li><li><a href="https://medium.com/@asheshb/programming-principles-android-app-architecture-by-example-part-2-5-41d87eaf30bf">Programming principles</a></li><li><a href="https://medium.com/@asheshb/layered-architecture-android-app-architecture-by-example-part-3-5-1ed5c644a152">Layered Architecture</a></li><li>Clean Architecture (you are here)</li><li><a href="https://medium.com/@asheshb/sample-app-android-app-architecture-by-example-part-5-5-a57ec04a93d9">Sample app</a></li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Qkwd85sipYDZqtQ8KvF8tg.jpeg" /><figcaption>Photo by Max Langelott on Unsplash</figcaption></figure><p>Simply speaking, architecture is about grouping the classes, putting a boundary around them and managing the way they communicate across the boundaries.</p><p>Grouping the classes can be done using packages. Boundaries can be created using layers (also could be a package or an Android module). That leaves us how the classes communicate with each other across the boundaries.</p><p>As discussed in <a href="https://medium.com/@asheshb/programming-principles-android-app-architecture-by-example-part-2-5-41d87eaf30bf#f9fc">part 2</a>, the job of a good architecture is to minimize the impact of a change. One way to do that is by minimizing the dependencies of a class. Fewer dependencies would lead to fewer reasons to change. <em>Class A</em> is said to be dependent on <em>Class B</em> when <em>Class A</em> contains an instance of <em>Class B</em>. If we remove the reference of <em>Class B</em> from <em>Class A</em> then <em>Class A</em> would have no dependency on <em>Class B</em>. Now, any change in <em>Class B</em> has no effect on <em>Class A</em>.</p><p>It would be best to have a class with no dependency but that’s not possible for most of the classes. That’s why we should try to do the next best thing. Depending on <em>Interface</em> instance rather concrete classes. There are two clear advantages to this:</p><ol><li><strong>A contract is formed and only necessary functions are exposed</strong>: An interface exposes a set of functions of the concrete class to the dependent class. The concrete class now know exactly the obligations it has to fulfill.</li><li><strong>Switching the concrete classes behind the interface</strong>: A Printer class is dependent on a Report interface. Now we can easily make PdfReport or XmlReport class implement Report interface and pass them to the Printer class as an instance of Report interface. This has been discussed in <a href="https://medium.com/@asheshb/programming-principles-android-app-architecture-by-example-part-2-5-41d87eaf30bf#25c5">part 2</a>.</li></ol><p>While communicating through the boundaries we need to be careful not to depend on concrete classes. The other side of the boundary can provide an interface and we can communicate through that. This way we minimize the dependency on another layer.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/772/0*k3Z1dez0hVJRelew.jpg" /><figcaption>credit: <a href="http://blog.cleancoder.com/uncle-bob/2012/08/13/the-clean-architecture.html">blog.cleancoder.com</a></figcaption></figure><p>Clean Architecture by Robert C. Martin is an amalgamation of many popular architecture. The focus is on inter boundary communication.</p><p>The architecture is presented in a circular form which may seem confusing but once we have understood a few concepts, things would be easy. First, we need to understand what are low level and high level classes? Let’s say it’s about dependencies. A low level class has many dependencies and as we move higher the dependencies are reduced.</p><ol><li><strong>Low level class</strong>: A low level class is the one which has many dependencies, mostly on third party libraries or elements outside the app. For example, Activity is dependent on Android framework to get events so we can say it’s a low level class. Similarly, a class which connects or use database directly would be a low level class as database feature is generally provided by third party.</li><li><strong>High level class</strong>: High level classes are dependency nirvana. As a class becomes less and less dependent on other classes, it achieves higher level. <a href="https://en.wikipedia.org/wiki/Business_logic">Business logic</a> classes are a good example of high level class. For example, a business logic class where we calculate discount based on the quantity is a high level class as it would only depend on core Java classes, if at all.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*a-96sMxg87wykYIQNCbLNA.jpeg" /><figcaption><strong>High level classes are dependency nirvana.</strong> Photo by <a href="https://unsplash.com/photos/-A_Sx8GrRWg?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Becca Tapert</a> on <a href="https://unsplash.com/search/photos/freedom?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>The more complicated environment a class need to exist, the lower is its level. It’s easy for a high level class to be used in another app or another language altogether. High level classes are where we put our business logic.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/772/0*k3Z1dez0hVJRelew.jpg" /><figcaption>credit: <a href="http://blog.cleancoder.com/uncle-bob/2012/08/13/the-clean-architecture.html">blog.cleancoder.com</a></figcaption></figure><p>Now if we see the circular Clean Architecture diagram we would notice that the outer layer consists of low level classes and as we move inside the level goes higher.</p><p>There are two important things to notice in the Clean Architecture diagram:</p><ol><li>The dependency is in one direction (arrows pointing inward). The outer(lower) level classes are dependent on inner(higher) level classes.</li><li>For communication, the classes use the <a href="https://medium.com/@asheshb/programming-principles-android-app-architecture-by-example-part-2-5-41d87eaf30bf#8ef7">Dependency Inversion Principle</a> (the small diagram on the bottom right).</li></ol><p>Now lets’ see all this in our Android project environment. For simplicity, we can have 3 layers as discussed in <a href="https://medium.com/@asheshb/layered-architecture-android-app-architecture-by-example-part-3-5-1ed5c644a152">part 3</a>.</p><ol><li><strong>UI layer</strong>: The UI related classes. Activity, Fragment, Adapter, etc.</li><li><strong>Domain layer</strong>: All the business logic classes. This is the code which is unique to our app.</li><li><strong>Data layer</strong>: These classes are data related. This could be a local or network based database or system service classes.</li></ol><p>We can see the classes in UI and Data layer would be low level classes and classes in Domain layer would be high level classes. This is because classes in UI and Data layer both interact with elements outside the app environment example, Android framework, Networking service etc.</p><p>As per the Clean Architecture there has to be unidirectional dependency from lower level classes to higher level classes. This means both UI and Data layer classes should depend on Domain layer classes.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/686/1*8fqkltlaEMqqEN7XMRJ6kA.png" /><figcaption>UI and Data layer both depend on Domain layer</figcaption></figure><p>UI layer depending on the Domain layer is fine. UI classes can create an instance of Domain layer class and ask for data or provide input. The issue is how Domain layer is going to fetch data or send data to Data layer if it can’t depend on Data layer classes?</p><p>This is where <a href="https://medium.com/@asheshb/programming-principles-android-app-architecture-by-example-part-2-5-41d87eaf30bf#8ef7">Dependency Inversion Principle</a> comes in use which let high level classes interact with lower level classes without directly depending on it. You can read about this in <a href="https://medium.com/@asheshb/programming-principles-android-app-architecture-by-example-part-2-5-41d87eaf30bf#8ef7">Part 2</a></p><p>As discussed in Layered Architecture a layer can interact with the adjacent layer only. This means if UI layer needs any data it will ask Domain layer which will ask Data layer. This sometimes becomes irritating and boring as the job of a layer is limited to just passing on the data. The advantage of this is we get an opportunity to intercept the data in every layer and modify it as per the requirement of that specific layer. This comes handy for future changes.</p><p>Now we know how about boundaries, layers and the ideal way to interact across boundaries. In the next part we will see how all this learning come together in the form of coding.</p><p>Thanks for reading this article. You can connect with me on <a href="https://www.linkedin.com/in/asheshb">LinkedIn</a> and <a href="https://twitter.com/asheshbt">Twitter</a></p><blockquote><strong><em>If you like this article, please recommend it by hitting the clap icon as many times you wish 👏 Let’s enable each other with the power of knowledge.</em></strong></blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=360c792d3fa7" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Layered Architecture — Android app architecture by example Part 3/5]]></title>
            <link>https://medium.com/@asheshb/layered-architecture-android-app-architecture-by-example-part-3-5-1ed5c644a152?source=rss-a08949e258d7------2</link>
            <guid isPermaLink="false">https://medium.com/p/1ed5c644a152</guid>
            <category><![CDATA[android-developers]]></category>
            <category><![CDATA[clean-architecture]]></category>
            <category><![CDATA[architecture-components]]></category>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[software-architecture]]></category>
            <dc:creator><![CDATA[Ashesh Bharadwaj]]></dc:creator>
            <pubDate>Tue, 19 Mar 2019 07:08:37 GMT</pubDate>
            <atom:updated>2019-07-03T07:05:11.387Z</atom:updated>
            <content:encoded><![CDATA[<h3>Layered Architecture — Android app architecture by example Part 3/5</h3><blockquote>Software architecture is the art of drawing lines that I call boundaries — Robert C. Martin</blockquote><p>This is the third article of a five-article series on Android app architecture:</p><ol><li><a href="https://medium.com/@asheshb/what-is-architecture-android-app-architecture-by-example-part-1-7ffa2cbfc0df">What is Architecture?</a></li><li><a href="https://medium.com/@asheshb/programming-principles-android-app-architecture-by-example-part-2-5-41d87eaf30bf">Programming principles</a></li><li>Layered Architecture (you are here)</li><li><a href="https://medium.com/@asheshb/clean-architecture-android-app-architecture-by-example-part-4-5-360c792d3fa7">Clean Architecture</a></li><li><a href="https://medium.com/@asheshb/sample-app-android-app-architecture-by-example-part-5-5-a57ec04a93d9">Sample app</a></li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fIUhS_zVyTAZWABXGC97gA.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/photos/jLB_0YS5WBc?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Sean Stratton</a> on <a href="https://unsplash.com/search/photos/layers-mountain-mist?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>Architecture is about putting the classes behind boundaries and how they communicate with each other through the boundaries.</p><p>I guess at some point most of us would have wondered looking at the project tree structure folders in Android Studio, how these folders/packages are named? Why someone is naming a package <em>service </em>and other is using<em> background? </em>Packages play an important role in the architecture as it helps us to organize related classes and present them as an interface.</p><p>Our journey to implement an architecture knowingly or unknowingly starts when we start putting activities/fragments in <em>ui </em>package<em>. </em>This is the beginning where we acknowledge the need of an architecture.</p><p>As I have mentioned before, <a href="https://en.wikipedia.org/wiki/Separation_of_concerns">separation of concerns</a> is one of the guiding principles of programming. By putting the classes in a package we introduce them as a single component. We make these classes focus on a single concern. This grouping of classes can be called as a layer. We create layers so that we can organize classes which serve the same purpose.</p><p>Layered architecture pattern comes naturally to us. We start putting similar classes into packages which can be called a layer. There could be n number of layers but at a minimum we require 3 layers.</p><ol><li><strong>UI layer</strong>: Here we can put activities, fragments, adapters and any other classes related to the user interface.</li><li><strong>Domain layer</strong>: Here we can put the business logic of our app. The code which is exclusive to our app.</li><li><strong>Data layer</strong>: The database related classes and any other system service.</li></ol><p>The goal in a layered architecture is to create boundaries where one layer only interacts with adjacent layer and this happens in a single direction.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/491/1*ngBEh0b-UEwC9vzpvlQdUw.png" /><figcaption>Each layer interacts with adjacent layer and the flow is in single direction</figcaption></figure><p>This means <em>UI</em> layer can’t directly interact with <em>Data</em> layer. This isolation helps us to minimize the impact of changes. Although there are cases where we can have an open layer which can be bypassed but more or less we follow interaction to the adjacent layer in a single direction.</p><p>This is a very simple architecture pattern which can be implemented with minimal effort. This is the reason Google <a href="https://developer.android.com/jetpack/docs/guide">guide to app architecture</a> also recommends a similar app architecture. This will help the beginners to start with an architecture.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/1*-yY0l4XD3kLcZz0rO1sfRA.png" /><figcaption>The interaction diagram recommended by Google “Guide to app architecture”</figcaption></figure><p>In the next part we will see a more evolved architecture pattern: <a href="https://medium.com/@asheshb/clean-architecture-android-app-architecture-by-example-part-4-5-360c792d3fa7">Clean Architecture</a>.</p><p>Thanks for reading this article. You can connect with me on <a href="https://www.linkedin.com/in/asheshb">LinkedIn</a> and <a href="https://twitter.com/asheshbt">Twitter</a></p><blockquote><strong><em>If you like this article, please recommend it by hitting the clap icon as many times you wish 👏 Let’s enable each other with the power of knowledge.</em></strong></blockquote><figure><a href="https://usejournal.com/?utm_source=medium.com&amp;utm_medium=noteworthy_blog&amp;utm_campaign=tech&amp;utm_content=guest_post_image"><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PYxBP_WV7LI6zc-zdUkNqQ.png" /></a></figure><p>📝 Read this story later in <a href="https://blog.usejournal.com/r/?url=https%3A%2F%2Fusejournal.com%2F%3Futm_source%3Dmedium.com%26utm_medium%3Dnoteworthy_blog%26utm_campaign%3Dtech%26utm_content%3Dguest_post_read_later_text">Journal</a>.</p><p>👩‍💻 Wake up every Sunday morning to the week’s most noteworthy stories in Tech waiting in your inbox. <a href="https://blog.usejournal.com/r/?url=https%3A%2F%2Fusejournal.com%2Fnewsletter%2Fnoteworthy-in-tech%2F%3Futm_source%3Dmedium.com%26utm_medium%3Dnoteworthy_blog%26utm_campaign%3Dtech%26utm_content%3Dguest_post_text">Read the Noteworthy in Tech newsletter</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1ed5c644a152" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Programming principles — Android app architecture by example Part 2/5]]></title>
            <link>https://medium.com/@asheshb/programming-principles-android-app-architecture-by-example-part-2-5-41d87eaf30bf?source=rss-a08949e258d7------2</link>
            <guid isPermaLink="false">https://medium.com/p/41d87eaf30bf</guid>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[clean-architecture]]></category>
            <category><![CDATA[software-architecture]]></category>
            <category><![CDATA[android-developers]]></category>
            <category><![CDATA[architecture-components]]></category>
            <dc:creator><![CDATA[Ashesh Bharadwaj]]></dc:creator>
            <pubDate>Sat, 09 Feb 2019 07:43:06 GMT</pubDate>
            <atom:updated>2019-07-03T07:04:53.496Z</atom:updated>
            <content:encoded><![CDATA[<h3>Programming principles — Android app architecture by example Part 2/5</h3><blockquote>Learning how to use a tool is one aspect and creating a quality product using it is another aspect.</blockquote><p>This is the second article of a five-article series on Android app architecture:</p><p>1. <a href="https://medium.com/@asheshb/what-is-architecture-android-app-architecture-by-example-part-1-7ffa2cbfc0df">What is Architecture?</a> <br>2. Programming principles (you are here)<br>3. <a href="https://blog.usejournal.com/layered-architecture-android-app-architecture-by-example-part-3-5-1ed5c644a152">Layered Architecture</a> <br>4. <a href="https://medium.com/@asheshb/clean-architecture-android-app-architecture-by-example-part-4-5-360c792d3fa7">Clean Architecture</a> <br>5. <a href="https://medium.com/@asheshb/sample-app-android-app-architecture-by-example-part-5-5-a57ec04a93d9">Sample app</a></p><p>Android Studio, Java/Kotlin, Gradle, Dagger, RxJava etc. are all tools which help us to create a solution. Overall quality of the solution though does not depend on the tools. To bring quality to our solutions we need to take guidance from the programming principles.</p><p>Programming principles guide us to do things the right way. It’s the right mix of tools and programming principles which help us create an optimal solution.</p><p>The mother of all programming principles is “<a href="https://en.wikipedia.org/wiki/Separation_of_concerns">Separation of concerns</a>”. Separation of concerns is focusing on a certain aspect of the software. This can be applied at function/class or higher levels. For example, a class which reads and writes data to a disk should not be concerned about how the data is processed. Its job should be only to read/write the data and the processing of the data should be handled by another class.</p><p><strong>Why do we want to do that? Why the separation of concerns?</strong></p><p>The answer lies in the fact that change is inevitable. If we were to release just one version of an app, we could overlook the programming principles and practices. It would have been sufficient just to make the app functional. This is never the case though. Apps keep changing as per the requirements of clients or users.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/745/1*Xw8y_f1BIu8_JVJ-WJz0hw.jpeg" /><figcaption>Change is inevitable</figcaption></figure><p>All the programming principles and best practices we apply is to prepare against the change. It’s not like “change” is bad but a change can bring errors to the app, a bug can creep in or the app can become unstable.</p><p>The change is inevitable so what we can do is, minimize the impact of the change by reducing the scope. A change which happens to one class or a function is much better than a change which creates ripples across the app. We separate function/classes/modules to reduce the impact of a change. Most of the programming principles we follow are directly or indirectly based on this separation.</p><p>One set of programming principles for object-oriented programming is <a href="https://en.wikipedia.org/wiki/SOLID"><strong>SOLID</strong></a> introduced by Robert C. Martin in 2000. These principles are widely followed by developers all over the world.</p><p>SOLID is an acronym for the following principles:</p><ol><li><strong>Single Responsibility Principle</strong> (SRP)</li><li><strong>Open/Closed Principle</strong> (OCP)</li><li><strong>Liskov Substitution Principle</strong> (LSP)</li><li><strong>Interface Segregation Principle </strong>(ISP)</li><li><strong>Dependency Inversion Principle (</strong>DIP)</li></ol><blockquote><strong>Single Responsibility Principle (SRP)<br></strong>A class should have only one reason to change</blockquote><p>This principle takes direct guidance from the separation of concerns. A class should be responsible for one thing or we can say that there should be only one reason for it to change. Only one reason because if there are two reasons to change then the probability of an error or instability becomes twice and so on.</p><p>The easiest way to implement this principle is to divide a class into small functions and then group these functions together based on their proximity(cohesiveness). This way we can create multiple classes from a single class. All of them having a single responsibility. We need to be careful not to overdo it.</p><p>Let’s take an example, if we have two functions writeDataToDisk and readDataFromDisk, should they be in separate classes DataReader and DataWriter or a single class DataAccess? Both these functions are tied to the same data and that data would be the reason to change so it makes sense to keep them in the same class. Although a lot of other factors come into play with experience it becomes easy for us to decide.</p><blockquote><strong>Open/Closed Principle (OCP)<br></strong>Classes should be open for extension but closed for modification</blockquote><p>A class should be open for extension but closed for modification. How can we extend without modifying the code? Here extend doesn’t mean the keyword <em>extend</em> but more like adapt to future changes. The answer lies in abstraction. Instead of a class depending on another concrete class, it should depend on an abstraction</p><p>For example, let’s say we have a ReportWriter class which calls getReportData() function of PdfReport to get the data and print it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/557/1*Vx2RcCMwP969lMCKZryqlg.png" /><figcaption>ReportWriter class calls PdfReport class function getReportData()</figcaption></figure><p>It is reasonable to assume that in near future we may need to print the report in text, xml or other formats. When that happens we will have to modify the ReportWriter class to adjust for any new format. That would be a violation of OCP, which says that a class should be closed for modification.</p><p>A better way to make our code future ready is to depend on abstraction.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/947/1*piocb4zxVisBMPx_2079Zw.png" /><figcaption>Instead of ReportWriter directly depending on concrete class it depends on abstraction (interface)</figcaption></figure><p>We can declare an interface Report and make the concrete class PdfReport implement it. We can pass PdfReport as Report object to the ReportWriter class. This way ReportWriter will depend on abstraction and wouldn’t be concerned with the concrete class which is implementing that interface.</p><blockquote><strong>Liskov Substitution Principle (LSP)</strong><br>Subtypes must be substitutable for their base types</blockquote><p>A child object should behave like its parent when passed as a parent. Let’s say Class B extends Class A. There is a function which requires Class A object but instead, if we pass Class B object (perfectly legal in object-oriented world) then Class B object should behave like Class A object.</p><p>For example, if Class A has a function parseData() which returns a String in JSON format. Class B extends class A and overrides parseData() where it returns a String without JSON formatting. Class B parseData() function will work fine where Class B object is required. The problem would happen if we pass Class B object where Class A object is required. The calling code would expect a JSON formatted string but instead would get a non formatted string.</p><p>This kind of violation brings instability to our apps.</p><blockquote><strong>Interface Segregation Principle (ISP)</strong><br>Many client specific interfaces are better than one general purpose interface</blockquote><p>There is a Service class which implements ServiceInterface. The ServiceInterface contains the functions required by its clients.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*tlMiSvzBpWBMC1Ybq6NRoQ.png" /><figcaption>A large single interface serving multiple client</figcaption></figure><p>A better way to handle this is to divide the large single interface into multiple interface and let clients use them. This way clients would know about only those methods which it requires. Remember, separation of concerns?</p><p>Now Service class is implementing 3 interfaces instead of a single large interface. Other than that there is no change in Service class but the exposure to clients are now reduced to only those functions they require.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*5qnczrOkD_cnrN_6b5O8_A.png" /><figcaption>The large interface is broken down into smaller interfaces.</figcaption></figure><blockquote><strong>Dependency Inversion Principle (DIP)</strong><br>High-level modules should not depend on low-level modules</blockquote><p>A high level class should not depend on low level class and both should depend on abstraction. Before going further let’s see what’s a high/low level class.</p><p>An Android app executes inside a boundary. Outside the boundary, there is the Android framework itself, various service manager etc. A class which is closest to the boundary is the lower level class. For example Activity/Fragment get events directly from the Android framework which is outside the boundary so it’s low level class. A low level class is based on implementation/details.</p><p>A high level class is more abstract, dealing with business logic (the code specific to your app). High level classes are less dependent on the platform/third party libraries.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/434/1*8PprF-9z2K7rc9_uc3cckA.png" /><figcaption>High level class shouldn’t depend on low level class</figcaption></figure><p>Let’s say Repository class needs to get data from DB class. Here Repository is a higher level class than DB class because DB class is more dependent on detail (SQLite). Now, Repository class can’t directly depend on DB class as it would be a violation of DIP.</p><p>What we do is create an Interface DataInterface which is implemented by DB class and then Repository class uses an object of DataInterface . The data interface is an abstract class and at the same level as Repository so it’s fine for the Repository to depend on it.</p><p>Also if we see both Repository and DB class now depend on abstraction. This helps to loosely couple the classes. Another advantage is now that we have DataInterface , a network class can implement it and Repository class (without any modification) would be ready to get data from the network. Reminds you something of Open Closed Principle?</p><p>Once we have an understanding of SOLID principles the next step should be to study various design patterns. Design patterns are the ready-made solutions to implement these principles in our app. For example, Open Closed Principle can be implemented using a strategy pattern or template method pattern.</p><p>In the next part we will learn what‘s layered architecture.</p><p>Thanks for reading this article. You can connect with me on <a href="https://www.linkedin.com/in/asheshb">LinkedIn</a> and <a href="https://twitter.com/asheshbt">Twitter</a></p><blockquote><strong><em>If you like this article, please recommend it by hitting the clap icon as many times you wish 👏 Let’s enable each other with the power of knowledge.</em></strong></blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=41d87eaf30bf" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[What is Architecture? — Android app architecture by example Part 1]]></title>
            <link>https://medium.com/@asheshb/what-is-architecture-android-app-architecture-by-example-part-1-7ffa2cbfc0df?source=rss-a08949e258d7------2</link>
            <guid isPermaLink="false">https://medium.com/p/7ffa2cbfc0df</guid>
            <category><![CDATA[android-developers]]></category>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[software-architecture]]></category>
            <category><![CDATA[architecture-components]]></category>
            <category><![CDATA[clean-architecture]]></category>
            <dc:creator><![CDATA[Ashesh Bharadwaj]]></dc:creator>
            <pubDate>Thu, 31 Jan 2019 06:39:15 GMT</pubDate>
            <atom:updated>2019-07-03T07:04:35.751Z</atom:updated>
            <content:encoded><![CDATA[<h3>What is Architecture? — Android app architecture by example Part 1/5</h3><p>This is the first article of a five-article series on Android app architecture. The goal of this series is to bring architecture to the mainstream android app development, especially those who are beginning with architecture.</p><ol><li><strong>What is Architecture?</strong> (you are here) : An overview of app architecture.</li><li><a href="https://medium.com/@asheshb/programming-principles-android-app-architecture-by-example-part-2-5-41d87eaf30bf"><strong>Programming principles</strong></a> : To design the architecture it’s essential that we understand the fundamentals of the programming. Architecture is essentially the good use of programming principles.</li><li><a href="https://blog.usejournal.com/layered-architecture-android-app-architecture-by-example-part-3-5-1ed5c644a152"><strong>Layered Architecture</strong></a> : A simple way to start with architecture. It comes naturally to most of the apps.</li><li><a href="https://medium.com/@asheshb/clean-architecture-android-app-architecture-by-example-part-4-5-360c792d3fa7"><strong>Clean Architecture</strong></a> : A look at Uncle Bob’s Clean Architecture and how we can adapt it for Android apps.</li><li><a href="https://medium.com/@asheshb/sample-app-android-app-architecture-by-example-part-5-5-a57ec04a93d9"><strong>Sample app</strong></a>: Theory is fine but it’s the practice which helps us cement those concepts in our mind. We will see how we implement acquired knowledge to build an Android app</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*aUWLd5vJ0dqHfK5j" /><figcaption>Photo by <a href="https://unsplash.com/@danist07?utm_source=medium&amp;utm_medium=referral">贝莉儿 NG</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p><strong>What is Architecture?</strong></p><p>The definition of architecture by IEEE(Institute of Electrical and Electronics Engineers) which is known for developing standards for the computer and electronics industry is:</p><blockquote>The fundamental concepts or properties of a system in its environment embodied in its elements, relationships, and in the principles of its design and evolution. — IEEE</blockquote><p>The reason most of the definitions sound complicated is that they have to be exhaustive. They have to cover each and every use case on every platform. Thankfully we can reduce the scope of the definition in the context of Object-Oriented Programming language (Java/Kotlin) on Android platform. Here is how we can simplify it:</p><blockquote><em>Architecture can be simply described as the placement of classes in an app and how they communicate. We draw an overview of roles and responsibilities of these classes while grouping them.</em></blockquote><p>I hope this would make more sense. At the time of designing architecture, we are not worried about the implementation of the classes, just their role, and responsibility.</p><p><strong>What are the prerequisites for Architecture?</strong></p><p>There are designated software architects who design the architecture but that’s usually in large organizations. For most of us who are working in small companies or as a freelancer, the responsibility falls on our shoulder. The good thing is architecture wouldn’t vary much from an app to app so if we create a blueprint it would fit most of the apps. That means we don’t have to explore the vast areas of architecture implementation. We can limit our scope and yet do fine.</p><p>The learning involved is mostly the practices we should follow anyway as a good developer.</p><p><strong>What are the advantages of an Architecture?</strong></p><p>If we were sure that there would be only one version to be released in the lifetime of an app, the architecture wouldn’t matter. These days devices have enough hardware resources to hide our shortcoming as a programmer. Gone are the days when we had to fight for few KBs of space or memory.</p><p>The main point of architecture is to prepare the app for future changes. Striving to achieve a structure which can accommodate these changes gracefully. In fact, most of the programming principles are based on separation/decoupling so that the scope of a change can be minimized.</p><p>A good architecture let us absorb those changes at the system level.</p><p><strong>What are the disadvantages of an Architecture?</strong></p><p>Everything has a pro and cons so it would be fair to assume that Architecture does have disadvantages too. These disadvantages though are not inherent to the architecture but in its implementation and maintenance.</p><ol><li><strong>Extra work in sense of time and effort</strong>: Designing and architecture is time taking process and before implementing any future change we need to consider its effect on the architecture.</li><li><strong>It’s a team effort</strong>: All developers need to be on board. Everyone needs to be made aware and educated (although at a different level) of the architecture.</li><li><strong>Requires strict discipline</strong>: We can’t rush things or take a shortcut. One misplaced change can ruin the integrity of an architecture. There will always be a comprise but we need to be careful about them.</li></ol><p>As we see all these factors are external to architecture and mostly related to effort and time. One thing I would like to mention in case it comes to someone’s mind. Implementation of extra code for architecture would only have a minuscule effect on performance if any. Performance should never be an excuse of taking shortcuts (unless in specific cases).</p><p>In the next part, we will learn about programming principles.</p><p>Thanks for reading this article. You can connect with me on <a href="https://www.linkedin.com/in/asheshb">LinkedIn</a> and <a href="https://twitter.com/asheshbt">Twitter</a></p><blockquote><strong><em>If you like this article, please recommend it by hitting the clap icon as many times you wish 👏 Let’s enable each other with the power of knowledge.</em></strong></blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7ffa2cbfc0df" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[10 things every Android app developer should know about Architecture]]></title>
            <link>https://proandroiddev.com/10-things-every-android-app-developer-should-know-about-architecture-75ffc37df556?source=rss-a08949e258d7------2</link>
            <guid isPermaLink="false">https://medium.com/p/75ffc37df556</guid>
            <category><![CDATA[android-development]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[software-architecture]]></category>
            <category><![CDATA[android-architecture]]></category>
            <category><![CDATA[clean-architecture]]></category>
            <dc:creator><![CDATA[Ashesh Bharadwaj]]></dc:creator>
            <pubDate>Fri, 07 Dec 2018 10:49:38 GMT</pubDate>
            <atom:updated>2019-07-04T13:31:20.994Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*O5DBD71UkJVmaea0gPRY0A.jpeg" /><figcaption><a href="https://www.pexels.com/photo/long-exposure-photography-white-dome-building-interior-911758/">Photo by gdtography from Pexels</a></figcaption></figure><p>Architecture in object-oriented words for a monolithic app can be simply described as the placement of classes in the system and how they communicate. We draw an overview of roles and responsibilities of these classes while grouping them.</p><p>Here are a few things which may help us understand about architecture:</p><p><strong>1. Architecture is language and platform agnostic</strong>: Architecture is based on the principles of programming. These guiding principles, be it <a href="https://en.wikipedia.org/wiki/SOLID">SOLID principle</a> or design/architectural patterns are stable and the knowledge can be applied across language and platforms. Investing the time in learning architecture pays off well. It not only help us design better architecture but also improve our coding skills.</p><p><strong>2. The confusion about MVP/MVVM</strong>: The responsibility of MVP (Model-View-Presenter)/MVVM (Model-View-ViewModel) is to separate UI from the code. We use <em>Presenter</em>/<em>ViewModel</em> to strip any logical code from the <em>View</em> (Activity/Fragment). This separation involves <em>VP</em>/<em>VVM</em> leaving us with <em>Model(M) </em>whose job is to provide data to the <em>Presenter</em>/<em>ViewModel.</em></p><p>What I Usually see is <em>Presenter</em> and <em>ViewModel</em> are implemented diligently with interfaces and observers but beyond that it’s chaos. I think this can be blamed on taking <em>VP</em>/<em>VVM</em> for the whole architecture of an app and not putting effort to shape the <em>Model</em>(<em>M</em>). MVP/MVVM plays a crucial role as an architectural pattern but the architecture of an app goes beyond UI separation.</p><p><strong>3. Architecture is important but not essential</strong>: Learning Architecture comes at a later stage in the evolution of an Android app developer. One of the reason is that our apps can excel functionally even without architecture so why spend extra time? How would we convince our boss/client that the extra time we spend may not have any immediate benefit?</p><p>It’s only after a few version we realize the mess we are in but then it’s too late. As we keep getting in the same mess, again and again, we start to understand the essentiality of architecture.</p><p><strong>4. Architecture improves changeability</strong>: Architecture wouldn’t matter much if there was only one version to be released of an app. In fact this is the approach many of us take, while developing app, which is shortsighted.</p><p>If I have to point out the biggest benefit of having a proper architecture that would be the ease of modification and its effectiveness.</p><p>It’s true that we can’t foresee everything that the app may become in future but a good architecture has enough flexibility to adjust to those unknown future changes.</p><p><strong>5. Architecture doesn’t require special knowledge</strong>: For a good programmer designing architecture comes naturally. This is somewhat a repeat of point #1 but it’s important so I thought to twist it a little and present again 😃</p><p>Dagger/RxJava or any other tool require us to learn things specific to them. In the case of architecture it’s just the programming principles and guidelines. The better we become at coding the better we are able to visualize the architecture of an app.</p><p><strong>6. Architecture requires discipline and patience</strong>: Architecture is like a tree. We can’t get fruits the day we plant it. It takes time and effort to design an architecture and it’s an ongoing process. Every change in the app would require a thoughtful decision before the implementation.</p><p>The major damage comes to an app architecture when we start taking shortcuts. The promises we make to ourselves (to clean the code later) only to be broken again and again.</p><p>We need to understand what we are getting into otherwise it would be a great start only to lose later. Architecture is our lifelong commitment to an app.</p><p><strong>7. Google Architecture components</strong>: In 2017 Google introduced <a href="https://developer.android.com/topic/libraries/architecture/">Architecture Components</a>. Architecture components are not there just to shift the mess from Activity/Fragment to ViewModel. These “Architecture” components are there to facilitate us in designing the architecture of the app. These components make it easy to implement the architecture which otherwise would have been a difficult task.</p><p>For example, <em>LiveData </em>makes it easy to observe changes in data that too respecting the lifecycle of Activity/Fragment. One may find <em>ViewModel </em>a way to survive configuration change but its real implication is to help us separate UI from the logic.</p><p><strong>8. We don’t have to go all the way to become a Software Architect</strong>: Every software architect is a developer but not every developer is an architect. In a large organization there are designated software architects. For them, it’s essential to have deep knowledge of various architectural patterns and how they work.</p><p>As a developer, it helps to have an understanding of architecture so that we can make sense of the abstractions implemented. This becomes more important if we are working freelance or in a startup with limited resource. In such a case, it would suffice to have a basic knowledge of architecture.</p><p>There are many simple patterns and principles we can adopt to create a decent architecture for our app without going too deep into architecture world.</p><p><strong>9. Benefits of architecture</strong>: As mentioned earlier one of the advantages is changeability. Adding new features and modifications become easy and quick if the components are properly separated and communicate in an organized manner. This separation leads to many other advantages like testability where we can easily test components in isolation.</p><p><strong>10. Where to start?</strong> Architecture is everywhere, embedded systems, web applications, enterprise software, mobiles apps etc. Each of them requires a different type of architecture. Unless we want to be <a href="https://en.wikipedia.org/wiki/Software_architect">software architect</a> we have to be selective of what we begin with.</p><p>A good read to understand the need of architecture and its implementation would be <a href="https://www.amazon.com/Clean-Architecture-Craftsmans-Software-Structure/dp/0134494164">Clean Architecture</a> by Robert C. Martin (aka Uncle Bob) and a little deep is <a href="https://www.amazon.com/Domain-Driven-Design-Tackling-Complexity-Software/dp/0321125215">Domain-Driven Design: Tackling Complexity in the Heart of Software</a> by Eric Evans.</p><p>Google provides <a href="https://developer.android.com/jetpack/docs/guide">guide to app architecture</a> with dependency flowing in one direction but that’s a bare minimum. Once you have grasped that you may want to implement a layered architecture pattern guided by <a href="http://blog.cleancoder.com/uncle-bob/2012/08/13/the-clean-architecture.html">Clean Architecture</a>. This approach is easy and would suit most of the apps. I have created a sample app following these principles. Here is the <a href="https://github.com/asheshb/AACPagingInfiniteScrollingWithNetworkSample">Github repo of the sample app</a>.</p><p>If you are new to programming, I would suggest starting with <a href="https://en.wikipedia.org/wiki/SOLID">SOLID principles</a> as they would make you architecture ready :)</p><p>You may also want to read my 5 article series <a href="https://medium.com/@asheshb/what-is-architecture-android-app-architecture-by-example-part-1-7ffa2cbfc0df"><strong>Android app architecture by example</strong></a> where I discuss the architecture in detail.</p><p>Thanks for reading this article. You can connect with me on <a href="https://www.linkedin.com/in/asheshb">LinkedIn</a> and <a href="https://twitter.com/asheshbt">Twitter</a></p><blockquote><strong><em>If you like this article, please recommend it by hitting the clap icon as many times you wish 👏 Let’s enable each other with the power of knowledge.</em></strong></blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=75ffc37df556" width="1" height="1" alt=""><hr><p><a href="https://proandroiddev.com/10-things-every-android-app-developer-should-know-about-architecture-75ffc37df556">10 things every Android app developer should know about Architecture</a> was originally published in <a href="https://proandroiddev.com/">ProAndroidDev</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Android Architecture Components and Paging library in Kotlin: Infinite scrolling with network]]></title>
            <link>https://medium.com/@asheshb/android-architecture-components-and-paging-library-in-kotlin-infinite-scrolling-with-network-c75b6cd183c0?source=rss-a08949e258d7------2</link>
            <guid isPermaLink="false">https://medium.com/p/c75b6cd183c0</guid>
            <category><![CDATA[pagination]]></category>
            <category><![CDATA[androiddev]]></category>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[android-app-development]]></category>
            <dc:creator><![CDATA[Ashesh Bharadwaj]]></dc:creator>
            <pubDate>Thu, 04 Oct 2018 06:36:17 GMT</pubDate>
            <atom:updated>2018-10-04T06:36:17.713Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*t40H91FnN0dn0attyOjYag.png" /></figure><p>One of the most common task for Android app developers is to show a list of items in <em>RecyclerView</em>.</p><p>This is also one of the most CPU/Memory intensive task considering hundreds of items could be scrolled in a matter of few seconds. The speed at which we consume data today, this is not an overstatement.</p><p>Google developer team has been working for a long time not only to optimize the process of fetching and rendering the data but also to make our job easy. <a href="https://developer.android.com/topic/libraries/architecture/paging/"><em>Paging library</em></a> which is part of the <a href="https://developer.android.com/jetpack/"><em>Android Jetpack</em></a> is the latest endeavor in this journey.</p><p>I have created a <a href="https://github.com/asheshb/AACPagingInfiniteScrollingWithNetworkSample">GitHub sample</a> to demonstrate the use of the Paging library. For this, you would require a basic knowledge of <a href="https://developer.android.com/topic/libraries/architecture/"><em>Android Architecture Components</em></a> (AAC) and <a href="https://developer.android.com/kotlin/get-started"><em>Kotlin</em></a>.</p><p>We will be using <em>Paging library</em> and <em>LiveData</em> to show the user a list of movies from The Movie Database (TMDb) API. We show the movies in descending order of release date so that the latest movies are shown at the top.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/491/1*OKWFqyEKu-Ihlai8zbkWFw.png" /><figcaption>Data flow cycle</figcaption></figure><p>This above illustration is the data flow cycle of the sample app. It’s divided into <strong>two operations</strong>:</p><ol><li><strong>Loading data from the database to <em>RecyclerView</em></strong>. This will be done automatically when the user opens the app.</li><li><strong>Fetching data from the TMDb API and saving it in the database</strong>. This will be on demand when the user scrolls the <em>RecyclerView</em>.</li></ol><p>The app needs to implement an architecture which separates various component making it easy for us to maintain and expand the code.</p><p>Here is an overview of the app architecture:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/681/1*wPWX_Rkd6rsH985emo9o3A.png" /><figcaption>App architecture</figcaption></figure><p>In the above illustration, the dependency is unidirectional. The <em>Fragment</em> depends on <em>ViewModel</em> which means <em>Fragment</em> uses <em>ViewModel</em> component and it has knowledge of it. The <em>ViewModel,</em> however, doesn’t know about the <em>Fragment</em>. It just exposes its data to be consumed by anyone. Similarly, Repository has no knowledge of <em>ViewModel</em>. The advantage of such a model is that we can easily replace components without worrying about breaking the system.</p><p>We can improve the architecture based on the <a href="https://en.wikipedia.org/wiki/Dependency_inversion_principle">Dependency Inversion Principle</a> (DIP) by implementing interfaces between the components so that they don’t directly depend on implementations. There are many other things to improve but for this sample app, the above shall do.</p><p><em>Android Architecture Components</em> have made our life easy with <em>LiveData</em> which not only help us observe changes in a data source but also relieves us from <em>Lifecycle</em> event maintenance.</p><p>In both the operations we will be using <em>LiveData</em> to monitor and render the changes:</p><ol><li>In <strong>1st operation</strong>, we will be using <em>LiveData</em> to observe any changes in the database and show them in fragment’s <em>RecyclerView</em>. This is a one-time setup.</li><li>In <strong>2nd operation</strong>, we will observe <em>LiveData</em> to show the progress of fetching the data from TMDB API. This is a recurring operation. Every time the user scrolls to the bottom we will get a new <em>LiveData</em> to monitor the fetching progress.</li></ol><p>Before we continue let us discuss a very handy class which Paging library provides, BoundaryCallback. As the name suggests it calls us back when boundaries of the items in the database have reached.</p><p>This class has 3 callback functions:</p><ol><li>onZeroItemsLoaded() : This is called when the database is empty. In this call we fetch movies from the TMDb API.</li><li>onItemAtFrontLoaded() : This function is called when an item is loaded at the top. We can use this call to fetch latest movies. For example if the movie loaded at top has release date of yesterday, we can fetch today’s released movies.</li><li>onItemAtEndLoaded(): This function is called when an item at the bottom is loaded. This usually means, depending on the <a href="https://developer.android.com/reference/android/arch/paging/PagedList.Config.Builder.html#setPrefetchDistance(int)">prefetch</a> setting, soon all the items from database would be shown. In this call we need to fetch more movies from the TMDb API.</li></ol><p>Using all these callbacks we can perform actions required to show the data to the user. <em>ViewModel</em> class is where all the action happens. It holds the <em>LiveData</em> from both operations.</p><p>Here are the details of the <strong>1st operation</strong>, loading items from the database and showing it in <em>RecyclerView</em>:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c3628a035cdaa7f5905c3ae93ed3cb6c/href">https://medium.com/media/c3628a035cdaa7f5905c3ae93ed3cb6c/href</a></iframe><p>The movies <em>LiveData</em> holds the value returned by LivePagedListBuilder which takes a dataSourceFactory and a <a href="https://developer.android.com/reference/android/arch/paging/PagedList.Config.Builder#setPageSize(int)">page size</a>. movies is observed by <em>Fragment</em> to show the data in <em>RecyclerView</em>.</p><p>LivePagedListBuilder can also take a PagedList.Config to customize the PagedList it creates. To know more about PagedList.Config, please have a <a href="https://www.youtube.com/watch?v=BE5bsyGGLf4&amp;feature=youtu.be&amp;t=511">look at this I/O 18 talk</a>. Also this talk is a great source of knowledge for working of Paging library.</p><p>For us, boundaryCallback is of interest. We have created it as an instance variable because we require the callbacks for 2nd operation.</p><p>Let’s use now see the details of the <strong>2nd operation</strong>, fetching more movies from TMDb API when the user scrolls to the bottom of the <em>RecyclerView</em>:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7868f3200ba7e790c21445f6c0d0ddbe/href">https://medium.com/media/7868f3200ba7e790c21445f6c0d0ddbe/href</a></iframe><p>Here we are using switchMap() to observe the <em>LiveData</em> boundaryState which is a member of boundaryCallback object. TheboundaryState gets updated by the three boundary callback functions we discussed earlier.</p><p>We pass the details to onBoundaryItemLoaded() function which calls repository function fetchMore() . This fetchMore() function makes an async network call and returns a LiveData&lt;LoadingStatus&gt; where it update various stages of fetching: loading, success or error. Based on this status, <em>Fragment</em> shows a loading indicator or error message. Once the data is fetched, it’s saved in database. This triggers the update in movies <em>Livedata </em>which is being observed by the <em>Fragment</em>. This completes the data flow cycle.</p><p>We are left with one more thing to perform. Regular update of the database. This can be done using <a href="https://developer.android.com/topic/libraries/architecture/workmanager/"><em>WorkManager</em></a>.<em> </em>We can schedule to update the database every day when the device is charging and on non metered network. For now, I have put a swipe refresh function which can be used to manually refresh the database.</p><p>Here is the sample GitHub repository: <a href="https://github.com/asheshb/AACPagingInfiniteScrollingWithNetworkSample">https://github.com/asheshb/AACPagingInfiniteScrollingWithNetworkSample</a></p><p><strong>Resources</strong></p><ol><li><a href="https://www.youtube.com/watch?v=BE5bsyGGLf4">Android Jetpack: manage infinite lists with RecyclerView and Paging (Google I/O ‘18)</a></li><li><a href="https://developer.android.com/topic/libraries/architecture/paging/">Paging library overview</a></li><li><a href="https://codelabs.developers.google.com/codelabs/android-paging/">Android Paging codelab</a></li><li><a href="https://github.com/googlesamples/android-architecture-components/tree/master/PagingWithNetworkSample">Paging With Network Sample (Google samples)</a></li></ol><p>Thanks for reading this article. You can connect with me on <a href="https://www.linkedin.com/in/asheshb">LinkedIn</a> and <a href="https://twitter.com/asheshbt">Twitter</a></p><blockquote><strong><em>If you like this article, please recommend it by hitting the clap icon 👏 and yes you can click it more than one time </em></strong><em>😃</em><strong><em> Let’s enable each other with the power of knowledge.</em></strong></blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c75b6cd183c0" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Just 1 Thing each from I/O 18 Android sessions]]></title>
            <link>https://medium.com/@asheshb/just-1-thing-each-from-i-o-18-android-sessions-b329949ff329?source=rss-a08949e258d7------2</link>
            <guid isPermaLink="false">https://medium.com/p/b329949ff329</guid>
            <category><![CDATA[android]]></category>
            <category><![CDATA[android-videos]]></category>
            <category><![CDATA[googleio]]></category>
            <category><![CDATA[androiddev]]></category>
            <category><![CDATA[android-app-development]]></category>
            <dc:creator><![CDATA[Ashesh Bharadwaj]]></dc:creator>
            <pubDate>Thu, 09 Aug 2018 06:38:56 GMT</pubDate>
            <atom:updated>2018-08-09T06:38:56.415Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*7bk-CoBYrGtWErRUW1FlDA.jpeg" /></figure><p>The highlight of Google I/O for developers are the sessions where experts from all over the world talk about the latest and best in Google related technologies. These talks are followed by articles and conferences all year round to further explain and familiarize us with them.</p><p>Here are links to some of the I/O 18 Android session videos I watched and my Just 1 Thing take of them. This 1 thing is not a gist of the session. It’s just this 1 thing which stood out for me.</p><p>I hope this 1 thing generate enough interest for you to watch the video and learn something new from that 👍</p><p>👉️<a href="https://www.youtube.com/watch?v=st1XVfkDWqk"><strong>Android Jetpack: sweetening Kotlin development with Android KTX</strong></a></p><p>It’s important to learn how but it’s more important to learn why. Jake Wharton explained how it’s decided whether a piece of code is worthy to be incorporated as an extension function. This applies not only to <a href="https://developer.android.com/kotlin/ktx">extension functions</a> but in general so a good video to watch.</p><p>👉<a href="https://youtu.be/V7E62C9GWFs"><strong>Android fireside chat</strong></a></p><p>This was an interesting session with developers having a candid Q&amp;A with Googlers. Google now promotes single activity apps. Use activity as main entry point and then let the fragment handle various screens. <a href="https://developer.android.com/topic/libraries/architecture/navigation/">Jetpack Navigation component</a> is major step towards it.</p><p>👉<a href="https://www.youtube.com/watch?v=IrMw7MEgADk"><strong>Modern Android development: Android Jetpack, Kotlin, and more</strong></a></p><p>Chet Hasse: “if you use enums, I don’t care”. This alone makes the video interesting enough to watch 😃 Dalvik was optimized for space, it couldn’t defragment so we had to be cautious while allocating memory. Lot of things changed between Dalvik and ART and we have been given more liberty with memory management.</p><p>👉<a href="https://www.youtube.com/watch?v=BE5bsyGGLf4"><strong>Android Jetpack: manage infinite lists with RecyclerView and Paging</strong></a></p><p>One of the most common task we do is to show a list of items in a <em>RecyclerView</em>. It starts getting complicated with pagination. With introduction to <a href="https://developer.android.com/topic/libraries/architecture/paging/">paging library</a> things have become simple so that we can coordinate with network to fetch more data. We get a <a href="https://developer.android.com/reference/android/arch/paging/PagedList.BoundaryCallback"><em>BoundaryCallback</em></a> which is out of data signal from database and our cue to fetch more data from the network.</p><p>👉<a href="https://www.youtube.com/watch?v=jdKUm8tGogw"><strong>Android Jetpack: what’s new in Android Support Library</strong></a></p><p><a href="https://developer.android.com/topic/libraries/support-library/androidx-overview">Android extension libraries</a> (Android X): Focused and smaller artifacts. For example to use ViewPager now we just require ViewPager library instead of earlier support v4. <br>Earlier binary compatibility could be broken even with minor version change but now major number is to indicate binary compatibility.</p><p>👉<a href="https://www.youtube.com/watch?v=WxAZk7A7OkM"><strong>What’s new in Android development tools</strong></a></p><p>Camera was always the weak point of Android emulator but now the emulator camera comes with a virtual 3D world environment where we can move around.</p><p>👉<a href="https://www.youtube.com/watch?v=gGOOkk2y_Ss"><strong>Best practices using compilers in Android Studio</strong></a></p><p><a href="https://r8.googlesource.com/r8/">R8 — the new shrinker</a>: This new shrinker will replace ProGuard, Desugar, D8 with one single process. Running all these steps in a single process saves a lot in build time.</p><p>️️👉<a href="https://www.youtube.com/watch?v=dx6LBaFqEHU"><strong>Android vitals: debug app performance and reap rewards</strong></a></p><p>I hate to open some apps just because I have to wait and see the circle of life going round and round for a few seconds every time. Once I saw an app code where the developer had set a timer to show his creatively designed splash screen. <br>Now we can monitor the <a href="https://developer.android.com/topic/performance/vitals/launch-time"><strong>app start time</strong></a>. There are 3 metrics for us to observe, cold start, warm start and hot start. This will be helpful specially for those developers who use Pixel as their testing device 😃</p><p>👉<a href="https://www.youtube.com/watch?v=Yi9-BqUxsno"><strong>What’s new in Android Runtime</strong></a></p><p>A smart phone is a compromise in every aspect (screen size, power, memory, storage) to provide us portability. Android from the very beginning has been walking on a thin wire, balancing between all these compromises.</p><p>One of the optimization which Android does is based on our usage profile. This way the part of the code which executes most gets preference with <a href="https://source.android.com/devices/tech/dalvik/#AOT_compilation">AOT compiler</a>. To improve it further comes the <strong>ART cloud profiles</strong>. Now with cloud profiles there is no need to wait to create usage profile. The default usage profiles can be downloaded at the time of installation and this profile comes from the usage pattern of other users, even from alpha/beta users.</p><p>👉<a href="https://www.youtube.com/watch?v=zdQRIYOST64"><strong>Drawn out: how Android renders</strong></a></p><p>Display List Properties: I don’t think anything new was introduced but the rendering process was well explained again. It’s interesting to see the display list properties which don’t call for whole redraw, for example alpha value of a view.</p><p>👉<a href="https://www.youtube.com/watch?v=WVPH48lUzGY"><strong>Android Jetpack: how to smartly use Fragments in your UI</strong></a></p><p>We have used fragments for many different things just for the want of lifecycle events. Now with Android architecture components we have better options. An alternative to headless fragment is using a <em>LifecycleObserver</em>. Similarly retained instance fragment can be replaced by <em>ViewModel</em>.</p><p>👉<a href="https://www.youtube.com/watch?v=pErTyQpA390"><strong>Android Jetpack: what’s new in Architecture Components</strong></a></p><p>The issue which kept many heads scratching, whether to start observing the <em>LiveData</em>, in <em>onCreate()</em> or <em>onCreateView()</em> in fragments? The problem was, fragment view has it’s own lifecycle (we destroy and create view on detach and attach) which made it difficult to use the correct event to start observing. This has been handled by giving the fragment view its own lifecycle which can be observed by LiveData to send the data at opportune moment.</p><p>👉<a href="https://www.youtube.com/watch?v=gpoqgQPwu_k"><strong>Improve app performance and stability with Firebase</strong></a></p><p><a href="https://firebase.google.com/docs/test-lab/">Firebase Test Lab</a>: Drag and drop your apk to test the app over 1000 devices with different screen size, manufactures, OS etc. We also get accessibility report. oh and one more thing, it now supports IOS too.</p><p>👉<a href="https://www.youtube.com/watch?v=eHjHlujp3Tg"><strong>Protips: a fresh look at advanced topics for Android experts</strong></a></p><p>It’s a nice talk if you are a beginner but nothing “pro”, “advanced topics” or “experts”. This heavily loaded title failed to deliver.</p><p>👉<a href="https://www.youtube.com/watch?v=wYMIadv9iF8"><strong>Frictionless Android testing: write once, run everywhere</strong></a></p><p>Project Nitrogen: A single entry point for all Android tests. Nitrogen covers the entire testing cycle with setup, execution and reporting.</p><p>👉<a href="https://www.youtube.com/watch?v=IrKoBFLwTN0"><strong>Android Jetpack: easy background processing with WorkManager</strong></a></p><p>Chaining work: Atomize different work together using <a href="https://developer.android.com/topic/libraries/architecture/workmanager">WorkManager</a> <em>beginWith</em> and <em>then</em> in a chain. It’s much more than just scheduling.</p><p>👉<a href="https://www.youtube.com/watch?v=w7K0jio8afM"><strong>Understanding Android memory usage</strong></a></p><p>Things that take space in our APK tend to take proportional space in memory too. If we do APK size optimization we get memory optimization as bonus.</p><p>👉<a href="https://www.youtube.com/watch?v=N5xONyp69eU"><strong>What’s new with the Android build system</strong></a></p><p><a href="https://developer.android.com/guide/app-bundle/configure#dynamic_feature_modules">Dynamic features</a>: Separate gradle sub projects which can be installed on demand. It’s very similar to instant app which uses feature plugin. How cool is that?</p><p>👉<a href="https://www.youtube.com/watch?v=x-FcOX6ErdI"><strong>Best practices for text on Android</strong></a></p><p>It’s not just the images, silly. Text measurements are expensive so in Android P there is a new feature, <a href="https://developer.android.com/reference/android/text/PrecomputedText">Precomputed Text</a>. Now we can call <em>PrecomputedText.create(&lt;string&gt;)</em> on any thread and then we can simply set the text in MainThread.</p><p>👉<a href="https://www.youtube.com/watch?v=8GCXtCjtg40"><strong>Android Jetpack: manage UI navigation with Navigation Controller</strong></a></p><p>There are so many entry points to an app and at different level that things get out of hand in no time. Not to mention the navigation within the app. To make it manageable we can use <a href="https://developer.android.com/topic/libraries/architecture/navigation/">navigation component</a> to create navigation graphs and let Android framework take care of the hassle.</p><p>👉<a href="https://www.youtube.com/watch?v=vywGgSrGODU"><strong>How to get one-meter location-accuracy from Android devices</strong></a></p><p>Wi-Fi Round trip Time (RTT): <a href="https://developer.android.com/reference/android/net/wifi/rtt/package-summary">Wi-Fi RTT</a> is time of flight of RF signals. It has the potential to estimate indoor position to accuracy of 1–2 meters. This is much better solution to what we use now Wi-Fi RSSI (Received Signal Strength Indicator) which calculates the distance as a function of signal strength.</p><p>👉<a href="https://www.youtube.com/watch?v=6P20npkvcb8"><strong>How to Kotlin — from the Lead Kotlin Language Designer</strong></a></p><p>Nice examples of how to efficiently convert Java code to Kotlin.</p><p>👉<a href="https://www.youtube.com/watch?v=r54roADX2MI"><strong>What’s new in Android security</strong></a></p><p><a href="https://developer.android.com/about/versions/pie/android-9.0#hardware-security-module">Strong Box</a>: A tamper-resistant hardware with dedicated CPU and RAM to make it resistant to shared resources attack.</p><p>👉<a href="https://www.youtube.com/watch?v=x9T5EYE-QWQ"><strong>Effective ProGuard keep rules for smaller applications</strong></a></p><p>Shrinking: Good talk explaining how shrinking works. We take the entry point of an application and keep adding the code which is going to be executed at runtime. Rest of the code is discarded.</p><p>👉<a href="https://www.youtube.com/watch?v=O5V9ZSL0BsM"><strong>Improve app performance with Android Studio Profilers</strong></a></p><p>Energy profiler: Battery is one of the most visible constraint of smartphones. Android framework has already put in place lot many things to save battery including <a href="https://developer.android.com/training/monitoring-device-state/doze-standby">Doze</a>, <a href="https://developer.android.com/about/versions/oreo/background">restrictions to background services</a> but this will go in vain if our code is not optimized. It’s for us developers to make sure that when the app does some work, it does it efficiently. Now we can monitor our app’s energy usage to see how much drain we are putting on the battery.</p><p>👉<a href="https://www.youtube.com/watch?v=ytZteMo4ETk"><strong>What’s new with ConstraintLayout and Android Studio design tools</strong></a></p><p>Constraint Helper: Constraint Helpers are components which don’t appear on the screen but help in designing the layout, for example guidelines. In ConstraintLayout 2.0 one of the helpers “Layer” let us manipulate graphically a collection of views. Those familiar with graphic editor can relate easily.</p><p>👉<a href="https://www.youtube.com/watch?v=Lcoc4aCLfqI"><strong>What’s new in Android accessibility</strong></a></p><p><a href="https://play.google.com/store/apps/details?id=com.google.android.apps.accessibility.auditor">Accessiblity Scanner</a>: For most of us our introduction to accessiblity would have come from the missing <em>ImageView</em> content description attribute warning in XML layout. Just like testing for bugs we also need to test our apps for accessiblity. <br>Accessiblity Scanner is an app which suggest improvements for Android apps. It even tells us if a higher text contrast is required.</p><p>👉<a href="https://www.youtube.com/watch?v=YT1JfH71k_k"><strong>Build with Google Pay</strong></a></p><p>Holistic view of all the transactions: <a href="https://play.google.com/store/apps/details?id=com.google.android.apps.walletnfcrel">Google pay</a> app will now show a holistic view of all our transactions including third party merchants like Uber. It will be helpful to see them all at one place as we are doing more and more financial transactions on our smart phones.</p><p>️️👉<a href="https://www.youtube.com/watch?v=n4LVFX2xGYQ"><strong>What’s new with Android TV</strong></a></p><p>JBL Link Bar (powered by android tv): “Voice is the new remote”. This Google Assistant sounds bar is expected to launch in Fall 2018. Imagine saying, “Hey Google fast forward 7 minutes” or asking for a channel by its name.</p><p>The only concern I have, my Google Home mini some times fail to listen when music is playing. Let’s see how accurately the sound bar manages to pick up the voice commands while playing audio.</p><p>Thanks for reading this article. You can connect with me on <a href="https://www.linkedin.com/in/asheshb">LinkedIn</a> and <a href="https://twitter.com/asheshbt">Twitter</a></p><blockquote><strong><em>If you like this article, please recommend it by hitting the clap icon 👏 and yes you can click it more than one time </em></strong><em>😃</em><strong><em> Let’s enable each other with the power of knowledge.</em></strong></blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b329949ff329" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[The collective wisdom of our society]]></title>
            <link>https://medium.com/@asheshb/the-collective-wisdom-of-our-society-dea16ac822af?source=rss-a08949e258d7------2</link>
            <guid isPermaLink="false">https://medium.com/p/dea16ac822af</guid>
            <category><![CDATA[evolution]]></category>
            <category><![CDATA[language]]></category>
            <category><![CDATA[wisdom]]></category>
            <category><![CDATA[society]]></category>
            <category><![CDATA[learning]]></category>
            <dc:creator><![CDATA[Ashesh Bharadwaj]]></dc:creator>
            <pubDate>Thu, 02 Aug 2018 07:15:30 GMT</pubDate>
            <atom:updated>2018-08-03T07:03:02.073Z</atom:updated>
            <content:encoded><![CDATA[<p>Like individuals, societies also have wisdom, collective wisdom. It’s this wisdom that decides the fate of a community, town, city or country. Our wisdom means nothing if we can’t influence this collective wisdom.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2Fl41lJ8ywG1ncm9FXW%2Ftwitter%2Fiframe&amp;display_name=Giphy&amp;url=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2Fl41lJ8ywG1ncm9FXW%2Fgiphy.gif&amp;image=https%3A%2F%2Fi.giphy.com%2Fmedia%2Fl41lJ8ywG1ncm9FXW%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="307" frameborder="0" scrolling="no"><a href="https://medium.com/media/bd55e1bb6e2e1d8fab82e85961ed77af/href">https://medium.com/media/bd55e1bb6e2e1d8fab82e85961ed77af/href</a></iframe><p>Over the course of time the words we use in our language change. It will be interesting to see this transformation as it’s the outcome of our collective wisdom.</p><p>Let’s see the transformation of the Hindi word for “physically challenged” people:</p><p>1. <em>अपंग </em>— अप (bad) + अंग (limb), a person with bad limb</p><p>the “bad” sounds negative so we came up with a new word</p><p>2. <em>विकलांग</em> — विकल (incomplete/difficult) + अंग ( limb), a person with incomplete/difficult limb</p><p>this is more practical and acceptable but then comes a complete U-turn of our collective wisdom</p><p>3. <em>दिव्यांग</em> — दिव्य (Divine) + अंग (limb), a person with divine limb</p><p>How delusional one has to be to run so far away from the reality?</p><p>In English also if we see, <em>Handicapped</em> &gt; <em>Physically challenged</em> &gt; <em>Differently abled</em>. The transformation has a similar pattern going from harsh, acceptable to disconnected.</p><p>Another example to observe is the transformation of the word <em>Negro</em> to <em>African American</em>. I may not know the correct order but it may be like <em>Negro</em> &gt; <em>Black</em> &gt; <em>Colored</em> &gt; <em>African American</em>.</p><p>Every word in the transformation chain reflects the state of the wisdom of our society. If we observe these transformations we may able to see how our society is evolving.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=dea16ac822af" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>