From aa1d927da6bdbf5ca9ccadbe69117351353c38de Mon Sep 17 00:00:00 2001 From: arkon Date: Fri, 24 Apr 2020 11:58:16 -0400 Subject: [PATCH] More FlowPreference migration --- .../tachiyomi/data/backup/BackupCreatorJob.kt | 3 +-- .../tachiyomi/data/download/DownloadCache.kt | 15 +++++++-------- .../tachiyomi/data/download/DownloadProvider.kt | 16 +++++++++++----- .../data/preference/PreferencesHelper.kt | 8 ++++---- .../tachiyomi/data/track/anilist/Anilist.kt | 9 ++++----- .../data/track/anilist/AnilistModels.kt | 3 +-- .../tachiyomi/data/track/bangumi/Bangumi.kt | 2 +- .../data/track/myanimelist/MyAnimeList.kt | 3 +-- .../tachiyomi/data/track/shikimori/Shikimori.kt | 2 +- .../ui/setting/SettingsBackupController.kt | 16 ++++++++-------- .../ui/setting/SettingsDownloadController.kt | 14 +++++++------- 11 files changed, 46 insertions(+), 45 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/backup/BackupCreatorJob.kt b/app/src/main/java/eu/kanade/tachiyomi/data/backup/BackupCreatorJob.kt index 7dae0653cb..581b082a7a 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/backup/BackupCreatorJob.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/backup/BackupCreatorJob.kt @@ -8,7 +8,6 @@ import androidx.work.WorkManager import androidx.work.Worker import androidx.work.WorkerParameters import eu.kanade.tachiyomi.data.preference.PreferencesHelper -import eu.kanade.tachiyomi.data.preference.getOrDefault import java.util.concurrent.TimeUnit import uy.kohesive.injekt.Injekt import uy.kohesive.injekt.api.get @@ -19,7 +18,7 @@ class BackupCreatorJob(private val context: Context, workerParams: WorkerParamet override fun doWork(): Result { val preferences = Injekt.get() val backupManager = BackupManager(context) - val uri = Uri.parse(preferences.backupsDirectory().getOrDefault()) + val uri = Uri.parse(preferences.backupsDirectory().get()) val flags = BackupCreateService.BACKUP_ALL backupManager.createBackup(uri, flags, true) return Result.success() diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadCache.kt b/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadCache.kt index aaa2d7c2b9..6082cfdb9b 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadCache.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadCache.kt @@ -6,9 +6,9 @@ import com.hippo.unifile.UniFile import eu.kanade.tachiyomi.data.database.models.Chapter import eu.kanade.tachiyomi.data.database.models.Manga import eu.kanade.tachiyomi.data.preference.PreferencesHelper -import eu.kanade.tachiyomi.data.preference.getOrDefault import eu.kanade.tachiyomi.source.SourceManager import java.util.concurrent.TimeUnit +import kotlinx.coroutines.flow.onEach import uy.kohesive.injekt.Injekt import uy.kohesive.injekt.api.get @@ -47,19 +47,18 @@ class DownloadCache( private var rootDir = RootDirectory(getDirectoryFromPreference()) init { - preferences.downloadsDirectory().asObservable() - .skip(1) - .subscribe { - lastRenew = 0L // invalidate cache - rootDir = RootDirectory(getDirectoryFromPreference()) - } + preferences.downloadsDirectory().asFlow() + .onEach { + lastRenew = 0L // invalidate cache + rootDir = RootDirectory(getDirectoryFromPreference()) + } } /** * Returns the downloads directory from the user's preferences. */ private fun getDirectoryFromPreference(): UniFile { - val dir = preferences.downloadsDirectory().getOrDefault() + val dir = preferences.downloadsDirectory().get() return UniFile.fromUri(context, Uri.parse(dir)) } diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadProvider.kt b/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadProvider.kt index 6413549b12..1d5f04ea75 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadProvider.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadProvider.kt @@ -7,9 +7,13 @@ import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.data.database.models.Chapter import eu.kanade.tachiyomi.data.database.models.Manga import eu.kanade.tachiyomi.data.preference.PreferencesHelper -import eu.kanade.tachiyomi.data.preference.getOrDefault import eu.kanade.tachiyomi.source.Source import eu.kanade.tachiyomi.util.storage.DiskUtil +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.Job +import kotlinx.coroutines.flow.launchIn +import kotlinx.coroutines.flow.onEach import uy.kohesive.injekt.injectLazy /** @@ -22,19 +26,21 @@ class DownloadProvider(private val context: Context) { private val preferences: PreferencesHelper by injectLazy() + private val scope = CoroutineScope(Job() + Dispatchers.Main) + /** * The root directory for downloads. */ - private var downloadsDir = preferences.downloadsDirectory().getOrDefault().let { + private var downloadsDir = preferences.downloadsDirectory().get().let { val dir = UniFile.fromUri(context, Uri.parse(it)) DiskUtil.createNoMediaFile(dir, context) dir } init { - preferences.downloadsDirectory().asObservable() - .skip(1) - .subscribe { downloadsDir = UniFile.fromUri(context, Uri.parse(it)) } + preferences.downloadsDirectory().asFlow() + .onEach { downloadsDir = UniFile.fromUri(context, Uri.parse(it)) } + .launchIn(scope) } /** diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt b/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt index 652d73885b..3bd69b4b5d 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt @@ -153,15 +153,15 @@ class PreferencesHelper(val context: Context) { .apply() } - fun trackToken(sync: TrackService) = rxPrefs.getString(Keys.trackToken(sync.id), "") + fun trackToken(sync: TrackService) = flowPrefs.getString(Keys.trackToken(sync.id), "") - fun anilistScoreType() = rxPrefs.getString("anilist_score_type", Anilist.POINT_10) + fun anilistScoreType() = flowPrefs.getString("anilist_score_type", Anilist.POINT_10) - fun backupsDirectory() = rxPrefs.getString(Keys.backupDirectory, defaultBackupDir.toString()) + fun backupsDirectory() = flowPrefs.getString(Keys.backupDirectory, defaultBackupDir.toString()) fun dateFormat() = rxPrefs.getObject(Keys.dateFormat, DateFormat.getDateInstance(DateFormat.SHORT), DateFormatConverter()) - fun downloadsDirectory() = rxPrefs.getString(Keys.downloadsDirectory, defaultDownloadsDir.toString()) + fun downloadsDirectory() = flowPrefs.getString(Keys.downloadsDirectory, defaultDownloadsDir.toString()) fun downloadOnlyOverWifi() = prefs.getBoolean(Keys.downloadOnlyOverWifi, true) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/track/anilist/Anilist.kt b/app/src/main/java/eu/kanade/tachiyomi/data/track/anilist/Anilist.kt index 2cc10f8f62..bba0ea0aaf 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/track/anilist/Anilist.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/track/anilist/Anilist.kt @@ -5,7 +5,6 @@ import android.graphics.Color import com.google.gson.Gson import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.data.database.models.Track -import eu.kanade.tachiyomi.data.preference.getOrDefault import eu.kanade.tachiyomi.data.track.TrackService import eu.kanade.tachiyomi.data.track.model.TrackSearch import rx.Completable @@ -75,7 +74,7 @@ class Anilist(private val context: Context, id: Int) : TrackService(id) { override fun getCompletionStatus(): Int = COMPLETED override fun getScoreList(): List { - return when (scorePreference.getOrDefault()) { + return when (scorePreference.get()) { // 10 point POINT_10 -> IntRange(0, 10).map(Int::toString) // 100 point @@ -91,7 +90,7 @@ class Anilist(private val context: Context, id: Int) : TrackService(id) { } override fun indexToScore(index: Int): Float { - return when (scorePreference.getOrDefault()) { + return when (scorePreference.get()) { // 10 point POINT_10 -> index * 10f // 100 point @@ -115,7 +114,7 @@ class Anilist(private val context: Context, id: Int) : TrackService(id) { override fun displayScore(track: Track): String { val score = track.score - return when (scorePreference.getOrDefault()) { + return when (scorePreference.get()) { POINT_5 -> when (score) { 0f -> "0 ★" else -> "${((score + 10) / 20).toInt()} ★" @@ -193,7 +192,7 @@ class Anilist(private val context: Context, id: Int) : TrackService(id) { override fun logout() { super.logout() - preferences.trackToken(this).set(null) + preferences.trackToken(this).delete() interceptor.setAuth(null) } diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/track/anilist/AnilistModels.kt b/app/src/main/java/eu/kanade/tachiyomi/data/track/anilist/AnilistModels.kt index 115a6e9058..8c8cfd5763 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/track/anilist/AnilistModels.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/track/anilist/AnilistModels.kt @@ -2,7 +2,6 @@ package eu.kanade.tachiyomi.data.track.anilist import eu.kanade.tachiyomi.data.database.models.Track import eu.kanade.tachiyomi.data.preference.PreferencesHelper -import eu.kanade.tachiyomi.data.preference.getOrDefault import eu.kanade.tachiyomi.data.track.TrackManager import eu.kanade.tachiyomi.data.track.model.TrackSearch import java.text.SimpleDateFormat @@ -80,7 +79,7 @@ fun Track.toAnilistStatus() = when (status) { private val preferences: PreferencesHelper by injectLazy() -fun Track.toAnilistScore(): String = when (preferences.anilistScoreType().getOrDefault()) { +fun Track.toAnilistScore(): String = when (preferences.anilistScoreType().get()) { // 10 point "POINT_10" -> (score.toInt() / 10).toString() // 100 point diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/track/bangumi/Bangumi.kt b/app/src/main/java/eu/kanade/tachiyomi/data/track/bangumi/Bangumi.kt index abe4115ef6..bb042f6454 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/track/bangumi/Bangumi.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/track/bangumi/Bangumi.kt @@ -126,7 +126,7 @@ class Bangumi(private val context: Context, id: Int) : TrackService(id) { override fun logout() { super.logout() - preferences.trackToken(this).set(null) + preferences.trackToken(this).delete() interceptor.newAuth(null) } diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/track/myanimelist/MyAnimeList.kt b/app/src/main/java/eu/kanade/tachiyomi/data/track/myanimelist/MyAnimeList.kt index b98caef18c..48a959b454 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/track/myanimelist/MyAnimeList.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/track/myanimelist/MyAnimeList.kt @@ -4,7 +4,6 @@ import android.content.Context import android.graphics.Color import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.data.database.models.Track -import eu.kanade.tachiyomi.data.preference.getOrDefault import eu.kanade.tachiyomi.data.track.TrackService import eu.kanade.tachiyomi.data.track.model.TrackSearch import okhttp3.HttpUrl.Companion.toHttpUrlOrNull @@ -145,7 +144,7 @@ class MyAnimeList(private val context: Context, id: Int) : TrackService(id) { getCSRF().isNotEmpty() && checkCookies() - fun getCSRF(): String = preferences.trackToken(this).getOrDefault() + fun getCSRF(): String = preferences.trackToken(this).get() private fun saveCSRF(csrf: String) = preferences.trackToken(this).set(csrf) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/track/shikimori/Shikimori.kt b/app/src/main/java/eu/kanade/tachiyomi/data/track/shikimori/Shikimori.kt index 95f8c89084..3469e84035 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/track/shikimori/Shikimori.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/track/shikimori/Shikimori.kt @@ -131,7 +131,7 @@ class Shikimori(private val context: Context, id: Int) : TrackService(id) { override fun logout() { super.logout() - preferences.trackToken(this).set(null) + preferences.trackToken(this).delete() interceptor.newAuth(null) } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsBackupController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsBackupController.kt index 978641109e..3f56d7ed3b 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsBackupController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsBackupController.kt @@ -22,7 +22,6 @@ import eu.kanade.tachiyomi.data.backup.BackupCreatorJob import eu.kanade.tachiyomi.data.backup.BackupRestoreService import eu.kanade.tachiyomi.data.backup.models.Backup import eu.kanade.tachiyomi.data.preference.PreferenceKeys as Keys -import eu.kanade.tachiyomi.data.preference.getOrDefault import eu.kanade.tachiyomi.ui.base.controller.DialogController import eu.kanade.tachiyomi.ui.base.controller.requestPermissionsSafe import eu.kanade.tachiyomi.ui.setting.backup.BackupNotifier @@ -125,7 +124,7 @@ class SettingsBackupController : SettingsController() { titleRes = R.string.pref_backup_directory onClick { - val currentDir = preferences.backupsDirectory().getOrDefault() + val currentDir = preferences.backupsDirectory().get() try { val intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE) startActivityForResult(intent, CODE_BACKUP_DIR) @@ -135,11 +134,12 @@ class SettingsBackupController : SettingsController() { } } - preferences.backupsDirectory().asObservable() - .subscribeUntilDestroy { path -> - val dir = UniFile.fromUri(context, Uri.parse(path)) - summary = dir.filePath + "/automatic" - } + preferences.backupsDirectory().asFlow() + .onEach { path -> + val dir = UniFile.fromUri(context, Uri.parse(path)) + summary = dir.filePath + "/automatic" + } + .launchIn(scope) } val backupNumber = intListPreference { key = Keys.numberOfBackups @@ -210,7 +210,7 @@ class SettingsBackupController : SettingsController() { backupFlags = flags // Get dirs - val currentDir = preferences.backupsDirectory().getOrDefault() + val currentDir = preferences.backupsDirectory().get() try { // Use Android's built-in file creator diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsDownloadController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsDownloadController.kt index de18b96c9e..07a723b313 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsDownloadController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsDownloadController.kt @@ -17,7 +17,6 @@ import eu.kanade.tachiyomi.data.database.DatabaseHelper import eu.kanade.tachiyomi.data.database.models.Category import eu.kanade.tachiyomi.data.preference.PreferenceKeys as Keys import eu.kanade.tachiyomi.data.preference.PreferencesHelper -import eu.kanade.tachiyomi.data.preference.getOrDefault import eu.kanade.tachiyomi.ui.base.controller.DialogController import eu.kanade.tachiyomi.util.preference.defaultValue import eu.kanade.tachiyomi.util.preference.entriesRes @@ -52,11 +51,12 @@ class SettingsDownloadController : SettingsController() { ctrl.showDialog(router) } - preferences.downloadsDirectory().asObservable() - .subscribeUntilDestroy { path -> - val dir = UniFile.fromUri(context, Uri.parse(path)) - summary = dir.filePath ?: path - } + preferences.downloadsDirectory().asFlow() + .onEach { path -> + val dir = UniFile.fromUri(context, Uri.parse(path)) + summary = dir.filePath ?: path + } + .launchIn(scope) } switchPreference { key = Keys.downloadOnlyOverWifi @@ -159,7 +159,7 @@ class SettingsDownloadController : SettingsController() { override fun onCreateDialog(savedViewState: Bundle?): Dialog { val activity = activity!! - val currentDir = preferences.downloadsDirectory().getOrDefault() + val currentDir = preferences.downloadsDirectory().get() val externalDirs = (getExternalDirs() + File(activity.getString(R.string.custom_dir))).map(File::toString) val selectedIndex = externalDirs.indexOfFirst { it in currentDir }