Skip to content

Instantly share code, notes, and snippets.

@RaheemJnr
Created October 4, 2021 07:18
Show Gist options
  • Save RaheemJnr/e550c30e13e4dd858a73c6fe8a4678c4 to your computer and use it in GitHub Desktop.
Save RaheemJnr/e550c30e13e4dd858a73c6fe8a4678c4 to your computer and use it in GitHub Desktop.
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