package Ġcom . mor c ine k . players . ui . teams Ċ Ċ import Ġandroid . os . Bundle Ċ import Ġandroid . view . View Ċ import Ġandroidx . fragment . app . Fragment Ċ import Ġandroidx . lifecycle . LiveData Ċ import Ġandroidx . lifecycle . ViewModel Ċ import Ġandroidx . navigation . Nav Controller Ċ import Ġandroidx . recyclerview . widget . Linear LayoutManager Ċ import Ġcom . mor c ine k . players . R Ċ import Ġcom . mor c ine k . players . core .* Ċ import Ġcom . mor c ine k . players . core . data . Player Data Ċ import Ġcom . mor c ine k . players . core . data . Team Data Ċ import Ġcom . mor c ine k . players . core . database .* Ċ import Ġcom . mor c ine k . players . core . extensions .* Ċ import Ġcom . mor c ine k . players . ui . lazy Nav Controller Ċ import Ġkotlinx . android . synt hetic . main . fragment _ add _ players . view .* Ċ import Ġkotlinx . android . synt hetic . main . vh _ selectable _ player . view .* Ċ import Ġorg . k oin . android x . view model . dsl . viewModel Ċ import Ġorg . k oin . dsl . module Ċ Ċ class ĠAdd Players Fragment Ġ: ĠBase Fragment ( R . layout . fragment _ add _ players ) Ġ{ ĊĊĠĠĠ Ġprivate Ġval ĠviewModel Ġby ĠviewModel With Fragment < Add Players ViewModel >() ĊĊĠĠĠ Ġprivate Ġval Ġnav Controller Ġby Ġlazy Nav Controller () ĊĊĠĠĠ Ġoverride Ġval Ġmenu Configuration Ġ= Ġcreate Menu Configuration Ġ{ ĊĠĠĠĠĠĠĠ Ġadd Action ( R . string . add _ players , ĠR . drawable . ic _ person _ add ) Ġ{ Ġnav Controller . navigate ( R . id . nav _ create _ player ) Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġoverride Ġfun Ġon View Created ( view : ĠView , ĠsavedInstanceState : ĠBundle ?) Ġ{ ĊĠĠĠĠĠĠĠ Ġsuper . on View Created ( view , ĠsavedInstanceState ) ĊĠĠĠĠĠĠĠ Ġview . apply Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġtitle . text Ġ= ĠviewModel . team Data . name ĊĠĠĠĠĠĠĠĠĠĠĠ Ġre cyclerView . apply Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġlayout Manager Ġ= ĠLinearLayoutManager ( activity ) ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġadapter Ġ= ĠSelection ListAdapter < Player Data >( R . layout . vh _ selectable _ player , Ġitem Callback ()) Ġ{ Ġ_, Ġitem Ġ-> ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġname . text Ġ= Ġ"$ item " ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ}. apply Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġselected Items Ġ= ĠviewModel . selected Players . value !! ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġobserve ( viewModel . players ) Ġ{ Ġsubmit List ( it ) Ġ} ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġon Selected Items Changed Ġ{ ĠviewModel . selected Players . post Value ( it ) Ġ} ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠĠĠĠĠ Ġnext Button . apply Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġobserve ( viewModel . is Next Enabled ) Ġ{ Ġis Enabled Ġ= Ġit Ġ} ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġset OnClickListener Ġ{ ĠviewModel . add Players To Team Ġ{ Ġ} Ġ} ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} Ċ } Ċ Ċ private Ġclass ĠAdd Players ViewModel ( val Ġreferences : ĠFirebase References , Ġval Ġteam Data : ĠTeam Data ) Ġ: ĠViewModel () Ġ{ ĊĊĠĠĠ Ġval Ġselected Players Ġ= Ġmutable SetValue LiveData < Player Data >() ĊĊĠĠĠ Ġfun Ġadd Players To Team ( do On Complete : Ġ() Ġ-> ĠUnit ) Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġchild Updates Ġ= Ġselected Players . value !!. map Ġ{ Ġ"${ it . key }/ team Key " Ġto Ġteam Data . key Ġ}. toMap () ĊĠĠĠĠĠĠĠ Ġreferences . players Reference (). update Children ( child Updates ). add On Complete Listener Ġ{ Ġdo On Complete () Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġval Ġplayers Ġ= Ġreferences . players Without Team LiveData ForValue Listener () ĊĊĠĠĠ Ġval Ġis Next Enabled : ĠLive Data < Boolean > Ġ= Ġselected Players . map Ġ{ Ġit . isNotEmpty () Ġ} Ċ } Ċ Ċ val Ġadd Players Module Ġ= Ġmodule Ġ{ ĊĠĠĠ ĠviewModel Ġ{ Ġ( fragment : ĠFragment ) Ġ-> ĠAdd Players ViewModel ( get (), Ġfragment . get Parcelable ()) Ġ} Ċ } Ċ