Helper method for list of preferences flow

This commit is contained in:
Jays2Kings 2021-04-26 00:55:37 -04:00
parent 820d240997
commit 47f6c54412
5 changed files with 22 additions and 28 deletions

View File

@ -43,6 +43,17 @@ fun <T> com.tfcporciuncula.flow.Preference<T>.asImmediateFlowIn(scope: Coroutine
.launchIn(scope) .launchIn(scope)
} }
fun <T> Collection<com.tfcporciuncula.flow.Preference<out T>>.asFlowsIn(scope: CoroutineScope, dropFirst: Boolean = false, block: () -> Unit): Collection<Job> {
return map { pref ->
pref.asFlow()
.apply {
if (dropFirst) drop(1)
}
.onEach { block() }
.launchIn(scope)
}
}
fun com.tfcporciuncula.flow.Preference<Boolean>.toggle() = set(!get()) fun com.tfcporciuncula.flow.Preference<Boolean>.toggle() = set(!get())
private class DateFormatConverter : Preference.Adapter<DateFormat> { private class DateFormatConverter : Preference.Adapter<DateFormat> {

View File

@ -51,6 +51,7 @@ import eu.kanade.tachiyomi.data.library.LibraryUpdateService
import eu.kanade.tachiyomi.data.notification.NotificationReceiver import eu.kanade.tachiyomi.data.notification.NotificationReceiver
import eu.kanade.tachiyomi.data.notification.Notifications import eu.kanade.tachiyomi.data.notification.Notifications
import eu.kanade.tachiyomi.data.preference.PreferencesHelper 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.data.preference.getOrDefault
import eu.kanade.tachiyomi.databinding.LibraryControllerBinding import eu.kanade.tachiyomi.databinding.LibraryControllerBinding
import eu.kanade.tachiyomi.source.LocalSource import eu.kanade.tachiyomi.source.LocalSource
@ -106,9 +107,6 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job import kotlinx.coroutines.Job
import kotlinx.coroutines.cancel import kotlinx.coroutines.cancel
import kotlinx.coroutines.delay 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.Injekt
import uy.kohesive.injekt.api.get import uy.kohesive.injekt.api.get
import java.util.Locale import java.util.Locale
@ -840,13 +838,8 @@ class LibraryController(
preferences.uniformGrid(), preferences.uniformGrid(),
preferences.gridSize(), preferences.gridSize(),
preferences.unreadBadgeType() preferences.unreadBadgeType()
).forEach { ).asFlowsIn(scope, true) {
it.asFlow() reattachAdapter()
.drop(1)
.onEach {
reattachAdapter()
}
.launchIn(scope)
} }
} }

View File

@ -45,6 +45,7 @@ import eu.kanade.tachiyomi.data.library.LibraryUpdateService
import eu.kanade.tachiyomi.data.notification.NotificationReceiver import eu.kanade.tachiyomi.data.notification.NotificationReceiver
import eu.kanade.tachiyomi.data.notification.Notifications import eu.kanade.tachiyomi.data.notification.Notifications
import eu.kanade.tachiyomi.data.preference.asImmediateFlow 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.preference.getOrDefault
import eu.kanade.tachiyomi.data.updater.UpdateChecker import eu.kanade.tachiyomi.data.updater.UpdateChecker
import eu.kanade.tachiyomi.data.updater.UpdateResult import eu.kanade.tachiyomi.data.updater.UpdateResult
@ -339,11 +340,10 @@ open class MainActivity : BaseActivity<MainActivityBinding>(), DownloadServiceLi
} }
preferences.incognitoMode() preferences.incognitoMode()
.asImmediateFlow { .asImmediateFlowIn(lifecycleScope) {
binding.toolbar.setIncognitoMode(it) binding.toolbar.setIncognitoMode(it)
binding.cardToolbar.setIncognitoMode(it) binding.cardToolbar.setIncognitoMode(it)
} }
.launchIn(lifecycleScope)
setExtensionsBadge() setExtensionsBadge()
} }

View File

@ -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.database.models.Manga
import eu.kanade.tachiyomi.data.preference.PreferencesHelper import eu.kanade.tachiyomi.data.preference.PreferencesHelper
import eu.kanade.tachiyomi.data.preference.asImmediateFlowIn 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.data.preference.toggle
import eu.kanade.tachiyomi.databinding.ReaderActivityBinding import eu.kanade.tachiyomi.databinding.ReaderActivityBinding
import eu.kanade.tachiyomi.source.model.Page import eu.kanade.tachiyomi.source.model.Page
@ -688,11 +689,7 @@ class ReaderActivity :
} }
listOf(preferences.cropBorders(), preferences.cropBordersWebtoon()) listOf(preferences.cropBorders(), preferences.cropBordersWebtoon())
.forEach { pref -> .asFlowsIn(scope) { updateCropBordersShortcut() }
pref.asFlow()
.onEach { updateCropBordersShortcut() }
.launchIn(scope)
}
preferences.rotation().asImmediateFlowIn(scope) { updateRotationShortcut(it) } preferences.rotation().asImmediateFlowIn(scope) { updateRotationShortcut(it) }
binding.chaptersSheet.shiftPageButton.setOnClickListener { binding.chaptersSheet.shiftPageButton.setOnClickListener {

View File

@ -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.LibraryServiceListener
import eu.kanade.tachiyomi.data.library.LibraryUpdateService import eu.kanade.tachiyomi.data.library.LibraryUpdateService
import eu.kanade.tachiyomi.data.preference.PreferencesHelper import eu.kanade.tachiyomi.data.preference.PreferencesHelper
import eu.kanade.tachiyomi.data.preference.asFlowsIn
import eu.kanade.tachiyomi.source.SourceManager import eu.kanade.tachiyomi.source.SourceManager
import eu.kanade.tachiyomi.util.system.executeOnIO import eu.kanade.tachiyomi.util.system.executeOnIO
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job import kotlinx.coroutines.Job
import kotlinx.coroutines.cancel 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.launch
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import uy.kohesive.injekt.Injekt import uy.kohesive.injekt.Injekt
@ -78,14 +76,9 @@ class RecentsPresenter(
preferences.groupChaptersHistory(), preferences.groupChaptersHistory(),
preferences.showReadInAllRecents(), preferences.showReadInAllRecents(),
preferences.groupChaptersUpdates() preferences.groupChaptersUpdates()
).forEach { ).asFlowsIn(scope, true) {
it.asFlow() resetOffsets()
.drop(1) getRecents()
.onEach {
resetOffsets()
getRecents()
}
.launchIn(scope)
} }
} }