package Ġcom . k ier on qu inn . app . t apt ap . ui . screens . container Ċ Ċ import Ġandroid . content . Context Ċ import Ġandroid . util . Log Ċ import Ġandroidx . annotation . Drawable Res Ċ import Ġandroidx . annotation . String Res Ċ import Ġandroidx . lifecycle . ViewModel Ċ import Ġandroidx . lifecycle . viewModel Scope Ċ import Ġcom . k ier on qu inn . app . t apt ap . R Ċ import Ġcom . k ier on qu inn . app . t apt ap . components . service . Tap Tap Service Router Ċ import Ġcom . k ier on qu inn . app . t apt ap . components . settings . Tap Tap Settings Ċ import Ġcom . k ier on qu inn . app . t apt ap . repositories . dem om ode . Demo Mode Repository Ċ import Ġcom . k ier on qu inn . app . t apt ap . repositories . room . Tap Tap Database Ċ import Ġcom . k ier on qu inn . app . t apt ap . service . foreground . Tap Tap Foreground Service Ċ import Ġcom . k ier on qu inn . app . t apt ap . utils . extensions . broadcast Receiver As Flow Ċ import Ġcom . k ier on qu inn . app . t apt ap . utils . extensions . instant Combine Ċ import Ġkotlinx . coroutines . delay Ċ import Ġkotlinx . coroutines . flow .* Ċ import Ġkotlinx . coroutines . launch Ċ import Ġkotlinx . coroutines . sync . Mutex Ċ import Ġkotlinx . coroutines . sync . with Lock Ċ Ċ abstract Ġclass ĠContainer Shared ViewModel : ĠViewModel () Ġ{ ĊĊĠĠĠ Ġabstract Ġval Ġis Service Running : ĠState Flow < Boolean > ĊĠĠĠ Ġabstract Ġval Ġfab State : ĠState Flow < Fab State > ĊĠĠĠ Ġabstract Ġval Ġfab Clicked : ĠFlow < Fab State . Fab Action > ĊĠĠĠ Ġabstract Ġval Ġsn ackbar Bus : ĠFlow < CharSequence > ĊĠĠĠ Ġabstract Ġfun Ġtoggle Service Enabled State ( context : ĠContext ) ĊĠĠĠ Ġabstract Ġfun Ġcheck Service State ( context : ĠContext ) ĊĠĠĠ Ġabstract Ġfun Ġrestart Service ( context : ĠContext ) ĊĠĠĠ Ġabstract Ġfun Ġset Fab State ( fab State : ĠF ab State ) ĊĠĠĠ Ġabstract Ġfun Ġon Fab Clicked ( fab Action : ĠF ab State . Fab Action ) ĊĠĠĠ Ġabstract Ġfun Ġshow Snackbar ( text : ĠCharSequence ) ĊĊĠĠĠ Ġsealed Ġclass ĠF ab State Ġ{ ĊĠĠĠĠĠĠĠ Ġobject ĠHidden : ĠF ab State () ĊĠĠĠĠĠĠĠ Ġdata Ġclass ĠSh own ( val Ġaction : ĠF ab Action ): ĠF ab State () ĊĊĠĠĠĠĠĠĠ Ġenum Ġclass ĠF ab Action (@ Drawable Res Ġval Ġicon Res : ĠInt , Ġ@ String Res Ġval Ġlabel Res : ĠInt ) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ ĠADD _ ACTION ( R . drawable . ic _ fab _ action _ add , ĠR . string . fab _ add _ action ), ĊĠĠĠĠĠĠĠĠĠĠĠ ĠDELETE ( R . drawable . ic _ fab _ action _ delete , ĠR . string . fab _ remove _ action ), ĊĠĠĠĠĠĠĠĠĠĠĠ ĠADD _ GATE ( R . drawable . ic _ fab _ action _ add , ĠR . string . fab _ add _ gate ), ĊĠĠĠĠĠĠĠĠĠĠĠ ĠADD _ REQUIRE MENT ( R . drawable . ic _ fab _ action _ add , ĠR . string . fab _ add _ requirement ), ĊĠĠĠĠĠĠĠĠĠĠĠ ĠREST ORE _ BACKUP ( R . drawable . ic _ backup _ restore _ restore , ĠR . string . settings _ backup _ restore _ restore ), ĊĠĠĠĠĠĠĠĠĠĠĠ ĠDOWN LOAD ( R . drawable . ic _ download , ĠR . string . settings _ update _ fab ) ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} Ċ Ċ } Ċ Ċ class ĠContainer Shared ViewModel Impl ( context : ĠContext , Ġprivate Ġval Ġdatabase : ĠT ap Tap Database , Ġprivate Ġval Ġsettings : ĠT ap Tap Settings , Ġprivate Ġval Ġdemo Mode Repository : ĠDemo Mode Repository , Ġservice Router : ĠT ap Tap Service Router ): ĠContainer Shared ViewModel () Ġ{ ĊĊĠĠĠ Ġprivate Ġval Ġservice State Check Ġ= ĠMutable Shared Flow < Unit >() ĊĊĠĠĠ Ġprivate Ġval Ġ_ is Service Running Ġ= Ġinstant Combine ( service Router . service Start Bus , Ġservice Router . service Stop Bus , Ġservice State Check ). map Ġ{ ĊĠĠĠĠĠĠĠ Ġget Service Running ( context ) ĊĠĠĠ Ġ}. state In ( viewModel Scope , ĠSh aring Started . E ager ly , Ġget Service Running ( context )) ĊĊĠĠĠ Ġoverride Ġval Ġis Service Running Ġ= Ġ_ is Service Running ĊĊĠĠĠ Ġprivate Ġval Ġ_ fab State Ġ= ĠMutable State Flow < Fab State >( Fab State . Hidden ) ĊĠĠĠ Ġoverride Ġval Ġfab State Ġ= Ġ_ fab State . as State Flow () ĊĊĠĠĠ Ġprivate Ġval Ġ_ fab Clicked Ġ= ĠMutable Shared Flow < Fab State . Fab Action >() ĊĠĠĠ Ġoverride Ġval Ġfab Clicked Ġ= Ġ_ fab Clicked . as Shared Flow () ĊĊĠĠĠ Ġprivate Ġval Ġ_ sn ackbar Bus Ġ= ĠMutable Shared Flow < CharSequence >() ĊĠĠĠ Ġoverride Ġval Ġsn ackbar Bus Ġ= Ġ_ sn ackbar Bus . as Shared Flow () ĊĊĠĠĠ Ġoverride Ġfun Ġtoggle Service Enabled State ( context : ĠContext ) Ġ{ ĊĠĠĠĠĠĠĠ ĠviewModel Scope . launch Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġval ĠnewState Ġ= Ġ!_ is Service Running . value ĊĠĠĠĠĠĠĠĠĠĠĠ Ġsettings . service Enabled . set ( newState ) ĊĠĠĠĠĠĠĠĠĠĠĠ ĠT ap Tap Foreground Service . stop ( context ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġif ( newState ) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ ĠT ap Tap Foreground Service . start ( context ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠĠĠĠĠ Ġdelay ( 1 0 0 ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġcheck Service State ( context ) ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġoverride Ġfun Ġcheck Service State ( context : ĠContext ) Ġ{ ĊĠĠĠĠĠĠĠ ĠviewModel Scope . launch Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġservice State Check . emit ( Unit ) ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġoverride Ġfun Ġrestart Service ( context : ĠContext ) Ġ{ ĊĠĠĠĠĠĠĠ ĠviewModel Scope . launch Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġrestart Service Locked ( context ) ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ/** ĊĠĠĠĠ Ġ* Ġ ĠStart Ġcalls Ġare Ġlocked Ġfor Ġ 2 . 5 Ġseconds Ġafter Ġlaunch Ġ( the oret ically Ġshould Ġbe Ġ 5 s , Ġbut Ġ 2 . 5 s ĊĠĠĠĠ Ġ* Ġ Ġseems Ġto Ġbe Ġenough Ġ 9 9 % Ġof Ġthe Ġtime ), Ġto Ġprevent ĠFore ground Service Did Not Start In Time Exception ĊĠĠĠĠ Ġ* Ġ Ġfrom Ġcalling Ġstop Service Ġbefore Ġstart Foreground Service ĊĠĠĠĠ Ġ*/ ĊĊĠĠĠ Ġprivate Ġval Ġcontainer Service Lock Ġ= ĠMutex () ĊĊĠĠĠ Ġprivate Ġsuspend Ġfun Ġrestart Service Locked ( context : ĠContext ) Ġ= Ġcontainer Service Lock . with Lock Ġ{ ĊĠĠĠĠĠĠĠ Ġif Ġ( demo Mode Repository . is Demo Mode Enabled () Ġ|| Ġsettings . service Enabled . get ()) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ ĠT ap Tap Foreground Service . start ( context , Ġtrue ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġdelay ( 2 5 0 0 L ) ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġoverride Ġfun Ġset Fab State ( fab State : ĠF ab State ) Ġ{ ĊĠĠĠĠĠĠĠ ĠviewModel Scope . launch Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ_ fab State . emit ( fab State ) ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġoverride Ġfun Ġon Fab Clicked ( fab Action : ĠF ab State . Fab Action ) Ġ{ ĊĠĠĠĠĠĠĠ ĠviewModel Scope . launch Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ_ fab Clicked . emit ( fab Action ) ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġoverride Ġfun Ġshow Snackbar ( text : ĠCharSequence ) Ġ{ ĊĠĠĠĠĠĠĠ ĠviewModel Scope . launch Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ_ sn ackbar Bus . emit ( text ) ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġprivate Ġfun Ġget Service Running ( context : ĠContext ): ĠBoolean Ġ{ ĊĠĠĠĠĠĠĠ Ġreturn ĠT ap Tap Foreground Service . isRunning ( context ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġinit Ġ{ ĊĠĠĠĠĠĠĠ Ġcontext . broadcast Receiver As Flow ( Tap Tap Foreground Service . ACTION _ SERVICE _ UPDATE ). apply Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ ĠviewModel Scope . launch Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġcollect Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġcheck Service State ( context ) ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠ Ġrestart Service ( context ) ĊĠĠĠĠĠĠĠ ĠviewModel Scope . launch Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġdatabase . restart Bus . collect Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġrestart Service ( context ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} Ċ Ċ }