1- package com.devbrackets.android.datastore
1+ package com.devbrackets.android.datastore.preference
22
33import androidx.datastore.core.DataStore
44import androidx.datastore.preferences.core.Preferences
55import com.devbrackets.android.datastore.converter.ValueConverter
66import com.devbrackets.android.datastore.converter.core.NoOpValueConverter
7- import com.devbrackets.android.datastore.delegate.getPreferencesKey
7+ import com.devbrackets.android.datastore.preference. delegate.getPreferencesKey
88import kotlinx.coroutines.CoroutineScope
99import kotlinx.coroutines.flow.Flow
1010import kotlinx.coroutines.flow.SharedFlow
@@ -33,8 +33,8 @@ inline fun <reified T> DataStore<Preferences>.sharedFlow(
3333 started : SharingStarted = SharingStarted .WhileSubscribed (replayExpirationMillis = 0),
3434 replay : Int = 0
3535): SharedFlow <T > {
36- return this . sharedFlow(
37- key = key,
36+ return sharedFlow(
37+ key = getPreferencesKey( key) ,
3838 defaultValue = defaultValue,
3939 converter = NoOpValueConverter (),
4040 scope = scope,
@@ -60,13 +60,48 @@ inline fun <reified T> DataStore<Preferences>.sharedFlow(
6060 * @param started The strategy that controls when sharing is started and stopped
6161 * @param replay The number of values replayed to new subscribers (cannot be negative, defaults to zero).
6262 */
63- inline fun <reified T , reified S > DataStore<Preferences>.sharedFlow (
63+ inline fun <T , reified S > DataStore<Preferences>.sharedFlow (
6464 key : String ,
6565 defaultValue : T ,
6666 converter : ValueConverter <T , S >,
6767 scope : CoroutineScope ,
6868 started : SharingStarted = SharingStarted .WhileSubscribed (replayExpirationMillis = 0),
6969 replay : Int = 0
70+ ): SharedFlow <T > {
71+ return sharedFlow(
72+ key = getPreferencesKey(key),
73+ defaultValue = defaultValue,
74+ converter = converter,
75+ scope = scope,
76+ started = started,
77+ replay = replay
78+ )
79+ }
80+
81+ /* *
82+ * A Kotlin delegate to retrieve a [SharedFlow] for values in a [Preferences] [DataStore].
83+ * For example
84+ * ```kotlin
85+ * val themeFlow = dataStore.sharedFlow("uiTheme", Theme.FOLLOW_SYSTEM, EnumValueConverter(Theme::class), viewModelScope)
86+ * ```
87+ *
88+ * @param key The unique id used to store and retrieve the preference, typically this is a
89+ * descriptive id such as "uiTheme"
90+ * @param defaultValue The value to return when [key] isn't contained by the [DataStore]
91+ * @param converter The [ValueConverter] to convert between the use type (e.g. an Enum)
92+ * and a format that the can be stored in preferences. Supported preference types
93+ * are `Int`, `Double`, `String`, `Boolean`, `Float`, and `Long`.
94+ * @param scope The [CoroutineScope] to use when constructing the [SharedFlow]
95+ * @param started The strategy that controls when sharing is started and stopped
96+ * @param replay The number of values replayed to new subscribers (cannot be negative, defaults to zero).
97+ */
98+ fun <T , S > DataStore<Preferences>.sharedFlow (
99+ key : Preferences .Key <S >,
100+ defaultValue : T ,
101+ converter : ValueConverter <T , S >,
102+ scope : CoroutineScope ,
103+ started : SharingStarted = SharingStarted .WhileSubscribed (replayExpirationMillis = 0),
104+ replay : Int = 0
70105): SharedFlow <T > {
71106 return this .flow(
72107 key = key,
@@ -115,16 +150,39 @@ inline fun <reified T> DataStore<Preferences>.flow(
115150 * and a format that the can be stored in preferences. Supported preference types
116151 * are `Int`, `Double`, `String`, `Boolean`, `Float`, and `Long`.
117152 */
118- inline fun <reified T , reified S > DataStore<Preferences>.flow (
153+ inline fun <T , reified S > DataStore<Preferences>.flow (
119154 key : String ,
120155 defaultValue : T ,
121156 converter : ValueConverter <T , S >
122157): Flow <T > {
123- val prefKey: Preferences .Key <S > = getPreferencesKey(key)
158+ return flow(
159+ key = getPreferencesKey(key),
160+ defaultValue = defaultValue,
161+ converter = converter
162+ )
163+ }
124164
165+ /* *
166+ * A Kotlin delegate to retrieve a [Flow] for values in a [Preferences] [DataStore].
167+ * For example
168+ * ```kotlin
169+ * val themeFlow = dataStore.flow("uiTheme", Theme.FOLLOW_SYSTEM, EnumValueConverter(Theme::class))
170+ * ```
171+ *
172+ * @param key The [Preferences.Key] used to store and retrieve the preference
173+ * @param defaultValue The value to return when [key] isn't contained by the [DataStore]
174+ * @param converter The [ValueConverter] to convert between the use type (e.g. an Enum)
175+ * and a format that the can be stored in preferences. Supported preference types
176+ * are `Int`, `Double`, `String`, `Boolean`, `Float`, and `Long`.
177+ */
178+ fun <T , S > DataStore<Preferences>.flow (
179+ key : Preferences .Key <S >,
180+ defaultValue : T ,
181+ converter : ValueConverter <T , S >
182+ ): Flow <T > {
125183 return data.map { prefs ->
126184 prefs.getOrDefault(
127- key = prefKey ,
185+ key = key ,
128186 defaultValue = defaultValue,
129187 converter = converter
130188 )
@@ -139,7 +197,7 @@ inline fun <reified T, reified S> DataStore<Preferences>.flow(
139197 * @param key The [Preferences.Key] used to retrieve the [Preferences] value
140198 * @param defaultValue The value to return when [key] isn't defined in [Preferences]
141199 */
142- inline fun <reified T > Preferences.getOrDefault (
200+ fun <T > Preferences.getOrDefault (
143201 key : Preferences .Key <T >,
144202 defaultValue : T
145203): T {
@@ -160,7 +218,8 @@ inline fun <reified T> Preferences.getOrDefault(
160218 * @param converter The [ValueConverter] used to convert between the stored type [S]
161219 * and the expected read/write type [T]
162220 */
163- inline fun <reified T , S > Preferences.getOrDefault (
221+ @Suppress(" UNCHECKED_CAST" )
222+ fun <T , S > Preferences.getOrDefault (
164223 key : Preferences .Key <S >,
165224 defaultValue : T ,
166225 converter : ValueConverter <T , S >
0 commit comments