package Ġcom . example . re stop ass . main . common . restaurant . comment Ċ Ċ import Ġandroid . os . Build Ċ import Ġandroid . os . Bundle Ċ import Ġandroid . text . Html Ċ import Ġandroid . view . LayoutInflater Ċ import Ġandroid . view . View Ċ import Ġandroid . view . ViewGroup Ċ import Ġandroid . widget . Adapter View Ċ import Ġandroid . widget . Array Adapter Ċ import Ġandroidx . annotation . Requires Api Ċ import Ġandroidx . fragment . app . Fragment Ċ import Ġandroidx . fragment . app . Fragment Transaction Ċ import Ġandroidx . lifecycle . ViewModel Provider Ċ import Ġandroidx . recyclerview . widget . Linear LayoutManager Ċ import Ġandroidx . recyclerview . widget . RecyclerView Ċ import Ġcom . bump tech . glide . G lide Ċ import Ġcom . example . re stop ass . R Ċ import Ġcom . example . re stop ass . domain . Restaurant ViewModel Ċ import Ġcom . example . re stop ass . main . MainActivity Ċ import Ġkotlinx . android . synt hetic . main . activity _ main .* Ċ import Ġkotlinx . android . synt hetic . main . fragment _ comment .* Ċ import Ġjava . time . LocalDate Ċ import Ġjava . time . LocalDateTime Ċ import Ġkotlin . math . round ToInt ĊĊ Ċ class ĠComments Fragment Ġ: ĠFragment (), ĠAdapter View . On ItemSelected Listener Ġ{ ĊĠĠĠ Ġprivate Ġlateinit Ġvar Ġre cyclerView : ĠRecyclerView ĊĠĠĠ Ġprivate Ġlateinit Ġvar Ġcomment Adapter : ĠComment Adapter ĊĊĠĠĠ Ġprivate Ġlateinit Ġvar Ġrestaurant ViewModel : ĠRest aurant ViewModel ĊĊĊĠĠĠ Ġoverride Ġfun ĠonCreate View ( ĊĠĠĠĠĠĠĠ Ġinflater : ĠLayoutInflater , ĊĠĠĠĠĠĠĠ Ġcontainer : ĠViewGroup ?, ĊĠĠĠĠĠĠĠ ĠsavedInstanceState : ĠBundle ? ĊĠĠĠ Ġ): ĠView ? Ġ{ ĊĠĠĠĠĠĠĠ Ġreturn Ġinflater . inflate ( R . layout . fragment _ comment , Ġcontainer , Ġfalse ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Requires Api ( Build . VERSION _ CODES . N ) ĊĠĠĠ Ġoverride Ġfun Ġon View Created ( view : ĠView , ĠsavedInstanceState : ĠBundle ?) Ġ{ ĊĠĠĠĠĠĠĠ Ġsuper . on View Created ( view , ĠsavedInstanceState ) ĊĊĠĠĠĠĠĠĠ Ġrestaurant ViewModel Ġ= ĠViewModel Provider ( require Activity ()). get ( Restaurant ViewModel :: class . java ) ĊĊĠĠĠĠĠĠĠ Ġcomment Adapter Ġ= ĠComment Adapter ( restaurant ViewModel . restaurant . comments !! ) ĊĠĠĠĠĠĠĠ Ġcomment Adapter . notify DataSet Changed () ĊĊĠĠĠĠĠĠĠ Ġre cyclerView Ġ= Ġcomments RecyclerView . apply Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġlayout Manager Ġ= ĠLinearLayoutManager ( this . context ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġadapter Ġ= Ġcomment Adapter ĊĠĠĠĠĠĠĠ Ġ} ĊĊĠĠĠĠĠĠĠ Ġval Ġrating Ġ= Ġrestaurant ViewModel . restaurant . comments !!. map Ġ{ Ġit . restaurant St ars Ġ}. sum () Ġ/ Ġrestaurant ViewModel . restaurant . comments !!. size ĊĠĠĠĠĠĠĠ Ġval Ġrating Text Ġ= Ġresources . getString Array ( R . array . comments Rating Descriptions )[ rating . round ToInt () Ġ- Ġ 1 ] ĊĠĠĠĠĠĠĠ Ġval Ġbest Plate Ġ Ġ= Ġrestaurant ViewModel . restaurant . comments !!. groupBy Ġ{ Ġit . dish . name Ġ} ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. map Values Ġ{ Ġit . value . map Ġ{ Ġit . dish St ars Ġ}. average ()}. max By Ġ{ Ġit . value Ġ} ?. key ĊĠĠĠĠĠĠĠ Ġval Ġmost Order Plate Ġ= Ġrestaurant ViewModel . restaurant . comments !!. groupBy Ġ{ Ġit . dish . name Ġ}. map Values Ġ{ Ġit . value . size Ġ}. max By Ġ{ Ġit . value Ġ} ?. key ĊĊĠĠĠĠĠĠĠ Ġview . apply Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ ĠG lide . with ( this ). load ( restaurant ViewModel . restaurant . img ). into ( comments Restaurant Img ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġcomments Restaurant Title . text Ġ= Ġrestaurant ViewModel . restaurant . name ĊĠĠĠĠĠĠĠĠĠĠĠ Ġcomments Title . text Ġ= Ġcontext . getString ( R . string . comments Title ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġcomments Subtitle . text Ġ= ĠHtml . from Html ( context . getString ( R . string . comments Subtitle , rating Text , restaurant ViewModel . restaurant . comments !!. size . toString ())) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġcomments Rating . text Ġ= ĠString . format ("% . 1 f ", Ġrating ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġcomments Best Plate . text Ġ= Ġbest Plate ĊĠĠĠĠĠĠĠĠĠĠĠ Ġcomments Most Order Plate . text Ġ= Ġmost Order Plate ĊĠĠĠĠĠĠĠĠĠĠĠ Ġcomments Progress Circle . set Progress ( apply Spinner Range ( rating ). toInt (), Ġtrue ) ĊĊĠĠĠĠĠĠĠĠĠĠĠ ĠArray Adapter . createFrom Resource ( ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġcontext , ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ ĠR . array . comments Spinner List , ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġandroid . R . layout . simple _ spinner _ item ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ). also Ġ{ Ġadapter Ġ-> ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġadapter . set DropDown View Resource ( android . R . layout . simple _ spinner _ dropdown _ item ) ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġcomments Spinner . adapter Ġ= Ġadapter ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ} ĊĊĠĠĠĠĠĠĠĠĠĠĠ Ġcomments Spinner . on ItemSelected Listener Ġ= Ġthis @ Comments Fragment ĊĊĠĠĠĠĠĠĠ Ġ} ĊĊĠĠĠĠĠĠĠ Ġ( activity Ġas ĠMainActivity ). main Back Button . visibility Ġ= ĠView . VISIBLE ĊĊĠĠĠ Ġ} ĊĊĠĠĠ Ġoverride Ġfun Ġon Nothing Selected ( parent : ĠAdapter View < *> ?) Ġ{ ĊĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Requires Api ( Build . VERSION _ CODES . O ) ĊĠĠĠ Ġoverride Ġfun Ġon ItemSelected ( parent : ĠAdapter View <* >, Ġview : ĠView ?, Ġpos : ĠInt , Ġid : ĠLong ) Ġ{ ĊĠĠĠĠĠĠĠ Ġwhen ( pos ) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ 0 Ġ-> Ġcomment Adapter . comments Ġ= Ġrestaurant ViewModel . restaurant . comments !!. sorted By Ġ{ ĠLocalDate . parse ( it . date ) Ġ}. reversed () ĊĠĠĠĠĠĠĠĠĠĠĠĠ 1 Ġ-> Ġcomment Adapter . comments Ġ= Ġrestaurant ViewModel . restaurant . comments !!. sorted By Ġ{ ĠLocalDate . parse ( it . date ) Ġ} ĊĠĠĠĠĠĠĠĠĠĠĠĠ 2 Ġ-> Ġcomment Adapter . comments Ġ= Ġrestaurant ViewModel . restaurant . comments !!. sorted By Ġ{ Ġit . dish St ars Ġ}. reversed () ĊĠĠĠĠĠĠĠĠĠĠĠĠ 3 Ġ-> Ġcomment Adapter . comments Ġ= Ġrestaurant ViewModel . restaurant . comments !!. sorted By Ġ{ Ġit . restaurant St ars Ġ}. reversed () ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠ Ġcomment Adapter . notify DataSet Changed () ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġprivate Ġfun Ġapply Spinner Range ( oldValue : ĠFloat ) Ġ: ĠFloat Ġ{ ĊĠĠĠĠĠĠĠ Ġreturn ĠoldValue Ġ* Ġ 2 0 ĊĠĠĠ Ġ} Ċ }