package Ġcom . example . flux code . ui . post Explorer Ċ Ċ import Ġandroid . app . Application Ċ import Ġandroid . os . Bundle Ċ import Ġandroid . view . LayoutInflater Ċ import Ġandroid . view . View Ċ import Ġandroid . view . ViewGroup Ċ import Ġandroidx . databinding . Data Binding Util Ċ import Ġandroidx . fragment . app . Fragment Ċ import Ġandroidx . lifecycle . Observer Ċ import Ġandroidx . lifecycle . ViewModel Ċ import Ġandroidx . lifecycle . ViewModel Provider Ċ import Ġandroidx . lifecycle . ViewModel Providers Ċ import Ġandroidx . navigation . fragment . find Nav Controller Ċ import Ġandroidx . recyclerview . widget . Grid LayoutManager Ċ import Ġcom . example . flux code . R Ċ import Ġcom . example . flux code . databinding . Fragment Post Explorer Binding Ċ import Ġcom . example . flux code . domain . Board Ċ import Ġcom . example . flux code . ui . home . Home Post ListAdapter Ċ import Ġcom . example . flux code . ui . home . Home Post List Listener Ċ Ċ class ĠPost Explorer Fragment Ġ: ĠFragment () Ġ{ ĊĠĠĠ Ġprivate Ġlateinit Ġvar Ġpost Explorer ViewModel : ĠPost Explorer ViewModel ĊĊĠĠĠ Ġoverride Ġfun ĠonCreate View ( in flater : ĠLayoutInflater , Ġcontainer : ĠViewGroup ?, ĠsavedInstanceState : ĠBundle ?): ĠView ? Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġbinding : ĠFragment Post Explorer Binding Ġ= ĠData Binding Util . inflate ( in flater , ĠR . layout . fragment _ post _ explorer , Ġcontainer , Ġfalse ) ĊĠĠĠĠĠĠĠ Ġval Ġapplication Ġ= Ġrequire NotNull ( this . activity ). application ĊĠĠĠĠĠĠĠ Ġval Ġboard Ġ= ĠPost Explorer Fragment Args . from Bundle ( arguments !! ). board ĊĊĠĠĠĠĠĠĠ Ġval Ġvm f Ġ= ĠPost Explorer ViewModel Factory ( application , Ġboard ) ĊĠĠĠĠĠĠĠ Ġpost Explorer ViewModel Ġ= ĠViewModel Providers . of ( this , Ġvm f ). get ( Post Explorer ViewModel :: class . java ) ĊĠĠĠĠĠĠĠ Ġbinding . viewModel Ġ= Ġpost Explorer ViewModel ĊĊĠĠĠĠĠĠĠ Ġ// Ġre cycler view ĊĠĠĠĠĠĠĠ Ġval Ġmanager Ġ= ĠGrid LayoutManager ( activity , Ġ 1 ) ĊĠĠĠĠĠĠĠ Ġbinding . post Explorer List . layout Manager Ġ= Ġmanager ĊĠĠĠĠĠĠĠ Ġval Ġadapter Ġ= ĠHome Post ListAdapter ( Home Post List Listener ( ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ_ view Post Ġ= Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġpost Explorer ViewModel . view Post ( it ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ}, ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ_ like Post Ġ= Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġpost Explorer ViewModel . like Post ( it ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠ Ġ)) ĊĠĠĠĠĠĠĠ Ġbinding . post Explorer List . adapter Ġ= Ġadapter ĊĠĠĠĠĠĠĠ Ġpost Explorer ViewModel . posts . observe ( view Lifecycle Owner , ĠObserver Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġadapter . submit List ( it . sorted With ( compare By ({ Ġp Ġ-> Ġ- p . likes Ġ}, Ġ{ p Ġ-> Ġ- p . no Comments } ))) ĊĠĠĠĠĠĠĠ Ġ}) ĊĊĠĠĠĠĠĠĠ Ġ// Ġsafe Args Ġnavigation ĊĠĠĠĠĠĠĠ Ġpost Explorer ViewModel . post . observe ( view Lifecycle Owner , ĠObserver Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġif ( it Ġ!= Ġnull ) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġthis . find Nav Controller (). navigate ( Post Explorer Fragment Directions . action Post Explorer Fragment To Post Fragment ( it )) ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġpost Explorer ViewModel . on Navig ated () ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠ Ġ}) ĊĊĠĠĠĠĠĠĠ Ġbinding . lifecycle Owner Ġ= Ġthis ĊĠĠĠĠĠĠĠ Ġreturn Ġbinding . root ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Suppress (" UN CHECK ED _ CAST ") ĊĠĠĠ Ġinternal Ġclass ĠPost Explorer ViewModel Factory ( val Ġapp : ĠApplication , Ġval Ġboard : ĠBoard ) Ġ: ĠViewModel Provider . New Instance Factory () Ġ{ ĊĠĠĠĠĠĠĠ Ġoverride Ġfun Ġ< T Ġ: ĠViewModel ?> Ġcreate ( model Class : ĠClass < T >): ĠT Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġif Ġ( model Class . isAssignableFrom ( Post Explorer ViewModel :: class . java )) Ġreturn ĠPost Explorer ViewModel ( app , Ġboard ) Ġas ĠT ĊĠĠĠĠĠĠĠĠĠĠĠ Ġthrow ĠIllegalArgumentException (" Unknown ĠViewModel Ġclass ") ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} Ċ }