Код: Выделить всё
class AuthPersistence(
private val context: Context
) {
private val Context.dataStore by preferencesDataStore(context.packageName)
actual suspend fun saveUsername(username: String) {
context.dataStore.edit {
it[usernameKey] = username
}
}
actual suspend fun getUsername(): String? {
return context.dataStore.data.firstOrNull()?.get(usernameKey)
}
private val usernameKey = stringPreferencesKey("user_name")
}
Мой подход мог бы заключаться в создании неизменяемой переменной, которая будет меняться каждый раз, когда я сохраняю новое имя пользователя. Но, как уже упоминалось; Я не уверен, что это путь Котлина.
Подробнее здесь: https://stackoverflow.com/questions/792 ... -functions
Мобильная версия