From f05b99ec1fba67699642e4ee21e0554c05c2f8f2 Mon Sep 17 00:00:00 2001 From: arkon Date: Mon, 25 May 2020 12:35:29 -0400 Subject: [PATCH] Convert source display mode preference to flow, remove RxPreferences --- app/build.gradle | 1 - .../data/preference/PreferencesHelper.kt | 7 +----- .../source/browse/BrowseSourceController.kt | 2 +- .../source/browse/BrowseSourcePresenter.kt | 22 +++++-------------- .../ui/browse/source/browse/SourceItem.kt | 5 ++--- .../ui/reader/ReaderSettingsSheet.kt | 12 ---------- 6 files changed, 10 insertions(+), 39 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 898eb23435..bdc53e020b 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -185,7 +185,6 @@ dependencies { implementation 'io.requery:sqlite-android:3.31.0' // Preferences - implementation 'com.f2prateek.rx.preferences:rx-preferences:1.0.2' implementation 'com.github.tfcporciuncula:flow-preferences:1.1.1' // Model View Presenter 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 3d09532164..e893ba3c0d 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 @@ -4,8 +4,6 @@ import android.content.Context import android.net.Uri import android.os.Environment import androidx.preference.PreferenceManager -import com.f2prateek.rx.preferences.Preference as RxPreference -import com.f2prateek.rx.preferences.RxSharedPreferences import com.tfcporciuncula.flow.FlowSharedPreferences import com.tfcporciuncula.flow.Preference import eu.kanade.tachiyomi.R @@ -21,8 +19,6 @@ import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.onEach -fun RxPreference.getOrDefault(): T = get() ?: defaultValue()!! - @OptIn(ExperimentalCoroutinesApi::class) fun Preference.asImmediateFlow(block: (value: T) -> Unit): Flow { block(get()) @@ -34,7 +30,6 @@ fun Preference.asImmediateFlow(block: (value: T) -> Unit): Flow { class PreferencesHelper(val context: Context) { private val prefs = PreferenceManager.getDefaultSharedPreferences(context) - private val rxPrefs = RxSharedPreferences.create(prefs) private val flowPrefs = FlowSharedPreferences(prefs) private val defaultDownloadsDir = Uri.fromFile( @@ -143,7 +138,7 @@ class PreferencesHelper(val context: Context) { fun lastVersionCode() = flowPrefs.getInt("last_version_code", 0) - fun catalogueAsList() = rxPrefs.getBoolean(Keys.catalogueAsList, false) + fun catalogueAsList() = flowPrefs.getBoolean(Keys.catalogueAsList, false) fun enabledLanguages() = flowPrefs.getStringSet(Keys.enabledLanguages, setOf("en", Locale.getDefault().language)) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/browse/BrowseSourceController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/browse/BrowseSourceController.kt index baa1d08be9..88495ba0d2 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/browse/BrowseSourceController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/browse/BrowseSourceController.kt @@ -435,7 +435,7 @@ open class BrowseSourceController(bundle: Bundle) : /** * Swaps the current display mode. */ - fun swapDisplayMode() { + private fun swapDisplayMode() { val view = view ?: return val adapter = adapter ?: return diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/browse/BrowseSourcePresenter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/browse/BrowseSourcePresenter.kt index 3eae04b1d4..a7aec42790 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/browse/BrowseSourcePresenter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/browse/BrowseSourcePresenter.kt @@ -29,6 +29,7 @@ import eu.kanade.tachiyomi.ui.browse.source.filter.TextSectionItem import eu.kanade.tachiyomi.ui.browse.source.filter.TriStateItem import eu.kanade.tachiyomi.ui.browse.source.filter.TriStateSectionItem import eu.kanade.tachiyomi.util.removeCovers +import kotlinx.coroutines.flow.subscribe import rx.Observable import rx.Subscription import rx.android.schedulers.AndroidSchedulers @@ -118,11 +119,7 @@ open class BrowseSourcePresenter( query = savedState.getString(::query.name, "") } - add( - prefs.catalogueAsList().asObservable() - .subscribe { setDisplayMode(it) } - ) - + isListMode = prefs.catalogueAsList().get() restartPager() } @@ -194,16 +191,6 @@ open class BrowseSourcePresenter( return pager.hasNextPage } - /** - * Sets the display mode. - * - * @param asList whether the current mode is in list or not. - */ - private fun setDisplayMode(asList: Boolean) { - isListMode = asList - subscribeToMangaInitializer() - } - /** * Subscribes to the initializer of manga details and updates the view if needed. */ @@ -289,7 +276,10 @@ open class BrowseSourcePresenter( * Changes the active display mode. */ fun swapDisplayMode() { - prefs.catalogueAsList().set(!isListMode) + val mode = !isListMode + prefs.catalogueAsList().set(mode) + isListMode = mode + subscribeToMangaInitializer() } /** diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/browse/SourceItem.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/browse/SourceItem.kt index e3f89c8961..2cff080efe 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/browse/SourceItem.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/browse/SourceItem.kt @@ -5,13 +5,12 @@ import android.view.View import android.view.ViewGroup.LayoutParams.MATCH_PARENT import android.widget.FrameLayout import androidx.recyclerview.widget.RecyclerView -import com.f2prateek.rx.preferences.Preference +import com.tfcporciuncula.flow.Preference import eu.davidea.flexibleadapter.FlexibleAdapter import eu.davidea.flexibleadapter.items.AbstractFlexibleItem import eu.davidea.flexibleadapter.items.IFlexible import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.data.database.models.Manga -import eu.kanade.tachiyomi.data.preference.getOrDefault import eu.kanade.tachiyomi.widget.AutofitRecyclerView import kotlinx.android.synthetic.main.source_grid_item.view.card import kotlinx.android.synthetic.main.source_grid_item.view.gradient @@ -20,7 +19,7 @@ class SourceItem(val manga: Manga, private val catalogueAsList: Preference() { override fun getLayoutRes(): Int { - return if (catalogueAsList.getOrDefault()) { + return if (catalogueAsList.get()) { R.layout.source_list_item } else { R.layout.source_grid_item diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderSettingsSheet.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderSettingsSheet.kt index 1654cf18f7..19e9e6c572 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderSettingsSheet.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderSettingsSheet.kt @@ -6,12 +6,10 @@ import android.widget.CompoundButton import android.widget.Spinner import androidx.annotation.ArrayRes import androidx.core.widget.NestedScrollView -import com.f2prateek.rx.preferences.Preference as RxPreference import com.google.android.material.bottomsheet.BottomSheetDialog import com.tfcporciuncula.flow.Preference import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.data.preference.PreferencesHelper -import eu.kanade.tachiyomi.data.preference.getOrDefault import eu.kanade.tachiyomi.ui.reader.viewer.pager.PagerViewer import eu.kanade.tachiyomi.ui.reader.viewer.webtoon.WebtoonViewer import eu.kanade.tachiyomi.util.view.invisible @@ -129,16 +127,6 @@ class ReaderSettingsSheet(private val activity: ReaderActivity) : BottomSheetDia setOnCheckedChangeListener { _, isChecked -> pref.set(isChecked) } } - /** - * Binds a spinner to an int preference with an optional offset for the value. - */ - private fun Spinner.bindToPreference(pref: RxPreference, offset: Int = 0) { - onItemSelectedListener = IgnoreFirstSpinnerListener { position -> - pref.set(position + offset) - } - setSelection(pref.getOrDefault() - offset, false) - } - /** * Binds a spinner to an int preference with an optional offset for the value. */