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
// Retriving Using the Flow Method | |
lifecycleScope.launch { | |
preferenceDataStoreHelper.getPreference(NAME_KEY,"").collect { | |
name = it | |
} | |
} | |
// Retriving Using the Not Flow / static data Method | |
lifecycleScope.launch { | |
val name = preferenceDataStoreHelper.getFirstPreference(NAME_KEY,"") |
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
// Retriving Using the Flow Method | |
viewModelScope.launch { | |
preferenceDataStoreHelper.getPreference(NAME_KEY,"").collect { | |
name = it | |
} | |
} | |
// Retriving Using the Not Flow / static data Method | |
viewModelScope.launch { | |
val name = preferenceDataStoreHelper.getFirstPreference(NAME_KEY,"") |
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.example.datastoretest.data.local | |
import android.content.Context | |
import androidx.datastore.preferences.core.Preferences | |
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.catch | |
import kotlinx.coroutines.flow.first |
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.example.datastoretest.data.local | |
import androidx.datastore.preferences.core.booleanPreferencesKey | |
import androidx.datastore.preferences.core.intPreferencesKey | |
import androidx.datastore.preferences.core.longPreferencesKey | |
import androidx.datastore.preferences.core.stringPreferencesKey | |
object PreferenceDataStoreConstants { | |
val IS_MINOR_KEY = booleanPreferencesKey("IS_MINOR_KEY") | |
val AGE_KEY = intPreferencesKey("AGE_KEY") |
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.example.datastoretest.data.local | |
import androidx.datastore.preferences.core.Preferences | |
import kotlinx.coroutines.flow.Flow | |
interface IPreferenceDataStoreAPI { | |
suspend fun <T> getPreference(key: Preferences.Key<T>,defaultValue: T):Flow<T> | |
suspend fun <T> getFirstPreference(key: Preferences.Key<T>,defaultValue: T):T | |
suspend fun <T> putPreference(key: Preferences.Key<T>,value:T) | |
suspend fun <T> removePreference(key: Preferences.Key<T>) |