-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
- Loading branch information
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Large diffs are not rendered by default.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Large diffs are not rendered by default.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Large diffs are not rendered by default.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package com.example.prefdatastore.data | ||
|
||
import android.content.Context | ||
import androidx.datastore.core.DataStore | ||
import androidx.datastore.preferences.core.Preferences | ||
import androidx.datastore.preferences.core.booleanPreferencesKey | ||
import androidx.datastore.preferences.core.edit | ||
import androidx.datastore.preferences.core.emptyPreferences | ||
import androidx.datastore.preferences.preferencesDataStore | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.map | ||
import kotlinx.coroutines.flow.catch | ||
import java.io.IOException | ||
|
||
|
||
private const val LAYOUT_PREFERENCES_NAME = "layout_preferences" | ||
|
||
private val Context.dataStore : DataStore<Preferences> by preferencesDataStore( | ||
name = LAYOUT_PREFERENCES_NAME | ||
) | ||
|
||
class SettingsDataStore(context:Context) { | ||
private val IS_LINEAR_LAYOUT_MANAGER = booleanPreferencesKey("is_linear_layout_manager") | ||
|
||
val preferenceFlow: Flow<Boolean> = context.dataStore.data | ||
.catch { | ||
if (it is IOException) { | ||
it.printStackTrace() | ||
emit(emptyPreferences()) | ||
} else { | ||
throw it | ||
} | ||
} | ||
.map { preferences -> | ||
// On the first run of the app, we will use LinearLayoutManager by default | ||
preferences[IS_LINEAR_LAYOUT_MANAGER] ?: true | ||
} | ||
|
||
suspend fun saveLayoutToPreferencesStore(isLinearLayoutManager: Boolean, context: Context){ | ||
//call dataStore.edit(), and pass in a block of code to store the new value. | ||
context.dataStore.edit { preferences-> | ||
preferences[IS_LINEAR_LAYOUT_MANAGER] = isLinearLayoutManager | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.