package Ġcom . mac is dev . m ile age app Ċ Ċ import Ġandroid . os . Bundle Ċ import Ġandroid . view . Menu Ċ import Ġandroidx . activity . view Models Ċ import Ġandroidx . appcompat . app . AppCompatActivity Ċ import Ġandroidx . core . content . Context Compat Ċ import Ġandroidx . core . graphics . Blend Mode Color Filter Compat Ċ import Ġandroidx . core . graphics . Blend Mode Compat Ċ import Ġandroidx . navigation . Nav Controller Ċ import Ġandroidx . navigation . find Nav Controller Ċ import Ġandroidx . navigation . fragment . Nav Host Fragment Ċ import Ġandroidx . navigation . ui . AppBar Configuration Ċ import Ġandroidx . navigation . ui . navigate Up Ċ import Ġandroidx . navigation . ui . setup ActionBar With Nav Controller Ċ import Ġandroidx . navigation . ui . setup With Nav Controller Ċ import Ġcom . google . firebase . analytics . Firebase Analytics Ċ import Ġcom . google . firebase . analytics . k tx . analytics Ċ import Ġcom . google . firebase . k tx . Firebase Ċ import Ġcom . mac is dev . m ile age app . databinding . Activity Main Binding Ċ import Ġcom . mac is dev . m ile age app . view Models . MainActivity ViewModel Ċ import Ġde . raph ae le b ner . room database backup . core . Room Backup Ċ Ċ class ĠMainActivity Ġ: ĠAppCompatActivity () Ġ{ Ċ ĉ comp anion Ġobject Ġ{ Ċĉ ĉ @ Suppress (" unused ") Ċĉ ĉ const Ġval ĠTAG Ġ= Ġ" M ile age App Tag " Ċ ĉ } ĊĊ ĉ private Ġlateinit Ġvar Ġgui : ĠActivity Main Binding ĊĊ ĉ private Ġlateinit Ġvar Ġfirebase Analytics : ĠFirebase Analytics ĊĊ ĉ late init Ġvar Ġroom Backup : ĠRoom Backup ĊĊ ĉ private Ġlateinit Ġvar Ġapp Bar Configuration : ĠApp Bar Configuration Ċ ĉ private Ġlateinit Ġvar Ġnav Controller : ĠNav Controller Ċ ĉ private Ġval Ġmain Activity VM : ĠMainActivity ViewModel Ġby Ġview Models () ĊĊ ĉ override Ġfun ĠonCreate ( savedInstanceState : ĠBundle ?) Ġ{ Ċĉ ĉ super . onCreate ( savedInstanceState ) Ċĉ ĉ firebase Analytics Ġ= ĠFirebase . analytics Ċĉ ĉ room Backup Ġ= ĠRoom Backup ( this ) Ċĉ ĉ gui Ġ= ĠActivity Main Binding . inflate ( layout Inflater ) Ċĉ ĉ set ContentView ( gui . root ) ĊĊĉ ĉ set Support ActionBar ( gui . toolbar ) ĊĊĉ ĉ // Drawer Ġcode Ċĉ ĉ // Get Ġthe Ġfragment Ġwhere Ġthe Ġothers Ġfragments Ġare Ġshown Ċĉ ĉ val Ġnav Host Fragment Ġ= Ġsupport FragmentManager . find Fragment ById ( R . id . nav _ host _ fragment ) Ġas ĠNav Host Fragment Ċĉ ĉ // Get Ġthe Ġcontroller Ġof Ġthe Ġnavigation Ġcomponent Ċĉ ĉ nav Controller Ġ= Ġnav Host Fragment . nav Controller Ċĉ ĉ // Bind Ġeach Ġmenu Ġitem Ġto Ġa Ġdestination Ġwith Ġthe Ġsame ĠID Ċĉ ĉ gui . nav View . setup With Nav Controller ( nav Controller ) Ċĉ ĉ // Set Ġthe Ġtop - level Ġfragment Ċĉ ĉ app Bar Configuration Ġ= ĠApp Bar Configuration ( set Of ( R . id . home Fragment ), Ġgui . drawer Layout ) Ċĉ ĉ // Bind s Ġthe Ġdrawer Ġto Ġthe Ġapp Ġbar Ċĉ ĉ setup ActionBar With Nav Controller ( nav Controller , Ġapp Bar Configuration ) ĊĊĉ ĉ gui . nav View . item Icon Tint List Ġ= Ġnull Ġ// Allow s Ġt int ing Ġof Ġvehicles Ġicons ĊĊĉ ĉ load Veh icles To Nav Drawer () Ċ ĉ } ĊĊ ĉ private Ġfun Ġload Veh icles To Nav Drawer () Ġ{ Ċĉ ĉ main Activity VM . vehicles List . observe ( this ) Ġ{ Ġvehicles List Ġ-> Ċĉĉ ĉ val Ġvehicles Menu Ġ= Ġgui . nav View . menu . getItem ( 0 ). sub Menu Ċĉĉ ĉ vehicles Menu . clear () Ċĉĉ ĉ vehicles List . forEach Ġ{ Ġvehicle Ġ-> Ċĉĉĉ ĉ val Ġicon Id Ġ= Ġresources . getIdentifier ( vehicle . icon , Ġ" drawable ", ĠpackageName ) Ċĉĉĉ ĉ val Ġcolor Id Ġ= Ġresources . getIdentifier ( vehicle . color , Ġ" color ", ĠpackageName ) ĊĊĉĉĉ ĉ val Ġicon Ġ= ĠContext Compat . get Drawable ( this , Ġicon Id ) Ċĉĉĉ ĉ icon ?. color Filter Ġ= ĠBlend Mode Color Filter Compat Ċĉĉĉĉ ĉ . create Blend Mode Color Filter Compat ( getColor ( color Id ), ĠBlend Mode Compat . SRC _ AT OP ) ĊĊĉĉĉ ĉ vehicles Menu Ċĉĉĉĉ ĉ . add ( R . id . vehicles _ group , ĠMenu . NONE , Ġ 0 , Ċĉĉĉĉĉ ĉ "${ vehicle . plate Number } Ġ- Ġ${ vehicle . maker } Ġ${ vehicle . model }") Ċĉĉĉĉ ĉ . setIcon ( icon ) Ċĉĉĉĉ ĉ . setOn MenuItem ClickListener Ġ{ Ċĉĉĉĉĉ ĉ val Ġdirections Ġ= ĠHome Fragment Directions . Ċĉĉĉĉĉ ĉ action Home Fragment To M ile age List Fragment ( vehicle . plate Number , Ġvehicle . maker , Ġvehicle . model ) Ċĉĉĉĉĉ ĉ nav Controller . navigate ( directions ) Ċĉĉĉĉĉ ĉ gui . drawer Layout . close Draw ers () Ċĉĉĉĉĉ ĉ true Ċĉĉĉĉ ĉ } Ċĉĉ ĉ } Ċĉĉ ĉ vehicles Menu Ċĉĉĉ ĉ . add ( R . id . vehicles _ group , ĠMenu . NONE , Ġ 0 , ĠgetString ( R . string . add _ vehicle )) Ċĉĉĉ ĉ . setIcon ( R . drawable . ic _ add _ circle ) Ċĉĉĉ ĉ . setOn MenuItem ClickListener Ġ{ Ċĉĉĉĉ ĉ val Ġdirections Ġ= ĠHome Fragment Directions Ċĉĉĉĉĉ ĉ . action Home Fragment ToAdd Vehicle Fragment ( false , Ġ"") Ċĉĉĉĉ ĉ nav Controller . navigate ( directions ) Ċĉĉĉĉ ĉ true Ċĉĉĉ ĉ } Ċĉ ĉ } Ċ ĉ } ĊĊ ĉ // Used Ġby Ġthe Ġnavigation UI Ġcomponent Ġ( drawer ) Ċ ĉ override Ġfun Ġon Support Navigate Up (): ĠBoolean Ġ{ Ċĉ ĉ val Ġnav Controller Ġ= Ġfind Nav Controller ( R . id . nav _ host _ fragment ) Ċĉ ĉ return Ġnav Controller . navigate Up ( app Bar Configuration ) Ġ|| Ġsuper . on Support Navigate Up () Ċ ĉ } Ċ }