package Ġcom . mar lon ma f ra . cor onav ir ustr ack ing app . features . home Ċ Ċ import Ġandroid . content . Context Ċ import Ġandroid . content . Intent Ċ import Ġandroid . os . Bundle Ċ import Ġandroidx . appcompat . app . AppCompatActivity Ċ import Ġandroidx . fragment . app . Fragment Ċ import Ġandroidx . lifecycle . Observer Ċ import Ġandroidx . lifecycle . ViewModel Provider Ċ import Ġcom . google . android . gms . ads . Ad Request Ċ import Ġcom . mar lon ma f ra . cor onav ir ustr ack ing app . Cor ona Tracking Application Ċ import Ġcom . mar lon ma f ra . cor onav ir ustr ack ing app . R Ċ import Ġcom . mar lon ma f ra . cor onav ir ustr ack ing app . extensions . show Sn ackBar Ċ import Ġcom . mar lon ma f ra . cor onav ir ustr ack ing app . features . home . countries . Countries Fragment Ċ import Ġcom . mar lon ma f ra . cor onav ir ustr ack ing app . features . home . overview . Overview Fragment Ċ import Ġcom . mar lon ma f ra . cor onav ir ustr ack ing app . network . Tracking Response Ċ import Ġkotlinx . android . synt hetic . main . activity _ main . ad View Ċ import Ġkotlinx . android . synt hetic . main . activity _ main . segment ed Tab Ċ import Ġkotlinx . android . synt hetic . main . activity _ main . swipe Refresh Layout Ċ import Ġkotlinx . android . synt hetic . main . activity _ main . view Pager Ċ import Ġjavax . inject . Inject Ċ Ċ const Ġval ĠTR ACK ING _ DATA Ġ= Ġ" TRACK ING _ DATA " Ċ Ċ class ĠMainActivity Ġ: ĠAppCompatActivity () Ġ{ ĊĊĠĠĠ Ġcompanion Ġobject Ġ{ ĊĠĠĠĠĠĠĠ Ġfun Ġnew Instance ( context : ĠContext , Ġtracking Response : ĠTracking Response ) Ġ= ĊĠĠĠĠĠĠĠĠĠĠĠ ĠIntent ( context , ĠMainActivity :: class . java ). apply Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġput Extra ( TRACK ING _ DATA , Ġtracking Response ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġprivate Ġval Ġtracking Response : ĠTracking Response Ġby Ġlazy Ġ{ ĊĠĠĠĠĠĠĠ Ġintent . extras ?. get Serializable ( ĊĠĠĠĠĠĠĠĠĠĠĠ ĠTR ACK ING _ DATA ĊĠĠĠĠĠĠĠ Ġ) Ġas ĠTracking Response ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Inject ĊĠĠĠ Ġlateinit Ġvar Ġfactory : ĠHome ViewModel Factory ĊĠĠĠ Ġprivate Ġval Ġhome ViewModel : ĠHome ViewModel Ġby Ġlazy Ġ{ ĊĠĠĠĠĠĠĠ ĠViewModel Provider ( this , Ġfactory ). get ( ĊĠĠĠĠĠĠĠĠĠĠĠ ĠHome ViewModel :: class . java ĊĠĠĠĠĠĠĠ Ġ) ĊĠĠĠ Ġ} ĊĠĠĠ Ġprivate Ġlateinit Ġvar Ġsub Tab Adapter : ĠSub Tab Adapter ĊĊĠĠĠ Ġoverride Ġfun ĠonCreate ( savedInstanceState : ĠBundle ?) Ġ{ ĊĠĠĠĠĠĠĠ Ġsuper . onCreate ( savedInstanceState ) ĊĠĠĠĠĠĠĠ ĠsetContentView ( R . layout . activity _ main ) ĊĠĠĠĠĠĠĠ ĠCor ona Tracking Application . app Component . inject ( this ) ĊĠĠĠĠĠĠĠ Ġsetup () ĊĠĠĠĠĠĠĠ Ġbind Observable s () ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġprivate Ġfun Ġbind Observable s () Ġ{ ĊĠĠĠĠĠĠĠ Ġhome ViewModel . handle Response ( tracking Response ) ĊĠĠĠĠĠĠĠ Ġhome ViewModel . progress Bar . observe ( this , ĠObserver Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġswipe Refresh Layout . is Refresh ing Ġ= Ġit ĊĠĠĠĠĠĠĠ Ġ}) ĊĠĠĠĠĠĠĠ Ġhome ViewModel . error . observe ( this , ĠObserver Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġshow ErrorMessage () ĊĠĠĠĠĠĠĠ Ġ}) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġprivate Ġfun Ġshow ErrorMessage () Ġ{ ĊĠĠĠĠĠĠĠ Ġshow Sn ackBar ( swipe Refresh Layout , ĠR . string . error _ try _ again ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġprivate Ġfun Ġsetup () Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġfragment List : ĠMutable List < Fragment > Ġ= ĠArrayList () ĊĠĠĠĠĠĠĠ Ġval Ġtitles : ĠMutable List < Int > Ġ= ĠArrayList () ĊĠĠĠĠĠĠĠ Ġtitles . add ( R . string . overview ) ĊĠĠĠĠĠĠĠ Ġtitles . add ( R . string . countries ) ĊĊĠĠĠĠĠĠĠ Ġfragment List . add ( Overview Fragment . newInstance ()) ĊĠĠĠĠĠĠĠ Ġfragment List . add ( Countries Fragment . newInstance ()) ĊĊĠĠĠĠĠĠĠ Ġsub Tab Adapter Ġ= ĠSub Tab Adapter ( support FragmentManager , Ġfragment List ) ĊĠĠĠĠĠĠĠ Ġview Pager . adapter Ġ= Ġthis . sub Tab Adapter ĊĠĠĠĠĠĠĠ Ġsegment ed Tab . setup With ViewPager ( view Pager ) ĊĠĠĠĠĠĠĠ Ġsegment ed Tab . setup ( titles ) ĊĊĠĠĠĠĠĠĠ Ġwith ( swipe Refresh Layout ) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġset On Refresh Listener Ġ{ Ġhome ViewModel . load () Ġ} ĊĠĠĠĠĠĠĠĠĠĠĠ Ġset Color Scheme Colors (* resources . getInt Array ( R . array . swipe _ refresh _ colors )) ĊĠĠĠĠĠĠĠ Ġ} ĊĊĠĠĠĠĠĠĠ Ġval Ġad Request Ġ= ĠAd Request . Builder (). build () ĊĠĠĠĠĠĠĠ Ġad View . load Ad ( ad Request ) ĊĠĠĠ Ġ} Ċ } Ċ