package Ġcom . example . click Ċ Ċ import Ġandroid . os . Bundle Ċ import Ġandroid . text . format . Date Utils Ċ import Ġandroid . view . LayoutInflater Ċ import Ġandroid . view . View Ċ import Ġandroid . view . ViewGroup Ċ import Ġandroidx . core . content . Context Compat Ċ import Ġandroidx . databinding . Data Binding Util Ċ import Ġandroidx . fragment . app . Fragment Ċ import Ġandroidx . lifecycle . Observer Ċ import Ġandroidx . lifecycle . ViewModel Provider Ċ import Ġandroidx . navigation . fragment . find Nav Controller Ċ import Ġcom . example . click . databinding . Fragment Game Binding Ċ Ċ class ĠGame Fragment Ġ: ĠFragment () Ġ{ ĊĠĠĠ Ġprivate Ġlateinit Ġvar Ġbinding : ĠFragment Game Binding ĊĠĠĠ Ġprivate Ġlateinit Ġvar ĠviewModel : ĠGame ViewModel ĊĊĠĠĠ Ġoverride Ġfun ĠonCreate View ( ĊĠĠĠĠĠĠĠ Ġinflater : ĠLayoutInflater , Ġcontainer : ĠViewGroup ?, ĊĠĠĠĠĠĠĠ ĠsavedInstanceState : ĠBundle ? ĊĠĠĠ Ġ): ĠView ? Ġ{ ĊĠĠĠĠĠĠĠ Ġbinding Ġ= ĠData Binding Util . inflate ( in flater , ĠR . layout . fragment _ game , Ġcontainer , Ġfalse ) ĊĠĠĠĠĠĠĠ ĠviewModel Ġ= ĠViewModel Provider ( this ). get ( Game ViewModel :: class . java ) ĊĊĠĠĠĠĠĠĠ Ġ// Set Ġup Ġob servers ĊĠĠĠĠĠĠĠ ĠviewModel . currentTime . observe ( view Lifecycle Owner , ĠObserver Ġ{ Ġtime Ġ-> Ġbinding . game Fragment Tv Timer . text Ġ= ĠDate Utils . format Elapsed Time ( time ) Ġ}) ĊĠĠĠĠĠĠĠ ĠviewModel . current Button . observe ( view Lifecycle Owner , ĠObserver Ġ{ Ġbutton Ġ-> Ġmove Button ( button ) Ġ}) ĊĠĠĠĠĠĠĠ ĠviewModel . score . observe ( view Lifecycle Owner , ĠObserver Ġ{ Ġscore Ġ-> Ġbinding . game Fragment Tv Score . text Ġ= Ġscore . toString () }) ĊĠĠĠĠĠĠĠ ĠviewModel . score Color . observe ( view Lifecycle Owner , ĠObserver Ġ{ Ġcolor Ġ-> Ġchange Score Color ( color ) Ġ}) ĊĠĠĠĠĠĠĠ ĠviewModel . game Finished . observe ( view Lifecycle Owner , ĠObserver Ġ{ Ġis Finished Ġ-> Ġif Ġ( is Finished ) Ġgame Over () Ġ}) ĊĊĠĠĠĠĠĠĠ Ġ// Set Ġclick Ġlisteners ĊĠĠĠĠĠĠĠ Ġbinding . game Fragment Bt 1 . setOnClickListener Ġ{ ĠviewModel . gain Point () Ġ} ĊĠĠĠĠĠĠĠ Ġbinding . game Fragment Bt 2 . setOnClickListener Ġ{ ĠviewModel . gain Point () Ġ} ĊĠĠĠĠĠĠĠ Ġbinding . game Fragment Bt 3 . setOnClickListener Ġ{ ĠviewModel . gain Point () Ġ} ĊĠĠĠĠĠĠĠ Ġbinding . game Fragment Bt 4 . setOnClickListener Ġ{ ĠviewModel . gain Point () Ġ} ĊĠĠĠĠĠĠĠ Ġbinding . game Fragment Lo Game . setOnClickListener { ĠviewModel . lose Point ()} ĊĊĠĠĠĠĠĠĠ Ġ// ĠInf late Ġthe Ġlayout Ġfor Ġthis Ġfragment ĊĠĠĠĠĠĠĠ Ġreturn Ġbinding . root ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ// Only Ġshow Ġa Ġgiven Ġbutton ĊĠĠĠ Ġprivate Ġfun Ġmove Button ( button : ĠInt ){ ĊĠĠĠĠĠĠĠ Ġbinding . game Fragment Bt 1 . visibility Ġ= ĠView . IN VISIBLE ĊĠĠĠĠĠĠĠ Ġbinding . game Fragment Bt 2 . visibility Ġ= ĠView . IN VISIBLE ĊĠĠĠĠĠĠĠ Ġbinding . game Fragment Bt 3 . visibility Ġ= ĠView . IN VISIBLE ĊĠĠĠĠĠĠĠ Ġbinding . game Fragment Bt 4 . visibility Ġ= ĠView . IN VISIBLE ĊĊĠĠĠĠĠĠĠ Ġwhen ( button ){ ĊĠĠĠĠĠĠĠĠĠĠĠĠ 1 Ġ-> Ġbinding . game Fragment Bt 1 . visibility Ġ= ĠView . VISIBLE ĊĠĠĠĠĠĠĠĠĠĠĠĠ 2 Ġ-> Ġbinding . game Fragment Bt 2 . visibility Ġ= ĠView . VISIBLE ĊĠĠĠĠĠĠĠĠĠĠĠĠ 3 Ġ-> Ġbinding . game Fragment Bt 3 . visibility Ġ= ĠView . VISIBLE ĊĠĠĠĠĠĠĠĠĠĠĠ Ġelse Ġ-> Ġbinding . game Fragment Bt 4 . visibility Ġ= ĠView . VISIBLE ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ// Change Ġthe Ġscore Ġcolor ĊĠĠĠ Ġprivate Ġfun Ġchange Score Color ( color : ĠString ){ ĊĠĠĠĠĠĠĠ Ġif Ġ( color Ġ== Ġ" purple "){ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġbinding . game Fragment Tv Score . setTextColor ( Context Compat . getColor ( require Context (). application Context , ĠR . color . purple _ 7 0 0 )) ĊĠĠĠĠĠĠĠ Ġ} Ġelse Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġbinding . game Fragment Tv Score . setTextColor ( Context Compat . getColor ( require Context (). application Context , ĠR . color . my _ red )) ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ// End Ġthe Ġgame ĊĠĠĠ Ġprivate Ġfun Ġgame Over (){ ĊĠĠĠĠĠĠĠ Ġfind Nav Controller (). navigate ( Game Fragment Directions . action Game Fragment To Score Fragment ( viewModel . score . value !! )) ĊĠĠĠ Ġ} Ċ }