<?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 Narendrasinh Dodiya on Medium]]></title>
        <description><![CDATA[Stories by Narendrasinh Dodiya on Medium]]></description>
        <link>https://medium.com/@narendrasinhdodiya?source=rss-747faafd699f------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*9Yd9w0GDEDGtV4JKAhY57A.jpeg</url>
            <title>Stories by Narendrasinh Dodiya on Medium</title>
            <link>https://medium.com/@narendrasinhdodiya?source=rss-747faafd699f------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 04:12:08 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@narendrasinhdodiya/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 Architecture: MVVM with Coroutines + Retrofit + Hilt + Kotlin Flow + Room]]></title>
            <link>https://medium.com/swlh/android-architecture-mvvm-with-coroutines-retrofit-hilt-kotlin-flow-room-48e67ca3b2c8?source=rss-747faafd699f------2</link>
            <guid isPermaLink="false">https://medium.com/p/48e67ca3b2c8</guid>
            <category><![CDATA[android]]></category>
            <category><![CDATA[architecture]]></category>
            <category><![CDATA[jetpack]]></category>
            <category><![CDATA[mvvm]]></category>
            <category><![CDATA[coroutine]]></category>
            <dc:creator><![CDATA[Narendrasinh Dodiya]]></dc:creator>
            <pubDate>Mon, 02 Nov 2020 13:16:48 GMT</pubDate>
            <atom:updated>2020-11-13T06:43:16.370Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tII9CNWo6eVyz5CZqCsWGA.jpeg" /><figcaption>Photo by <a href="https://pixabay.com/photos/flight-falcon-9-spacex-landing-1245695/">Pixabay</a></figcaption></figure><p>When you are starting a new Android App the first thing you want is to have the best architecture for your App. Ideally, you would want your architecture to be built with the best development trends in Android.</p><p>The quest for the perfect architecture starts by researching and learning top architecture trends. As the MVVM being the <a href="https://developer.android.com/jetpack/guide#recommended-app-arch">officially recommended architecture</a>, it is an ideal choice to make.</p><p>There are many articles on building MVVM architecture using LiveData, ViewModel, Room, Retrofit, Coroutines, Dagger, RxJava, repository pattern, and the single source of truth strategy, etc. But, when it comes to putting all pieces together, things get tricky.</p><p>The official <a href="https://github.com/android/architecture-samples"><strong>Android Architecture blueprint</strong></a> provides excellent MVVM architecture samples. But, the samples don’t use any networking library(e.g. Retrofit) library and instead mocks the Remote Data sources.</p><p>Also, I came across this article on why you should not be using LivData in repositories <a href="https://proandroiddev.com/no-more-livedata-in-your-repository-there-are-better-options-25a7557b0730"><strong>‘No more LiveData in your repository’</strong></a>. And the architecture blueprint samples use the LiveData in repositories.</p><h3>Motivation</h3><p>Ideally, you would want to build something very scalable yet very concise, easy to understand, and simple to implement. Thanks to the introduction of the new official members, <a href="https://developer.android.com/kotlin/coroutines">Kotlin Coroutines</a>, <a href="https://kotlinlang.org/docs/reference/coroutines/flow.html">Kotlin Flow</a>, and Android <a href="https://developer.android.com/training/dependency-injection/hilt-android">Hilt</a>.</p><p>We will build an MVVM architecture using all these concepts. Let’s get started.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vRrq4vpE78CeYEk_7rxAdg.png" /></figure><p>The best way to learn is to implement it. So, we will be building a sample app that uses the TMDB API to show a list of trending movies. The detail screen shows details about the selected movie.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Q86TpA9XAzFeeZV-EHVVig.png" /></figure><p>You can find the complete source code of this sample project on GitHub.</p><p><a href="https://github.com/devnarendra08/DemoTMDB">devnarendra08/DemoTMDB</a></p><p>Let’s get started!</p><h3><strong><em>Model</em></strong></h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5768ba9c716ebe61167d5359a8688703/href">https://medium.com/media/5768ba9c716ebe61167d5359a8688703/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4973e98b4509079ef40a100f7e75cfde/href">https://medium.com/media/4973e98b4509079ef40a100f7e75cfde/href</a></iframe><h3><strong><em>View</em></strong></h3><p>We don’t need to create the Factory for our ListingViewModel. Hilt handles all the pain. <a href="https://developer.android.com/training/dependency-injection/hilt-jetpack">Refer</a>.</p><p>The listing screen observes the viewModel.movieList LiveData exposed by the ViewModel and feeds the data to the MovieAdapter to display.</p><p>Every response is wrapped inside the <a href="https://gist.github.com/devnarendra08/7775fcb9a9b03f63afda55353462d68b">Result</a> class, which holds the 3 different statuses of the response(SUCCESS, ERROR, LOADING). <br>The data property holds the success value in case of successful response and error and message properties in case of failure.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c7919d49042c3852a0c0162bccbcd6f6/href">https://medium.com/media/c7919d49042c3852a0c0162bccbcd6f6/href</a></iframe><h3><strong><em>ViewModel</em></strong></h3><p>The Hilt will inject movieRepository dependency in the constructor.</p><p>Kotlin Coroutines makes it super easy to do the IO work.fetchMovies() launches a Coroutine in the viewModelScope provided by the ViewModel KTX library. <br>You don’t need to worry about canceling the operations when ViewModel goes away. The viewModelScope will manage everything for you. You can read more about Coroutines scopes and Structured concurrency <a href="https://medium.com/@elizarov/structured-concurrency-722d765aa952">here</a>.</p><p>movieRepository.fetchTrendingMovies() returns the Flow object. The collect will be invoked when any value is emitted to the Flow. We will learn more about Flow in the next section.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/97e242759165d50055dd25963a9816a7/href">https://medium.com/media/97e242759165d50055dd25963a9816a7/href</a></iframe><blockquote><strong><em>Repository<br></em></strong><em>The repository will be responsible to provide the data either from the Remote or Local data sources.</em></blockquote><p>flow{} builder constructs the Flow object. The Flow exposes the data as a stream like RxJava. The flowOn(Dispatchers.IO) specifies the Coroutine context for the execution. The emit() will invoke the collect() in our ViewModel. Here we will emit 3 different values.</p><p>emit(fetchTrendingMoviesCached()) emits the cached values from the Room database data.<br>emit(Result.loading()) emits the loading state.<br>emit(result) emits the result from the API response.</p><p>We will cache the data when the response is successful.</p><p><strong>Note:</strong> The approach that we have used in this sample app is less complex and gives you control over the Loading status. But, If you really want a more clean Single Source of the Truth strategy for your data, you can easily do that by returning Flow from your Room Dao. The Room library already has nice support for this.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2aa753a34b0792ef03990937be4a16e7/href">https://medium.com/media/2aa753a34b0792ef03990937be4a16e7/href</a></iframe><blockquote><strong><em>Remote Data Source<br></em></strong><em>The remote data source will be responsible for fetching the data from the REST APIs.</em></blockquote><p>The MovieRemoteDataSource uses the Retrofit library to fetch the data from the TMDB REST APIs. Hilt injects the required dependency in the constructor.</p><p>getResponse is the <a href="https://kotlinlang.org/docs/reference/lambdas.html">Higher-Order Kotlin function</a> which accepts the Retrofit Service function as the method parameter and returns the Retrofit response wrapped inside Result class. This makes our code concise.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4751103da65a55ff65c8788fbb9be0ff/href">https://medium.com/media/4751103da65a55ff65c8788fbb9be0ff/href</a></iframe><p>Retrofit already has Coroutine suspend function support. We just need to add the suspend keyword in our service interface.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7a989941485ce2c73be628acafa0a3b4/href">https://medium.com/media/7a989941485ce2c73be628acafa0a3b4/href</a></iframe><blockquote><strong><em>Local Data Source: Room Dao<br></em></strong><em>The local data source will be responsible for storing and fetching the data from the Room Database.</em></blockquote><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d39e9be30337750f5668aeeff4397ce7/href">https://medium.com/media/d39e9be30337750f5668aeeff4397ce7/href</a></iframe><blockquote><strong><em>Hilt: Dependency Injection<br></em></strong>The Hilt is built on top of the Dagger and it is very less boilerplate and it is part of the Jetpack libraries and easily integrates with other Jetpack libraries.</blockquote><p>The only dependencies we need to construct is the Retrofit and the Room database. Which is pretty straight forward.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ae4ea30261b14110ff01ce2e36d7b889/href">https://medium.com/media/ae4ea30261b14110ff01ce2e36d7b889/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/56cd8f51b755b6c9cf2364e6208ecf67/href">https://medium.com/media/56cd8f51b755b6c9cf2364e6208ecf67/href</a></iframe><p>You can read more about using Hilt in your App from <a href="https://developer.android.com/training/dependency-injection/hilt-android">here</a>.</p><p>That’s it. We are done!<br>You can find the complete source code of the sample project on GitHub.</p><p><a href="https://github.com/devnarendra08/DemoTMDB">devnarendra08/DemoTMDB</a></p><p>I haven’t implemented pagination in the listing screen to keep the sample app very small and simple to understand.</p><blockquote><strong><em>You might not need Kotlin Flow at all.</em></strong></blockquote><p>We returned everything as Flow from our Repository. But, if you just need one shot operation, then the same can be achieved by simply using the suspend function. You can learn more from <a href="https://medium.com/androiddevelopers/lessons-learnt-using-coroutines-flow-4a6b285c0d06">here</a>.<br>In our case, Flow helps us to emit the statuses along with the success and error response.</p><blockquote><strong>Disclaimer</strong><br><em>We must first understand the core concept of any new approach or library before randomly putting everything together.</em></blockquote><h3>Summary</h3><ul><li>We learned how to put all new Android concepts together and make it work seamlessly.</li><li>We learned how we can use Kotlin Flow/suspend function to make your app concise and readable.</li><li>We learned how Hilt Dependency Injection makes your code less boilerplate and testable.</li><li>We learned how to use Retrofit with Coroutines and wrap the response with success, failure, and loading states.</li></ul><p>Please let me know your valuable inputs in the comments.</p><p>I would appreciate <strong>Claps </strong>if you find this article useful. Your Claps will keep me motivated to continue doing the good work.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=48e67ca3b2c8" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swlh/android-architecture-mvvm-with-coroutines-retrofit-hilt-kotlin-flow-room-48e67ca3b2c8">Android Architecture: MVVM with Coroutines + Retrofit + Hilt + Kotlin Flow + Room</a> was originally published in <a href="https://medium.com/swlh">The Startup</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>