<?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 Ankoku on Medium]]></title>
        <description><![CDATA[Stories by Ankoku on Medium]]></description>
        <link>https://medium.com/@4ankoku?source=rss-9152203836fd------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*eDReSVAVoqznbj5om1eGmQ.jpeg</url>
            <title>Stories by Ankoku on Medium</title>
            <link>https://medium.com/@4ankoku?source=rss-9152203836fd------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 09:59:30 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@4ankoku/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[Kotlin MVVM (Model-View-ViewModel)]]></title>
            <link>https://medium.com/@4ankoku/kotlin-mvvm-model-view-viewmodel-f3ecd3188881?source=rss-9152203836fd------2</link>
            <guid isPermaLink="false">https://medium.com/p/f3ecd3188881</guid>
            <category><![CDATA[mvvm]]></category>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[android]]></category>
            <dc:creator><![CDATA[Ankoku]]></dc:creator>
            <pubDate>Mon, 16 Dec 2019 15:12:30 GMT</pubDate>
            <atom:updated>2019-12-16T15:49:36.454Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/754/1*hiypGQVOat8W3411_SUaTg.jpeg" /></figure><p>So, let’s talk about <strong><em>Kotlin MVVM</em></strong>. What is MVVM? MVVM or <strong><em>Model-View-ModelView</em></strong> is an <strong>architectural pattern</strong> that allow us to write code more <strong>managable</strong>, <strong>maintainable</strong>, <strong>cleaner</strong> and <strong>testable</strong>. MVVM also supported and encouraged by <a href="http://google.com/">Google</a> itself. There are many library can be used in MVVM like <a href="https://developer.android.com/jetpack/androidx/releases/lifecycle">Lifecycler-aware components</a>, <a href="https://developer.android.com/topic/libraries/architecture/livedata">LiveData</a>, <a href="https://developer.android.com/topic/libraries/architecture/viewmodel">ViewModel</a> and many more.</p><p>In this article (My First Article), let’s build some apps using Kotlin MVVM and <strong>REST API</strong> from <a href="https://www.themoviedb.org/"><strong><em>TheMovieDB</em></strong></a>.</p><p>Go to themoviedb.org and create an account. If you already had an moviedb account, go to setting and you will see API section in Settings sidebar.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1010/1*r5gQQ5b_c6l5u_JJC2Hfig.png" /></figure><p>And here’s the link for <a href="https://developers.themoviedb.org/3/getting-started/introduction">TheMovieDB API</a> link. Now lets move to Android Studio.</p><blockquote>In this MVVM we will use Library <a href="http://implementation &#39;androidx.lifecycle:lifecycle-extensions:2.0.0&#39;">ViewModel and LiveData using Androidx Lifecycle</a>, Androidx <a href="https://developer.android.com/jetpack/androidx/releases/recyclerview">RecyclerView</a> and <a href="https://developer.android.com/jetpack/androidx/releases/cardview">CardView</a>, <a href="https://github.com/square/retrofit">Retrofit</a>, <a href="https://github.com/square/retrofit/tree/master/retrofit-converters/gson">Converter-GSON</a>, <a href="https://github.com/square/retrofit/tree/master/retrofit-adapters/rxjava2">Adapter-RXJava2</a>, <a href="https://github.com/google/gson">Google GSON</a>, <a href="https://github.com/bumptech/glide">Glide</a>, <a href="https://developer.android.com/jetpack/androidx/releases/paging">Paging-Runtime</a>, <a href="https://github.com/ReactiveX/RxAndroid">RXJava and RXAndroid</a>.</blockquote><p>Make a new Android Studio Project, make sure that you use a minimum <strong>Android Version 5.0</strong> or <strong>API 21</strong>. And here’s library version that i use for the project, just for <strong>avoid</strong> there are <strong>version error</strong> in some <strong>library</strong> we use:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8d6afe44d4a21d2a98b88ba27408b965/href">https://medium.com/media/8d6afe44d4a21d2a98b88ba27408b965/href</a></iframe><p>Then, make an package inside <strong>com.example.project_name.</strong> Create a new <strong>Activity</strong>, let’s call it <strong>SingleMovie</strong>. in <strong><em>activity_single_movie.xml</em></strong><em>, </em>change the xml to this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/27ad49b271d53a7c7610d036687e11ec/href">https://medium.com/media/27ad49b271d53a7c7610d036687e11ec/href</a></iframe><p>You can see a <strong><em>ProgressBar</em></strong> and <strong><em>TextView</em></strong> with text <strong>Connection Problem</strong>. We can use it to our <strong>Network State</strong> if there are no <strong>Internet Connection</strong>, and the text will appear because we set it as invisible in this line: <em>android:visibility=”gone”.</em></p><p>Then, make a package <strong><em>data</em></strong> inside com.example.project_name. also inside the <strong>data</strong> package we’ll going to <strong>add</strong> 3 package: <strong><em>api</em></strong>, <strong><em>repository</em></strong>, <strong><em>vo</em></strong>. Add <strong>Kotlin Class</strong> called <strong><em>NetworkState</em></strong> inside <strong>repository</strong> package:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/48e572f3b36e562d0b35221d34e0a07b/href">https://medium.com/media/48e572f3b36e562d0b35221d34e0a07b/href</a></iframe><p>As you can see, we add <strong>enum</strong> as a <strong><em>Status</em></strong> for our class. But, what is <strong>enum</strong>? enum is <strong>special data type</strong> that allows a variable to <strong>hold a value</strong> from a set of predefined constants. You can see in <strong><em>NetworkState Class</em></strong>, we add companion object for tie the function or property to a class rather than to instance of it.Another example for basic enum usage:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b6d5ce9ce1853616f4df37129150f4fa/href">https://medium.com/media/b6d5ce9ce1853616f4df37129150f4fa/href</a></iframe><blockquote>Go to this <a href="https://developers.themoviedb.org/3/movies/get-popular-movies">API link to get popular movie that available in TheMovieDB.org</a>.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7Dob5072_GBCgJAe2Y5rOQ.png" /></figure><p>As you can see that there are variable for our api_key, use your API Key that you see in Settings menu. When you click the<em> SEND REQUEST</em>, you will see JSON Format from TheMovieDB that have been we request for. If you use firefox, you can copy the link request and start a new tab.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aLXo-exu_SMamC1pzrXC0w.png" /></figure><p>Let’s copy some id movie id: {id} in JSON that we have request</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/562/1*oPHs88IZlDZCH_JQaZYHxw.png" /></figure><blockquote>And then go to <a href="https://developers.themoviedb.org/3/movies/get-movie-details">Movie Details</a>, copy the movie id that you have copied before. And you will see the movie details</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CACV0ejFJWHZNEoQbbHNgg.png" /></figure><p>In Android Studio, go to <strong>File -&gt; Settings</strong>, and then go to plugin. Install JSON To Kotlin Class (JsonToKotlinClass)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1023/1*ua-CCFgFbKxMLjofYFqGqQ.png" /></figure><p>Once you install the plugin, <strong>restart the IDE</strong>, Left-click in <strong><em>vo</em> </strong>repository -&gt; <strong>new</strong> and you will see “<strong><em>kotlin data class file from Json</em></strong>”. Copy the movie details response and then paste it in <strong><em>Generate Kotlin Data Class</em></strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/668/1*BcumjrdJ0qi6MBl3lYc2aw.png" /></figure><blockquote>Delete in <strong>JSON Text</strong>: <strong>belongs_to_collection</strong>, <strong>background_path</strong>, <strong>adult</strong>, <strong>genres</strong>, <strong>production_companies</strong>&amp;<strong>countries</strong>.</blockquote><blockquote>Then click <strong>advanced</strong>. In <strong>annotation</strong>, change it to <strong>Gson</strong>. And also in <strong>Other</strong>, select the options ‘<strong>enable order by alphabetical</strong>’ and ‘<strong>only create annotations when needed</strong>’</blockquote><p>Then make the <strong>MovieDetails</strong> Class become like this and delete <strong>SpokenLanguage</strong> Class:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/38676f6ba60acbc29f555a56475c79af/href">https://medium.com/media/38676f6ba60acbc29f555a56475c79af/href</a></iframe><p>Then, inside <strong><em>API </em></strong>repository we will add <strong>Interface</strong> call <strong><em>TheMovieDBInterface</em></strong> and add this code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/751f16a5ff4d1c6f2887711a455660b4/href">https://medium.com/media/751f16a5ff4d1c6f2887711a455660b4/href</a></iframe><p>Then create Kotlin <strong><em>Object</em></strong> in <strong>API </strong>package called <strong><em>TheMovieDBClient</em></strong>:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3149a83f65caac1a431c544a9bd46960/href">https://medium.com/media/3149a83f65caac1a431c544a9bd46960/href</a></iframe><h3>Fetching Data with RXJava</h3><p>Now , Create Kotlin Class called “<strong><em>MovieDetailsNetworkDataSource</em></strong>”. Then, add it in your code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2910337b6d0d7776480248be74b3aa29/href">https://medium.com/media/2910337b6d0d7776480248be74b3aa29/href</a></iframe><p>And then create 2 Kotlin Class in <strong>single_movie_details </strong>package called “<strong><em>MovieDetailsRepository</em></strong>” and “<strong><em>SingleMovieViewModel</em></strong>”.</p><blockquote><strong><em>MovieDetailsRepository</em></strong></blockquote><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ab930f5e66c16bda637eff8fb61cac4c/href">https://medium.com/media/ab930f5e66c16bda637eff8fb61cac4c/href</a></iframe><blockquote><strong><em>SingleMovieViewModel</em></strong></blockquote><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cf1612efd946d379040fc15d7b5941f4/href">https://medium.com/media/cf1612efd946d379040fc15d7b5941f4/href</a></iframe><blockquote>We almost there, let’s do the finishing touch in our app in Movie Details Activity</blockquote><blockquote>Write this code to <strong><em>SingleMovie</em></strong> Class in <strong>single_movie_details</strong> package<em>:</em></blockquote><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/323857bc99034205115fa9e2795284ef/href">https://medium.com/media/323857bc99034205115fa9e2795284ef/href</a></iframe><p>In <strong><em>activity_main</em></strong> create <strong>button</strong> with id ‘<strong><em>btn</em></strong>’ so in MainActivity.kt we can add inside onCreate():</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5fcf8dcacee74e829567e862cda25700/href">https://medium.com/media/5fcf8dcacee74e829567e862cda25700/href</a></iframe><p>And finally, try to start the app. Thank for reading</p><h3>Reference:</h3><ul><li><a href="https://www.youtube.com/channel/UCgm7yqTC6m7epclaFnRA3ew">OX Coding</a></li><li><a href="https://developers.themoviedb.org/3/">API Docs</a></li><li><a href="https://developer.android.com/jetpack/androidx/">AndroidX Overview | Android Developers</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f3ecd3188881" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>