Skip to content

Instantly share code, notes, and snippets.

@ahmed-shehataa
Created January 10, 2022 13:07
Show Gist options
  • Save ahmed-shehataa/4150effede7e9c76c228320e2dee84c4 to your computer and use it in GitHub Desktop.
Save ahmed-shehataa/4150effede7e9c76c228320e2dee84c4 to your computer and use it in GitHub Desktop.
A DataStoreManager for local database in android working with dagger-hilt(DI).
import android.content.Context
import androidx.datastore.preferences.core.*
import androidx.datastore.preferences.preferencesDataStore
import com.limeint.BuildConfig
import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.map
import javax.inject.Inject
import javax.inject.Singleton
const val BEARER = "Bearer"
private val Context.dataStore by preferencesDataStore("settings")
@Singleton //You can ignore this annotation as return `datastore` from `preferencesDataStore` is singletone
class DataStoreManager @Inject constructor(@ApplicationContext appContext: Context) {
private val mDataStore = appContext.dataStore
companion object {
//var context: Context? = null
const val IS_PASSED_INTRO = "passedIntro"
const val IS_LOGGED_IN = "loggedIn"
const val USER_TOKEN = "userToken"
const val LANGUAGE = "lang"
const val IS_DARK = "isDark"
}
/**
* for writing user token to datastore and mark user as logged in
*/
suspend fun writeUserTokenAndMarkAsLoggedIn(userToken: String) {
mDataStore.edit { settings ->
settings[booleanPreferencesKey(IS_LOGGED_IN)] = true
settings[stringPreferencesKey(USER_TOKEN)] = userToken
}
}
/**
* for reading user token from datastore
*/
suspend fun readUserToken(): String {
val bearer = BEARER
val fakeToken = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwczpcL1wvbGltZWVpbnQuY29tXC9iYXNlXC9wdWJsaWNcL2FwaVwvYXV0aFwvbG9naW4iLCJpYXQiOjE2NDE3MTAwODQsImV4cCI6MTY3MzI0NjA4NCwibmJmIjoxNjQxNzEwMDg0LCJqdGkiOiI3Mkt5Tno1MUgzdHJTYUJGIiwic3ViIjozLCJwcnYiOiIyM2JkNWM4OTQ5ZjYwMGFkYjM5ZTcwMWM0MDA4NzJkYjdhNTk3NmY3In0.UbsWTkZCYWE1yNnOxLG7zPchi4-9iFvNIJYTeA46xpM"
val token = if(BuildConfig.DEBUG) fakeToken else {
mDataStore.data.map { settings ->
settings[stringPreferencesKey(USER_TOKEN)] ?: ""
}.first().toString()
}
return "$bearer $token"
}
suspend fun write(key: String, value: String) {
mDataStore.edit { settings ->
settings[stringPreferencesKey(key)] = value
}
}
suspend fun read(key: String, defaultValue: String): String {
return mDataStore.data.map { settings ->
settings[stringPreferencesKey(key)] ?: defaultValue
}.first().toString()
}
suspend fun write(key: String, value: Int) {
mDataStore.edit { settings ->
settings[intPreferencesKey(key)] = value
}
}
suspend fun read(key: String, defaultValue: Int): Int {
return mDataStore.data.map { settings ->
settings[intPreferencesKey(key)] ?: defaultValue
}.first().toInt()
}
suspend fun write(key: String, value: Double) {
mDataStore.edit { settings ->
settings[doublePreferencesKey(key)] = value
}
}
suspend fun read(key: String, defaultValue: Double): Double {
return mDataStore.data.map { settings ->
settings[doublePreferencesKey(key)] ?: defaultValue
}.first().toDouble()
}
suspend fun write(key: String, value: Boolean) {
mDataStore.edit { settings ->
settings[booleanPreferencesKey(key)] = value
}
}
suspend fun read(key: String, defaultValue: Boolean): Boolean {
return mDataStore.data.map { settings ->
settings[booleanPreferencesKey(key)] ?: defaultValue
}.first()
}
fun readTheme(key: String, defaultValue: Boolean): Flow<Boolean> {
return mDataStore.data.map { settings ->
settings[booleanPreferencesKey(key)] ?: defaultValue
}
}
suspend fun clearDataStore() {
mDataStore.edit { it.clear() }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment