/* Ċ Ġ* ĠCopyright Ġ( C ) Ġ 2 0 1 8 ĠSimon ĠV ig ĠTh erk ild sen Ċ Ġ* Ċ Ġ* ĠLicensed Ġunder Ġthe ĠApache ĠLicense , ĠVersion Ġ 2 . 0 Ġ( the Ġ" License "); Ċ Ġ* Ġyou Ġmay Ġnot Ġuse Ġthis Ġfile Ġexcept Ġin Ġcompliance Ġwith Ġthe ĠLicense . Ċ Ġ* ĠYou Ġmay Ġobtain Ġa Ġcopy Ġof Ġthe ĠLicense Ġat Ċ Ġ* Ċ Ġ* ĠĠĠ Ġhttp :// www . apache . org / licenses / LICENSE - 2 . 0 Ċ Ġ* Ċ Ġ* ĠUnless Ġrequired Ġby Ġapplicable Ġlaw Ġor Ġagreed Ġto Ġin Ġwriting , Ġsoftware Ċ Ġ* Ġdistributed Ġunder Ġthe ĠLicense Ġis Ġdistributed Ġon Ġan Ġ" AS ĠIS " ĠBASIS , Ċ Ġ* ĠWITHOUT ĠWARRANTIES ĠOR ĠCONDITIONS ĠOF ĠANY ĠKIND , Ġeither Ġexpress Ġor Ġimplied . Ċ Ġ* ĠSee Ġthe ĠLicense Ġfor Ġthe Ġspecific Ġlanguage Ġgoverning Ġpermissions Ġand Ċ Ġ* Ġlimitations Ġunder Ġthe ĠLicense . Ċ Ġ*/ Ċ Ċ package Ġnet . sim on vt . c ath ode . ui . show Ċ Ċ import Ġandroid . content . Context Ċ import Ġandroid . text . format . Date Utils Ċ import Ġandroidx . lifecycle . LiveData Ċ import Ġandroidx . lifecycle . Observer Ċ import Ġandroidx . lifecycle . viewModel Scope Ċ import Ġkotlinx . coroutines . async Ċ import Ġnet . sim on vt . c ath ode . actions . comments . Sync Episode Comments Ċ import Ġnet . sim on vt . c ath ode . actions . invoke Async Ċ import Ġnet . sim on vt . c ath ode . actions . se asons . Sync Season Ċ import Ġnet . sim on vt . c ath ode . common . data . Mapped Cursor LiveData Ċ import Ġnet . sim on vt . c ath ode . entity . Comment Ċ import Ġnet . sim on vt . c ath ode . entity . Episode Ċ import Ġnet . sim on vt . c ath ode . entity mapper . Comment List Mapper Ċ import Ġnet . sim on vt . c ath ode . entity mapper . Comment Mapper Ċ import Ġnet . sim on vt . c ath ode . entity mapper . Episode Mapper Ċ import Ġnet . sim on vt . c ath ode . provider . Database Contract . Comment Columns Ċ import Ġnet . sim on vt . c ath ode . provider . Provider S che matic . Comments Ċ import Ġnet . sim on vt . c ath ode . provider . Provider S che matic . Episode s Ċ import Ġnet . sim on vt . c ath ode . provider . helper . Episode Database Helper Ċ import Ġnet . sim on vt . c ath ode . provider . helper . Show Database Helper Ċ import Ġnet . sim on vt . c ath ode . ui . Refresh able ViewModel Ċ import Ġjavax . inject . Inject Ċ Ċ class ĠEp isode ViewModel Ġ@ Inject Ġconstructor ( ĊĠ Ġprivate Ġval Ġcontext : ĠContext , ĊĠ Ġprivate Ġval Ġshow Helper : ĠShow Database Helper , ĊĠ Ġprivate Ġval Ġepisode Helper : ĠEp isode Database Helper , ĊĠ Ġprivate Ġval Ġsync Season : ĠSync Season , ĊĠ Ġprivate Ġval Ġsync Episode Comments : ĠSync Episode Comments Ċ ) Ġ: ĠRefresh able ViewModel () Ġ{ ĊĊĠ Ġprivate Ġvar Ġepisode Id Ġ= Ġ- 1 L ĊĊĠ Ġlateinit Ġvar Ġepisode : ĠLive Data < Episode > ĊĠĠĠ Ġprivate Ġset ĊĠ Ġlateinit Ġvar Ġuser Comments : ĠLive Data < List < Comment >> ĊĠĠĠ Ġprivate Ġset ĊĠ Ġlateinit Ġvar Ġcomments : ĠLive Data < List < Comment >> ĊĠĠĠ Ġprivate Ġset ĊĊĠ Ġfun Ġset Episode Id ( episode Id : ĠLong ) Ġ{ ĊĠĠĠ Ġif Ġ( this . episode Id Ġ== Ġ- 1 L ) Ġ{ ĊĠĠĠĠĠ Ġthis . episode Id Ġ= Ġepisode Id ĊĊĠĠĠĠĠ Ġepisode Ġ= ĠM apped Cursor LiveData ( ĊĠĠĠĠĠĠĠ Ġcontext , ĊĠĠĠĠĠĠĠ ĠEp isode s . with Id ( episode Id ), ĊĠĠĠĠĠĠĠ ĠEp isode Mapper . projection , ĊĠĠĠĠĠĠĠ Ġnull , ĊĠĠĠĠĠĠĠ Ġnull , ĊĠĠĠĠĠĠĠ Ġnull , ĊĠĠĠĠĠĠĠ ĠEp isode Mapper ĊĠĠĠĠĠ Ġ) ĊĠĠĠĠĠ Ġuser Comments Ġ= ĠM apped Cursor LiveData ( ĊĠĠĠĠĠĠĠ Ġcontext , ĊĠĠĠĠĠĠĠ ĠComments . from Episode ( episode Id ), ĊĠĠĠĠĠĠĠ ĠComment Mapper . projection , ĊĠĠĠĠĠĠĠ ĠComment Columns . IS _ USER _ COMMENT Ġ+ Ġ"= 1 ", ĊĠĠĠĠĠĠĠ Ġnull , ĊĠĠĠĠĠĠĠ Ġnull , ĊĠĠĠĠĠĠĠ ĠComment List Mapper ĊĠĠĠĠĠ Ġ) ĊĠĠĠĠĠ Ġcomments Ġ= ĠM apped Cursor LiveData ( ĊĠĠĠĠĠĠĠ Ġcontext , ĊĠĠĠĠĠĠĠ ĠComments . from Episode ( episode Id ), ĊĠĠĠĠĠĠĠ ĠComment Mapper . projection , ĊĠĠĠĠĠĠĠ ĠComment Columns . IS _ USER _ COMMENT Ġ+ Ġ"= 0 ĠAND Ġ" Ġ+ ĠComment Columns . S PO IL ER Ġ+ Ġ"= 0 ", ĊĠĠĠĠĠĠĠ Ġnull , ĊĠĠĠĠĠĠĠ ĠComment Columns . LIK ES Ġ+ Ġ" ĠDESC ĠLIMIT Ġ 3 ", ĊĠĠĠĠĠĠĠ ĠComment List Mapper ĊĠĠĠĠĠ Ġ) ĊĊĠĠĠĠĠ Ġepisode . observe Fore ver ( episode Observer ) ĊĠĠĠ Ġ} ĊĠ Ġ} ĊĊĠ Ġoverride Ġfun Ġon Cle ared () Ġ{ ĊĠĠĠ Ġepisode . remove Observer ( episode Observer ) ĊĠĠĠ Ġsuper . on Cle ared () ĊĠ Ġ} ĊĊĠ Ġprivate Ġval Ġepisode Observer Ġ= ĠObserver < Episode > Ġ{ Ġepisode Ġ-> ĊĠĠĠ ĠviewModel Scope . async Ġ{ ĊĠĠĠĠĠ Ġif Ġ( System . currentTimeMillis () Ġ> Ġepisode . last Comment Sync Ġ+ ĠSY NC _ INTERVAL _ COMMENTS ) Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġshow Id Ġ= Ġepisode Helper . get Show Id ( episode Id ) ĊĠĠĠĠĠĠĠ Ġval Ġtr akt Id Ġ= Ġshow Helper . get Tr akt Id ( show Id ) ĊĠĠĠĠĠĠĠ Ġsync Episode Comments . invoke Async ( ĊĠĠĠĠĠĠĠĠĠ ĠSync Episode Comments . Params ( ĊĠĠĠĠĠĠĠĠĠĠĠ Ġtr akt Id , ĊĠĠĠĠĠĠĠĠĠĠĠ Ġepisode . season , ĊĠĠĠĠĠĠĠĠĠĠĠ Ġepisode . episode ĊĠĠĠĠĠĠĠĠĠ Ġ) ĊĠĠĠĠĠĠĠ Ġ) ĊĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĠ Ġ} ĊĊĠ Ġoverride Ġsuspend Ġfun Ġon Refresh () Ġ{ ĊĠĠĠ Ġval Ġshow Id Ġ= Ġepisode Helper . get Show Id ( episode Id ) ĊĠĠĠ Ġval Ġtr akt Id Ġ= Ġshow Helper . get Tr akt Id ( show Id ) ĊĠĠĠ Ġval Ġseason Ġ= Ġepisode Helper . get Season ( episode Id ) ĊĠĠĠ Ġval Ġnumber Ġ= Ġepisode Helper . getNumber ( episode Id ) ĊĊĠĠĠ Ġval Ġseason Deferred Ġ= Ġsync Season . invoke Async ( Sync Season . Params ( tr akt Id , Ġseason )) ĊĠĠĠ Ġval Ġcomments Deferred Ġ= ĊĠĠĠĠĠ Ġsync Episode Comments . invoke Async ( Sync Episode Comments . Params ( tr akt Id , Ġseason , Ġnumber )) ĊĊĠĠĠ Ġseason Deferred . await () ĊĠĠĠ Ġcomments Deferred . await () ĊĠ Ġ} ĊĊĠ Ġcompanion Ġobject Ġ{ ĊĠĠĠ Ġprivate Ġconst Ġval ĠSY NC _ INTERVAL _ COMMENTS Ġ= Ġ 3 Ġ* ĠDate Utils . HOUR _ IN _ M ILL IS ĊĠ Ġ} Ċ } Ċ