package Ġpl . sr w . bill calculator . data . bill Ċ Ċ import Ġandroid . app . backup . Backup Manager Ċ import Ġandroid . arch . lifecycle . LiveData Ċ import Ġandroid . arch . lifecycle . Mutable LiveData Ċ import Ġandroid . content . Context Ċ import Ġandroid . support . annotation . Check Result Ċ import Ġio . reactivex . Single Ċ import Ġio . reactivex . android . schedul ers . Android Schedulers Ċ import Ġpl . sr w . bill calculator . bill . Saved B ills Registry Ċ import Ġpl . sr w . bill calculator . db . Bill Ċ import Ġpl . sr w . bill calculator . db . History Ċ import Ġpl . sr w . bill calculator . db . Prices Ċ import Ġpl . sr w . bill calculator . persistence . Database Ċ import Ġpl . sr w . bill calculator . persistence . type . Bill Type Ċ import Ġjavax . inject . Inject Ċ import Ġjavax . inject . Singleton Ċ Ċ @ SuppressWarnings (" Too Many Functions ") Ċ @ Singleton Ċ class ĠHistory Repo Ġ@ Inject Ġconstructor ( ĊĠĠĠ Ġprivate Ġval Ġapplication Context : ĠContext , ĊĠĠĠ Ġprivate Ġval Ġsaved B ills Registry : ĠSaved B ills Registry Ċ ) Ġ{ ĊĊĠĠĠ Ġprivate Ġval Ġall Ġ= ĠMutable LiveData < List < History >>() ĊĠĠĠ Ġprivate Ġvar Ġdeleted : ĠCollection < Pair < Bill , ĠPr ices >> Ġ= Ġempty List () ĊĊĠĠĠ Ġinit Ġ{ ĊĠĠĠĠĠĠĠ Ġload All Async () ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġfun ĠgetAll (): ĠLive Data < List < History >> Ġ= Ġall ĊĊĠĠĠ Ġ@ Check Result ĊĠĠĠ Ġfun Ġinsert ( bill : ĠBill ): ĠBill Ġ{ ĊĠĠĠĠĠĠĠ ĠDatabase . insert ( bill ) ĊĠĠĠĠĠĠĠ Ġload All Async () ĊĠĠĠĠĠĠĠ ĠBackup Manager ( application Context ). data Changed () ĊĠĠĠĠĠĠĠ Ġsaved B ills Registry . register ( bill ) ĊĠĠĠĠĠĠĠ Ġreturn Ġbill ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Check Result ĊĠĠĠ Ġfun Ġinsert ( prices : ĠPr ices ): ĠPr ices Ġ{ ĊĠĠĠĠĠĠĠ ĠDatabase . insert ( prices ) ĊĠĠĠĠĠĠĠ Ġreturn Ġprices ĊĠĠĠ Ġ} Ċ Ċ // ĠĠĠ Ġfun Ġinsert New ( prices : ĠPr ices , Ġb ull : ĠBill ) Ġ{ Ġbill . prices Id Ġ= Ġprices . id Ġ} Ġ// ĠFIXME : Ġsingle Ġcall Ġfor Ġsave Ġwith Ġbackup Ġupdate ĊĊĠĠĠ Ġfun Ġdelete B ills With Prices ( b ills : ĠCollection < Bill >) Ġ{ ĊĠĠĠĠĠĠĠ Ġb ills . forEach Ġ{ Ġdelete Single Bill With Prices ( it ) Ġ} ĊĠĠĠĠĠĠĠ Ġload All Async () ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġfun Ġdelete Bill With Prices ( bill : ĠBill ) Ġ{ ĊĠĠĠĠĠĠĠ Ġdelete Single Bill With Prices ( bill , Ġtrue ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġfun Ġcache B ills For Undo Delete ( b ills : ĠCollection < Bill >) Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġprices Ġ= Ġb ills . map Ġ{ Ġload Prices For Bill ( it ) Ġ} ĊĠĠĠĠĠĠĠ Ġdeleted Ġ= Ġb ills . zip ( prices ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġfun Ġcache Bill For Undo Delete ( bill : ĠBill ) Ġ{ ĊĠĠĠĠĠĠĠ Ġdeleted Ġ= ĠlistOf ( Pair ( bill , Ġload Prices For Bill ( bill ))) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġfun Ġundo Delete () Ġ{ ĊĠĠĠĠĠĠĠ Ġfor Ġ(( bill , Ġprices ) Ġin Ġdeleted ) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ ĠDatabase . insert ( prices ) ĊĠĠĠĠĠĠĠĠĠĠĠ ĠDatabase . insert ( bill ) ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠ Ġdeleted Ġ= Ġempty List () ĊĠĠĠĠĠĠĠ Ġload All Async () ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġfun Ġis Undo Delete Possible () Ġ= Ġ! deleted . isEmpty () ĊĊĠĠĠ Ġprivate Ġfun Ġdelete Single Bill With Prices ( bill : ĠBill , Ġupdate Observer : ĠBoolean Ġ= Ġfalse ) Ġ{ ĊĠĠĠĠĠĠĠ ĠDatabase . delete Bill With Prices ( Bill Type . valueOf ( bill ), Ġbill . id , Ġbill . prices Id ) ĊĠĠĠĠĠĠĠ Ġif Ġ( update Observer ) Ġload All Async () ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġprivate Ġfun Ġload All Async () Ġ{ ĊĠĠĠĠĠĠĠ ĠSingle . from Callable Ġ{ ĠDatabase . get History () Ġ} ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. subscribe On ( Android Schedulers . main Thread ()) Ġ// ĠFIXME : ĠGreen Dao Ġrequires Ġsingle Ġthread ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. subscribe Ġ{ Ġlist Ġ-> Ġall . value Ġ= Ġlist Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġprivate Ġfun Ġload Prices For Bill ( bill : ĠBill ): ĠPr ices Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġbill Type Ġ= ĠBill Type . valueOf ( bill ) ĊĠĠĠĠĠĠĠ Ġval Ġload Prices Ġ= ĠDatabase . load Prices ( bill Type , Ġbill . prices Id ) ĊĠĠĠĠĠĠĠ Ġreturn ĠcheckNotNull ( load Prices , Ġ{ Ġ" Prices Ġnot Ġfound Ġfor Ġid Ġ${ bill . prices Id }" Ġ}) ĊĠĠĠ Ġ} Ċ } Ċ