package Ġcom . she ma 1 0 2 . spac ex tracker . data . repository Ċ Ċ import Ġandroidx . lifecycle . LiveData Ċ import Ġcom . she ma 1 0 2 . spac ex tracker . data . db . dao . La unches Dao Ċ import Ġcom . she ma 1 0 2 . spac ex tracker . data . db . dao . Next Launch Dao Ċ import Ġcom . she ma 1 0 2 . spac ex tracker . data . db . dao . Road ster Dao Ċ import Ġcom . she ma 1 0 2 . spac ex tracker . data . db . entity . Launch Entry Ċ import Ġcom . she ma 1 0 2 . spac ex tracker . data . db . entity . Next Launch Entry Ċ import Ġcom . she ma 1 0 2 . spac ex tracker . data . db . entity . Road ster Entry Ċ import Ġcom . she ma 1 0 2 . spac ex tracker . data . db . unit localized . Unit Specific Road ster Ċ import Ġcom . she ma 1 0 2 . spac ex tracker . data . network . Sp ac ex Network DataSource Ċ import Ġkotlinx . coroutines . Dispatch ers Ċ import Ġkotlinx . coroutines . Global Scope Ċ import Ġkotlinx . coroutines . launch Ċ import Ġkotlinx . coroutines . with Context Ċ import Ġorg . th reet en . bp . Z onedDateTime Ċ import Ġjavax . inject . Inject Ċ Ċ class ĠSp ac ex Repository Impl Ġ@ Inject Ġconstructor ( ĊĠĠĠ Ġprivate Ġval Ġroad ster Dao : ĠRoad ster Dao , ĊĠĠĠ Ġprivate Ġval Ġnext Launch Dao : ĠNext Launch Dao , ĊĠĠĠ Ġprivate Ġval Ġla unches Dao : ĠLa unches Dao , ĊĠĠĠ Ġprivate Ġval Ġla unches Network DataSource : ĠSp ac ex Network DataSource Ċ ) Ġ: ĠSp ac ex Repository Ġ{ ĊĠĠĠ Ġinit Ġ{ ĊĠĠĠĠĠĠĠ Ġla unches Network DataSource . downloaded Road ster Entry . observe Fore ver Ġ{ Ġnew Road ster Ġ-> ĊĠĠĠĠĠĠĠĠĠĠĠ Ġpersist F etched Road ster ( new Road ster ) ĊĠĠĠĠĠĠĠ Ġ} ĊĊĠĠĠĠĠĠĠ Ġla unches Network DataSource . downloaded Next Launch . observe Fore ver Ġ{ Ġnew Next Launch Ġ-> ĊĠĠĠĠĠĠĠĠĠĠĠ Ġpersist F etched Next Launch ( new Next Launch ) ĊĠĠĠĠĠĠĠ Ġ} ĊĊĠĠĠĠĠĠĠ Ġla unches Network DataSource . downloaded La unches . observe Fore ver Ġ{ Ġla unches Ġ-> ĊĠĠĠĠĠĠĠĠĠĠĠ Ġpersist F etched La unches ( la unches ) ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġoverride Ġsuspend Ġfun ĠgetNext Launch (): ĠLive Data < Next Launch Entry > Ġ{ ĊĠĠĠĠĠĠĠ Ġreturn Ġwith Context ( Dispatch ers . IO ) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġinit Next Launch () ĊĠĠĠĠĠĠĠĠĠĠĠ Ġreturn @ with Context Ġnext Launch Dao . getNext Launch () ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġprivate Ġfun Ġpersist F etched Next Launch ( f etched Next Launch : ĠNext Launch Entry ) Ġ{ ĊĠĠĠĠĠĠĠ ĠGlobal Scope . launch ( Dispatch ers . IO ) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġnext Launch Dao . upsert ( f etched Next Launch ) ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġprivate Ġsuspend Ġfun Ġinit Next Launch () Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġlast Update Ġ= Ġnext Launch Dao . getNext Launch Last UpdateTime () ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ?: ĠZ onedDateTime . now (). minus Years ( 1 0 0 ) ĊĠĠĠĠĠĠĠ Ġif Ġ( is Fetch Next Launch Needed ( last Update )) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġfetch Next Launch () ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġprivate Ġsuspend Ġfun Ġfetch Next Launch () Ġ{ ĊĠĠĠĠĠĠĠ Ġla unches Network DataSource . fetch Next Launch () ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġprivate Ġfun Ġis Fetch Next Launch Needed ( last Fetch Time : ĠZ onedDateTime ): ĠBoolean Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġupdate Window Ġ= ĠZ onedDateTime . now (). minus Minutes ( 3 0 ) ĊĠĠĠĠĠĠĠ Ġreturn Ġlast Fetch Time . is Before ( update Window ) ĊĠĠĠ Ġ} ĊĊĊĠĠĠ Ġoverride Ġsuspend Ġfun Ġget Road ster ( metric : ĠBoolean ): ĠLive Data < out ĠUnit Specific Road ster > Ġ{ ĊĠĠĠĠĠĠĠ Ġreturn Ġwith Context ( Dispatch ers . IO ) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġinit Road ster () ĊĠĠĠĠĠĠĠĠĠĠĠ Ġreturn @ with Context Ġif Ġ( metric ) Ġroad ster Dao . get Road ster Metric () ĊĠĠĠĠĠĠĠĠĠĠĠ Ġelse Ġroad ster Dao . get Road ster Imp erial () ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġprivate Ġfun Ġpersist F etched Road ster ( f etched Road ster Entry : ĠRoad ster Entry ) Ġ{ ĊĠĠĠĠĠĠĠ ĠGlobal Scope . launch ( Dispatch ers . IO ) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġroad ster Dao . upsert ( f etched Road ster Entry ) ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġprivate Ġsuspend Ġfun Ġinit Road ster () Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġlast Update Ġ= Ġroad ster Dao . get Road ster Last UpdateTime () ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ?: ĠZ onedDateTime . now (). minus Years ( 1 0 0 ) ĊĠĠĠĠĠĠĠ Ġif Ġ( is Fetch Road ster Needed ( last Update )) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġfetch Road ster () ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġprivate Ġsuspend Ġfun Ġfetch Road ster () Ġ{ ĊĠĠĠĠĠĠĠ Ġla unches Network DataSource . fetch Road ster () ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġprivate Ġfun Ġis Fetch Road ster Needed ( last Fetch Time : ĠZ onedDateTime ): ĠBoolean Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġupdate Window Ġ= ĠZ onedDateTime . now (). minus Minutes ( 6 0 ) ĊĠĠĠĠĠĠĠ Ġreturn Ġlast Fetch Time . is Before ( update Window ) ĊĠĠĠ Ġ} ĊĊĊĠĠĠ Ġoverride Ġsuspend Ġfun Ġget La unches (): ĠLive Data < List < Launch Entry >> Ġ{ ĊĠĠĠĠĠĠĠ Ġreturn Ġwith Context ( Dispatch ers . IO ) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġinit La unches () ĊĠĠĠĠĠĠĠĠĠĠĠ Ġreturn @ with Context Ġla unches Dao . getAll La unches () ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĊĊĠĠĠ Ġoverride Ġsuspend Ġfun Ġget Launch With Id ( id : ĠString ): ĠLive Data < Launch Entry > Ġ{ ĊĠĠĠĠĠĠĠ Ġreturn Ġla unches Dao . get Launch With Id ( id ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġprivate Ġfun Ġpersist F etched La unches ( f etched La unches : ĠList < Launch Entry >) Ġ{ ĊĠĠĠĠĠĠĠ ĠGlobal Scope . launch ( Dispatch ers . IO ) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġla unches Dao . upsert ( f etched La unches ) ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġprivate Ġsuspend Ġfun Ġinit La unches () Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġlast Update Of Random Launch Ġ= Ġla unches Dao . getRandom Launch Last UpdateTime () ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ?: ĠZ onedDateTime . now (). minus Years ( 1 0 0 ) ĊĠĠĠĠĠĠĠ Ġif Ġ( is Fetch La unches Needed ( last Update Of Random Launch )) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġfetch La unches () ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġprivate Ġsuspend Ġfun Ġfetch La unches () Ġ{ ĊĠĠĠĠĠĠĠ Ġla unches Network DataSource . fetch La unches () ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġprivate Ġfun Ġis Fetch La unches Needed ( last Fetch Time : ĠZ onedDateTime ): ĠBoolean Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġth irty Minutes Ago Ġ= ĠZ onedDateTime . now (). minus Minutes ( 1 2 0 ) ĊĠĠĠĠĠĠĠ Ġreturn Ġlast Fetch Time . is Before ( th irty Minutes Ago ) ĊĠĠĠ Ġ} Ċ }