Last active
April 24, 2021 13:56
-
-
Save hamurcuabi/63d627d999703a81906581bbbfe5b1e3 to your computer and use it in GitHub Desktop.
Datastore demoData store demo
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
// Preferences DataStore | |
implementation "androidx.datastore:datastore-preferences:1.0.0-beta01" | |
// Lifecycle components | |
implementation "androidx.lifecycle:lifecycle-extensions:2.2.0" | |
implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.3.1" | |
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.1" | |
implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.3.1" |
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
package com.emrehmrc.pref_datastore.helper | |
import android.content.Context | |
import androidx.datastore.preferences.core.* | |
import androidx.datastore.preferences.preferencesDataStore | |
/** | |
* Rev 1.0 | |
* Author EmreHamurcu | |
* Date 4/24/2021 | |
* FileName DataStoreHelper | |
*/ | |
const val USER_PREFERENCES_NAME = "data_store_preferences" | |
//extension for data store | |
val Context.myDataStore by preferencesDataStore( | |
name = USER_PREFERENCES_NAME | |
) | |
object PreferenceKeys { | |
val MY_STRING = stringPreferencesKey("my_string") | |
val MY_BOOLEAN = booleanPreferencesKey("my_boolean") | |
val MY_INT = intPreferencesKey("my_int") | |
val MY_LONG = longPreferencesKey("my_long") | |
val MY_FLOAT = floatPreferencesKey("my_float") | |
} |
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
package com.emrehmrc.pref_datastore.repo | |
import android.content.Context | |
import androidx.datastore.preferences.core.Preferences | |
import androidx.datastore.preferences.core.edit | |
import androidx.datastore.preferences.core.emptyPreferences | |
import com.emrehmrc.pref_datastore.helper.myDataStore | |
import kotlinx.coroutines.flow.Flow | |
import kotlinx.coroutines.flow.catch | |
import kotlinx.coroutines.flow.map | |
import java.io.IOException | |
/** | |
* Rev 1.0 | |
* Author EmreHamurcu | |
* Date 4/24/2021 | |
* FileName DataStoreRepository | |
*/ | |
class DataStoreRepository(private val context: Context) { | |
fun readStringFromDataStore(key: Preferences.Key<String>): Flow<String> { | |
return context.myDataStore.data | |
.catch { ex -> | |
if (ex is IOException) { | |
emit(emptyPreferences()) | |
} else throw ex | |
} | |
.map { preferences -> | |
val showCompleted = preferences[key] ?: "null" | |
showCompleted | |
} | |
} | |
fun readBooleanFromDataStore(key: Preferences.Key<Boolean>): Flow<Boolean> { | |
return context.myDataStore.data | |
.catch { ex -> | |
if (ex is IOException) { | |
emit(emptyPreferences()) | |
} else throw ex | |
} | |
.map { preferences -> | |
val isChecked = preferences[key] ?: false | |
isChecked | |
} | |
} | |
fun readIntegerFromDataStore(key: Preferences.Key<Int>): Flow<Int> { | |
return context.myDataStore.data | |
.catch { ex -> | |
if (ex is IOException) { | |
emit(emptyPreferences()) | |
} else throw ex | |
} | |
.map { preferences -> | |
val isChecked = preferences[key] ?: -1 | |
isChecked | |
} | |
} | |
fun readLongFromDataStore(key: Preferences.Key<Long>): Flow<Long> { | |
return context.myDataStore.data | |
.catch { ex -> | |
if (ex is IOException) { | |
emit(emptyPreferences()) | |
} else throw ex | |
} | |
.map { preferences -> | |
val isChecked = preferences[key] ?: 0 | |
isChecked | |
} | |
} | |
fun readFloatFromDataStore(key: Preferences.Key<Float>): Flow<Float> { | |
return context.myDataStore.data | |
.catch { ex -> | |
if (ex is IOException) { | |
emit(emptyPreferences()) | |
} else throw ex | |
} | |
.map { preferences -> | |
val isChecked = preferences[key] ?: 0f | |
isChecked | |
} | |
} | |
suspend fun saveStringToDataStore(key: Preferences.Key<String>, name: String) { | |
context.myDataStore.edit { preferences -> | |
preferences[key] = name | |
} | |
} | |
suspend fun saveBooleanToDataStore(key: Preferences.Key<Boolean>, isChecked: Boolean) { | |
context.myDataStore.edit { preferences -> | |
preferences[key] = isChecked | |
} | |
} | |
suspend fun saveIntToDataStore(key: Preferences.Key<Int>, int: Int) { | |
context.myDataStore.edit { preferences -> | |
preferences[key] = int | |
} | |
} | |
suspend fun saveLongToDataStore(key: Preferences.Key<Long>, long: Long) { | |
context.myDataStore.edit { preferences -> | |
preferences[key] = long | |
} | |
} | |
suspend fun saveFloatToDataStore(key: Preferences.Key<Float>, float: Float) { | |
context.myDataStore.edit { preferences -> | |
preferences[key] = float | |
} | |
} | |
} |
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
package com.emrehmrc.pref_datastore.ui | |
import android.os.Bundle | |
import androidx.appcompat.app.AppCompatActivity | |
import androidx.lifecycle.ViewModelProvider | |
import com.emrehmrc.pref_datastore.MainActivityViewModel | |
import com.emrehmrc.pref_datastore.MainActivityViewModelFactory | |
import com.emrehmrc.pref_datastore.databinding.ActivityMainBinding | |
/** | |
* Rev 1.0 | |
* Author EmreHamurcu | |
* Date 4/24/2021 | |
* FileName MainActivity | |
*/ | |
class MainActivity : AppCompatActivity() { | |
private lateinit var mainViewModel: MainActivityViewModel | |
private lateinit var binding: ActivityMainBinding | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
binding = ActivityMainBinding.inflate(layoutInflater) | |
setContentView(binding.root) | |
mainViewModel = ViewModelProvider(this, MainActivityViewModelFactory(application)).get( | |
MainActivityViewModel::class.java | |
) | |
//Read String | |
mainViewModel.readStringDataStore.observe(this, { | |
binding.txtName.text = it | |
}) | |
//Read boolean | |
mainViewModel.readBooleanDataStore.observe(this, { | |
binding.cb.isChecked = it | |
}) | |
//Update data store | |
binding.btnUpdate.setOnClickListener { | |
mainViewModel.saveStringToDataStore(binding.edtName.text.toString()) | |
mainViewModel.saveBooleanToDataStore(binding.cb.isChecked) | |
} | |
} | |
} |
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
package com.emrehmrc.pref_datastore | |
import android.app.Application | |
import androidx.lifecycle.* | |
import com.emrehmrc.pref_datastore.helper.PreferenceKeys | |
import com.emrehmrc.pref_datastore.repo.DataStoreRepository | |
import kotlinx.coroutines.Dispatchers | |
import kotlinx.coroutines.launch | |
/** | |
* Rev 1.0 | |
* Author EmreHamurcu | |
* Date 4/24/2021 | |
* FileName MainActivityViewModel | |
*/ | |
class MainActivityViewModel( | |
application: Application, | |
) : AndroidViewModel(application) { | |
private val repository = DataStoreRepository(application) | |
val readStringDataStore = | |
repository.readStringFromDataStore(PreferenceKeys.MY_STRING) | |
.asLiveData() | |
val readBooleanDataStore = | |
repository.readBooleanFromDataStore(PreferenceKeys.MY_BOOLEAN) | |
.asLiveData() | |
val readIntDataStore = | |
repository.readIntegerFromDataStore(PreferenceKeys.MY_INT) | |
.asLiveData() | |
fun saveStringToDataStore(myName: String) = viewModelScope.launch(Dispatchers.IO) { | |
repository.saveStringToDataStore(PreferenceKeys.MY_STRING, myName) | |
} | |
fun saveBooleanToDataStore(isChecked: Boolean) = viewModelScope.launch(Dispatchers.IO) { | |
repository.saveBooleanToDataStore(PreferenceKeys.MY_BOOLEAN, isChecked) | |
} | |
fun saveIntToDataStore(isChecked: Boolean) = viewModelScope.launch(Dispatchers.IO) { | |
repository.saveBooleanToDataStore(PreferenceKeys.MY_BOOLEAN, isChecked) | |
} | |
} | |
class MainActivityViewModelFactory( | |
private val application: Application, | |
) : ViewModelProvider.Factory { | |
override fun <T : ViewModel> create(modelClass: Class<T>): T { | |
if (modelClass.isAssignableFrom(MainActivityViewModel::class.java)) { | |
@Suppress("UNCHECKED_CAST") | |
return MainActivityViewModel(application) as T | |
} | |
throw IllegalArgumentException("Unknown ViewModel class") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment