Reorganize some util files

This commit is contained in:
arkon
2020-02-02 22:04:11 -05:00
parent 9a3ec56eb4
commit 9f9de27a57
18 changed files with 65 additions and 18 deletions

View File

@@ -0,0 +1,42 @@
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 <reified T> SharedPreferences.getKey(key: String, default: T, dispatcher: CoroutineContext = Dispatchers.Default): Flow<T> {
val flow: Flow<T> = 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 <reified T> 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<String>) as T
is MutableSet<*> -> getStringSet(key, default as MutableSet<String>) as T
else -> throw IllegalArgumentException("Generic type not handled: ${T::class.java.name}")
}
}

View File

@@ -1,4 +1,4 @@
package eu.kanade.tachiyomi.util
package eu.kanade.tachiyomi.util.storage
import android.content.Context
import android.content.Intent
@@ -7,6 +7,7 @@ import android.os.Environment
import androidx.core.content.ContextCompat
import androidx.core.os.EnvironmentCompat
import com.hippo.unifile.UniFile
import eu.kanade.tachiyomi.util.Hash
import java.io.File
object DiskUtil {

View File

@@ -1,4 +1,4 @@
package eu.kanade.tachiyomi.util
package eu.kanade.tachiyomi.util.storage
import org.jsoup.Jsoup
import org.jsoup.nodes.Document

View File

@@ -1,4 +1,4 @@
package eu.kanade.tachiyomi.util
package eu.kanade.tachiyomi.util.storage
import android.content.Context
import android.net.Uri

View File

@@ -1,4 +1,4 @@
package eu.kanade.tachiyomi.util
package eu.kanade.tachiyomi.util.storage
import okio.BufferedSource
import okio.buffer