package Ġcom . mark ode vc ic . pe ko Ċ Ċ import Ġandroid . app . Activity Ċ import Ġandroidx . core . app . Component Activity Ċ import Ġandroidx . fragment . app . Fragment Ċ import Ġandroidx . lifecycle .* Ċ import Ġkotlinx . coroutines . Coroutine Scope Ċ import Ġkotlinx . coroutines . Dispatch ers Ċ import Ġkotlinx . coroutines . Job Ċ import Ġkotlinx . coroutines . launch Ċ import Ġkotlin . coroutines . Coroutine Context Ċ Ċ class ĠPermissions LiveData Ġ: ĠLive Data < Permission Result >() Ġ{ ĊĊ ĉ private Ġval Ġrequest er Ġ= ĠLive Data Request er ( this ) ĊĊ ĉ override Ġfun Ġobserve ( owner : ĠLifecycle Owner , Ġobserver : ĠObserver < in ĠPermission Result >) Ġ{ Ċĉ ĉ super . observe ( owner , Ġobserver ) Ċĉ ĉ requester . on Ob serve ( owner ) Ċ ĉ } ĊĊ ĉ fun Ġcheck Permissions ( var arg Ġpermissions : ĠString ) Ġ{ Ċĉ ĉ requester . check Permissions (* permissions ) Ċ ĉ } ĊĊ ĉ fun Ġpost Result ( value : ĠPermission Result ) Ġ{ Ċĉ ĉ super . post Value ( value ) Ċ ĉ } Ċ } Ċ Ċ private Ġclass ĠLive Data Request er ( private Ġval Ġlive Data : ĠPermissions LiveData ) Ġ{ Ċ ĉ private Ġlateinit Ġvar Ġlifecycle Owner Scope : ĠLifecycle Owner Scope Ċ ĉ var Ġowner : ĠLifecycle Owner ? Ġ= Ġnull ĊĊ ĉ fun Ġcheck Permissions ( var arg Ġpermissions : ĠString ) Ġ{ Ċĉ ĉ val Ġowner Copy Ġ= Ġowner Ġ?: Ġthrow ĠIllegalStateException (" Lifecycle Ġowner Ġnot Ġregistered ") Ċĉ ĉ if Ġ( owner Copy . lifecycle . currentState Ġ== ĠLifecycle . State . DESTROY ED ) Ġ{ Ċĉĉ ĉ return Ċĉ ĉ } Ċĉ ĉ else Ġif Ġ(! P ek o . is Request InProgress ()) Ġ{ Ċĉĉ ĉ when Ġ( owner Copy ) Ġ{ Ċĉĉĉ ĉ is ĠComponent Activity , Ċĉĉĉ ĉ is ĠFragment Ġ-> Ġ{ Ċĉĉĉĉ ĉ lifecycle Owner Scope Ġ= ĠLifecycle Owner Scope ( owner Copy ) Ġ{ Ġowner Ġ= Ġnull Ġ} Ċĉĉĉĉ ĉ lifecycle Owner Scope . launch Ġ{ Ċĉĉĉĉĉ ĉ val Ġactivity Ġ= Ġif Ġ( owner Copy Ġis ĠFragment ) Ċĉĉĉĉĉĉ ĉ ( owner Copy . activity Ċĉĉĉĉĉĉĉĉ ĉ ?: Ġthrow ĠIllegalStateException (" Fragment Ġshould Ġbe Ġin Ġa Ġstate " Ġ+ Ċĉĉĉĉĉĉĉĉĉĉ ĉ " Ġwhere Ġactivity Ġis Ġnot Ġnull ")) Ċĉĉĉĉĉ ĉ else Ġowner Copy Ġas ĠActivity Ċĉĉĉĉĉ ĉ val Ġresult Ġ= ĠP ek o . request Permissions Async ( activity , Ġ* permissions ) Ċĉĉĉĉĉ ĉ live Data . post Result ( result ) Ċĉĉĉĉ ĉ } Ċĉĉĉ ĉ } Ċĉĉĉ ĉ else Ġ-> Ġthrow ĠIllegalArgumentException (" Unsupported Ġlifecycle Ġowner ") Ċĉĉ ĉ } Ċĉ ĉ } Ġelse Ġ{ Ċĉĉ ĉ resume Request ( owner Copy ) Ċĉ ĉ } Ċ ĉ } ĊĊ ĉ fun Ġon Ob serve ( owner : ĠLifecycle Owner ) Ġ{ Ċĉ ĉ this . owner Ġ= Ġowner Ċĉ ĉ if Ġ( P ek o . is Request InProgress ()) Ġ{ Ċĉĉ ĉ resume Request ( owner ) Ċĉ ĉ } Ċ ĉ } ĊĊ ĉ private Ġfun Ġresume Request ( owner : ĠLifecycle Owner ) Ġ{ Ċĉ ĉ lifecycle Owner Scope Ġ= ĠLifecycle Owner Scope ( owner ) Ġ{ Ġthis . owner Ġ= Ġnull Ġ} Ċĉ ĉ lifecycle Owner Scope . launch Ġ{ Ċĉĉ ĉ val Ġresult Ġ= ĠP ek o . resume Request () Ċĉĉ ĉ live Data . post Result ( result ) Ċĉ ĉ } Ċ ĉ } Ċ } Ċ Ċ private Ġclass ĠLifecycle Owner Scope ( private Ġval Ġlifecycle Owner : ĠLifecycle Owner , Ċĉĉĉĉĉĉĉĉ Ġ Ġprivate Ġval Ġon Destroy Callback : Ġ() Ġ-> ĠUnit ) Ġ: ĠCor outine Scope Ġ{ Ċ ĉ private Ġval Ġjob Ġ= ĠJob () Ċ ĉ override Ġval Ġcoroutine Context : ĠCor outine Context Ċĉ ĉ get () Ġ= ĠDispatch ers . Main Ġ+ Ġjob ĊĊ ĉ init Ġ{ Ċĉ ĉ val Ġobserver Ġ= Ġobject Ġ: ĠLifecycle Observer Ġ{ ĊĊĉĉ ĉ @ On Lifecycle Event ( Lifecycle . Event . ON _ DESTROY ) Ċĉĉ ĉ fun Ġon Destroy () Ġ{ Ċĉĉĉ ĉ if Ġ( is Changing Config ()) Ġ{ Ċĉĉĉĉ ĉ job . cancel ( Activity Rot ating Exception ()) Ċĉĉĉ ĉ } Ġelse Ġ{ Ċĉĉĉĉ ĉ job . cancel () Ċĉĉĉ ĉ } Ċĉĉĉ ĉ on Destroy Callback () Ċĉĉĉ ĉ lifecycle Owner . lifecycle . remove Observer ( this ) Ċĉĉ ĉ } ĊĊĉĉ ĉ private Ġfun Ġis Changing Config (): ĠBoolean Ġ{ Ċĉĉĉ ĉ return Ġwhen Ġ( lifecycle Owner ) Ġ{ Ċĉĉĉĉ ĉ is ĠActivity Ġ-> Ġlifecycle Owner . is Changing Configurations Ċĉĉĉĉ ĉ is ĠFragment Ġ-> Ġlifecycle Owner . activity ?. is Changing Configurations Ġ== Ġtrue Ċĉĉĉĉ ĉ else Ġ-> Ġfalse Ċĉĉĉ ĉ } Ċĉĉ ĉ } Ċĉ ĉ } Ċĉ ĉ lifecycle Owner . lifecycle . add Observer ( observer ) Ċ ĉ } Ċ }