package Ġcom . address book . android . main Ċ Ċ import Ġandroid . content . Context Ċ import Ġandroid . content . Intent Ċ import Ġandroid . os . Bundle Ċ import Ġandroid . view . View Ċ import Ġandroidx . appcompat . app . AlertDialog Ċ import Ġandroidx . appcompat . app . AppCompatActivity Ċ import Ġandroidx . lifecycle . ViewModel Provider Ċ import Ġandroidx . recyclerview . widget . Divider Item Decoration Ċ import Ġandroidx . recyclerview . widget . Linear LayoutManager Ċ import Ġandroidx . recyclerview . widget . RecyclerView Ċ import Ġcom . address book . android . R Ċ import Ġcom . address book . android . databinding . Activity Address Book Listing Binding Ċ import Ġcom . address book . android . main . resp osit ory . Address Book Repository Ċ import Ġcom . address book . android . main . resp osit ory . Address View Factory Ċ import Ġcom . address book . android . main . viewModel . Address ViewModel Ċ import Ġcom . address book . android . room Database . db . Address Book Ċ import Ġcom . address book . android . room Database . db . Address Book Database Ċ import Ġcom . address book . android . util . Constant Ċ import Ġcom . address book . android . util . Globals Ċ import Ġjava . io . Serializable Ċ import Ġjava . util .* Ċ Ċ class ĠAddress Book Listing Activity Ġ: ĠAppCompatActivity (), ĠAddress Book Adapter . On Address View ActionListener Ġ{ ĊĊĊĠĠĠ Ġprivate Ġvar Ġaddress book List : ĠList < Address Book > Ġ= ĠArrayList () ĊĠĠĠ Ġprivate Ġvar Ġadapter Address Book List : ĠAddress Book Adapter ? Ġ= Ġnull ĊĠĠĠ Ġprivate Ġvar Ġglobals : ĠGlobals ? Ġ= Ġnull ĊĠĠĠ Ġprivate Ġvar ĠADD _ EDIT _ ADDRESS _ REQ _ CODE Ġ= Ġ 1 0 1 0 ĊĠĠĠ Ġprivate Ġlateinit Ġvar Ġbinding : ĠActivity Address Book Listing Binding ĊĊĠĠĠ Ġprivate Ġlateinit Ġvar ĠviewModel : ĠAddress ViewModel ĊĠĠĠ Ġlateinit Ġvar Ġaddress Book Database : ĠAddress Book Database ĊĠĠĠ Ġlateinit Ġvar Ġaddress Book Repository : ĠAddress Book Repository ĊĠĠĠ Ġlateinit Ġvar Ġfactory : ĠAddress View Factory ĊĊĠĠĠ Ġoverride Ġfun ĠonCreate ( savedInstanceState : ĠBundle ?) Ġ{ ĊĠĠĠĠĠĠĠ Ġsuper . onCreate ( savedInstanceState ) ĊĠĠĠĠĠĠĠ Ġbinding Ġ= ĠActivity Address Book Listing Binding . inflate ( layout Inflater ) ĊĠĠĠĠĠĠĠ ĠsetContentView ( binding . root ) ĊĠĠĠĠĠĠĠ Ġinit () ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġprivate Ġfun Ġinit () Ġ{ ĊĠĠĠĠĠĠĠ Ġset Toolbar () ĊĠĠĠĠĠĠĠ ĠsetUp List () ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġprivate Ġfun Ġset Toolbar () Ġ{ ĊĊĠĠĠĠĠĠĠ Ġglobals Ġ= Ġapplication Context Ġas ĠGlobals ĊĠĠĠĠĠĠĠ Ġset Support ActionBar ( binding . toolbar . toolbar ) ĊĠĠĠĠĠĠĠ Ġbinding . toolbar . toolbar Title . text Ġ= ĠgetString ( R . string . lbl _ address _ book ) ĊĠĠĠĠĠĠĠ Ġbinding . toolbar . toolbar Left . visibility Ġ= ĠView . VISIBLE ĊĠĠĠĠĠĠĠ Ġbinding . toolbar . toolbar Right . visibility Ġ= ĠView . VISIBLE ĊĠĠĠĠĠĠĠ Ġbinding . toolbar . toolbar Left . text Ġ= ĠgetString ( R . string . action _ logout ) ĊĠĠĠĠĠĠĠ Ġbinding . toolbar . toolbar Right . text Ġ= ĠgetString ( R . string . action _ add ) ĊĊĠĠĠĠĠĠĠ Ġbinding . toolbar . toolbar Right . setOnClickListener ( click Listener ) ĊĠĠĠĠĠĠĠ Ġbinding . toolbar . toolbar Left . setOnClickListener ( click Listener ) ĊĊĠĠĠ Ġ} ĊĊĊĠĠĠ Ġprivate Ġfun ĠsetUp List () Ġ{ ĊĊĠĠĠĠĠĠĠ Ġaddress Book Database Ġ= ĠAddress Book Database ( this ) ĊĠĠĠĠĠĠĠ Ġaddress Book Repository Ġ= ĠAddress Book Repository ( address Book Database ) ĊĠĠĠĠĠĠĠ Ġfactory Ġ= ĠAddress View Factory ( address Book Repository ) ĊĠĠĠĠĠĠĠ ĠviewModel Ġ= ĠViewModel Provider ( this , factory )[ Address ViewModel :: class . java ] ĊĊĠĠĠĠĠĠĠ ĠviewModel . getAll Address (). observe ( this , Ġandroidx . lifecycle . Observer Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġaddress book List Ġ= Ġit ĊĠĠĠĠĠĠĠĠĠĠĠ Ġset Adapter () ĊĊĠĠĠĠĠĠĠ Ġ}) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġprivate Ġfun Ġset Adapter () Ġ{ ĊĠĠĠĠĠĠĠ Ġif Ġ( address book List . isNotEmpty ()) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġif Ġ( adapter Address Book List Ġ== Ġnull ) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġadapter Address Book List Ġ= ĠAddress Book Adapter ( this ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠĠĠĠĠ Ġadapter Address Book List !!. setData ( address book List ) ĊĊĠĠĠĠĠĠĠĠĠĠĠ Ġbinding . rv Address List . apply Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġlayout Manager Ġ= ĠLinearLayoutManager ( context ) ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġadd Item Decoration ( Divider Item Decoration ( this @ Address Book Listing Activity , ĠRecyclerView . VERTICAL )) ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġset Has Fixed Size ( true ) ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġis Nested Scrolling Enabled Ġ= Ġfalse ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġis Focus able Ġ= Ġtrue ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġadapter Ġ= Ġadapter Address Book List ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠ Ġhandle Empty List () ĊĠĠĠ Ġ} ĊĊĊĊĠĠĠ Ġprivate Ġfun ĠgetContext (): ĠContext Ġ{ ĊĠĠĠĠĠĠĠ Ġreturn Ġthis @ Address Book Listing Activity ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġprivate Ġfun Ġhandle Empty List () Ġ{ ĊĊĠĠĠĠĠĠĠ Ġif Ġ( address book List . isEmpty ()) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġbinding . tv No List . visibility Ġ= ĠView . VISIBLE ĊĠĠĠĠĠĠĠĠĠĠĠ Ġbinding . rv Address List . visibility Ġ= ĠView . GONE ĊĠĠĠĠĠĠĠ Ġ} Ġelse Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġbinding . tv No List . visibility Ġ= ĠView . GONE ĊĠĠĠĠĠĠĠĠĠĠĠ Ġbinding . rv Address List . visibility Ġ= ĠView . VISIBLE ĊĠĠĠĠĠĠĠ Ġ} ĊĊĠĠĠ Ġ} ĊĊĠĠĠ Ġprivate Ġval Ġclick Listener Ġ= ĠView . OnClickListener Ġ{ Ġview Ġ-> ĊĠĠĠĠĠĠĠ Ġwhen Ġ( view . id ) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ ĠR . id . toolbar _ right Ġ-> Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġsave Address Book () ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ} ĊĊĠĠĠĠĠĠĠĠĠĠĠ ĠR . id . toolbar _ left Ġ-> Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġglobals ?. setUser Details ( null ) ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġglobals ?. logout Process ( getContext ()) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġprivate Ġfun Ġsave Address Book () Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġadd Book Ġ= ĠIntent ( this , ĠEdit Remove Address Book Activity :: class . java ) ĊĠĠĠĠĠĠĠ ĠstartActivity For Result ( add Book , ĠADD _ EDIT _ ADDRESS _ REQ _ CODE ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġprivate Ġfun Ġedit Address Book ( address Book : ĠAddress Book ) Ġ{ ĊĊĠĠĠĠĠĠĠ Ġval Ġedit Book Ġ= ĠIntent ( getContext (), ĠEdit Remove Address Book Activity :: class . java ) ĊĠĠĠĠĠĠĠ Ġedit Book . putExtra ( Constant . Key _ edit Address Book , Ġaddress Book Ġas ĠSerializable ) ĊĠĠĠĠĠĠĠ ĠstartActivity For Result ( edit Book , ĠADD _ EDIT _ ADDRESS _ REQ _ CODE ) ĊĊĠĠĠ Ġ} ĊĊĠĠĠ Ġoverride Ġfun Ġon Address Deleted ( address Book : ĠAddress Book ) Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġdialog Ġ= ĠAlertDialog . Builder ( this , ĠR . style . Theme Overlay _ AppCompat _ Dialog ) ĊĠĠĠĠĠĠĠ Ġdialog . setTitle (" Delete ĠAddress !! ") ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ. setMessage (" Are Ġyou Ġsure Ġyou Ġwant Ġto Ġdelete Ġthis Ġaddress ?") ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ. set Positive Button ( R . string . yes ) Ġ{ Ġ_, Ġ_ Ġ-> ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ ĠviewModel . delete Product ( address Book ) ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ}. set Negative Button ( R . string . cancel , Ġnull ). create (). show () ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġoverride Ġfun Ġon Address Edited ( address Book : ĠAddress Book ) Ġ{ ĊĠĠĠĠĠĠĠ Ġedit Address Book ( address Book ) ĊĠĠĠ Ġ} Ċ } Ċ