30 lines
1.1 KiB
Kotlin
30 lines
1.1 KiB
Kotlin
package com.bllry.filedrop
|
|
|
|
import android.content.Context
|
|
import androidx.datastore.core.DataStore
|
|
import androidx.datastore.preferences.core.Preferences
|
|
import androidx.datastore.preferences.core.edit
|
|
import androidx.datastore.preferences.core.stringPreferencesKey
|
|
import androidx.datastore.preferences.preferencesDataStore
|
|
import kotlinx.coroutines.flow.Flow
|
|
import kotlinx.coroutines.flow.map
|
|
|
|
private val Context.dataStore: DataStore<Preferences> by preferencesDataStore(name = "settings")
|
|
|
|
/** Persisted connection settings: the PC address and the shared auth token. */
|
|
class Settings(private val context: Context) {
|
|
|
|
private val addressKey = stringPreferencesKey("address")
|
|
private val tokenKey = stringPreferencesKey("token")
|
|
|
|
val address: Flow<String> = context.dataStore.data.map { it[addressKey].orEmpty() }
|
|
val token: Flow<String> = context.dataStore.data.map { it[tokenKey].orEmpty() }
|
|
|
|
suspend fun setAddress(value: String) {
|
|
context.dataStore.edit { it[addressKey] = value.trim() }
|
|
}
|
|
|
|
suspend fun setToken(value: String) {
|
|
context.dataStore.edit { it[tokenKey] = value.trim() }
|
|
}
|
|
}
|