Created
October 4, 2021 07:18
-
-
Save RaheemJnr/e550c30e13e4dd858a73c6fe8a4678c4 to your computer and use it in GitHub Desktop.
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
class StoreUserEmail(private val context: Context) { | |
// to make sure there's only one instance | |
companion object { | |
private val Context.dataStoree: DataStore<Preferences> by preferencesDataStore("userEmail") | |
val USER_EMAIL_KEY = stringPreferencesKey("user_email") | |
} | |
//get the saved email | |
val getEmail: Flow<String?> = context.dataStoree.data | |
.map { preferences -> | |
preferences[USER_EMAIL_KEY] ?: "FirstLast@gmail.com" | |
} | |
//save email into datastore | |
suspend fun saveEmail(name: String) { | |
context.dataStoree.edit { preferences -> | |
preferences[USER_EMAIL_KEY] = name | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment