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)
}
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())
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.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,14 +838,9 @@ class LibraryController(
preferences.uniformGrid(),
preferences.gridSize(),
preferences.unreadBadgeType()
).forEach {
it.asFlow()
.drop(1)
.onEach {
).asFlowsIn(scope, true) {
reattachAdapter()
}
.launchIn(scope)
}
}
override fun onChangeStarted(handler: ControllerChangeHandler, type: ControllerChangeType) {

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.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<MainActivityBinding>(), DownloadServiceLi
}
preferences.incognitoMode()
.asImmediateFlow {
.asImmediateFlowIn(lifecycleScope) {
binding.toolbar.setIncognitoMode(it)
binding.cardToolbar.setIncognitoMode(it)
}
.launchIn(lifecycleScope)
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.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 {

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.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,15 +76,10 @@ class RecentsPresenter(
preferences.groupChaptersHistory(),
preferences.showReadInAllRecents(),
preferences.groupChaptersUpdates()
).forEach {
it.asFlow()
.drop(1)
.onEach {
).asFlowsIn(scope, true) {
resetOffsets()
getRecents()
}
.launchIn(scope)
}
}
fun onCreate() {