/* Ċ Ġ* ĠMIT ĠLicense Ċ Ġ* Ċ Ġ* ĠCopyright Ġ( c ) Ġ 2 0 1 9 ĠA det un ji ĠD ahun si Ċ Ġ* Ċ Ġ* ĠPermission Ġis Ġhereby Ġgranted , Ġfree Ġof Ġcharge , Ġto Ġany Ġperson Ġobtaining Ġa Ġcopy Ċ Ġ* Ġof Ġthis Ġsoftware Ġand Ġassociated Ġdocumentation Ġfiles Ġ( the Ġ" Software "), Ġto Ġdeal Ċ Ġ* Ġin Ġthe ĠSoftware Ġwithout Ġrestriction , Ġincluding Ġwithout Ġlimitation Ġthe Ġrights Ċ Ġ* Ġto Ġuse , Ġcopy , Ġmodify , Ġmerge , Ġpublish , Ġdistribute , Ġsublicense , Ġand / or Ġsell Ċ Ġ* Ġcopies Ġof Ġthe ĠSoftware , Ġand Ġto Ġpermit Ġpersons Ġto Ġwhom Ġthe ĠSoftware Ġis Ċ Ġ* Ġfurnished Ġto Ġdo Ġso , Ġsubject Ġto Ġthe Ġfollowing Ġconditions : Ċ Ġ* Ċ Ġ* ĠThe Ġabove Ġcopyright Ġnotice Ġand Ġthis Ġpermission Ġnotice Ġshall Ġbe Ġincluded Ġin Ġall Ċ Ġ* Ġcopies Ġor Ġsubstantial Ġportions Ġof Ġthe ĠSoftware . Ċ Ġ* Ċ Ġ* ĠTHE ĠSOFTWARE ĠIS ĠPROVIDED Ġ" AS ĠIS ", ĠWITHOUT ĠWARRANTY ĠOF ĠANY ĠKIND , ĠEXPRESS ĠOR Ċ Ġ* ĠIMPLIED , ĠINCLUDING ĠBUT ĠNOT ĠLIMITED ĠTO ĠTHE ĠWARRANTIES ĠOF ĠMERCHANTABILITY , Ċ Ġ* ĠFITNESS ĠFOR ĠA ĠPARTICULAR ĠPURPOSE ĠAND ĠNONINFRINGEMENT . ĠIN ĠNO ĠEVENT ĠSHALL ĠTHE Ċ Ġ* ĠAUTHORS ĠOR ĠCOPYRIGHT ĠHOLDERS ĠBE ĠLIABLE ĠFOR ĠANY ĠCLAIM , ĠDAMAGES ĠOR ĠOTHER Ċ Ġ* ĠLIABILITY , ĠWHETHER ĠIN ĠAN ĠACTION ĠOF ĠCONTRACT , ĠTORT ĠOR ĠOTHERWISE , ĠARISING ĠFROM , Ċ Ġ* ĠOUT ĠOF ĠOR ĠIN ĠCONNECTION ĠWITH ĠTHE ĠSOFTWARE ĠOR ĠTHE ĠUSE ĠOR ĠOTHER ĠDEALINGS ĠIN ĠTHE Ċ Ġ* ĠSOFTWARE . Ċ Ġ*/ Ċ Ċ package Ġcom . main street code . team mate . view model Ċ Ċ import Ġandroidx . recyclerview . widget . Diff Util Ċ import Ġcom . main street code . team mate . model . Game Ċ import Ġcom . main street code . team mate . model . Stat Ċ import Ġcom . main street code . team mate . model . Stat Aggregate Ċ import Ġcom . main street code . team mate . model . Team Ċ import Ġcom . main street code . team mate . model . User Ċ import Ġcom . main street code . team mate . repository . Repo Provider Ċ import Ġcom . main street code . team mate . repository . Stat Repo Ċ import Ġcom . main street code . team mate . repository . User Repo Ċ import Ġcom . main street code . team mate . rest . Team mate Service Ċ import Ġcom . main street code . team mate . util . Functional Diff Ċ import Ġcom . main street code . team mate . util . replace List Ċ import Ġcom . main street code . team mate . view model . events . Alert Ċ import Ġcom . main street code . team mate . view model . g of ers . Stat Go fer Ċ import Ġcom . tun jid . android x . recyclerview . diff . Different iable Ċ import Ġio . reactivex . Flow able Ċ import Ġio . reactivex . Single Ċ import Ġio . reactivex . android . schedul ers . Android Schedulers . main Thread Ċ import Ġjava . util .* Ċ Ċ /** Ċ Ġ* ĠViewModel Ġfor Ġ[ tournament s ][ Stat ] Ċ Ġ*/ Ċ Ċ class ĠStat ViewModel Ġ: ĠM apped ViewModel < Game , ĠStat >() Ġ{ ĊĊĠĠĠ Ġprivate Ġval Ġapi Ġ= ĠTeam mate Service . get Api Instance () ĊĊĠĠĠ Ġprivate Ġval Ġmodel List Map Ġ= ĠHashMap < String , ĠMutable List < Different iable >>() ĊĠĠĠ Ġval Ġstat Ag greg ates : ĠMutable List < Different iable > Ġ= ĠArrayList () ĊĊĠĠĠ Ġprivate Ġval Ġrepository : ĠStat Repo Ġ= ĠRepo Provider . for Repo ( Stat Repo :: class . java ) ĊĊĠĠĠ Ġfun Ġgo fer ( stat : ĠStat ): ĠStat Go fer Ġ{ ĊĠĠĠĠĠĠĠ Ġval ĠonError Ġ= Ġ{ Ġthrowable : ĠThrowable Ġ-> Ġcheck For Invalid Object ( throwable , Ġstat , Ġstat . game ) Ġ} ĊĠĠĠĠĠĠĠ Ġval Ġuser Function Ġ= Ġ{ Ġ_: ĠTeam Ġ-> ĠRepo Provider . for Repo ( User Repo :: class . java ). currentUser Ġ} ĊĠĠĠĠĠĠĠ Ġval Ġel igible Team Source Ġ= Ġ{ Ġ_: ĠStat Ġ-> ĠGame ViewModel . get El igible Teams For Game ( stat . game ) Ġ} ĊĠĠĠĠĠĠĠ Ġreturn ĠStat Go fer ( ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġstat , ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ ĠonError , ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġuser Function , ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġrepository :: get , ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġrepository :: create OrUpdate , ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġthis :: delete , ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġel igible Team Source ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġoverride Ġfun Ġvalue Class (): ĠClass < Stat > Ġ= ĠStat :: class . java ĊĊĠĠĠ Ġoverride Ġfun Ġfetch ( key : ĠGame , Ġfetch Latest : ĠBoolean ): ĠFlow able < List < Stat >> Ġ= ĊĠĠĠĠĠĠĠĠĠĠĠ Ġrepository . models Before ( key , Ġget Query Date ( fetch Latest , Ġkey ) Ġ{ Ġit . created Ġ}) ĊĊĠĠĠ Ġoverride Ġfun ĠgetModel List ( key : ĠGame ): ĠMutable List < Different iable > Ġ= ĊĠĠĠĠĠĠĠĠĠĠĠ Ġmodel List Map . get Or Put ( key . id ) Ġ{ Ġmutable ListOf () Ġ} ĊĊĠĠĠ Ġoverride Ġfun Ġon Invalid Key ( key : ĠGame ) Ġ{ ĊĠĠĠĠĠĠĠ Ġsuper . on Invalid Key ( key ) ĊĠĠĠĠĠĠĠ Ġpush Model Alert ( Alert . deletion ( key )) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġfun Ġdelete ( stat : ĠStat ): ĠSingle < Stat > Ġ= ĊĠĠĠĠĠĠĠĠĠĠĠ Ġrepository . delete ( stat ). do On Success Ġ{ Ġdeleted Ġ-> ĠgetModel List ( stat . game ). remove ( deleted ) Ġ} ĊĊĠĠĠ Ġfun Ġcan Edit Game Stats ( game : ĠGame ): ĠSingle < Boolean > Ġ= ĊĠĠĠĠĠĠĠĠĠĠĠ Ġis Priv ileged In Game ( game ). map Ġ{ Ġis Priv ileged Ġ-> Ġis Priv ileged Or Is Ref ere e ( is Priv ileged , ĠRepo Provider . for Repo ( User Repo :: class . java ). currentUser , Ġgame ) Ġ} ĊĊĠĠĠ Ġfun Ġis Priv ileged In Game ( game : ĠGame ): ĠSingle < Boolean > Ġ= Ġwhen Ġ{ ĊĠĠĠĠĠĠĠ Ġgame . between Users () Ġ-> ĠSingle . just ( game . is Comp eting ( Repo Provider . for Repo ( User Repo :: class . java ). currentUser )) ĊĠĠĠĠĠĠĠ Ġelse Ġ-> ĠGame ViewModel . get El igible Teams For Game ( game ). count (). map Ġ{ Ġvalue Ġ-> Ġvalue Ġ> Ġ 0 Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġfun Ġaggregate ( request : ĠStat Aggregate . Request ): ĠSingle < Diff Util . Diff Result > Ġ= ĠFunctional Diff . of ( ĊĠĠĠĠĠĠĠĠĠĠĠ Ġapi . stats Aggregate ( request ). map ( Stat Aggregate . Result :: get Ag greg ates ), ĊĠĠĠĠĠĠĠĠĠĠĠ Ġstat Ag greg ates , ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ:: replace List ĊĠĠĠ Ġ). observe On ( main Thread ()) ĊĊĠĠĠ Ġprivate Ġfun Ġis Priv ileged Or Is Ref ere e ( is Priv ileged : ĠBoolean , Ġcurrent : ĠUser , Ġgame : ĠGame ): ĠBoolean Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġref ere e Ġ= Ġgame . ref ere e ĊĠĠĠĠĠĠĠ Ġreturn Ġ( if Ġ( ref ere e . isEmpty ) Ġis Priv ileged Ġelse Ġcurrent Ġ== Ġref ere e ) Ġ&& Ġ! game . compet itors Not Accepted () ĊĠĠĠ Ġ} Ċ } Ċ