package Ġcom . nish an th . news app . news . domain Ċ Ċ import Ġcom . nish an th . news app . core . domain . Network Bound Resource Ċ import Ġcom . nish an th . news app . core . ui . ViewState Ċ import Ġcom . nish an th . news app . news . api . News Service Ċ import Ġcom . nish an th . news app . news . model . News Articles Ċ import Ġcom . nish an th . news app . news . model . News Source Response Ċ import Ġcom . nish an th . news app . news . storage . News Articles Dao Ċ import Ġdagger . Bind s Ċ import Ġdagger . Module Ċ import Ġkotlinx . coroutines . Dispatch ers Ċ import Ġkotlinx . coroutines . flow . Flow Ċ import Ġkotlinx . coroutines . flow . flow On Ċ import Ġretrofit 2 . Response Ċ import Ġjavax . inject . Inject Ċ import Ġjavax . inject . Singleton Ċ Ċ /** Ċ Ġ* ĠRepository Ġabstract s Ġthe Ġlogic Ġof Ġfetching Ġthe Ġdata Ġand Ġpers isting Ġit Ġfor Ċ Ġ* Ġoffline . ĠThey Ġare Ġthe Ġdata Ġsource Ġas Ġthe Ġsingle Ġsource Ġof Ġtruth . Ċ Ġ*/ Ċ interface ĠNews Repository Ġ{ ĊĊĠĠĠ Ġ/** ĊĠĠĠĠ Ġ* ĠGets Ġt ne Ġcached Ġnews Ġarticle Ġfrom Ġdatabase Ġand Ġtries Ġto Ġget ĊĠĠĠĠ Ġ* Ġfresh Ġnews Ġarticles Ġfrom Ġweb Ġand Ġsave Ġinto Ġdatabase ĊĠĠĠĠ Ġ* Ġif Ġthat Ġfails Ġthen Ġcontinues Ġshowing Ġcached Ġdata . ĊĠĠĠĠ Ġ*/ ĊĠĠĠ Ġfun Ġget News Articles (): ĠFlow < ViewState < List < News Articles >>> Ċ } Ċ Ċ @ Singleton Ċ class ĠDefault News Repository Ġ@ Inject Ġconstructor ( ĊĠĠĠĠĠĠĠ Ġprivate Ġval Ġnews Dao : ĠNews Articles Dao , ĊĠĠĠĠĠĠĠ Ġprivate Ġval Ġnews Service : ĠNews Service Ċ ) Ġ: ĠNews Repository Ġ{ ĊĊĠĠĠ Ġoverride Ġfun Ġget News Articles (): ĠFlow < ViewState < List < News Articles >>> Ġ{ ĊĠĠĠĠĠĠĠ Ġreturn Ġobject Ġ: ĠNetwork Bound Resource < List < News Articles >, ĠNews Source Response >() Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġoverride Ġsuspend Ġfun Ġsave Network Result ( response : ĠNews Source Response ) Ġ= Ġnews Dao . clear And Cache Articles ( response . articles ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ// ĠAlways Ġtry Ġto Ġfetch Ġnew Ġarticles ĊĠĠĠĠĠĠĠĠĠĠĠ Ġoverride Ġfun Ġshould Fetch ( data : ĠList < News Articles >? ): ĠBoolean Ġ= Ġtrue ĊĠĠĠĠĠĠĠĠĠĠĠ Ġoverride Ġfun Ġload From Db (): ĠFlow < List < News Articles >> Ġ= Ġnews Dao . get News Articles () ĊĠĠĠĠĠĠĠĠĠĠĠ Ġoverride Ġsuspend Ġfun Ġfetch From Network (): ĠResponse < News Source Response > Ġ= Ġnews Service . get News From Google () ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠ Ġ. as Flow () ĊĠĠĠĠĠĠĠ Ġ. flow On ( Dispatch ers . IO ) ĊĠĠĠ Ġ} Ċ } Ċ Ċ @ Module Ċ interface ĠNews Repository Module Ġ{ ĊĠĠĠ Ġ/* ĠEx poses Ġthe Ġconcrete Ġimplementation Ġfor Ġthe Ġinterface Ġ*/ ĊĠĠĠ Ġ@ Bind s Ġfun Ġit ( it : ĠDefault News Repository ): ĠNews Repository Ċ }