package Ġorg . ny pl . simpl ified . card creator . ui Ċ Ċ import Ġandroid . Manifest Ċ import Ġandroid . content . Intent Ċ import Ġandroid . content . pm . PackageManager Ċ import Ġandroid . net . Uri Ċ import Ġandroid . os . Bundle Ċ import Ġandroid . view . LayoutInflater Ċ import Ġandroid . view . View Ċ import Ġandroid . view . ViewGroup Ċ import Ġandroidx . core . app . Activity Compat Ċ import Ġandroidx . core . content . Context Compat Ċ import Ġandroidx . fragment . app . Fragment Ċ import Ġandroidx . fragment . app . view Models Ċ import Ġandroidx . lifecycle . flow With Lifecycle Ċ import Ġandroidx . lifecycle . lifecycle Scope Ċ import Ġkotlinx . coroutines . flow . collect Ċ import Ġkotlinx . coroutines . launch Ċ import Ġorg . ny pl . simpl ified . android . k tx . view Lifecycle Aware Ċ import Ġorg . ny pl . simpl ified . card creator . Card Creator Activity Ċ import Ġorg . ny pl . simpl ified . card creator . Card Creator Contract Ċ import Ġorg . ny pl . simpl ified . card creator . databinding . Fragment Confirmation Binding Ċ import Ġorg . ny pl . simpl ified . card creator . utils . Cache Ċ import Ġorg . ny pl . simpl ified . card creator . view model . Confirmation Event Ċ import Ġorg . ny pl . simpl ified . card creator . view model . Confirmation ViewModel Ċ import Ġorg . ny pl . simpl ified . card creator . view model . Confirmation ViewModel Factory Ċ import Ġorg . slf 4 j . LoggerFactory Ċ Ċ class ĠConfirm ation Fragment Ġ: ĠFragment () Ġ{ ĊĊĠ Ġprivate Ġval Ġlogger Ġ= ĠLoggerFactory . getLogger ( Confirmation Fragment :: class . java ) ĊĊĠ Ġprivate Ġval Ġargs Ġby Ġlazy Ġ{ ĠConfirm ation Fragment Args . from Bundle ( require Arguments ()) Ġ} ĊĊĠ Ġprivate Ġval ĠviewModel Ġby Ġview Models < Confirmation ViewModel > Ġ{ ĠConfirm ation ViewModel Factory ( args ) Ġ} ĊĠ Ġprivate Ġvar Ġbinding Ġby Ġview Lifecycle Aware < Fragment Confirmation Binding >() ĊĊĠ Ġprivate Ġval Ġstorage Request Code Ġ= Ġ 2 0 3 ĊĠ Ġprivate Ġval ĠdateFormat Ġ= Ġ" dd - MM - yyyy " ĊĊĠ Ġoverride Ġfun ĠonCreate View ( ĊĠĠĠ Ġinflater : ĠLayoutInflater , ĊĠĠĠ Ġcontainer : ĠViewGroup ?, ĊĠĠĠ ĠsavedInstanceState : ĠBundle ? ĊĠ Ġ): ĠView Ġ{ ĊĠĠĠ Ġbinding Ġ= ĠFragment Confirmation Binding . inflate ( in flater , Ġcontainer , Ġfalse ) ĊĠĠĠ Ġbinding . viewModel Ġ= ĠviewModel ĊĠĠĠ Ġbinding . format Ġ= ĠdateFormat ĊĠĠĠ Ġbinding . lifecycle Owner Ġ= Ġview Lifecycle Owner ĊĠĠĠ Ġreturn Ġbinding . root ĊĠ Ġ} ĊĊĠ Ġoverride Ġfun Ġon View Created ( view : ĠView , ĠsavedInstanceState : ĠBundle ?) Ġ{ ĊĠĠĠ Ġsuper . on View Created ( view , ĠsavedInstanceState ) ĊĊĠĠĠ Ġlifecycle Scope . launch Ġ{ ĊĠĠĠĠĠ ĠviewModel . state . flow With Lifecycle ( lifecycle ). collect Ġ{ ĊĠĠĠĠĠĠĠ Ġit . events . first OrNull ()?. let Ġ{ Ġevent Ġ-> Ġhandle Event ( event ) Ġ} ĊĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĠ Ġ} ĊĊĠ Ġprivate Ġfun Ġhandle Event ( event : ĠConfirm ation Event ) Ġ{ ĊĠĠĠ Ġwhen Ġ( event ) Ġ{ ĊĠĠĠĠĠ ĠConfirm ation Event . Card Confirmed Ġ-> Ġreturn Result () ĊĠĠĠĠĠ ĠConfirm ation Event . Save Card Permissions Check Ġ-> Ġcheck And Request Permissions () ĊĠĠĠĠĠ Ġis ĠConfirm ation Event . Add Saved Card To Gallery Ġ-> Ġadd Card To Gallery ( event . file Uri ) ĊĠĠĠ Ġ} ĊĠĠĠ ĠviewModel . event HasBeen Handled ( event . id ) ĊĠ Ġ} ĊĊĠ Ġprivate Ġfun Ġcheck And Request Permissions () Ġ{ ĊĠĠĠ Ġif Ġ( Context Compat . check Self Permission ( ĊĠĠĠĠĠĠĠ Ġrequire Context (), ĊĠĠĠĠĠĠĠ ĠManifest . permission . WRITE _ EXTERNAL _ STORAGE ĊĠĠĠĠĠ Ġ) ĊĠĠĠĠĠ Ġ!= ĠPackage Manager . PERMISSION _ GRAN TED ĊĠĠĠ Ġ) Ġ{ ĊĠĠĠĠĠ Ġlogger . debug (" Request ing Ġstorage Ġpermission ") ĊĠĠĠĠĠ ĠActivity Compat . request Permissions ( ĊĠĠĠĠĠĠĠ Ġrequire Activity (), ĊĠĠĠĠĠĠĠ ĠarrayOf ( Manifest . permission . WRITE _ EXTERNAL _ STORAGE ), ĊĠĠĠĠĠĠĠ Ġstorage Request Code ĊĠĠĠĠĠ Ġ) ĊĠĠĠ Ġ} Ġelse Ġ{ ĊĠĠĠĠĠ Ġsave Library Card Image () ĊĠĠĠ Ġ} ĊĠ Ġ} ĊĊĠ Ġprivate Ġfun Ġsave Library Card Image () Ġ{ ĊĠĠĠ Ġval Ġcard Ġ= Ġbinding . library Card ĊĠĠĠ Ġcard . is Drawing Cache Enabled Ġ= Ġtrue ĊĠĠĠ ĠviewModel . create And Store Digital Card ( card . drawing Cache ) ĊĠ Ġ} ĊĊĠ Ġ/** ĊĠĠ Ġ* ĠAdds Ġlibrary Ġcard Ġimage Ġto Ġdevice Ġphoto Ġgallery ĊĠĠ Ġ*/ ĊĠ Ġprivate Ġfun Ġadd Card To Gallery ( file Uri : ĠUri ) Ġ{ ĊĠĠĠ Ġval Ġmedia Scan Intent Ġ= ĠIntent ( Intent . ACTION _ MEDIA _ SCAN NER _ SCAN _ FILE ) ĊĠĠĠ Ġmedia Scan Intent . data Ġ= Ġfile Uri ĊĠĠĠ Ġrequire Activity (). send Broadcast ( media Scan Intent ) ĊĠ Ġ} ĊĊĠ Ġ/** ĊĠĠ Ġ* ĠListen Ġfor Ġresult Ġfrom Ġstorage Ġpermission Ġrequest , Ġthis Ġmethod Ġis Ġa Ġcallback Ġprovided Ġby ĊĠĠ Ġ* ĠAndroid Ġfor Ġthe Ġrequest Permissions () Ġmethod ĊĠĠ Ġ* ĊĠĠ Ġ* Ġ@ param Ġrequest Code Ġ- ĠString Ġuser Ġdefined Ġrequest Ġcode Ġto Ġidentify Ġthe Ġrequest ĊĠĠ Ġ* Ġ@ param Ġpermissions Ġ- ĠString ĠArray Ġof Ġpermissions Ġrequested ĊĠĠ Ġ* Ġ@ param Ġgrant Results Ġ- ĠInteger Ġarray Ġof Ġwhat Ġthe Ġuser Ġhas Ġgranted / den ied ĊĠĠ Ġ*/ ĊĠ Ġoverride Ġfun Ġon Request Permissions Result ( ĊĠĠĠ Ġrequest Code : ĠInt , ĊĠĠĠ Ġpermissions : ĠArray < String >, ĊĠĠĠ Ġgrant Results : ĠInt Array ĊĠ Ġ) Ġ{ ĊĠĠĠ Ġwhen Ġ( request Code ) Ġ{ ĊĠĠĠĠĠ Ġstorage Request Code Ġ-> Ġ{ ĊĠĠĠĠĠĠĠ Ġ// ĠIf Ġrequest Ġis Ġcancelled , Ġthe Ġresult Ġarrays Ġare Ġempty . ĊĠĠĠĠĠĠĠ Ġif Ġ(( grant Results . isNotEmpty () Ġ&& Ġgrant Results [ 0 ] Ġ== ĠPackage Manager . PERMISSION _ GRAN TED )) Ġ{ ĊĠĠĠĠĠĠĠĠĠ Ġlogger . debug (" Storage Ġpermission Ġgranted ") ĊĠĠĠĠĠĠĠĠĠ Ġsave Library Card Image () ĊĠĠĠĠĠĠĠ Ġ} Ġelse Ġ{ ĊĠĠĠĠĠĠĠĠĠ Ġlogger . debug (" Storage Ġpermission ĠNOT Ġgranted ") ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠ Ġreturn ĊĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĠ Ġ} ĊĊĠ Ġprivate Ġfun Ġreturn Result () Ġ{ ĊĠĠĠ Ġval Ġbundle Ġ= ĠCard Creator Contract . result Bundle ( ĊĠĠĠĠĠ ĠviewModel . state . value . data . barcode , ĊĠĠĠĠĠ ĠviewModel . state . value . data . password ĊĠĠĠ Ġ) ĊĠĠĠ Ġval Ġintent Ġ= ĠIntent (). apply Ġ{ ĊĠĠĠĠĠ Ġput Extras ( bundle ) ĊĠĠĠ Ġ} ĊĠĠĠ Ġif Ġ( require Activity (). intent . getBoolean Extra (" is LoggedIn ", Ġfalse )) Ġ{ ĊĠĠĠĠĠ Ġrequire Activity (). set Result ( Card Creator Activity . CHILD _ CARD _ CREATED , Ġintent ) ĊĠĠĠ Ġ} Ġelse Ġ{ ĊĠĠĠĠĠ Ġrequire Activity (). set Result ( Card Creator Activity . CARD _ CREATED , Ġintent ) ĊĠĠĠ Ġ} ĊĠĠĠ ĠCache ( require Context ()). clear () ĊĠĠĠ Ġrequire Activity (). finish () ĊĠ Ġ} Ċ } Ċ