package Ġcom . v ish al ga ur . shopping app . ui . login Signup Ċ Ċ import Ġandroid . app . Application Ċ import Ġandroid . os . Bundle Ċ import Ġandroid . view . View Ċ import Ġandroidx . appcompat . app . AppCompatActivity Ċ import Ġandroidx . lifecycle . ViewModel Ċ import Ġandroidx . lifecycle . ViewModel Provider Ċ import Ġcom . google . android . material . sn ackbar . Snackbar Ċ import Ġcom . v ish al ga ur . shopping app . R Ċ import Ġcom . v ish al ga ur . shopping app . data . UserData Ċ import Ġcom . v ish al ga ur . shopping app . databinding . Activity O tp Binding Ċ import Ġcom . v ish al ga ur . shopping app . ui . OTP Status Ċ import Ġcom . v ish al ga ur . shopping app . ui . launch Home Ċ import Ġcom . v ish al ga ur . shopping app . view Models . O tp ViewModel Ċ Ċ class ĠO tp Activity Ġ: ĠAppCompatActivity () Ġ{ Ċ ĉ private Ġlateinit Ġvar Ġbinding : ĠActivity O tp Binding ĊĊ ĉ private Ġlateinit Ġvar ĠviewModel : ĠO tp ViewModel ĊĊ ĉ private Ġlateinit Ġvar Ġfrom Where : ĠString ĊĊ ĉ class ĠO tp ViewModel Factory ( Ċĉ ĉ private Ġval Ġapplication : ĠApplication , Ġprivate Ġval Ġu Data : ĠUser Data Ċ ĉ ) Ġ: ĠViewModel Provider . Factory Ġ{ Ċĉ ĉ @ Suppress (" UN CHECK ED _ CAST ") Ċĉ ĉ override Ġfun Ġ< T Ġ: ĠViewModel ?> Ġcreate ( model Class : ĠClass < T >): ĠT Ġ{ Ċĉĉ ĉ if Ġ( model Class . isAssignableFrom ( O tp ViewModel :: class . java )) Ġ{ Ċĉĉĉ ĉ return ĠO tp ViewModel ( application , Ġu Data ) Ġas ĠT Ċĉĉ ĉ } Ċĉĉ ĉ throw ĠIllegalArgumentException (" Unknown ĠViewModel ĠClass ") Ċĉ ĉ } Ċ ĉ } ĊĊ ĉ override Ġfun ĠonCreate ( savedInstanceState : ĠBundle ?) Ġ{ Ċĉ ĉ super . onCreate ( savedInstanceState ) Ċĉ ĉ binding Ġ= ĠActivity O tp Binding . inflate ( layout Inflater ) Ċĉ ĉ val Ġu Data : ĠUser Data ? Ġ= Ġintent . get Parcelable Extra (" u Data ") Ċĉ ĉ from Where Ġ= Ġintent . getString Extra (" from "). toString () Ċĉ ĉ if Ġ( u Data Ġ!= Ġnull ) Ġ{ Ċĉĉ ĉ val ĠviewModel Factory Ġ= ĠO tp ViewModel Factory ( application , Ġu Data ) Ċĉĉ ĉ viewModel Ġ= Ċĉĉĉ ĉ ViewModel Provider ( this , ĠviewModel Factory ). get ( O tp ViewModel :: class . java ) ĊĊĉĉ ĉ viewModel . verify Phone OTP Start ( u Data . mobile , Ġthis ) Ċĉ ĉ } Ċĉ ĉ set Views () ĊĊĉ ĉ set Observer s () Ċĉ ĉ set ContentView ( binding . root ) Ċ ĉ } ĊĊ ĉ private Ġfun Ġset Observer s () Ġ{ Ċĉ ĉ viewModel . otp Status . observe ( this ) Ġ{ Ċĉĉ ĉ when Ġ( it ) Ġ{ Ċĉĉĉ ĉ OTP Status . WRONG Ġ-> Ġbinding . otp Verify Error . visibility Ġ= ĠView . VISIBLE Ċĉĉĉ ĉ else Ġ-> Ġbinding . otp Verify Error . visibility Ġ= ĠView . GONE Ċĉĉ ĉ } Ċĉ ĉ } ĊĊĉ ĉ viewModel . is User LoggedIn . observe ( this ) Ġ{ Ċĉĉ ĉ if Ġ( it Ġ== Ġtrue ) Ġ{ Ċĉĉĉ ĉ if Ġ( from Where Ġ== ĠgetString ( R . string . signup _ fragment _ label )) Ġ{ Ċĉĉĉĉ ĉ viewModel . sign Up () Ċĉĉĉ ĉ } Ġelse Ġ{ Ċĉĉĉĉ ĉ val Ġremember Me Ġ= Ġintent . getBoolean Extra (" login Remember Me ", Ġfalse ) Ċĉĉĉĉ ĉ viewModel . login ( remember Me ) Ċĉĉĉ ĉ } Ċĉĉĉ ĉ launch Home ( this ) Ċĉĉĉ ĉ finish () Ċĉĉ ĉ } Ċĉ ĉ } ĊĊĉ ĉ viewModel . is OT PS ent . observe ( this ) Ġ{ Ċĉĉ ĉ if ( it Ġ== Ġtrue ) Ġ{ Ċĉĉĉ ĉ binding . loader Layout . loader Card . visibility Ġ= ĠView . GONE Ċĉĉĉ ĉ val Ġcontext View Ġ= Ġbinding . loader Layout . loader Card Ċĉĉĉ ĉ Snackbar . make ( context View , ĠR . string . otp _ sent _ msg , ĠSn ackbar . LENGTH _ SHORT ). show () Ċĉĉ ĉ } Ċĉ ĉ } Ċ ĉ } ĊĊ ĉ private Ġfun Ġset Views () Ġ{ Ċĉ ĉ binding . otp Verify Error . visibility Ġ= ĠView . GONE Ċĉ ĉ binding . loader Layout . loader Card . visibility Ġ= ĠView . VISIBLE Ċĉ ĉ binding . loader Layout . loading Message . text Ġ= ĠgetString ( R . string . sending _ otp _ msg ) Ċĉ ĉ binding . loader Layout . circular Loader . show Animation Behavior Ċĉ ĉ binding . otp Verify Btn . setOnClickListener Ġ{ Ċĉĉ ĉ on Verify () Ċĉ ĉ } Ċ ĉ } ĊĊ ĉ private Ġfun Ġon Verify () Ġ{ Ċĉ ĉ val Ġo tp Ġ= Ġbinding . otp O tp EditText . text . toString () Ċĉ ĉ viewModel . verify OTP ( otp ) ĊĊ ĉ } Ċ }