package Ġcom . ch ir ag . rand omp e ople carousel test . view model Ċ Ċ import Ġandroidx . lifecycle . LiveData Ċ import Ġandroidx . lifecycle . Mutable LiveData Ċ import Ġandroidx . lifecycle . ViewModel Ċ import Ġandroidx . lifecycle . viewModel Scope Ċ import Ġcom . ch ir ag . rand omp e ople carousel test . database . dao . F avour ite People Dao Ċ import Ġcom . ch ir ag . rand omp e ople carousel test . model . Random People Model Ċ import Ġcom . ch ir ag . rand omp e ople carousel test . model . User Ċ import Ġcom . ch ir ag . rand omp e ople carousel test . networking . Server Response Ċ import Ġcom . ch ir ag . rand omp e ople carousel test . repository . Main Repository Ċ import Ġkotlinx . coroutines . Experimental Cor outines Api Ċ import Ġkotlinx . coroutines . Flow Preview Ċ import Ġkotlinx . coroutines . Internal Cor outines Api Ċ import Ġkotlinx . coroutines . flow . collect Ċ import Ġkotlinx . coroutines . launch ĊĊ Ċ /** Ċ Ġ* ĠCreated Ġby ĠCh ir ag ĠS id hi w ala Ġon Ġ 6 / 9 / 2 0 . Ċ Ġ*/ Ċ @ Flow Preview Ċ @ Experimental Cor outines Api Ċ @ Internal Cor outines Api Ċ class ĠMain ViewModel ( private Ġval Ġmain Repository : ĠMain Repository ) Ġ: ĠViewModel () Ġ{ ĊĠĠĠ Ġprivate Ġval Ġrandom User Mutable LiveData Ġ= ĠMutable LiveData < Server Response < Random People Model >>() ĊĠĠĠ Ġval Ġrandom User LiveData : ĠLive Data < Server Response < Random People Model >> Ġ= ĊĠĠĠĠĠĠĠ Ġrandom User Mutable LiveData ĊĊĠĠĠ Ġ// ĠGet ĠRandom Ġuser Ġfrom ĠAPI ĊĠĠĠ Ġfun ĠgetRandom User () Ġ{ ĊĠĠĠĠĠĠĠ ĠviewModel Scope . launch Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġmain Repository . call Random User Api (). collect Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġrandom User Mutable LiveData . value Ġ= Ġit ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ// Insert Ġpeople Ġin Ġfavour ite Ġpeople Ġdatabase Ġtable ĊĠĠĠ Ġfun Ġinsert F avour ite People ( user : ĠUser ) Ġ{ ĊĠĠĠĠĠĠĠ ĠviewModel Scope . launch Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġmain Repository . insert F avour ite People ( user ) ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} Ċ Ċ }