From 47f6c54412e703cb4a54cbe2b59a1c55aa6fc80d Mon Sep 17 00:00:00 2001 From: Jays2Kings Date: Mon, 26 Apr 2021 00:55:37 -0400 Subject: [PATCH] Helper method for list of preferences flow --- .../data/preference/PreferencesHelper.kt | 11 +++++++++++ .../tachiyomi/ui/library/LibraryController.kt | 13 +++---------- .../eu/kanade/tachiyomi/ui/main/MainActivity.kt | 4 ++-- .../kanade/tachiyomi/ui/reader/ReaderActivity.kt | 7 ++----- .../tachiyomi/ui/recents/RecentsPresenter.kt | 15 ++++----------- 5 files changed, 22 insertions(+), 28 deletions(-) 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 bbafae4217..dfbdbba4c5 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 @@ -43,6 +43,17 @@ fun com.tfcporciuncula.flow.Preference.asImmediateFlowIn(scope: Coroutine .launchIn(scope) } +fun Collection>.asFlowsIn(scope: CoroutineScope, dropFirst: Boolean = false, block: () -> Unit): Collection { + return map { pref -> + pref.asFlow() + .apply { + if (dropFirst) drop(1) + } + .onEach { block() } + .launchIn(scope) + } +} + fun com.tfcporciuncula.flow.Preference.toggle() = set(!get()) private class DateFormatConverter : Preference.Adapter { diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryController.kt index b09f0615b5..53785cb731 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryController.kt @@ -51,6 +51,7 @@ import eu.kanade.tachiyomi.data.library.LibraryUpdateService import eu.kanade.tachiyomi.data.notification.NotificationReceiver import eu.kanade.tachiyomi.data.notification.Notifications import eu.kanade.tachiyomi.data.preference.PreferencesHelper +import eu.kanade.tachiyomi.data.preference.asFlowsIn import eu.kanade.tachiyomi.data.preference.getOrDefault import eu.kanade.tachiyomi.databinding.LibraryControllerBinding import eu.kanade.tachiyomi.source.LocalSource @@ -106,9 +107,6 @@ import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Job import kotlinx.coroutines.cancel import kotlinx.coroutines.delay -import kotlinx.coroutines.flow.drop -import kotlinx.coroutines.flow.launchIn -import kotlinx.coroutines.flow.onEach import uy.kohesive.injekt.Injekt import uy.kohesive.injekt.api.get import java.util.Locale @@ -840,13 +838,8 @@ class LibraryController( preferences.uniformGrid(), preferences.gridSize(), preferences.unreadBadgeType() - ).forEach { - it.asFlow() - .drop(1) - .onEach { - reattachAdapter() - } - .launchIn(scope) + ).asFlowsIn(scope, true) { + reattachAdapter() } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/main/MainActivity.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/main/MainActivity.kt index 3452bde947..bebeb8f95d 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/main/MainActivity.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/main/MainActivity.kt @@ -45,6 +45,7 @@ import eu.kanade.tachiyomi.data.library.LibraryUpdateService import eu.kanade.tachiyomi.data.notification.NotificationReceiver import eu.kanade.tachiyomi.data.notification.Notifications import eu.kanade.tachiyomi.data.preference.asImmediateFlow +import eu.kanade.tachiyomi.data.preference.asImmediateFlowIn import eu.kanade.tachiyomi.data.preference.getOrDefault import eu.kanade.tachiyomi.data.updater.UpdateChecker import eu.kanade.tachiyomi.data.updater.UpdateResult @@ -339,11 +340,10 @@ open class MainActivity : BaseActivity(), DownloadServiceLi } preferences.incognitoMode() - .asImmediateFlow { + .asImmediateFlowIn(lifecycleScope) { binding.toolbar.setIncognitoMode(it) binding.cardToolbar.setIncognitoMode(it) } - .launchIn(lifecycleScope) setExtensionsBadge() } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderActivity.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderActivity.kt index 9545a03c6f..c2db8b2a23 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderActivity.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderActivity.kt @@ -42,6 +42,7 @@ 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.asImmediateFlowIn +import eu.kanade.tachiyomi.data.preference.asFlowsIn import eu.kanade.tachiyomi.data.preference.toggle import eu.kanade.tachiyomi.databinding.ReaderActivityBinding import eu.kanade.tachiyomi.source.model.Page @@ -688,11 +689,7 @@ class ReaderActivity : } listOf(preferences.cropBorders(), preferences.cropBordersWebtoon()) - .forEach { pref -> - pref.asFlow() - .onEach { updateCropBordersShortcut() } - .launchIn(scope) - } + .asFlowsIn(scope) { updateCropBordersShortcut() } preferences.rotation().asImmediateFlowIn(scope) { updateRotationShortcut(it) } binding.chaptersSheet.shiftPageButton.setOnClickListener { diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/recents/RecentsPresenter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/recents/RecentsPresenter.kt index 34bcbd0858..e568514b75 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/recents/RecentsPresenter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/recents/RecentsPresenter.kt @@ -12,15 +12,13 @@ import eu.kanade.tachiyomi.data.download.model.DownloadQueue import eu.kanade.tachiyomi.data.library.LibraryServiceListener import eu.kanade.tachiyomi.data.library.LibraryUpdateService import eu.kanade.tachiyomi.data.preference.PreferencesHelper +import eu.kanade.tachiyomi.data.preference.asFlowsIn import eu.kanade.tachiyomi.source.SourceManager import eu.kanade.tachiyomi.util.system.executeOnIO import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Job import kotlinx.coroutines.cancel -import kotlinx.coroutines.flow.drop -import kotlinx.coroutines.flow.launchIn -import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.launch import kotlinx.coroutines.withContext import uy.kohesive.injekt.Injekt @@ -78,14 +76,9 @@ class RecentsPresenter( preferences.groupChaptersHistory(), preferences.showReadInAllRecents(), preferences.groupChaptersUpdates() - ).forEach { - it.asFlow() - .drop(1) - .onEach { - resetOffsets() - getRecents() - } - .launchIn(scope) + ).asFlowsIn(scope, true) { + resetOffsets() + getRecents() } }