From 64c47bbaedeeca6d2bff81cf37baee01e2067306 Mon Sep 17 00:00:00 2001 From: arkon Date: Fri, 26 Mar 2021 22:31:21 -0400 Subject: [PATCH] Split general and reading mode sheet settings --- .../reader/setting/ReaderGeneralSettings.kt | 120 +----- .../setting/ReaderReadingModeSettings.kt | 106 ++++++ .../ui/reader/setting/ReaderSettingsSheet.kt | 8 +- .../util/preference/PreferenceExtensions.kt | 51 +++ .../layout/reader_color_filter_settings.xml | 14 +- .../res/layout/reader_general_settings.xml | 357 +++++++----------- .../main/res/layout/reader_pager_settings.xml | 74 ++-- .../layout/reader_reading_mode_settings.xml | 86 +++++ .../res/layout/reader_webtoon_settings.xml | 90 +++-- .../main/res/layout/source_filter_sheet.xml | 6 +- app/src/main/res/layout/source_list_item.xml | 4 +- 11 files changed, 497 insertions(+), 419 deletions(-) create mode 100644 app/src/main/java/eu/kanade/tachiyomi/ui/reader/setting/ReaderReadingModeSettings.kt create mode 100644 app/src/main/java/eu/kanade/tachiyomi/util/preference/PreferenceExtensions.kt create mode 100644 app/src/main/res/layout/reader_reading_mode_settings.xml diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/setting/ReaderGeneralSettings.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/setting/ReaderGeneralSettings.kt index d6744b5dc..2ed793d58 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/setting/ReaderGeneralSettings.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/setting/ReaderGeneralSettings.kt @@ -3,22 +3,14 @@ package eu.kanade.tachiyomi.ui.reader.setting import android.content.Context import android.util.AttributeSet import android.view.LayoutInflater -import android.widget.CompoundButton -import android.widget.Spinner -import androidx.annotation.ArrayRes import androidx.core.view.isVisible import androidx.core.widget.NestedScrollView -import androidx.lifecycle.lifecycleScope -import com.tfcporciuncula.flow.Preference import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.data.preference.PreferencesHelper -import eu.kanade.tachiyomi.data.preference.asImmediateFlow import eu.kanade.tachiyomi.databinding.ReaderGeneralSettingsBinding import eu.kanade.tachiyomi.ui.reader.ReaderActivity -import eu.kanade.tachiyomi.ui.reader.viewer.pager.PagerViewer -import eu.kanade.tachiyomi.ui.reader.viewer.webtoon.WebtoonViewer -import eu.kanade.tachiyomi.widget.IgnoreFirstSpinnerListener -import kotlinx.coroutines.flow.launchIn +import eu.kanade.tachiyomi.util.preference.bindToIntPreference +import eu.kanade.tachiyomi.util.preference.bindToPreference import uy.kohesive.injekt.injectLazy /** @@ -35,29 +27,12 @@ class ReaderGeneralSettings @JvmOverloads constructor(context: Context, attrs: A addView(binding.root) initGeneralPreferences() - - when ((context as ReaderActivity).viewer) { - is PagerViewer -> initPagerPreferences() - is WebtoonViewer -> initWebtoonPreferences() - } } /** * Init general reader preferences. */ private fun initGeneralPreferences() { - binding.viewer.onItemSelectedListener = IgnoreFirstSpinnerListener { position -> - (context as ReaderActivity).presenter.setMangaViewer(position) - - val mangaViewer = (context as ReaderActivity).presenter.getMangaViewer() - if (mangaViewer == ReadingModeType.WEBTOON.prefValue || mangaViewer == ReadingModeType.CONTINUOUS_VERTICAL.prefValue) { - initWebtoonPreferences() - } else { - initPagerPreferences() - } - } - binding.viewer.setSelection((context as ReaderActivity).presenter.manga?.viewer ?: 0, false) - binding.rotationMode.bindToPreference(preferences.rotation(), 1) binding.backgroundColor.bindToIntPreference(preferences.readerTheme(), R.array.reader_themes_values) binding.showPageNumber.bindToPreference(preferences.showPageNumber()) @@ -73,95 +48,4 @@ class ReaderGeneralSettings @JvmOverloads constructor(context: Context, attrs: A binding.cutoutShort.bindToPreference(preferences.cutoutShort()) } } - - /** - * Init the preferences for the pager reader. - */ - private fun initPagerPreferences() { - binding.webtoonPrefsGroup.root.isVisible = false - binding.pagerPrefsGroup.root.isVisible = true - - binding.pagerPrefsGroup.tappingPrefsGroup.isVisible = preferences.readWithTapping().get() - - binding.pagerPrefsGroup.tappingInverted.bindToPreference(preferences.pagerNavInverted()) - - binding.pagerPrefsGroup.pagerNav.bindToPreference(preferences.navigationModePager()) - binding.pagerPrefsGroup.scaleType.bindToPreference(preferences.imageScaleType(), 1) - binding.pagerPrefsGroup.zoomStart.bindToPreference(preferences.zoomStart(), 1) - binding.pagerPrefsGroup.cropBorders.bindToPreference(preferences.cropBorders()) - - // Makes so that dual page invert gets hidden away when turning of dual page split - binding.dualPageSplit.bindToPreference(preferences.dualPageSplitPaged()) - preferences.dualPageSplitPaged() - .asImmediateFlow { binding.dualPageInvert.isVisible = it } - .launchIn((context as ReaderActivity).lifecycleScope) - binding.dualPageInvert.bindToPreference(preferences.dualPageInvertPaged()) - } - - /** - * Init the preferences for the webtoon reader. - */ - private fun initWebtoonPreferences() { - binding.pagerPrefsGroup.root.isVisible = false - binding.webtoonPrefsGroup.root.isVisible = true - - binding.webtoonPrefsGroup.tappingPrefsGroup.isVisible = preferences.readWithTapping().get() - - binding.webtoonPrefsGroup.tappingInverted.bindToPreference(preferences.webtoonNavInverted()) - - binding.webtoonPrefsGroup.webtoonNav.bindToPreference(preferences.navigationModeWebtoon()) - binding.webtoonPrefsGroup.cropBordersWebtoon.bindToPreference(preferences.cropBordersWebtoon()) - binding.webtoonPrefsGroup.webtoonSidePadding.bindToIntPreference(preferences.webtoonSidePadding(), R.array.webtoon_side_padding_values) - - // Makes so that dual page invert gets hidden away when turning of dual page split - binding.dualPageSplit.bindToPreference(preferences.dualPageSplitWebtoon()) - preferences.dualPageSplitWebtoon() - .asImmediateFlow { binding.dualPageInvert.isVisible = it } - .launchIn((context as ReaderActivity).lifecycleScope) - binding.dualPageInvert.bindToPreference(preferences.dualPageInvertWebtoon()) - } - - /** - * Binds a checkbox or switch view with a boolean preference. - */ - private fun CompoundButton.bindToPreference(pref: Preference) { - isChecked = pref.get() - setOnCheckedChangeListener { _, isChecked -> pref.set(isChecked) } - } - - /** - * Binds a spinner to an int preference with an optional offset for the value. - */ - private fun Spinner.bindToPreference(pref: Preference, offset: Int = 0) { - onItemSelectedListener = IgnoreFirstSpinnerListener { position -> - pref.set(position + offset) - } - setSelection(pref.get() - offset, false) - } - - /** - * Binds a spinner to an enum preference. - */ - private inline fun > Spinner.bindToPreference(pref: Preference) { - val enumConstants = T::class.java.enumConstants - - onItemSelectedListener = IgnoreFirstSpinnerListener { position -> - enumConstants?.get(position)?.let { pref.set(it) } - } - - enumConstants?.indexOf(pref.get())?.let { setSelection(it, false) } - } - - /** - * Binds a spinner to an int preference. The position of the spinner item must - * correlate with the [intValues] resource item (in arrays.xml), which is a - * of int values that will be parsed here and applied to the preference. - */ - private fun Spinner.bindToIntPreference(pref: Preference, @ArrayRes intValuesResource: Int) { - val intValues = resources.getStringArray(intValuesResource).map { it.toIntOrNull() } - onItemSelectedListener = IgnoreFirstSpinnerListener { position -> - pref.set(intValues[position]!!) - } - setSelection(intValues.indexOf(pref.get()), false) - } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/setting/ReaderReadingModeSettings.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/setting/ReaderReadingModeSettings.kt new file mode 100644 index 000000000..27f2350e3 --- /dev/null +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/setting/ReaderReadingModeSettings.kt @@ -0,0 +1,106 @@ +package eu.kanade.tachiyomi.ui.reader.setting + +import android.content.Context +import android.util.AttributeSet +import android.view.LayoutInflater +import androidx.core.view.isVisible +import androidx.core.widget.NestedScrollView +import androidx.lifecycle.lifecycleScope +import eu.kanade.tachiyomi.R +import eu.kanade.tachiyomi.data.preference.PreferencesHelper +import eu.kanade.tachiyomi.data.preference.asImmediateFlow +import eu.kanade.tachiyomi.databinding.ReaderReadingModeSettingsBinding +import eu.kanade.tachiyomi.ui.reader.ReaderActivity +import eu.kanade.tachiyomi.ui.reader.viewer.pager.PagerViewer +import eu.kanade.tachiyomi.ui.reader.viewer.webtoon.WebtoonViewer +import eu.kanade.tachiyomi.util.preference.bindToIntPreference +import eu.kanade.tachiyomi.util.preference.bindToPreference +import eu.kanade.tachiyomi.widget.IgnoreFirstSpinnerListener +import kotlinx.coroutines.flow.launchIn +import uy.kohesive.injekt.injectLazy + +/** + * Sheet to show reader and viewer preferences. + */ +class ReaderReadingModeSettings @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null) : + NestedScrollView(context, attrs) { + + private val preferences: PreferencesHelper by injectLazy() + + private val binding = ReaderReadingModeSettingsBinding.inflate(LayoutInflater.from(context), this, false) + + init { + addView(binding.root) + + initGeneralPreferences() + + when ((context as ReaderActivity).viewer) { + is PagerViewer -> initPagerPreferences() + is WebtoonViewer -> initWebtoonPreferences() + } + } + + /** + * Init general reader preferences. + */ + private fun initGeneralPreferences() { + binding.viewer.onItemSelectedListener = IgnoreFirstSpinnerListener { position -> + (context as ReaderActivity).presenter.setMangaViewer(position) + + val mangaViewer = (context as ReaderActivity).presenter.getMangaViewer() + if (mangaViewer == ReadingModeType.WEBTOON.prefValue || mangaViewer == ReadingModeType.CONTINUOUS_VERTICAL.prefValue) { + initWebtoonPreferences() + } else { + initPagerPreferences() + } + } + binding.viewer.setSelection((context as ReaderActivity).presenter.manga?.viewer ?: 0, false) + } + + /** + * Init the preferences for the pager reader. + */ + private fun initPagerPreferences() { + binding.webtoonPrefsGroup.root.isVisible = false + binding.pagerPrefsGroup.root.isVisible = true + + binding.pagerPrefsGroup.tappingPrefsGroup.isVisible = preferences.readWithTapping().get() + + binding.pagerPrefsGroup.tappingInverted.bindToPreference(preferences.pagerNavInverted()) + + binding.pagerPrefsGroup.pagerNav.bindToPreference(preferences.navigationModePager()) + binding.pagerPrefsGroup.scaleType.bindToPreference(preferences.imageScaleType(), 1) + binding.pagerPrefsGroup.zoomStart.bindToPreference(preferences.zoomStart(), 1) + binding.pagerPrefsGroup.cropBorders.bindToPreference(preferences.cropBorders()) + + // Makes so that dual page invert gets hidden away when turning of dual page split + binding.pagerPrefsGroup.dualPageSplit.bindToPreference(preferences.dualPageSplitPaged()) + preferences.dualPageSplitPaged() + .asImmediateFlow { binding.pagerPrefsGroup.dualPageInvert.isVisible = it } + .launchIn((context as ReaderActivity).lifecycleScope) + binding.pagerPrefsGroup.dualPageInvert.bindToPreference(preferences.dualPageInvertPaged()) + } + + /** + * Init the preferences for the webtoon reader. + */ + private fun initWebtoonPreferences() { + binding.pagerPrefsGroup.root.isVisible = false + binding.webtoonPrefsGroup.root.isVisible = true + + binding.webtoonPrefsGroup.tappingPrefsGroup.isVisible = preferences.readWithTapping().get() + + binding.webtoonPrefsGroup.tappingInverted.bindToPreference(preferences.webtoonNavInverted()) + + binding.webtoonPrefsGroup.webtoonNav.bindToPreference(preferences.navigationModeWebtoon()) + binding.webtoonPrefsGroup.cropBordersWebtoon.bindToPreference(preferences.cropBordersWebtoon()) + binding.webtoonPrefsGroup.webtoonSidePadding.bindToIntPreference(preferences.webtoonSidePadding(), R.array.webtoon_side_padding_values) + + // Makes so that dual page invert gets hidden away when turning of dual page split + binding.webtoonPrefsGroup.dualPageSplit.bindToPreference(preferences.dualPageSplitWebtoon()) + preferences.dualPageSplitWebtoon() + .asImmediateFlow { binding.webtoonPrefsGroup.dualPageInvert.isVisible = it } + .launchIn((context as ReaderActivity).lifecycleScope) + binding.webtoonPrefsGroup.dualPageInvert.bindToPreference(preferences.dualPageInvertWebtoon()) + } +} diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/setting/ReaderSettingsSheet.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/setting/ReaderSettingsSheet.kt index b98623635..2f796be93 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/setting/ReaderSettingsSheet.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/setting/ReaderSettingsSheet.kt @@ -8,16 +8,18 @@ import eu.kanade.tachiyomi.widget.sheet.TabbedBottomSheetDialog class ReaderSettingsSheet(private val activity: ReaderActivity) : TabbedBottomSheetDialog(activity) { + private val readingModeSettings = ReaderReadingModeSettings(activity) private val generalSettings = ReaderGeneralSettings(activity) private val colorFilterSettings = ReaderColorFilterSettings(activity) private val sheetBackgroundDim = window?.attributes?.dimAmount ?: 0.25f init { + val filterTabIndex = getTabViews().indexOf(colorFilterSettings) binding.tabs.addOnTabSelectedListener(object : SimpleTabSelectedListener() { // Remove dimmed backdrop so color filter changes can be previewed override fun onTabSelected(tab: TabLayout.Tab?) { - val isFilterTab = tab?.position == 1 + val isFilterTab = tab?.position == filterTabIndex window?.setDimAmount(if (isFilterTab) 0f else sheetBackgroundDim) activity.setMenuVisibility(!isFilterTab) } @@ -25,12 +27,14 @@ class ReaderSettingsSheet(private val activity: ReaderActivity) : TabbedBottomSh } override fun getTabViews() = listOf( + readingModeSettings, generalSettings, colorFilterSettings, ) override fun getTabTitles() = listOf( - R.string.action_settings, + R.string.pref_category_reading_mode, + R.string.pref_category_general, R.string.custom_filter, ) } diff --git a/app/src/main/java/eu/kanade/tachiyomi/util/preference/PreferenceExtensions.kt b/app/src/main/java/eu/kanade/tachiyomi/util/preference/PreferenceExtensions.kt new file mode 100644 index 000000000..abc592f3c --- /dev/null +++ b/app/src/main/java/eu/kanade/tachiyomi/util/preference/PreferenceExtensions.kt @@ -0,0 +1,51 @@ +package eu.kanade.tachiyomi.util.preference + +import android.widget.CompoundButton +import android.widget.Spinner +import androidx.annotation.ArrayRes +import com.tfcporciuncula.flow.Preference +import eu.kanade.tachiyomi.widget.IgnoreFirstSpinnerListener + +/** + * Binds a checkbox or switch view with a boolean preference. + */ +fun CompoundButton.bindToPreference(pref: Preference) { + isChecked = pref.get() + setOnCheckedChangeListener { _, isChecked -> pref.set(isChecked) } +} + +/** + * Binds a spinner to an int preference with an optional offset for the value. + */ +fun Spinner.bindToPreference(pref: Preference, offset: Int = 0) { + onItemSelectedListener = IgnoreFirstSpinnerListener { position -> + pref.set(position + offset) + } + setSelection(pref.get() - offset, false) +} + +/** + * Binds a spinner to an enum preference. + */ +inline fun > Spinner.bindToPreference(pref: Preference) { + val enumConstants = T::class.java.enumConstants + + onItemSelectedListener = IgnoreFirstSpinnerListener { position -> + enumConstants?.get(position)?.let { pref.set(it) } + } + + enumConstants?.indexOf(pref.get())?.let { setSelection(it, false) } +} + +/** + * Binds a spinner to an int preference. The position of the spinner item must + * correlate with the [intValues] resource item (in arrays.xml), which is a + * of int values that will be parsed here and applied to the preference. + */ +fun Spinner.bindToIntPreference(pref: Preference, @ArrayRes intValuesResource: Int) { + val intValues = resources.getStringArray(intValuesResource).map { it.toIntOrNull() } + onItemSelectedListener = IgnoreFirstSpinnerListener { position -> + pref.set(intValues[position]!!) + } + setSelection(intValues.indexOf(pref.get()), false) +} diff --git a/app/src/main/res/layout/reader_color_filter_settings.xml b/app/src/main/res/layout/reader_color_filter_settings.xml index 8eedd6ff4..80899cf9b 100644 --- a/app/src/main/res/layout/reader_color_filter_settings.xml +++ b/app/src/main/res/layout/reader_color_filter_settings.xml @@ -10,13 +10,6 @@ android:layout_width="match_parent" android:layout_height="wrap_content"> - - + + diff --git a/app/src/main/res/layout/reader_general_settings.xml b/app/src/main/res/layout/reader_general_settings.xml index 05eb03dcc..12489d36d 100644 --- a/app/src/main/res/layout/reader_general_settings.xml +++ b/app/src/main/res/layout/reader_general_settings.xml @@ -5,246 +5,157 @@ android:layout_width="match_parent" android:layout_height="match_parent" android:clipToPadding="false" - android:paddingStart="24dp" - android:paddingTop="0dp" - android:paddingEnd="24dp" - android:paddingBottom="24dp"> + android:padding="16dp"> - + + + + + + + android:layout_height="wrap_content" + android:text="@string/pref_page_transitions" + android:textColor="?android:attr/textColorSecondary" + app:layout_constraintTop_toBottomOf="@id/general_prefs" /> - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + diff --git a/app/src/main/res/layout/reader_pager_settings.xml b/app/src/main/res/layout/reader_pager_settings.xml index c9b17165e..594b2e995 100644 --- a/app/src/main/res/layout/reader_pager_settings.xml +++ b/app/src/main/res/layout/reader_pager_settings.xml @@ -35,6 +35,26 @@ app:layout_constraintStart_toEndOf="@id/verticalcenter" app:layout_constraintTop_toBottomOf="@id/pager_prefs" /> + + + + + + + + + + - - - - - - + app:layout_constraintStart_toEndOf="parent" + tools:ignore="MissingConstraints" /> diff --git a/app/src/main/res/layout/reader_reading_mode_settings.xml b/app/src/main/res/layout/reader_reading_mode_settings.xml new file mode 100644 index 000000000..cd6c17991 --- /dev/null +++ b/app/src/main/res/layout/reader_reading_mode_settings.xml @@ -0,0 +1,86 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/app/src/main/res/layout/reader_webtoon_settings.xml b/app/src/main/res/layout/reader_webtoon_settings.xml index 42511903b..5b71d8e3c 100644 --- a/app/src/main/res/layout/reader_webtoon_settings.xml +++ b/app/src/main/res/layout/reader_webtoon_settings.xml @@ -16,25 +16,6 @@ app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> - - - - - - + + + + + + + + + + + + - - + app:layout_constraintStart_toEndOf="parent" + tools:ignore="MissingConstraints" /> diff --git a/app/src/main/res/layout/source_filter_sheet.xml b/app/src/main/res/layout/source_filter_sheet.xml index 1974e84b8..213f43d61 100644 --- a/app/src/main/res/layout/source_filter_sheet.xml +++ b/app/src/main/res/layout/source_filter_sheet.xml @@ -1,6 +1,7 @@ @@ -15,7 +16,7 @@ android:paddingEnd="?attr/listPreferredItemPaddingEnd"> + app:tint="?attr/colorOnSurface" + tools:ignore="ContentDescription" />