Retrofit with Kotlin Flows and MVVM
Published in
3 min readDec 26, 2022
In this story we will use Retrofit with coroutine flows and use them in ViewModel.
We also want to build architecture which is scalable, easy to understand and maintain, so want to arrange our code based on following diagram
Let’s get started! 🚀
UseCase:
We will fetch list of Books and load into view using MVVM.
Dependencies:
Latest version you can see here
com.squareup.retrofit2:retrofit:2.9.0
com.squareup.retrofit2:converter-moshi:2.9.0
com.squareup.moshi:moshi-kotlin:1.14.0
org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.4
org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.4
We will be using Moshi with retrofit
Models
Each architecture layer must have their own model and Dto model will be use to move across different layers
Creating Book data class for pasrding Book json, so must be in Networking layer
data class Book (
@Json val id: String,
@Json val title: String,
@Json val author: String
)