package Ġcom . apple object . room demo Ċ Ċ import Ġandroid . os . Bundle Ċ import Ġandroid . util . Log Ċ import Ġandroid . widget . Toast Ċ import Ġandroidx . appcompat . app . AppCompatActivity Ċ import Ġandroidx . databinding . Data Binding Util Ċ import Ġandroidx . lifecycle . ViewModel Provider Ċ import Ġandroidx . recyclerview . widget . Linear LayoutManager Ċ import Ġcom . apple object . room demo . databinding . Activity Main Binding Ċ import Ġcom . apple object . room demo . db . Subscriber Ċ import Ġcom . apple object . room demo . db . Subscriber Database Ċ import Ġcom . apple object . room demo . db . Subscriber Repository Ċ import Ġcom . apple object . room demo . view model . Subscriber ViewModel Ċ import Ġcom . apple object . room demo . view model . Subscriber ViewModel Factory Ċ Ċ class ĠMainActivity Ġ: ĠAppCompatActivity () Ġ{ ĊĊĠĠĠ Ġlateinit Ġvar Ġbinding : ĠActivity Main Binding ĊĠĠĠ Ġlateinit Ġvar Ġsubscriber ViewModel : ĠSubscriber ViewModel ĊĠĠĠ Ġlateinit Ġvar Ġadapter : ĠMy RecyclerView Adapter ĊĊĊĠĠĠ Ġoverride Ġfun ĠonCreate ( savedInstanceState : ĠBundle ?) Ġ{ ĊĠĠĠĠĠĠĠ Ġsuper . onCreate ( savedInstanceState ) ĊĠĠĠĠĠĠĠ ĠsetContentView ( R . layout . activity _ main ) ĊĊĠĠĠĠĠĠĠ Ġbinding Ġ= ĠData Binding Util . set ContentView ( this , ĠR . layout . activity _ main ) ĊĊĠĠĠĠĠĠĠ Ġval Ġdao Ġ= ĠSubscriber Database . getInstance ( application ). subscriber DAO ĊĊĠĠĠĠĠĠĠ Ġval Ġrepository Ġ= ĠSubscriber Repository ( dao ) ĊĊĠĠĠĠĠĠĠ Ġval Ġfactory Ġ= ĠSubscriber ViewModel Factory ( repository ) ĊĊĠĠĠĠĠĠĠ Ġsubscriber ViewModel Ġ= ĠViewModel Provider ( this , Ġfactory ). get ( Subscriber ViewModel :: class . java ) ĊĊĠĠĠĠĠĠĠ Ġbinding . my ViewModel Ġ= Ġsubscriber ViewModel ĊĠĠĠĠĠĠĠ Ġbinding . lifecycle Owner Ġ= Ġthis ĊĊĠĠĠĠĠĠĠ Ġinit RecyclerView () ĊĠĠĠĠĠĠĠ Ġsubscriber ViewModel . message . observe ( this , Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġit . getContent IfNot Handled ()?. let Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ ĠToast . makeText ( this , Ġit , ĠToast . LENGTH _ LONG ). show () ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠ Ġ}) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġprivate Ġfun Ġinit RecyclerView () Ġ{ ĊĠĠĠĠĠĠĠ Ġbinding . subscriber RecyclerView . layout Manager Ġ= ĠLinearLayoutManager ( this ) ĊĠĠĠĠĠĠĠ Ġadapter Ġ= ĊĠĠĠĠĠĠĠĠĠĠĠ ĠMy RecyclerView Adapter Ġ{ Ġselected Item : ĠSubscriber Ġ-> Ġlist Item Clicked ( selected Item ) Ġ} ĊĠĠĠĠĠĠĠ Ġbinding . subscriber RecyclerView . adapter Ġ= Ġadapter ĊĠĠĠĠĠĠĠ Ġdisplay ListOf Sub scribers () ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġprivate Ġfun Ġdisplay ListOf Sub scribers () Ġ{ ĊĠĠĠĠĠĠĠ Ġsubscriber ViewModel . subscribers . observe ( this , Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ ĠLog . i (" MY TAG ", Ġit . toString ()) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġadapter . set List ( it ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġadapter . notify DataSet Changed () ĊĊĠĠĠĠĠĠĠ Ġ}) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġprivate Ġfun Ġlist Item Clicked ( subscriber : ĠSubscriber ) Ġ{ ĊĠĠĠĠĠĠĠ Ġ// ĠToast . makeText ( this , Ġ" Selected Ġitem Ġ: Ġ${ subscriber . name }", ĠToast . LENGTH _ LONG ). show () ĊĠĠĠĠĠĠĠ Ġsubscriber ViewModel . init Update And Delete ( subscriber ) ĊĠĠĠ Ġ} Ċ }