package Ġorg . simple . clin ic . setup Ċ Ċ import Ġcom . f 2 pr ate ek . rx . preferences 2 . Preference Ċ import Ġcom . spotify . mob ius . rx 2 . Rx Mob ius Ċ import Ġdagger . ass isted . Ass isted Ċ import Ġdagger . ass isted . Ass isted Factory Ċ import Ġdagger . ass isted . Ass isted Inject Ċ import Ġio . reactivex . Observable Transformer Ċ import Ġio . reactivex . Scheduler Ċ import Ġio . reactivex . Single Ċ import Ġorg . simple . clin ic . App Database Ċ import Ġorg . simple . clin ic . app config . AppConfig Repository Ċ import Ġorg . simple . clin ic . app config . Country Ċ import Ġorg . simple . clin ic . main . Typed Preference Ċ import Ġorg . simple . clin ic . main . Typed Preference . Type . Database Maintenance Run At Ċ import Ġorg . simple . clin ic . main . Typed Preference . Type . On boarding Complete Ċ import Ġorg . simple . clin ic . setup . r uncheck . Allow Application To Run Ċ import Ġorg . simple . clin ic . user . User Ċ import Ġorg . simple . clin ic . util . User Clock Ċ import Ġorg . simple . clin ic . util . Utc Clock Ċ import Ġorg . simple . clin ic . util . scheduler . Schedulers Provider Ċ import Ġorg . simple . clin ic . util . to Optional Ċ import Ġjava . time . Instant Ċ import Ġjava . util . Optional Ċ Ċ class ĠSetup Activity Effect Handler Ġ@ Ass isted Inject Ġconstructor ( ĊĠĠĠ Ġ@ Ass isted Ġprivate Ġval Ġui Actions : ĠUi Actions , ĊĠĠĠ Ġprivate Ġval Ġuser Dao : ĠUser . Room Dao , ĊĠĠĠ Ġprivate Ġval Ġapp Config Repository : ĠApp Config Repository , ĊĠĠĠ Ġprivate Ġval Ġs chedulers Provider : ĠS chedulers Provider , ĊĠĠĠ Ġprivate Ġval Ġapp Database : ĠApp Database , ĊĠĠĠ Ġprivate Ġval Ġclock : ĠU tc Clock , ĊĠĠĠ Ġprivate Ġval Ġallow Application To Run : ĠAllow Application To Run , ĊĠĠĠ Ġ@ Typed Preference ( On boarding Complete ) Ġprivate Ġval Ġon boarding Complete Preference : ĠPreference < Boolean >, ĊĠĠĠ Ġ@ Typed Preference ( Database Maintenance Run At ) Ġprivate Ġval Ġdatabase Maintenance Run At : ĠPreference < Optional < Instant >>, ĊĠĠĠ Ġprivate Ġval Ġuser Clock : ĠUser Clock , ĊĠĠĠ Ġprivate Ġval Ġload V 1 Country : ĠLoad V 1 Country Ċ ) Ġ{ ĊĊĠ Ġ@ Ass isted Factory ĊĠ Ġinterface ĠFactory Ġ{ ĊĠĠĠ Ġfun Ġcreate ( ui Actions : ĠUi Actions ): ĠSetup Activity Effect Handler ĊĠ Ġ} ĊĊĠ Ġfun Ġbuild (): ĠObservable Transformer < Setup Activity Effect , ĠSetup Activity Event > Ġ{ ĊĠĠĠ Ġreturn ĠRx Mob ius ĊĠĠĠĠĠĠĠ Ġ. subtype Effect Handler < Setup Activity Effect , ĠSetup Activity Event >() ĊĠĠĠĠĠĠĠ Ġ. add Transformer ( Fetch UserDetails :: class . java , Ġfetch UserDetails ( schedul ers Provider . io ())) ĊĠĠĠĠĠĠĠ Ġ. addAction ( Go To MainActivity :: class . java , Ġui Actions :: goTo MainActivity , Ġs chedulers Provider . ui ()) ĊĠĠĠĠĠĠĠ Ġ. addAction ( Show On boarding Screen :: class . java , Ġui Actions :: show Splash Screen , Ġs chedulers Provider . ui ()) ĊĠĠĠĠĠĠĠ Ġ// ĠWe Ġcould Ġtechnically Ġalso Ġimplicitly Ġwait Ġon Ġthe Ġdatabase Ġto ĊĠĠĠĠĠĠĠ Ġ// Ġinitialize Ġby Ġquerying Ġthe Ġactual Ġuser Ġdata Ġand Ġmake Ġit Ġa ĊĠĠĠĠĠĠĠ Ġ// Ġproperty Ġof Ġthe ĠFetch UserDetails Ġeffect , Ġbut Ġthat Ġhas Ġthe ĊĠĠĠĠĠĠĠ Ġ// Ġproblem Ġof Ġmaking Ġit Ġhidden Ġbehaviour . ĊĊĠĠĠĠĠĠĠ Ġ// ĠIf Ġthe Ġuser Ġdetails Ġis Ġremoved Ġfrom Ġthis Ġscreen Ġin Ġthe ĊĠĠĠĠĠĠĠ Ġ// Ġfuture , Ġthen Ġwe Ġwill Ġalso Ġlose Ġthe Ġfeature Ġof Ġthis Ġscreen ĊĠĠĠĠĠĠĠ Ġ// Ġshowing Ġthe Ġplaceholder ĠUI Ġuntil Ġthe Ġdatabase Ġis Ġmigrated . ĊĊĠĠĠĠĠĠĠ Ġ// ĠIn Ġthis Ġcase , Ġit Ġmight Ġbe Ġbetter Ġto Ġhave Ġthis Ġas Ġan Ġexplicit ĊĠĠĠĠĠĠĠ Ġ// Ġeffect Ġso Ġthat Ġthe Ġintention Ġis Ġclear . ĊĠĠĠĠĠĠĠ Ġ. add Transformer ( Initialize Database :: class . java , Ġinitialize Database ( schedul ers Provider . io ())) ĊĠĠĠĠĠĠĠ Ġ. addAction ( Show Country Selection Screen :: class . java , Ġui Actions :: show Country Selection Screen , Ġs chedulers Provider . ui ()) ĊĠĠĠĠĠĠĠ Ġ. add Transformer ( Run Database Maintenance :: class . java , Ġrun Database Maintenance ()) ĊĠĠĠĠĠĠĠ Ġ. add Transformer ( Fetch Database Maintenance Last Run At Time :: class . java , Ġload Last Database Maintenance Time ()) ĊĠĠĠĠĠĠĠ Ġ. add Consumer ( Show Not Allowed To Run Message :: class . java , Ġ{ Ġui Actions . show Dis allowed To Run Error ( it . reason ) Ġ}, Ġs chedulers Provider . ui ()) ĊĠĠĠĠĠĠĠ Ġ. add Transformer ( Check If App Can Run :: class . java , Ġcheck Application Allowed To Run ()) ĊĠĠĠĠĠĠĠ Ġ. add Transformer ( Save Country And Deployment :: class . java , Ġsave Country And Deployment ()) ĊĠĠĠĠĠĠĠ Ġ. add Transformer ( Delete Stored Country V 1 :: class . java , Ġdelete Stored Country V 1 ()) ĊĠĠĠĠĠĠĠ Ġ. build () ĊĠ Ġ} ĊĊĠ Ġprivate Ġfun Ġdelete Stored Country V 1 (): ĠObservable Transformer < Delete Stored Country V 1 , ĠSetup Activity Event > Ġ{ ĊĠĠĠ Ġreturn ĠObservable Transformer Ġ{ Ġeffects Ġ-> ĊĠĠĠĠĠ Ġeffects ĊĠĠĠĠĠĠĠĠĠ Ġ. subscribe On ( schedul ers Provider . io ()) ĊĠĠĠĠĠĠĠĠĠ Ġ. map Ġ{ Ġapp Config Repository . delete Stored Country V 1 () Ġ} ĊĠĠĠĠĠĠĠĠĠ Ġ. map Ġ{ ĠStored Country V 1 Deleted Ġ} ĊĠĠĠ Ġ} ĊĠ Ġ} ĊĊĠ Ġprivate Ġfun Ġfetch UserDetails ( scheduler : ĠScheduler ): ĠObservable Transformer < Fetch UserDetails , ĠSetup Activity Event > Ġ{ ĊĠĠĠ Ġreturn ĠObservable Transformer Ġ{ Ġeffect Stream Ġ-> ĊĠĠĠĠĠ Ġeffect Stream ĊĠĠĠĠĠĠĠĠĠ Ġ. flatMap Single Ġ{ ĠSingle . from Callable (:: read UserDetails From Storage ). subscribe On ( scheduler ) Ġ} ĊĠĠĠĠĠĠĠĠĠ Ġ. map Ġ{ Ġ( has User Completed On boarding , Ġlogged In User , Ġuser Selected Country ) Ġ-> ĊĠĠĠĠĠĠĠĠĠĠĠ ĠUser Details F etched ( ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġhas User Completed On boarding Ġ= Ġhas User Completed On boarding , ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġlogged In User Ġ= Ġlogged In User , ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġuser Selected Country Ġ= Ġuser Selected Country , ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġuser Selected Country V 1 Ġ= Ġload V 1 Country . load (), ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġcurrent Deployment Ġ= Ġapp Config Repository . current Deployment (). to Optional () ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ) ĊĠĠĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĠ Ġ} ĊĊĠ Ġprivate Ġfun Ġread UserDetails From Storage (): ĠTrip le < Boolean , ĠOptional < User >, ĠOptional < Country >> Ġ{ ĊĠĠĠ Ġval Ġhas User Completed On boarding Ġ= Ġon boarding Complete Preference . get () ĊĠĠĠ Ġval Ġlogged In User Ġ= Ġuser Dao . user Immediate (). to Optional () ĊĠĠĠ Ġval Ġuser Selected Country Ġ= Ġapp Config Repository . current Country () ĊĊĠĠĠ Ġreturn ĠTrip le ( has User Completed On boarding , Ġlogged In User , Ġuser Selected Country . to Optional ()) ĊĠ Ġ} ĊĊĠ Ġprivate Ġfun Ġinitialize Database ( scheduler : ĠScheduler ): ĠObservable Transformer < Initialize Database , ĠSetup Activity Event > Ġ{ ĊĠĠĠ Ġreturn ĠObservable Transformer Ġ{ Ġeffect Stream Ġ-> ĊĠĠĠĠĠ Ġeffect Stream ĊĠĠĠĠĠĠĠĠĠ Ġ. flatMap Single Ġ{ Ġuser Dao . user Count (). subscribe On ( scheduler ) Ġ} ĊĠĠĠĠĠĠĠĠĠ Ġ. map Ġ{ ĠDatabase Initialized Ġ} ĊĠĠĠ Ġ} ĊĠ Ġ} ĊĊĠ Ġprivate Ġfun Ġrun Database Maintenance (): ĠObservable Transformer < Run Database Maintenance , ĠSetup Activity Event > Ġ{ ĊĠĠĠ Ġreturn ĠObservable Transformer Ġ{ Ġeffects Ġ-> ĊĠĠĠĠĠ Ġeffects ĊĠĠĠĠĠĠĠĠĠ Ġ. observe On ( schedul ers Provider . io ()) ĊĠĠĠĠĠĠĠĠĠ Ġ. do On Next Ġ{ Ġapp Database . prune ( now Ġ= ĠInstant . now ( user Clock )) Ġ} ĊĠĠĠĠĠĠĠĠĠ Ġ. do On Next Ġ{ Ġdatabase Maintenance Run At . set ( Optional . of ( Instant . now ( clock ))) Ġ} ĊĠĠĠĠĠĠĠĠĠ Ġ. map Ġ{ ĠDatabase Maintenance Completed Ġ} ĊĠĠĠ Ġ} ĊĠ Ġ} ĊĊĠ Ġprivate Ġfun Ġload Last Database Maintenance Time (): ĠObservable Transformer < Fetch Database Maintenance Last Run At Time , ĠSetup Activity Event > Ġ{ ĊĠĠĠ Ġreturn ĠObservable Transformer Ġ{ Ġeffects Ġ-> ĊĠĠĠĠĠ Ġeffects ĊĠĠĠĠĠĠĠĠĠ Ġ. observe On ( schedul ers Provider . io ()) ĊĠĠĠĠĠĠĠĠĠ Ġ. map Ġ{ Ġdatabase Maintenance Run At . get () Ġ} ĊĠĠĠĠĠĠĠĠĠ Ġ. map (:: Database Maintenance Last Run At Time Loaded ) ĊĠĠĠ Ġ} ĊĠ Ġ} ĊĊĠ Ġprivate Ġfun Ġcheck Application Allowed To Run (): ĠObservable Transformer < Check If App Can Run , ĠSetup Activity Event > Ġ{ ĊĠĠĠ Ġreturn ĠObservable Transformer Ġ{ Ġeffects Ġ-> ĊĠĠĠĠĠ Ġeffects ĊĠĠĠĠĠĠĠĠĠ Ġ. observe On ( schedul ers Provider . io ()) ĊĠĠĠĠĠĠĠĠĠ Ġ. map Ġ{ Ġallow Application To Run . check () Ġ} ĊĠĠĠĠĠĠĠĠĠ Ġ. map (:: App Allowed To Run Check Completed ) ĊĠĠĠ Ġ} ĊĠ Ġ} ĊĊĠ Ġprivate Ġfun Ġsave Country And Deployment (): ĠObservable Transformer < Save Country And Deployment , ĠSetup Activity Event > Ġ{ ĊĠĠĠ Ġreturn ĠObservable Transformer Ġ{ Ġeffects Ġ-> ĊĠĠĠĠĠ Ġeffects ĊĠĠĠĠĠĠĠĠĠ Ġ. observe On ( schedul ers Provider . io ()) ĊĠĠĠĠĠĠĠĠĠ Ġ. do On Next Ġ{ Ġeffect Ġ-> ĊĠĠĠĠĠĠĠĠĠĠĠ Ġapp Config Repository . save Current Country ( effect . country ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġapp Config Repository . save Deployment ( effect . deployment ) ĊĠĠĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠĠĠ Ġ. map Ġ{ ĠCountry And Deployment Saved Ġ} ĊĠĠĠ Ġ} ĊĠ Ġ} Ċ } Ċ