package Ġcom . by ted ance . scene demo . architecture _ patterns . mv vm Ċ Ċ import Ġandroid . arch . lifecycle . Mutable LiveData Ċ import Ġandroid . arch . lifecycle . Observer Ċ import Ġandroid . arch . lifecycle . ViewModel Ċ import Ġandroid . os . Bundle Ċ import Ġandroid . view . Gravity Ċ import Ġandroid . view . LayoutInflater Ċ import Ġandroid . view . View Ċ import Ġandroid . view . ViewGroup Ċ import Ġandroid . widget . Button Ċ import Ġandroid . widget . Frame Layout Ċ import Ġandroid . widget . TextView Ċ import Ġcom . by ted ance . scene . Scene Ċ import Ġcom . by ted ance . scene . group . Group Scene Ċ import Ġcom . by ted ance . scene . k tx . activity ViewModels ĊĊ Ċ class ĠViewModel Scene Samples Ġ: ĠGroup Scene () Ġ{ ĊĠĠĠ Ġprivate Ġval ĠviewModel : ĠSample ViewModel Ġby Ġactivity ViewModels () ĊĠĠĠ Ġprivate Ġlateinit Ġvar ĠtextView : ĠTextView ĊĠĠĠ Ġprivate Ġlateinit Ġvar Ġchild Scene Container : ĠViewGroup ĊĊĠĠĠ Ġoverride Ġfun ĠonCreate View ( in flater : ĠLayoutInflater , Ġcontainer : ĠViewGroup , ĠsavedInstanceState : ĠBundle ?): ĠViewGroup Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġlayout Ġ= ĠFrame Layout ( require Scene Context ()) ĊĠĠĠĠĠĠĠ ĠtextView Ġ= ĠTextView ( require Scene Context ()) ĊĠĠĠĠĠĠĠ ĠtextView . gravity Ġ= ĠGr avity . CENTER ĊĠĠĠĠĠĠĠ Ġlayout . addView ( textView , ĠViewGroup . LayoutParams ( ViewGroup . LayoutParams . MATCH _ PARENT , Ġ 3 0 0 )) ĊĊĠĠĠĠĠĠĠ Ġchild Scene Container Ġ= ĠFrame Layout ( require Scene Context ()) ĊĠĠĠĠĠĠĠ Ġval Ġchild Scene Container Params Ġ= ĠViewGroup . Margin LayoutParams ( ViewGroup . LayoutParams . MATCH _ PARENT , Ġ 3 0 0 ) ĊĠĠĠĠĠĠĠ Ġchild Scene Container Params . top Margin Ġ= Ġ 5 0 0 ĊĠĠĠĠĠĠĠ Ġlayout . addView ( child Scene Container , Ġchild Scene Container Params ) ĊĠĠĠĠĠĠĠ Ġchild Scene Container . id Ġ= ĠView . generate View Id () ĊĠĠĠĠĠĠĠ Ġreturn Ġlayout ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġoverride Ġfun Ġon Activity Created ( savedInstanceState : ĠBundle ?) Ġ{ ĊĠĠĠĠĠĠĠ Ġsuper . on Activity Created ( savedInstanceState ) ĊĠĠĠĠĠĠĠ ĠviewModel . counter . observe ( this , ĠObserver < Int > Ġ{ Ġt Ġ-> ĠtextView . text Ġ= Ġ"" Ġ+ Ġt Ġ}) ĊĊĠĠĠĠĠĠĠ Ġadd ( child Scene Container . id , ĠViewModel Scene Samples Child (), Ġ" Child ") ĊĠĠĠ Ġ} Ċ } Ċ Ċ class ĠViewModel Scene Samples Child Ġ: ĠScene () Ġ{ ĊĠĠĠ Ġprivate Ġval ĠviewModel : ĠSample ViewModel Ġby Ġactivity ViewModels () ĊĊĠĠĠ Ġoverride Ġfun ĠonCreate View ( in flater : ĠLayoutInflater , Ġcontainer : ĠViewGroup , ĠsavedInstanceState : ĠBundle ?): ĠView Ġ{ ĊĠĠĠĠĠĠĠ Ġreturn ĠButton ( require Scene Context ()). apply Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġtext Ġ= Ġ" Click Ġto Ġ+ 1 " ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġoverride Ġfun Ġon Activity Created ( savedInstanceState : ĠBundle ?) Ġ{ ĊĠĠĠĠĠĠĠ Ġsuper . on Activity Created ( savedInstanceState ) ĊĠĠĠĠĠĠĠ Ġrequire View (). setOnClickListener Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġval Ġcount Value Ġ= ĠviewModel . counter . value Ġ?: Ġ 0 ĊĠĠĠĠĠĠĠĠĠĠĠ ĠviewModel . counter . value Ġ= Ġcount Value Ġ+ Ġ 1 ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} Ċ } Ċ Ċ class ĠSample ViewModel Ġ: ĠViewModel () Ġ{ ĊĠĠĠ Ġval Ġcounter : ĠMutable LiveData < Int > Ġ= ĠMutable LiveData () Ċ }