-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(bank-sdk): Convert the digital invoice's
HelpActivity
into…
… a fragment Needs to be added to the Bank SDK's navigation graph. BSDK-258
- Loading branch information
1 parent
681f362
commit 6bebd9d
Showing
7 changed files
with
83 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
bank-sdk/sdk/src/main/java/net/gini/android/bank/sdk/util/AutoClearedValue.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package net.gini.android.bank.sdk.util | ||
|
||
import androidx.fragment.app.Fragment | ||
import androidx.lifecycle.DefaultLifecycleObserver | ||
import androidx.lifecycle.LifecycleOwner | ||
import kotlin.properties.ReadWriteProperty | ||
import kotlin.reflect.KProperty | ||
|
||
/** | ||
* A lazy property that gets cleaned up when the fragment's view is destroyed. | ||
* | ||
* Accessing this variable while the fragment's view is destroyed will throw NPE. | ||
*/ | ||
internal class AutoClearedValue<T : Any>(val fragment: Fragment) : ReadWriteProperty<Fragment, T> { | ||
private var _value: T? = null | ||
|
||
init { | ||
fragment.lifecycle.addObserver(object: DefaultLifecycleObserver { | ||
override fun onCreate(owner: LifecycleOwner) { | ||
fragment.viewLifecycleOwnerLiveData.observe(fragment) { viewLifecycleOwner -> | ||
viewLifecycleOwner?.lifecycle?.addObserver(object: DefaultLifecycleObserver { | ||
override fun onDestroy(owner: LifecycleOwner) { | ||
_value = null | ||
} | ||
}) | ||
} | ||
} | ||
}) | ||
} | ||
|
||
override fun getValue(thisRef: Fragment, property: KProperty<*>): T { | ||
return _value ?: throw IllegalStateException( | ||
"should never call auto-cleared-value get when it might not be available" | ||
) | ||
} | ||
|
||
override fun setValue(thisRef: Fragment, property: KProperty<*>, value: T) { | ||
_value = value | ||
} | ||
} | ||
|
||
/** | ||
* Creates an [AutoClearedValue] associated with this fragment. | ||
*/ | ||
internal fun <T : Any> Fragment.autoCleared() = AutoClearedValue<T>(this) |
File renamed without changes.
File renamed without changes.
File renamed without changes.