// Ġ ĠCOMP 4 5 2 1 ĠĠĠ ĠH ON ĠK IN ĠT AT ĠĠĠĠĠ 2 0 5 1 4 3 3 2 ĠĠĠĠĠĠĠ Ġk thon @ connect . ust . hk Ċ // Ġ ĠCOMP 4 5 2 1 ĠĠĠ ĠL AI ĠCHE UK ĠHE I ĠĠĠ 2 0 4 6 4 0 4 4 ĠĠĠĠĠĠĠ Ġch l ai ak @ connect . ust . hk Ċ // Ġ ĠCOMP 4 5 2 1 ĠĠĠ ĠCHAN ĠH OK ĠH IM ĠĠĠĠ 2 0 4 3 5 3 9 2 ĠĠĠĠĠĠĠ Ġhh chan al @ connect . ust . hk Ċ Ċ package Ġcom . y ose m ite y ss . gre entr ans it . app . home Ċ Ċ import Ġandroidx . lifecycle . ViewModel Ċ import Ġandroidx . lifecycle . viewModel Scope Ċ import Ġcom . y ose m ite y ss . gre entr ans it . domain . models . Near by Route Ċ import Ġcom . y ose m ite y ss . gre entr ans it . domain . models . Near by Stop Ċ import Ġcom . y ose m ite y ss . gre entr ans it . domain . states . Resource Ċ import Ġcom . y ose m ite y ss . gre entr ans it . domain . use cases . near by . Get Near by Routes UseCase Ċ import Ġdagger . h ilt . android . lifecycle . H ilt ViewModel Ċ import Ġkotlinx . coroutines . flow . Mutable State Flow Ċ import Ġkotlinx . coroutines . flow . State Flow Ċ import Ġkotlinx . coroutines . flow . as State Flow Ċ import Ġkotlinx . coroutines . flow . collect Ċ import Ġkotlinx . coroutines . launch Ċ import Ġjavax . inject . Inject Ċ Ċ @ H ilt ViewModel Ċ class ĠHome ViewModel Ġ@ Inject Ġconstructor ( ĊĠĠĠ Ġprivate Ġval Ġget Near by Routes UseCase : ĠGet Near by Routes UseCase Ċ ) Ġ: ĠViewModel () Ġ{ ĊĊĠĠĠ Ġprivate Ġval Ġ_ near by Routes Count Ġ= ĠMutable State Flow ( 0 ) ĊĠĠĠ Ġval Ġnearby Routes Count : ĠState Flow < Int > Ġ= Ġ_ near by Routes Count . as State Flow () ĊĊĠĠĠ Ġprivate Ġval Ġ_ home Ui State Ġ= ĠMutable State Flow < Home Ui State >( Home Ui State . Loading ) ĊĠĠĠ Ġval Ġhome Ui State : ĠState Flow < Home Ui State > Ġ= Ġ_ home Ui State . as State Flow () ĊĊĠĠĠ Ġfun Ġfetch Near by Routes ( near by Stops : ĠList < Near by Stop >) Ġ= ĠviewModel Scope . launch Ġ{ ĊĠĠĠĠĠĠĠ Ġget Near by Routes UseCase ( near by Stops ). collect Ġ{ Ġres Ġ-> ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ_ home Ui State . value Ġ= Ġwhen Ġ( res ) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġis ĠResource . Success Ġ-> Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ_ near by Routes Count . value Ġ= Ġres . data . size ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ ĠHome Ui State . Success ( build Near by Routes List Models ( res . data )) ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġis ĠResource . Error Ġ-> ĠHome Ui State . Error ( res . message ) ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġis ĠResource . Loading Ġ-> ĠHome Ui State . Loading ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġprivate Ġfun Ġbuild Near by Routes List Models ( ĊĠĠĠĠĠĠĠ Ġnearby Route Et as : ĠList < Near by Route > ĊĠĠĠ Ġ): ĠList < Near by Routes ListModel > Ġ= Ġbuild List Ġ{ ĊĠĠĠĠĠĠĠ Ġif Ġ( near by Route Et as . isEmpty ()){ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġadd ( Near by Routes ListModel . Near by Routes Empty Model ) ĊĠĠĠĠĠĠĠ Ġ} Ġelse Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġadd All ( near by Route Et as . map Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ ĠN ear by Routes ListModel . Near by Routes Item Model ( ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġroute Id Ġ= Ġit . id , ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġroute Code Ġ= Ġit . code , ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġroute Dest Ġ= Ġit . dest ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ}) ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} Ċ } Ċ Ċ sealed Ġclass ĠHome Ui State Ġ{ ĊĠĠĠ Ġdata Ġclass ĠSuccess ( val Ġdata : ĠList < Near by Routes ListModel >) Ġ: ĠHome Ui State () ĊĠĠĠ Ġdata Ġclass ĠError ( val Ġmessage : ĠString ?) Ġ: ĠHome Ui State () ĊĠĠĠ Ġobject ĠLoading Ġ: ĠHome Ui State () Ċ }