package Ġcom . anc ient lo re . me ment o Ċ Ċ import Ġandroid . app . Activity Ċ import Ġandroid . content . Intent Ċ import Ġandroid . os . Bundle Ċ import Ġandroid . support . annotation . Worker Thread Ċ import Ġandroid . support . v 7 . widget . Linear LayoutManager Ċ import Ġcom . anc ient lo re . me ment o . Alarm Activity . Companion . EXTRA _ ALARM Ċ import Ġcom . anc ient lo re . me ment o . database . Al arms Database Ċ import Ġcom . anc ient lo re . me ment o . databinding . Activity Main Binding Ċ import Ġkotlinx . android . synt hetic . main . activity _ main .* Ċ import Ġjava . util . concurrent . ExecutorService Ċ import Ġjava . util . concurrent . Executors Ċ Ċ class ĠMainActivity Ġ: ĠBase Activity < Activity Main Binding , ĠMainActivity ViewModel >() Ġ{ ĊĊ ĉ comp anion Ġobject Ġ{ Ċĉ ĉ const Ġval ĠINTENT _ ADD _ ALARM Ġ= Ġ 1 0 1 Ċĉ ĉ const Ġval ĠINTENT _ MO DIFY _ ALARM Ġ= Ġ 1 0 2 Ċ ĉ } ĊĊ ĉ private Ġval Ġdb Exec : ĠExecutor Service Ġ= ĠExec utors . new Single Thread Executor Ġ{ Ġr Ġ-> ĠThread ( r , Ġ" db _ worker ") Ġ} ĊĊ ĉ private Ġval Ġdb Ġby Ġlazy Ġ{ ĠAl arms Database . getInstance ( this ) Ġ} ĊĊ ĉ private Ġlateinit Ġvar Ġlist Adapter : ĠAl arms ListAdapter ĊĊ ĉ override Ġfun ĠonCreate ( savedInstanceState : ĠBundle ?) Ġ{ Ċĉ ĉ super . onCreate ( savedInstanceState ) ĊĊĉ ĉ alarm ListView . layout Manager Ġ= ĠLinearLayoutManager ( this , ĠLinearLayoutManager . VERTICAL , Ġfalse ) ĊĊĉ ĉ db Exec . submit Ġ{ Ċĉĉ ĉ list Adapter Ġ= ĠAl arms ListAdapter ( this , Ġdb . alarm Dao (). getAll (). to Mutable List ()) Ċĉĉ ĉ list Adapter . listener Ġ= ĠviewModel Ċĉĉ ĉ alarm ListView . adapter Ġ= Ġlist Adapter Ċĉ ĉ } ĊĊĉ ĉ viewModel . add Alarm Event () Ċĉĉĉ ĉ . take Until ( destroy Event ) Ċĉĉĉ ĉ . subscribe Ġ{ Ġadd Alarm Intent () Ġ} ĊĊĉ ĉ viewModel . alarm Selected Event () Ċĉĉĉ ĉ . take Until ( destroy Event ) Ċĉĉĉ ĉ . subscribe Ġ{ Ġmodify Alarm Intent ( it ) Ġ} ĊĊĉ ĉ viewModel . alarm Sw itched Event () Ċĉĉĉ ĉ . take Until ( destroy Event ) Ċĉĉĉ ĉ . subscribe Ġ{ Ġswitch Alarm State ( it . first , Ġit . second ) Ġ} Ċ ĉ } ĊĊ ĉ override Ġfun Ġon Activity Result ( request Code : ĠInt , ĠresultCode : ĠInt , Ġdata : ĠIntent ?) Ġ{ Ċĉ ĉ super . on Activity Result ( request Code , ĠresultCode , Ġdata ) ĊĊĉ ĉ if Ġ( result Code Ġ!= ĠActivity . RESULT _ OK ) Ġreturn ĊĊĉ ĉ when Ġ( request Code ) Ġ{ Ċĉĉ ĉ INT ENT _ ADD _ ALARM Ġ-> Ġdata ?. get Parcelable Extra < Alarm >( Alarm Activity . EXTRA _ ALARM ) Ċĉĉĉĉ ĉ ?. run Ġ{ Ġadd Alarm ( this ) Ġ} Ċĉĉ ĉ INT ENT _ MO DIFY _ ALARM Ġ-> Ġdata ?. getLong Extra ( Alarm Activity . EXTRA _ DELETE _ ALARM _ ID , Ġ- 1 ) Ċĉĉĉĉ ĉ . take If Ġ{ Ġit Ġ!= Ġ- 1 L Ġ} Ċĉĉĉĉ ĉ ?. run Ġ{ Ġlist Adapter . find Item ( this ) Ġ} Ċĉĉĉĉ ĉ ?. run Ġ{ Ġdelete Alarm ( this ) Ġ} Ċĉ ĉ } Ċ ĉ } ĊĊ ĉ override Ġfun Ġget Layout Id () Ġ= ĠR . layout . activity _ main ĊĊ ĉ override Ġfun Ġget Binding Variable () Ġ= ĠBR . viewModel ĊĊ ĉ override Ġfun Ġcreate ViewModel () Ġ= ĠMainActivity ViewModel () ĊĊ ĉ override Ġfun Ġget Title Id () Ġ= ĠR . string . app _ name ĊĊ ĉ private Ġfun Ġadd Alarm ( alarm : ĠAlarm ) Ġ{ Ċĉ ĉ db Exec . submit Ġ{ Ċĉĉ ĉ alarm . id Ġ= Ġadd Alarm To Db ( alarm ) Ċĉĉ ĉ alarm . schedule ( this ) ĊĊĉĉ ĉ run On Ui Thread Ġ{ Ċĉĉĉ ĉ list Adapter . addItem ( alarm ) Ċĉĉ ĉ } Ċĉ ĉ } Ċ ĉ } ĊĊ ĉ private Ġfun Ġupdate Alarm ( alarm : ĠAlarm ) Ġ{ Ċĉ ĉ alarm . schedule ( this ) ĊĊĉ ĉ update Alarm In Db ( alarm ) Ċĉ ĉ run On Ui Thread Ġ{ Ġlist Adapter . update Item ( alarm ) Ġ} Ċ ĉ } ĊĊ ĉ private Ġfun Ġdelete Alarm ( alarm : ĠAlarm ) Ġ{ Ċĉ ĉ alarm . cancel ( this ) ĊĊĉ ĉ delete Alarm In Db ( alarm ) Ċĉ ĉ run On Ui Thread Ġ{ Ġlist Adapter . delete Item ( alarm ) Ġ} Ċ ĉ } ĊĊ ĉ @ Worker Thread Ċ ĉ private Ġfun Ġadd Alarm To Db ( alarm : ĠAlarm ) Ġ= Ġdb . alarm Dao (). insert ( alarm ) ĊĊ ĉ private Ġfun Ġupdate Alarm In Db ( alarm : ĠAlarm ) Ġ{ Ċĉ ĉ db Exec . submit Ġ{ Ġdb . alarm Dao (). update ( alarm ) Ġ} Ċ ĉ } ĊĊ ĉ private Ġfun Ġdelete Alarm In Db ( alarm : ĠAlarm ) Ġ{ Ċĉ ĉ db Exec . submit Ġ{ Ġdb . alarm Dao (). delete ( alarm ) Ġ} Ċ ĉ } ĊĊ ĉ private Ġfun Ġadd Alarm Intent () Ġ{ Ċĉ ĉ val Ġintent Ġ= ĠIntent ( this , ĠAlarm Activity :: class . java ) Ċĉ ĉ startActivity For Result ( intent , ĠINTENT _ ADD _ ALARM ) Ċ ĉ } ĊĊ ĉ private Ġfun Ġmodify Alarm Intent ( alarm : ĠAlarm ) Ġ{ Ċĉ ĉ val Ġintent Ġ= ĠIntent ( this , ĠAlarm Activity :: class . java ) Ċĉ ĉ intent . putExtra ( EXTRA _ ALARM , Ġalarm ) Ċĉ ĉ startActivity For Result ( intent , ĠINTENT _ MO DIFY _ ALARM ) Ċ ĉ } ĊĊ ĉ private Ġfun Ġswitch Alarm State ( alarm : ĠAlarm , Ġis Enabled : ĠBoolean ) Ġ{ Ċĉ ĉ alarm . enabled Ġ= Ġis Enabled ĊĊĉ ĉ update Alarm ( alarm ) Ċ ĉ } Ċ } Ċ