package me.ash.reader import android.content.Context import android.util.Log import androidx.datastore.core.DataStore import androidx.datastore.preferences.core.Preferences import androidx.datastore.preferences.core.edit import androidx.datastore.preferences.core.emptyPreferences import androidx.datastore.preferences.core.intPreferencesKey import androidx.datastore.preferences.preferencesDataStore import kotlinx.coroutines.flow.catch import kotlinx.coroutines.flow.first import kotlinx.coroutines.flow.map import kotlinx.coroutines.runBlocking import java.io.IOException val Context.dataStore: DataStore by preferencesDataStore(name = "settings") suspend fun DataStore.put(dataStoreKeys: DataStoreKeys, value: T) { this.edit { it[dataStoreKeys.key] = value } } @Suppress("UNCHECKED_CAST") fun DataStore.get(dataStoreKeys: DataStoreKeys): T? { return runBlocking { this@get.data.catch { exception -> if (exception is IOException) { Log.e("RLog", "Get data store error $exception") exception.printStackTrace() emit(emptyPreferences()) } else { throw exception } }.map { it[dataStoreKeys.key] }.first() as T } } sealed class DataStoreKeys { abstract val key: Preferences.Key object CurrentAccountId : DataStoreKeys() { override val key: Preferences.Key get() = intPreferencesKey("currentAccountId") } }