package eu.kanade.tachiyomi.util import android.content.SharedPreferences import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.channels.awaitClose import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.channelFlow import kotlinx.coroutines.flow.flowOn import kotlin.coroutines.CoroutineContext @ExperimentalCoroutinesApi inline fun SharedPreferences.getKey(key: String, default: T, dispatcher: CoroutineContext = Dispatchers.Default): Flow { val flow: Flow = channelFlow { offer(getItem(key, default)) val listener = SharedPreferences.OnSharedPreferenceChangeListener { _, k -> if (key == k) { offer(getItem(key, default)!!) } } registerOnSharedPreferenceChangeListener(listener) awaitClose { unregisterOnSharedPreferenceChangeListener(listener) } } return flow .flowOn(dispatcher) } inline fun SharedPreferences.getItem(key: String, default: T): T { @Suppress("UNCHECKED_CAST") return when (default) { is String -> getString(key, default) as T is Int -> getInt(key, default) as T is Long -> getLong(key, default) as T is Boolean -> getBoolean(key, default) as T is Float -> getFloat(key, default) as T is Set<*> -> getStringSet(key, default as Set) as T is MutableSet<*> -> getStringSet(key, default as MutableSet) as T else -> throw IllegalArgumentException("Generic type not handled: ${T::class.java.name}") } }