CoroutinesFlow and ViewModel Retrofit API - Practice with CoroutinesFlow, ViewModel and Retrofit API

Overview

Practice with Coroutines Flow, ViewModel and get data with Retrofit from two API

Project example for learn ViewModel, flow and Coroutines with fetching data with retrofit Api

Show data with logd on MainActivity.class onCreate() method

    override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    binding = ActivityMainBinding.inflate(layoutInflater)
    setContentView(binding.root)

    val networkHelper = NetworkHelper(this)
    postViewModel =
        ViewModelProvider(this, ViewModelFactory(networkHelper))[PostViewModel::class.java]

    postViewModel.getUserWithPost().observe(this, Observer {
        when (it) {
            is UserResource.Loading -> {
                Log.d(TAG, "onCreate: Loading...")
            }
            is UserResource.Error -> {
                Log.d(TAG, "onCreate: ${it.message}")
            }
            is UserResource.Success -> {
                Log.d(TAG, "onCreate: ${it.userWithPost.userList}")
                Log.d(TAG, "onCreate: ${it.userWithPost.postList}")
            }
        }
    })

Used Libraries

ViewModelScope

implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.0"

Coroutines

implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.2'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.2'

Retrofit

implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
You might also like...
MarsRealEstate is a simple demo app using ViewModel & LiveData with Retrofit, Glide and Moshi in Kotlin.
MarsRealEstate is a simple demo app using ViewModel & LiveData with Retrofit, Glide and Moshi in Kotlin.

Android-NASA-Real-Estate-Mars Is Android app that uses Kotlin and MVVM architecture is a simple app that uses Coroutines, LiveData with Retrofit, Glid

Wallpaper app made using Hilt, Retrofit, Room, Navigation Components, MVI, Coroutines, Flows, ViewModel, LiveData, Datastore Preference.
Wallpaper app made using Hilt, Retrofit, Room, Navigation Components, MVI, Coroutines, Flows, ViewModel, LiveData, Datastore Preference.

Android Picture Engine Wallpaper app made using Hilt, Retrofit, Room, Navigation Components, MVI, Coroutines, Flows, ViewModel, LiveData, Datastore Pr

The News App has been carried out within the framework of the MVVM architecture, information about news is obtained by consulting an API, it is built usisng Jetpack Copose, Coroutines, Dependency Injection with Hilt and Retrofit

Journalist The News App consists of an application that displays the latest news from EEUU from an API that provides official and updated information.

Shumba money weather app, using openweather api and retrofit
Shumba money weather app, using openweather api and retrofit

Shumba Money Weather App Language KOTLIN Libraries used 1.) Retrofit library - REST Client for Java and Android. It makes it relatively easy to retrie

Android native news App using API from thenewsapi.com and Retrofit Library
Android native news App using API from thenewsapi.com and Retrofit Library

FlashBytes-Android-News-App Android Material Design News App using API from https://www.thenewsapi.com/ and Retrofit Library Screen Splash, Onboard Sc

A quiz app built with trivia api. This app was built with mvvm architecture, dagger-hilt, retrofit, room database, and navigation components.
A quiz app built with trivia api. This app was built with mvvm architecture, dagger-hilt, retrofit, room database, and navigation components.

A quiz app built with trivia api. This app was built with mvvm architecture, dagger-hilt, retrofit, room database, and navigation components.

 A simple Pokedex App getting API with Retrofit, maintaining data using LiveData, and Material Design based on MVVM architecture
A simple Pokedex App getting API with Retrofit, maintaining data using LiveData, and Material Design based on MVVM architecture

PokedexApp Pokedex A simple Pokedex App getting API with Retrofit, maintaining data using LiveData, and Material Design based on MVVM architecture. Te

Projeto de Prova Semestral. Aplicativo Android com login e registro utilizando Firebase Authentication e consumo de API com Retrofit.
Projeto de Prova Semestral. Aplicativo Android com login e registro utilizando Firebase Authentication e consumo de API com Retrofit.

Ocean-Tech-Android Projeto de Prova Semestral. Aplicativo Android com login e cadastro utilizando Firebase Authentication e consumo de API com Retrofi

Aplicación android con MVVM, Room, Retrofit y Dagger Hilt, coonsumiento la API de TMDB.

TMDB Aplicación android con MVVM, Room, Retrofit y Dagger Hilt, coonsumiento la API de TMDB. To-Do: ☑ Diseño de aplicación con MVVM e inyeccion de dep

Releases(singleliveevent)
Owner
Komiljon Zokirov
I am working on creating Android apps! I offer you my knowledge as an Android programmer! This will help you to realize your ideas! I have some projects which
Komiljon Zokirov
TODO API Client Kata for Kotlin Developers. The main goal is to practice integration testing using MockWebServer

KataTODOApiClient for Kotlin We are here to practice integration testsing using HTTP stubbing. We are going to use MockWebServer to simulate a HTTP se

Karumi 61 Nov 20, 2022
Smart-flight - This app was created to learn and practice Kotlin language

Smart Flight This app was created to learn and practice Kotlin language. Works o

Kamil 1 Feb 9, 2022
This is an android project developed in order to practice and prepare for the Google AAD Exam

TaskMaker This is an android project developed in order to practice and prepare for the Google Associate Android Developer Exam (AAD). I tried to impl

Babatunde Owoleke 11 Jun 30, 2022
Maxibon kata for Kotlin Developers. The main goal is to practice property based testing.

Kata Maxibon for Kotlin. We are here to practice property based testing. We are going to use KotlinTest to write our tests. We are going to practice p

Karumi 44 Oct 3, 2022
KataContacts written in Kotlin. The main goal is to practice Clean Architecture Development

KataContacts written in Kotlin We are here to practice Clean Architecture Development. Clean Architecture is a way of structuring code. We are going t

Karumi 48 Oct 3, 2022
Screenshot Kata for Android Developers with Kotlin. The main goal is to practice UI Screenshot Testing.

KataScreenshot in Kotlin We are here to practice UI testing using screenshot tests for Android. We are going to use Espresso to interact with the Appl

Karumi 76 Nov 20, 2022
Super Heroes Kata for Android Developers in Kotlin. The main goal is to practice UI Testing.

KataSuperHeroes in Kotlin We are here to practice UI Testing. We are going to use Espresso to interact with the Application UI. We are going to use Ko

Karumi 86 Nov 20, 2022
Kata to practice Clean Architecture & MVI by building TodoList Application

Mvi Todo Kata Application Features TODO specification Title Description Due Date Priority Supported Operations Create a new TODO Remove a TODO Select

seungmin shin 0 Nov 3, 2021
practice with Refactoring2.0 written by Martin Fowler

practice-with-Refactoring2.0 practice with Refactoring2.0 written by Martin Fowler Chap 1. 첫번째 예시 리팩터링 2판의 예시는 자바스크립트로 이뤄져있다. 코틀린에 익숙해지기 위해 코틀린으로 코드를

김민수 2 Jan 10, 2022
Android Practice (1sem)

Lab6) 1.Залил 3 простых экрана заставка регистрация авторизация профиль Lab7) Изменил в прошлых экранах некоторые поля EditText с квадратного на кругл

Daniel 0 Nov 30, 2022