package Ġio . github . den is 1 9 8 6 . android . base . util . extensions Ċ Ċ import Ġandroid . app . Application Ċ import Ġandroidx . fragment . app . Fragment Ċ import Ġandroidx . fragment . app . Fragment Activity Ċ import Ġandroidx . lifecycle . Saved State ViewModel Factory Ċ import Ġandroidx . lifecycle . ViewModel Ċ import Ġandroidx . lifecycle . ViewModel Provider Ċ Ċ /** ĠA Ġset Ġof Ġextension Ġmethods Ġfor Ġproviding ĠViewModel Ġvia Ġ[ android x . lifecycle . ViewModel Provider ] Ġfor Ġa Ġgiven ĠFragment Ġor ĠActivity . Ċ Ġ* Ċ Ġ* ĠCreated Ġby Ġden is . d ru zh inin Ġon Ġ 0 9 . 1 0 . 2 0 1 9 . Ċ Ġ*/ Ċ Ċ /** Ċ Ġ* Ġ@ return ĠViewModel Ġinstance Ġfor Ġa Ġgiven Ġfragment . ĠThis ĠviewModel Ġwill Ġbe Ġable Ġto Ġsave Ġits Ġstate Ġon Ġprocess Ġre creation . Ċ Ġ*/ Ċ inline Ġfun Ġ< re ified ĠVM : ĠViewModel > ĠFragment . viewModel Provider With Saved State ( app : ĠApplication , ĊĠĠĠĠĠĠĠ Ġmode : ĠLazy Thread Safety Mode Ġ= ĠLazy Thread Safety Mode . NONE ) Ġ= Ġlazy ( mode ) Ġ{ ĊĠĠĠ Ġval Ġsave State Factory Ġ= ĠSaved State ViewModel Factory ( app , Ġthis ) ĊĠĠĠ Ġval Ġprovider Ġ= ĠViewModel Provider ( this , Ġsave State Factory ) ĊĠĠĠ Ġprovider . get ( VM :: class . java ) Ċ } Ċ Ċ /** Ċ Ġ* Ġ@ return ĠViewModel Ġinstance Ġfor Ġa Ġgiven Ġactivity . ĠThis ĠviewModel Ġwill Ġbe Ġable Ġto Ġsave Ġits Ġstate Ġon Ġprocess Ġre creation . Ċ Ġ*/ Ċ inline Ġfun Ġ< re ified ĠVM : ĠViewModel > ĠFragment Activity . viewModel Provider With Saved State ( app : ĠApplication , ĊĠĠĠĠĠĠĠ Ġmode : ĠLazy Thread Safety Mode Ġ= ĠLazy Thread Safety Mode . NONE ) Ġ= Ġlazy ( mode ) Ġ{ ĊĠĠĠ Ġval Ġsave State Factory Ġ= ĠSaved State ViewModel Factory ( app , Ġthis ) ĊĠĠĠ Ġval Ġprovider Ġ= ĠViewModel Provider ( this , Ġsave State Factory ) ĊĠĠĠ Ġprovider . get ( VM :: class . java ) Ċ } Ċ Ċ /** Ċ Ġ* Ġ@ return ĠViewModel Ġinstance Ġfor Ġan Ġactivity , Ġassociated Ġwith Ġa Ġgiven Ġfragment . ĠThis ĠviewModel Ġwill Ġbe Ġable Ġto Ġsave Ġits Ġstate Ġon Ġprocess Ġre creation . Ċ Ġ*/ Ċ inline Ġfun Ġ< re ified ĠVM : ĠViewModel > ĠFragment . activity ViewModel Provider With Saved State ( app : ĠApplication , ĊĠĠĠĠĠĠĠ Ġmode : ĠLazy Thread Safety Mode Ġ= ĠLazy Thread Safety Mode . NONE ) Ġ= Ġlazy ( mode ) Ġ{ ĊĠĠĠ Ġval Ġowner Ġ= Ġthis . context Ġas ĠFragment Activity ĊĠĠĠ Ġval Ġsave State Factory Ġ= ĠSaved State ViewModel Factory ( app , Ġthis ) ĊĠĠĠ Ġval Ġprovider Ġ= ĠViewModel Provider ( owner , Ġsave State Factory ) ĊĠĠĠ Ġprovider . get ( VM :: class . java ) Ċ } Ċ Ċ /** Ċ Ġ* Ġ@ return ĠViewModel Ġinstance Ġfor Ġa Ġgiven Ġfragment . Ċ Ġ*/ Ċ inline Ġfun Ġ< re ified ĠVM : ĠViewModel > ĠFragment . viewModel Provider ( ĊĠĠĠĠĠĠĠ Ġmode : ĠLazy Thread Safety Mode Ġ= ĠLazy Thread Safety Mode . NONE , ĊĠĠĠĠĠĠĠ Ġfactory : ĠViewModel Provider . Factory ? Ġ= Ġnull ) Ġ= Ġlazy ( mode ) Ġ{ ĊĠĠĠ Ġval Ġprovider Ġ= Ġif Ġ( factory Ġ== Ġnull ) ĠViewModel Provider ( this ) Ġelse ĠViewModel Provider ( this , Ġfactory ) ĊĠĠĠ Ġprovider . get ( VM :: class . java ) Ċ } Ċ Ċ /** Ċ Ġ* Ġ@ return ĠViewModel Ġinstance Ġfor Ġa Ġgiven Ġactivity . Ċ Ġ*/ Ċ inline Ġfun Ġ< re ified ĠVM : ĠViewModel > ĠFragment Activity . viewModel Provider ( ĊĠĠĠĠĠĠĠ Ġmode : ĠLazy Thread Safety Mode Ġ= ĠLazy Thread Safety Mode . NONE , ĊĠĠĠĠĠĠĠ Ġfactory : ĠViewModel Provider . Factory ? Ġ= Ġnull ) Ġ= Ġlazy ( mode ) Ġ{ ĊĠĠĠ Ġval Ġprovider Ġ= Ġif Ġ( factory Ġ== Ġnull ) ĠViewModel Provider ( this ) Ġelse ĠViewModel Provider ( this , Ġfactory ) ĊĠĠĠ Ġprovider . get ( VM :: class . java ) Ċ } Ċ Ċ /** Ċ Ġ* Ġ@ return ĠViewModel Ġinstance Ġfor Ġan Ġactivity , Ġassociated Ġwith Ġa Ġgiven Ġfragment . Ċ Ġ*/ Ċ inline Ġfun Ġ< re ified ĠVM : ĠViewModel > ĠFragment . activity ViewModel Provider ( ĊĠĠĠĠĠĠĠ Ġmode : ĠLazy Thread Safety Mode Ġ= ĠLazy Thread Safety Mode . NONE , ĊĠĠĠĠĠĠĠ Ġfactory : ĠViewModel Provider . Factory ? Ġ= Ġnull ) Ġ= Ġlazy ( mode ) Ġ{ ĊĠĠĠ Ġval Ġowner Ġ= Ġthis . context Ġas ĠFragment Activity ĊĠĠĠ Ġval Ġprovider Ġ= Ġif Ġ( factory Ġ== Ġnull ) ĠViewModel Provider ( owner ) Ġelse ĠViewModel Provider ( owner , Ġfactory ) ĊĠĠĠ Ġprovider . get ( VM :: class . java ) Ċ }