-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathAndroidExt.kt
102 lines (94 loc) · 3.81 KB
/
AndroidExt.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package com.arkivanov.essenty.statekeeper
import android.os.Bundle
import androidx.savedstate.SavedStateRegistry
import androidx.savedstate.SavedStateRegistryOwner
private const val KEY_STATE = "STATE_KEEPER_STATE"
/**
* Creates a new instance of [StateKeeper] and attaches it to the provided AndroidX [SavedStateRegistry].
*
* @param savedStateRegistry a [SavedStateRegistry] to attach the returned [StateKeeper] to.
* @param discardSavedState a flag indicating whether any previously saved state should be discarded or not,
* default value is `false`.
* @param isSavingAllowed called before saving the state.
* When `true` then the state will be saved, otherwise it won't. Default value is `true`.
*/
fun StateKeeper(
savedStateRegistry: SavedStateRegistry,
discardSavedState: Boolean = false,
isSavingAllowed: () -> Boolean = { true },
): StateKeeper =
StateKeeper(
savedStateRegistry = savedStateRegistry,
key = KEY_STATE,
discardSavedState = discardSavedState,
isSavingAllowed = isSavingAllowed,
)
/**
* Creates a new instance of [StateKeeper] and attaches it to the provided AndroidX [SavedStateRegistry].
*
* @param savedStateRegistry a [SavedStateRegistry] to attach the returned [StateKeeper] to.
* @param key a key to access the provided [SavedStateRegistry], to be used by the returned [StateKeeper].
* @param discardSavedState a flag indicating whether any previously saved state should be discarded or not,
* default value is `false`.
* @param isSavingAllowed called before saving the state.
* When `true` then the state will be saved, otherwise it won't. Default value is `true`.
*/
fun StateKeeper(
savedStateRegistry: SavedStateRegistry,
key: String,
discardSavedState: Boolean = false,
isSavingAllowed: () -> Boolean = { true },
): StateKeeper {
val dispatcher =
StateKeeperDispatcher(
savedState = savedStateRegistry
.consumeRestoredStateForKey(key = key)
?.getSerializableContainer(key = KEY_STATE)
?.takeUnless { discardSavedState },
)
savedStateRegistry.registerSavedStateProvider(key = key) {
Bundle().apply {
if (isSavingAllowed()) {
putSerializableContainer(key = KEY_STATE, value = dispatcher.save())
}
}
}
return dispatcher
}
/**
* Creates a new instance of [StateKeeper] and attaches it to the AndroidX [SavedStateRegistry].
*
* @param discardSavedState a flag indicating whether any previously saved state should be discarded or not,
* default value is `false`.
* @param isSavingAllowed called before saving the state.
* When `true` then the state will be saved, otherwise it won't. Default value is `true`.
*/
fun SavedStateRegistryOwner.stateKeeper(
discardSavedState: Boolean = false,
isSavingAllowed: () -> Boolean = { true },
): StateKeeper =
stateKeeper(
key = KEY_STATE,
discardSavedState = discardSavedState,
isSavingAllowed = isSavingAllowed,
)
/**
* Creates a new instance of [StateKeeper] and attaches it to the AndroidX [SavedStateRegistry].
*
* @param key a key to access this [SavedStateRegistry], to be used by the returned [StateKeeper].
* @param discardSavedState a flag indicating whether any previously saved state should be discarded or not,
* default value is `false`.
* @param isSavingAllowed called before saving the state.
* When `true` then the state will be saved, otherwise it won't. Default value is `true`.
*/
fun SavedStateRegistryOwner.stateKeeper(
key: String,
discardSavedState: Boolean = false,
isSavingAllowed: () -> Boolean = { true },
): StateKeeper =
StateKeeper(
savedStateRegistry = savedStateRegistry,
key = key,
discardSavedState = discardSavedState,
isSavingAllowed = isSavingAllowed
)