Last active
June 30, 2023 13:38
-
-
Save Shvet/c93f84825bee51eb0e07b252cb7c1633 to your computer and use it in GitHub Desktop.
Session Class to store user credential locally.
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.shvet.composelogin.di | |
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.core.stringPreferencesKey | |
import kotlinx.coroutines.flow.Flow | |
import kotlinx.coroutines.flow.catch | |
import kotlinx.coroutines.flow.map | |
import javax.inject.Inject | |
class Session @Inject constructor(private val dataStore: DataStore<Preferences>) { | |
companion object { | |
const val DATA = "Data" | |
private const val IsLogin = "IsLogin" | |
private const val NAME = "Name" | |
private const val PASSWORD = "Password" | |
val name = stringPreferencesKey(NAME) | |
val isLogin = booleanPreferencesKey(IsLogin) | |
val password = stringPreferencesKey(PASSWORD) | |
} | |
fun isUserLoggedIn(): Flow<Boolean> { | |
return dataStore.data.catch { | |
emit(emptyPreferences()) | |
}.map { preference -> | |
preference[isLogin] ?: false | |
} | |
} | |
suspend fun setUserLoggedIn(isUserLoggedIn: Boolean) { | |
dataStore.edit { preference -> | |
preference[isLogin] = isUserLoggedIn | |
} | |
} | |
fun getUserName(): Flow<String> { | |
return dataStore.data.catch { | |
emit(emptyPreferences()) | |
}.map { preferences -> | |
preferences[name] ?: "" | |
} | |
} | |
suspend fun setUserName(userName: String) { | |
dataStore.edit { preference -> | |
preference[name] = userName | |
} | |
} | |
fun getPassword(): Flow<String> { | |
return dataStore.data.catch { | |
emit(emptyPreferences()) | |
}.map { value: Preferences -> | |
value[password] ?: "" | |
} | |
} | |
suspend fun setPassword(userPassWord: String) { | |
dataStore.edit { preference -> | |
preference[password] = userPassWord | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment