package Ġcom . r pf coding . bor uto character viewer . presentation . screens . search Ċ Ċ import Ġandroidx . compose . runtime . State Ċ import Ġandroidx . compose . runtime . mutable State Of Ċ import Ġandroidx . lifecycle . ViewModel Ċ import Ġandroidx . lifecycle . viewModel Scope Ċ import Ġandroidx . paging . Paging Data Ċ import Ġandroidx . paging . cached In Ċ import Ġcom . r pf coding . bor uto character viewer . data . local . entity . Hero Entity Ċ import Ġcom . r pf coding . bor uto character viewer . domain . use _ cases . hero es . Save Se ar ched Hero es UseCase Ċ import Ġcom . r pf coding . bor uto character viewer . domain . use _ cases . hero es . Search Hero es UseCase Ċ import Ġdagger . h ilt . android . lifecycle . H ilt ViewModel Ċ import Ġkotlinx . coroutines . Dispatch ers Ċ import Ġkotlinx . coroutines . delay Ċ import Ġkotlinx . coroutines . flow .* Ċ import Ġkotlinx . coroutines . launch Ċ import Ġjavax . inject . Inject Ċ Ċ @ H ilt ViewModel Ċ class ĠSearch ViewModel Ġ@ Inject Ġconstructor ( ĊĠĠĠ Ġprivate Ġval Ġsearch Hero es UseCase : ĠSearch Hero es UseCase , ĊĠĠĠ Ġprivate Ġval Ġsave Se ar ched Hero es UseCase : ĠSave Se ar ched Hero es UseCase Ċ ) Ġ: ĠViewModel () Ġ{ ĊĊĠĠĠ Ġprivate Ġvar Ġ_ search Query Ġ= Ġmutable State Of ("") ĊĠĠĠ Ġval Ġsearch Query : ĠState < String > Ġget () Ġ= Ġ_ search Query ĊĊĠĠĠ Ġprivate Ġval Ġ_ sear ched Hero es Ġ= ĠMutable State Flow < Paging Data < Hero Entity >>( Paging Data . empty ()) ĊĠĠĠ Ġval Ġsearched Hero es : ĠState Flow < Paging Data < Hero Entity >> Ġget () Ġ= Ġ_ sear ched Hero es ĊĊĠĠĠ Ġprivate Ġval Ġ_ search Screen Event Ġ= ĠMutable Shared Flow < Search Screen Event >() ĊĠĠĠ Ġval Ġsearch Screen Event Ġ= Ġ_ search Screen Event . as Shared Flow () ĊĊĠĠĠ Ġfun Ġupdate Search Query ( query : ĠString ) Ġ{ ĊĠĠĠĠĠĠĠ Ġ_ search Query . value Ġ= Ġquery ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġfun Ġsearch Hero es ( query : ĠString ) Ġ{ ĊĠĠĠĠĠĠĠ ĠviewModel Scope . launch ( Dispatch ers . IO ) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġsearch Hero es UseCase ( query Ġ= Ġquery ). cached In ( viewModel Scope ). collect Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ_ sear ched Hero es . value Ġ= Ġit ĊĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġdelay ( 1 0 0 0 L ) ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ_ search Screen Event . emit ( Search Screen Event . SearchResult ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġfun Ġsave Se ar ched Hero es ( hero es : ĠList < Hero Entity >) Ġ{ ĊĠĠĠĠĠĠĠ ĠviewModel Scope . launch ( Dispatch ers . IO ) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġsave Se ar ched Hero es UseCase ( hero es Ġ= Ġhero es ) ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} Ċ } Ċ Ċ sealed Ġclass ĠSearch Screen Event Ġ{ ĊĠĠĠ Ġobject ĠSearch Result Ġ: ĠSearch Screen Event () Ċ } Ċ