Retrofit with Kotlin Flows and MVVM

Saqib
Android Tech Stack
Published in
3 min readDec 26, 2022

--

In this story we will use Retrofit with coroutine flows and use them in ViewModel.

Photo by kazuend on Unsplash

We also want to build architecture which is scalable, easy to understand and maintain, so want to arrange our code based on following diagram

f

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
)

--

--

Saqib
Android Tech Stack

Senior Mobile Engineer (Android & iOS) , Berlin | Sharing my development experience