From 17fc8deb19909c3955d30a5312c8ef806c56aefc Mon Sep 17 00:00:00 2001 From: arkon Date: Fri, 24 Apr 2020 11:31:19 -0400 Subject: [PATCH] Migrate column preference to FlowPreference --- .../data/preference/PreferencesHelper.kt | 4 +-- .../tachiyomi/ui/library/LibraryController.kt | 15 ++++++---- .../ui/setting/SettingsLibraryController.kt | 16 +++++----- .../source/browse/BrowseSourceController.kt | 30 +++++++++++-------- .../main/res/layout/extension_controller.xml | 1 - .../res/layout/source_main_controller.xml | 1 - 6 files changed, 36 insertions(+), 31 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 80893023bf..68fa2e86ae 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 @@ -144,9 +144,9 @@ class PreferencesHelper(val context: Context) { fun readWithVolumeKeysInverted() = flowPrefs.getBoolean(Keys.readWithVolumeKeysInverted, false) - fun portraitColumns() = rxPrefs.getInteger(Keys.portraitColumns, 0) + fun portraitColumns() = flowPrefs.getInt(Keys.portraitColumns, 0) - fun landscapeColumns() = rxPrefs.getInteger(Keys.landscapeColumns, 0) + fun landscapeColumns() = flowPrefs.getInt(Keys.landscapeColumns, 0) fun updateOnlyNonCompleted() = prefs.getBoolean(Keys.updateOnlyNonCompleted, false) 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 f68e15b793..50d8d03b09 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 @@ -17,15 +17,16 @@ import androidx.appcompat.widget.SearchView import androidx.core.graphics.drawable.DrawableCompat import com.bluelinelabs.conductor.ControllerChangeHandler import com.bluelinelabs.conductor.ControllerChangeType -import com.f2prateek.rx.preferences.Preference import com.google.android.material.tabs.TabLayout import com.jakewharton.rxrelay.BehaviorRelay import com.jakewharton.rxrelay.PublishRelay +import com.tfcporciuncula.flow.Preference import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.data.database.models.Category import eu.kanade.tachiyomi.data.database.models.Manga import eu.kanade.tachiyomi.data.library.LibraryUpdateService import eu.kanade.tachiyomi.data.preference.PreferencesHelper +import eu.kanade.tachiyomi.data.preference.asImmediateFlow import eu.kanade.tachiyomi.databinding.LibraryControllerBinding import eu.kanade.tachiyomi.ui.base.controller.NucleusController import eu.kanade.tachiyomi.ui.base.controller.RootController @@ -154,11 +155,13 @@ class LibraryController( } .launchIn(scope) - getColumnsPreferenceForCurrentOrientation().asObservable() - .doOnNext { mangaPerRow = it } - .skip(1) - // Set again the adapter to recalculate the covers height - .subscribeUntilDestroy { reattachAdapter() } + getColumnsPreferenceForCurrentOrientation().asImmediateFlow { + mangaPerRow = it + + // Set the adapter again to recalculate the covers height + reattachAdapter() + } + .launchIn(scope) if (selectedMangas.isNotEmpty()) { createActionModeIfNeeded() diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsLibraryController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsLibraryController.kt index fa13afcf1c..2c2a1a8078 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsLibraryController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsLibraryController.kt @@ -14,7 +14,6 @@ import eu.kanade.tachiyomi.data.library.LibraryUpdateJob import eu.kanade.tachiyomi.data.preference.PreferenceKeys as Keys import eu.kanade.tachiyomi.data.preference.PreferencesHelper import eu.kanade.tachiyomi.data.preference.asImmediateFlow -import eu.kanade.tachiyomi.data.preference.getOrDefault import eu.kanade.tachiyomi.ui.base.controller.DialogController import eu.kanade.tachiyomi.ui.base.controller.withFadeTransaction import eu.kanade.tachiyomi.ui.category.CategoryController @@ -31,9 +30,9 @@ import eu.kanade.tachiyomi.util.preference.switchPreference import eu.kanade.tachiyomi.util.preference.titleRes import kotlinx.android.synthetic.main.pref_library_columns.view.landscape_columns import kotlinx.android.synthetic.main.pref_library_columns.view.portrait_columns +import kotlinx.coroutines.flow.combine import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.onEach -import rx.Observable import uy.kohesive.injekt.Injekt import uy.kohesive.injekt.api.get @@ -61,16 +60,15 @@ class SettingsLibraryController : SettingsController() { } } - Observable.combineLatest( - preferences.portraitColumns().asObservable(), - preferences.landscapeColumns().asObservable() - ) { portraitCols, landscapeCols -> Pair(portraitCols, landscapeCols) } - .subscribeUntilDestroy { (portraitCols, landscapeCols) -> + preferences.portraitColumns().asFlow() + .combine(preferences.landscapeColumns().asFlow()) { portraitCols, landscapeCols -> Pair(portraitCols, landscapeCols) } + .onEach { (portraitCols, landscapeCols) -> val portrait = getColumnValue(portraitCols) val landscape = getColumnValue(landscapeCols) summary = "${context.getString(R.string.portrait)}: $portrait, " + "${context.getString(R.string.landscape)}: $landscape" } + .launchIn(scope) } } @@ -201,8 +199,8 @@ class SettingsLibraryController : SettingsController() { private val preferences: PreferencesHelper = Injekt.get() - private var portrait = preferences.portraitColumns().getOrDefault() - private var landscape = preferences.landscapeColumns().getOrDefault() + private var portrait = preferences.portraitColumns().get() + private var landscape = preferences.landscapeColumns().get() override fun onCreateDialog(savedViewState: Bundle?): Dialog { val dialog = MaterialDialog(activity!!) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/source/browse/BrowseSourceController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/source/browse/BrowseSourceController.kt index 9544acef4d..1de7215bf8 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/source/browse/BrowseSourceController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/source/browse/BrowseSourceController.kt @@ -15,14 +15,15 @@ import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView import com.afollestad.materialdialogs.MaterialDialog import com.afollestad.materialdialogs.list.listItems -import com.f2prateek.rx.preferences.Preference import com.google.android.material.snackbar.Snackbar +import com.tfcporciuncula.flow.Preference import eu.davidea.flexibleadapter.FlexibleAdapter import eu.davidea.flexibleadapter.items.IFlexible import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.data.database.models.Category import eu.kanade.tachiyomi.data.database.models.Manga import eu.kanade.tachiyomi.data.preference.PreferencesHelper +import eu.kanade.tachiyomi.data.preference.asImmediateFlow import eu.kanade.tachiyomi.databinding.SourceControllerBinding import eu.kanade.tachiyomi.source.CatalogueSource import eu.kanade.tachiyomi.source.LocalSource @@ -43,12 +44,15 @@ import eu.kanade.tachiyomi.util.view.snack import eu.kanade.tachiyomi.util.view.visible import eu.kanade.tachiyomi.widget.AutofitRecyclerView import eu.kanade.tachiyomi.widget.EmptyView +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.Job +import kotlinx.coroutines.cancel import kotlinx.coroutines.flow.filter import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.onEach import reactivecircus.flowbinding.appcompat.QueryTextEvent import reactivecircus.flowbinding.appcompat.queryTextEvents -import rx.Subscription import timber.log.Timber import uy.kohesive.injekt.injectLazy @@ -91,9 +95,9 @@ open class BrowseSourceController(bundle: Bundle) : private var recycler: RecyclerView? = null /** - * Subscription for the number of manga per row. + * Scope for the number of manga per row flow. */ - private var numColumnsSubscription: Subscription? = null + private var numColumnsScope: CoroutineScope? = null /** * Endless loading item. @@ -157,8 +161,7 @@ open class BrowseSourceController(bundle: Bundle) : } override fun onDestroyView(view: View) { - numColumnsSubscription?.unsubscribe() - numColumnsSubscription = null + numColumnsScope?.cancel() adapter = null snack = null recycler = null @@ -166,7 +169,7 @@ open class BrowseSourceController(bundle: Bundle) : } private fun setupRecycler(view: View) { - numColumnsSubscription?.unsubscribe() + numColumnsScope?.cancel() var oldPosition = RecyclerView.NO_POSITION val oldRecycler = binding.catalogueView.getChildAt(1) @@ -186,11 +189,14 @@ open class BrowseSourceController(bundle: Bundle) : } } else { (binding.catalogueView.inflate(R.layout.source_recycler_autofit) as AutofitRecyclerView).apply { - numColumnsSubscription = getColumnsPreferenceForCurrentOrientation().asObservable() - .doOnNext { spanCount = it } - .skip(1) - // Set again the adapter to recalculate the covers height - .subscribe { adapter = this@BrowseSourceController.adapter } + numColumnsScope = CoroutineScope(Job() + Dispatchers.Main) + getColumnsPreferenceForCurrentOrientation().asImmediateFlow { + spanCount = it + + // Set the adapter again to recalculate the covers height + adapter = this@BrowseSourceController.adapter + } + .launchIn(numColumnsScope!!) (layoutManager as GridLayoutManager).spanSizeLookup = object : GridLayoutManager.SpanSizeLookup() { override fun getSpanSize(position: Int): Int { diff --git a/app/src/main/res/layout/extension_controller.xml b/app/src/main/res/layout/extension_controller.xml index 26e0c57e88..348b616c39 100644 --- a/app/src/main/res/layout/extension_controller.xml +++ b/app/src/main/res/layout/extension_controller.xml @@ -22,7 +22,6 @@ android:id="@+id/fast_scroller" android:layout_width="wrap_content" android:layout_height="match_parent" - android:layout_centerHorizontal="true" android:layout_gravity="end" app:fastScrollerBubbleEnabled="false" tools:visibility="visible" /> diff --git a/app/src/main/res/layout/source_main_controller.xml b/app/src/main/res/layout/source_main_controller.xml index 15e5c41f4d..cc0e815d5c 100644 --- a/app/src/main/res/layout/source_main_controller.xml +++ b/app/src/main/res/layout/source_main_controller.xml @@ -16,7 +16,6 @@ android:id="@+id/fast_scroller" android:layout_width="wrap_content" android:layout_height="match_parent" - android:layout_centerHorizontal="true" android:layout_gravity="end" app:fastScrollerBubbleEnabled="false" tools:visibility="visible" />