Last active
April 17, 2023 14:02
-
-
Save shibbirweb/d4e028e47f960d6d2308229ec67459c8 to your computer and use it in GitHub Desktop.
Android - Kotlin - Data Store Manager
This file contains hidden or 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
| package com.***.data.dataStoreManager | |
| import android.content.Context | |
| import androidx.datastore.core.DataStore | |
| import androidx.datastore.preferences.core.* | |
| import androidx.datastore.preferences.preferencesDataStore | |
| import kotlinx.coroutines.flow.Flow | |
| import kotlinx.coroutines.flow.first | |
| import kotlinx.coroutines.flow.map | |
| // At the top level of your kotlin file: | |
| val Context.userSettingsDataStore: DataStore<Preferences> by preferencesDataStore(name = "user_settings") | |
| object DataStoreManager { | |
| suspend fun saveValue(context: Context, key: String, value: String) { | |
| val wrappedKey = stringPreferencesKey(key) | |
| context.userSettingsDataStore.edit { | |
| it[wrappedKey] = value | |
| } | |
| } | |
| suspend fun saveValue(context: Context, key: String, value: Int) { | |
| val wrappedKey = intPreferencesKey(key) | |
| context.userSettingsDataStore.edit { | |
| it[wrappedKey] = value | |
| } | |
| } | |
| suspend fun saveValue(context: Context, key: String, value: Double) { | |
| val wrappedKey = doublePreferencesKey(key) | |
| context.userSettingsDataStore.edit { | |
| it[wrappedKey] = value | |
| } | |
| } | |
| suspend fun saveValue(context: Context, key: String, value: Long) { | |
| val wrappedKey = longPreferencesKey(key) | |
| context.userSettingsDataStore.edit { | |
| it[wrappedKey] = value | |
| } | |
| } | |
| suspend fun saveValue(context: Context, key: String, value: Boolean) { | |
| val wrappedKey = booleanPreferencesKey(key) | |
| context.userSettingsDataStore.edit { | |
| it[wrappedKey] = value | |
| } | |
| } | |
| suspend fun getStringValue(context: Context, key: String, default: String = ""): String { | |
| val wrappedKey = stringPreferencesKey(key) | |
| val valueFlow: Flow<String> = context.userSettingsDataStore.data.map { | |
| it[wrappedKey] ?: default | |
| } | |
| return valueFlow.first() | |
| } | |
| suspend fun getIntValue(context: Context, key: String, default: Int = 0): Int { | |
| val wrappedKey = intPreferencesKey(key) | |
| val valueFlow: Flow<Int> = context.userSettingsDataStore.data.map { | |
| it[wrappedKey] ?: default | |
| } | |
| return valueFlow.first() | |
| } | |
| suspend fun getDoubleValue(context: Context, key: String, default: Double = 0.0): Double { | |
| val wrappedKey = doublePreferencesKey(key) | |
| val valueFlow: Flow<Double> = context.userSettingsDataStore.data.map { | |
| it[wrappedKey] ?: default | |
| } | |
| return valueFlow.first() | |
| } | |
| suspend fun getLongValue(context: Context, key: String, default: Long = 0L): Long { | |
| val wrappedKey = longPreferencesKey(key) | |
| val valueFlow: Flow<Long> = context.userSettingsDataStore.data.map { | |
| it[wrappedKey] ?: default | |
| } | |
| return valueFlow.first() | |
| } | |
| suspend fun getBooleanValue(context: Context, key: String, default: Boolean = false): Boolean { | |
| val wrappedKey = booleanPreferencesKey(key) | |
| val valueFlow: Flow<Boolean> = context.userSettingsDataStore.data.map { | |
| it[wrappedKey] ?: default | |
| } | |
| return valueFlow.first() | |
| } | |
| } |
This file contains hidden or 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
| package com.***.datastoraexample | |
| import androidx.appcompat.app.AppCompatActivity | |
| import android.os.Bundle | |
| import android.widget.Button | |
| import android.widget.EditText | |
| import kotlinx.coroutines.Dispatchers | |
| import kotlinx.coroutines.GlobalScope | |
| import kotlinx.coroutines.launch | |
| import kotlinx.coroutines.withContext | |
| /** | |
| * Do you still use shared preferecnes????? no no no. | |
| * let's use data store in kotlin with coroutine | |
| * | |
| * 1. project setting | |
| * 2. xml | |
| * 3. data store manager | |
| * 4. let's test | |
| * 5. let's run app | |
| * - if I just click get button before I save value, than the edit text will show '123' | |
| * - again | |
| * - let's save value | |
| * - good, thanks | |
| * | |
| * youtube video url: https://youtu.be/Cw1_7Cp61mI | |
| */ | |
| class MainActivity : AppCompatActivity() { | |
| private lateinit var editText: EditText | |
| override fun onCreate(savedInstanceState: Bundle?) { | |
| super.onCreate(savedInstanceState) | |
| setContentView(R.layout.activity_main) | |
| editText = findViewById(R.id.editText) | |
| findViewById<Button>(R.id.buttonSave).setOnClickListener { | |
| val text = editText.text.toString().trim() | |
| if (text == "") { | |
| return@setOnClickListener | |
| } | |
| GlobalScope.launch { | |
| // don't forget "@MainActivity" | |
| DataStoreManager.saveValue(this@MainActivity, "testKey", text) | |
| } | |
| } | |
| findViewById<Button>(R.id.buttonGet).setOnClickListener { | |
| GlobalScope.launch(Dispatchers.IO) { | |
| // don't forget "@MainActivity" | |
| // DataStoreManager.getStringValue(this@MainActivity, "testKey") | |
| // or | |
| val value = DataStoreManager.getStringValue(this@MainActivity, "testKey", default = "123") | |
| withContext(Dispatchers.Main) { | |
| editText.setText(value) | |
| } | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment