package Ġnet . ow lf amily . android . data exchange Ċ Ċ import Ġandroid . os . Bundle Ċ import Ġandroidx . fragment . app . Fragment Ċ import Ġandroidx . fragment . app . Fragment Activity Ċ import Ġandroidx . lifecycle . ViewModel Provider Ċ import Ġio . reactivex . subjects . Behavior Subject Ċ import Ġio . reactivex . subjects . Publish Subject Ċ Ċ /** Ċ Ġ* ĠAll Ġof Ġextensions Ġare Ġsame Ġwith Ġ[ Fragment Extensions . kt ] Ċ Ġ* Ġexcept ĠActivity Data Ex ch anger Key , Ġget OrCreate Arguments , ĠgetData Exchange Arguments Ċ Ġ*/ Ċ Ċ /** Ċ Ġ* Ġwill Ġbe Ġused Ġ' key ' Ġof Ġbundle Ġin Ġintent Ċ Ġ*/ Ċ @ Suppress (" Spell Checking Inspection ") Ċ const Ġval ĠActivity Data Ex ch anger Key Ġ= Ġ" net . ow lf amily . android . data exchange . Activity Data Ex ch anger Key " Ċ Ċ /** ĊĠ Ġ* Ġ@ return Ġbundle Ġfor ĠData Exchange Ġfeatures . Ġthis Ġwill Ġenable Ġto Ġ' Activity ' Ġbe ĠOwner Ġof Ġ[ Archive ]. Ġlike Ġ[ Fragment ]. Ċ Ġ*/ Ċ fun ĠFragment Activity . get OrCreate Arguments (): ĠBundle Ġ{ ĊĠĠĠ Ġvar Ġresult : ĠBundle ? Ġ= Ġintent . get Bundle Extra ( Activity Data Ex ch anger Key ) ĊĠĠĠ Ġif ( result Ġ== Ġnull ){ ĊĠĠĠĠĠĠĠ Ġresult Ġ= ĠBundle () ĊĠĠĠĠĠĠĠ Ġintent . putExtra ( Activity Data Ex ch anger Key , Ġresult ) ĊĠĠĠ Ġ} ĊĠĠĠ Ġreturn Ġresult Ċ } Ċ Ċ /** Ċ Ġ* Ġ@ return Ġsame Ġas Ġ[ get OrCreate Arguments ] Ġbut Ġ' null ' Ġif Ġnot Ġexists Ċ Ġ*/ Ċ fun ĠFragment Activity . getData Exchange Arguments (): ĠBundle ? Ġ{ ĊĠĠĠ Ġreturn Ġintent . get Bundle Extra ( Activity Data Ex ch anger Key ) Ċ } Ċ Ċ fun ĠFragment Activity . getData Exchange Caller Id (): ĠString ? Ġ{ ĊĠĠĠ Ġreturn ĠgetData Exchange Arguments ()?. get Bundle ( Data Exchange Helper . data Exchange Bundle Id )?. getString ( ĊĠĠĠĠĠĠĠ ĠData Exchange Helper . data Exchange Caller Id ĊĠĠĠ Ġ) Ċ } Ċ Ċ fun ĠFragment Activity . getData Exchange Archive ItemId (): ĠString ? Ġ{ ĊĠĠĠ Ġreturn ĠgetData Exchange Arguments ()?. get Bundle ( Data Exchange Helper . data Exchange Bundle Id )?. getString ( ĊĠĠĠĠĠĠĠ ĠData Exchange Helper . data Exchange Archive ItemId ĊĠĠĠ Ġ) Ċ } Ċ Ċ fun ĠFragment Activity . get OrCreate Data Exchange UniqueId (): ĠString Ġ{ ĊĠĠĠ Ġreturn ĠgetData Exchange UniqueId ( true ) !! Ċ } Ċ Ċ fun ĠFragment Activity . getData Exchange UniqueId ( create If Empty : Boolean Ġ= Ġtrue ): ĠString ? Ġ{ ĊĠĠĠ Ġvar Ġbundle Ġ= ĠgetData Exchange Arguments ()?. get Bundle ( Data Exchange Helper . data Exchange Bundle Id ) ĊĠĠĠ Ġbundle ?. getString ( Data Exchange Helper . data Exchange UniqueId )?. let Ġ{ ĊĠĠĠĠĠĠĠ Ġreturn Ġit ĊĠĠĠ Ġ} ĊĠĠĠ Ġif (! create If Empty ) Ġ{ Ġreturn Ġnull Ġ} ĊĊĠĠĠ Ġif ( bundle Ġ== Ġnull ){ ĊĠĠĠĠĠĠĠ Ġbundle Ġ= ĠBundle () ĊĠĠĠĠĠĠĠ Ġget OrCreate Arguments (). put Bundle ( Data Exchange Helper . data Exchange Bundle Id , Ġbundle ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġval Ġid Ġ= ĠData Exchange Helper . create Random ID () ĊĠĠĠ Ġbundle . putString ( Data Exchange Helper . data Exchange UniqueId , id ) ĊĠĠĠ Ġreturn Ġid Ċ } ĊĊ Ċ fun ĠFragment Activity . getData Exchange ViewModel (): ĠData Exchange ViewModel Ġ{ ĊĠĠĠ Ġreturn ĠViewModel Provider ( this ). get ( Data Exchange ViewModel :: class . java ) Ċ } ĊĊ Ċ fun ĠFragment Activity . get Archive Subject ( owner Id : String ): ĠBehavior Subject < Archive > Ġ{ ĊĠĠĠ Ġval Ġvm Ġ= ĠgetData Exchange ViewModel () ĊĠĠĠ Ġreturn Ġvm . get OrCreate Archive ( owner Id ) Ċ } Ċ Ċ fun ĠFragment Activity . get Owned Archive Subject (): ĠBehavior Subject < Archive > Ġ{ ĊĠĠĠ Ġreturn Ġget Archive Subject ( get OrCreate Data Exchange UniqueId ()) Ċ } Ċ Ċ fun ĠFragment Activity . get Caller Archive Subject (): ĠBehavior Subject < Archive >? Ġ{ ĊĠĠĠ ĠgetData Exchange Caller Id ()?. let Ġ{ ĊĠĠĠĠĠĠĠ Ġreturn Ġget Archive Subject ( it ) ĊĠĠĠ Ġ} ĊĠĠĠ Ġreturn Ġnull Ċ } ĊĊĊ Ċ fun ĠFragment Activity . remove Archive Subject ( owner Id : String ): ĠBoolean Ġ{ ĊĠĠĠ Ġval Ġvm Ġ= ĠgetData Exchange ViewModel () ĊĠĠĠ Ġreturn Ġvm . remove Archive ( owner Id ) Ċ } Ċ fun ĠFragment Activity . remove Owned Archive Subject (): ĠBoolean Ġ{ ĊĠĠĠ ĠgetData Exchange UniqueId ( false )?. let Ġ{ ĊĠĠĠĠĠĠĠ Ġreturn Ġremove Archive Subject ( owner Id Ġ= Ġit ) ĊĠĠĠ Ġ} ĊĠĠĠ Ġreturn Ġfalse Ċ } Ċ fun ĠFragment Activity . remove Caller Archive Subject (): ĠBoolean Ġ{ ĊĠĠĠ ĠgetData Exchange Caller Id ()?. let Ġ{ ĊĠĠĠĠĠĠĠ Ġreturn Ġremove Archive Subject ( owner Id Ġ= Ġit ) ĊĠĠĠ Ġ} ĊĠĠĠ Ġreturn Ġfalse Ċ } ĊĊ Ċ fun Ġ< T > ĠFragment Activity . get Archive Item Subject ( archive ItemId : String , Ġowner Id : String ): ĠBehavior Subject < Archive . Item < T >> Ġ{ ĊĠĠĠ Ġval Ġvm Ġ= ĠgetData Exchange ViewModel () ĊĊĠĠĠ Ġval Ġdata Pack Ġ= Ġvm . get OrCreate Archive ( owner Id ). value !! ĊĠĠĠ Ġreturn Ġdata Pack . get OrCreate Item Subject < T >( archive ItemId Ġ= Ġarchive ItemId ) Ċ } Ċ fun Ġ< T > ĠFragment Activity . get Owned Archive Item Subject ( archive ItemId : String ): ĠBehavior Subject < Archive . Item < T >> Ġ{ ĊĠĠĠ Ġreturn Ġget Archive Item Subject ( archive ItemId , Ġget OrCreate Data Exchange UniqueId ()) Ċ } Ċ fun Ġ< T > ĠFragment Activity . get Caller Archive Item Subject (): ĠBehavior Subject < Archive . Item < T >> ? Ġ{ ĊĠĠĠ Ġval Ġarchive ItemId Ġ= ĠgetData Exchange Archive ItemId () Ġ?: Ġreturn Ġnull ĊĠĠĠ Ġval Ġcaller Id Ġ= ĠgetData Exchange Caller Id () Ġ?: Ġreturn Ġnull ĊĠĠĠ Ġreturn Ġget Archive Item Subject ( archive ItemId Ġ= Ġarchive ItemId , Ġowner Id Ġ= Ġcaller Id ) Ċ } ĊĊĊ Ċ fun ĠFragment Activity . remove Archive Item Subject ( archive ItemId : String , Ġowner Id : String ): ĠBoolean Ġ{ ĊĠĠĠ Ġval Ġvm Ġ= ĠgetData Exchange ViewModel () ĊĊĠĠĠ Ġval Ġdata Pack Ġ= Ġvm . get OrCreate Archive ( owner Id ). value !! ĊĠĠĠ Ġreturn Ġdata Pack . removeItem Subject ( archive ItemId Ġ= Ġarchive ItemId ) Ċ } Ċ fun ĠFragment Activity . remove Owned Archive Item Subject ( archive ItemId : String ): ĠBoolean Ġ{ ĊĠĠĠ Ġreturn Ġremove Archive Item Subject ( archive ItemId Ġ= Ġarchive ItemId , Ġowner Id Ġ= Ġget OrCreate Data Exchange UniqueId ()) Ċ } Ċ fun ĠFragment Activity . remove Caller Archive Item Subject (): ĠBoolean Ġ{ ĊĠĠĠ Ġval Ġarchive ItemId Ġ= ĠgetData Exchange Archive ItemId () Ġ?: Ġreturn Ġfalse ĊĠĠĠ Ġval Ġcaller Id Ġ= ĠgetData Exchange Caller Id () Ġ?: Ġreturn Ġfalse ĊĊĠĠĠ Ġreturn Ġremove Archive Item Subject ( archive ItemId Ġ= Ġarchive ItemId , Ġowner Id Ġ= Ġcaller Id ) Ċ } ĊĊ Ċ fun ĠFragment Activity . has Archive Item Subject ( archive ItemId : String , Ġowner Id : String ): ĠBoolean Ġ{ ĊĠĠĠ Ġval Ġvm Ġ= ĠgetData Exchange ViewModel () ĊĠĠĠ Ġval Ġarchive Ġ= Ġvm . get OrCreate Archive ( owner Id ). value !! ĊĠĠĠ Ġreturn Ġarchive . has Item Subject ( archive ItemId ) Ċ } Ċ fun ĠFragment Activity . has Owned Archive Item Subject ( archive ItemId : String ): ĠBoolean Ġ{ ĊĠĠĠ Ġreturn Ġhas Archive Item Subject ( archive ItemId Ġ= Ġarchive ItemId , Ġowner Id Ġ= Ġget OrCreate Data Exchange UniqueId ()) Ċ } Ċ fun ĠFragment Activity . has Caller Archive Item Subject (): ĠBoolean Ġ{ ĊĠĠĠ Ġval Ġarchive ItemId Ġ= ĠgetData Exchange Archive ItemId () Ġ?: Ġreturn Ġfalse ĊĠĠĠ Ġval Ġcaller Id Ġ= ĠgetData Exchange Caller Id () Ġ?: Ġreturn Ġfalse ĊĠĠĠ Ġreturn Ġhas Archive Item Subject ( archive ItemId Ġ= Ġarchive ItemId , Ġowner Id Ġ= Ġcaller Id ) Ċ } ĊĊĊ Ċ fun ĠFragment Activity . get Archive Item StateChange Subject ( owner Id : String ): ĠPublish Subject < Pair < String , ĠArchive . Item <* >>> Ġ{ ĊĠĠĠ Ġval Ġvm Ġ= ĠgetData Exchange ViewModel () ĊĊĠĠĠ Ġval Ġdata Pack Ġ= Ġvm . get OrCreate Archive ( owner Id ). value !! ĊĠĠĠ Ġreturn Ġdata Pack . item StateChanged Subject Ċ } Ċ fun ĠFragment Activity . get Owned Archive Item StateChange Subject (): ĠPublish Subject < Pair < String , ĠArchive . Item <* >>> Ġ{ ĊĠĠĠ Ġreturn Ġget Archive Item StateChange Subject ( get OrCreate Data Exchange UniqueId ()) Ċ } Ċ fun ĠFragment Activity . get Caller Archive Item StateChange Subject (): ĠPublish Subject < Pair < String , ĠArchive . Item <* >>> ? Ġ{ ĊĠĠĠ ĠgetData Exchange Caller Id ()?. let Ġ{ ĊĠĠĠĠĠĠĠ Ġreturn Ġget Archive Item StateChange Subject ( it ) ĊĠĠĠ Ġ} ĊĠĠĠ Ġreturn Ġnull Ċ } ĊĊĊ Ċ fun ĠFragment Activity . has Archive Item State ( owner Id : String , Ġarchive ItemId : String ): Boolean { ĊĠĠĠ Ġval Ġdata Archive Ġ= Ġget Archive Subject ( owner Id ). value Ġ?: Ġreturn Ġfalse ĊĠĠĠ Ġif ( data Archive . is Item Null Or Unknown State ( archive ItemId )){ ĊĠĠĠĠĠĠĠ Ġreturn Ġfalse ĊĠĠĠ Ġ} ĊĠĠĠ Ġreturn Ġtrue Ċ } Ċ fun ĠFragment Activity . has Caller Archive Item State (): Boolean { ĊĠĠĠ Ġval Ġarchive ItemId Ġ= ĠgetData Exchange Archive ItemId () Ġ?: Ġreturn Ġfalse ĊĠĠĠ Ġval Ġcaller Id Ġ= ĠgetData Exchange Caller Id () Ġ?: Ġreturn Ġfalse ĊĠĠĠ Ġreturn Ġhas Archive Item State ( owner Id Ġ= Ġcaller Id , Ġarchive ItemId Ġ= Ġarchive ItemId ) Ċ } Ċ fun ĠFragment Activity . has Owned Archive Item State ( archive ItemId : String ): Boolean { ĊĠĠĠ Ġreturn Ġhas Archive Item State ( owner Id Ġ= Ġget OrCreate Data Exchange UniqueId (), Ġarchive ItemId Ġ= Ġarchive ItemId ) Ċ } ĊĊĊ Ċ fun Ġ< T > ĠFragment Activity . set Archive Item ( owner Id : String , Ġarchive ItemId : String , Ġitem State : ĠArchive . Item . State , Ġdata : T ? Ġ= Ġnull ): Boolean { ĊĠĠĠ Ġval Ġdata Bus Vm Ġ= ĠgetData Exchange ViewModel () ĊĠĠĠ Ġdata Bus Vm . save Item ( owner Id Ġ= Ġowner Id , Ġarchive ItemId Ġ= Ġarchive ItemId , Ġitem State Ġ= Ġitem State , Ġdata Ġ= Ġdata ) ĊĠĠĠ Ġreturn Ġtrue Ċ } Ċ fun Ġ< T > ĠFragment Activity . set Caller Archive Item ( item State : ĠArchive . Item . State , Ġdata : T ? Ġ= Ġnull ): Boolean { ĊĠĠĠ Ġval Ġarchive ItemId Ġ= ĠgetData Exchange Archive ItemId () Ġ?: Ġreturn Ġfalse ĊĠĠĠ Ġval Ġcaller Id Ġ= ĠgetData Exchange Caller Id () Ġ?: Ġreturn Ġfalse ĊĠĠĠ Ġreturn Ġset Archive Item ( owner Id Ġ= Ġcaller Id , Ġarchive ItemId Ġ= Ġarchive ItemId , Ġitem State Ġ= Ġitem State , Ġdata Ġ= Ġdata ) Ċ } Ċ fun Ġ< T > ĠFragment Activity . set Owned Archive Item ( archive ItemId : String , Ġitem State : ĠArchive . Item . State , Ġdata : T ? Ġ= Ġnull ): Boolean { ĊĠĠĠ Ġreturn Ġset Archive Item ( owner Id Ġ= Ġget OrCreate Data Exchange UniqueId (), Ġarchive ItemId Ġ= Ġarchive ItemId , Ġitem State Ġ= Ġitem State , Ġdata Ġ= Ġdata ) Ċ } ĊĊĊ Ċ fun ĠFragment Activity . set Archive Item State With Null Data ( owner Id : String , Ġarchive ItemId : String , Ġitem State : ĠArchive . Item . State ): Boolean { ĊĠĠĠ Ġval Ġdata Bus Vm Ġ= ĠgetData Exchange ViewModel () ĊĠĠĠ Ġdata Bus Vm . save Item ( owner Id Ġ= Ġowner Id , Ġarchive ItemId Ġ= Ġarchive ItemId , Ġitem State Ġ= Ġitem State , Ġdata Ġ= Ġnull ) ĊĠĠĠ Ġreturn Ġtrue Ċ } Ċ fun ĠFragment Activity . set Caller Archive Item State With Null Data ( item State : ĠArchive . Item . State ): Boolean { ĊĠĠĠ Ġval Ġarchive ItemId Ġ= ĠgetData Exchange Archive ItemId () Ġ?: Ġreturn Ġfalse ĊĠĠĠ Ġval Ġcaller Id Ġ= ĠgetData Exchange Caller Id () Ġ?: Ġreturn Ġfalse ĊĠĠĠ Ġreturn Ġset Archive Item State With Null Data ( owner Id Ġ= Ġcaller Id , Ġarchive ItemId Ġ= Ġarchive ItemId , Ġitem State Ġ= Ġitem State ) Ċ } Ċ fun ĠFragment Activity . set Owned Archive Item State With Null Data ( archive ItemId : String , Ġitem State : ĠArchive . Item . State ): Boolean { ĊĠĠĠ Ġreturn Ġset Archive Item State With Null Data ( get OrCreate Data Exchange UniqueId (), Ġarchive ItemId , Ġitem State ) Ċ } ĊĊ Ċ fun ĠFragment Activity . reset Archive Item State ( owner Id : String , Ġarchive ItemId : String ): Boolean { ĊĠĠĠ Ġreturn Ġset Archive Item State With Null Data ( owner Id = owner Id , Ġarchive ItemId Ġ= Ġarchive ItemId , Ġitem State Ġ= ĠArchive . Item . State . Unknown ) Ċ } Ċ fun ĠFragment Activity . reset Caller Archive Item State (): Boolean { ĊĠĠĠ Ġval Ġarchive ItemId Ġ= ĠgetData Exchange Archive ItemId () Ġ?: Ġreturn Ġfalse ĊĠĠĠ Ġval Ġcaller Id Ġ= ĠgetData Exchange Caller Id () Ġ?: Ġreturn Ġfalse ĊĠĠĠ Ġreturn Ġreset Archive Item State ( owner Id Ġ= Ġcaller Id , Ġarchive ItemId Ġ= Ġarchive ItemId ) Ċ } Ċ fun ĠFragment Activity . reset Owned Archive Item State ( archive ItemId : String ): Boolean { ĊĠĠĠ Ġreturn Ġreset Archive Item State ( owner Id Ġ= Ġget OrCreate Data Exchange UniqueId (), Ġarchive ItemId Ġ= Ġarchive ItemId ) Ċ } Ċ