Cleanup reader spinner layouts

This commit is contained in:
arkon 2021-03-27 17:59:52 -04:00
parent 2911fe7a1a
commit c4b975b777
10 changed files with 53 additions and 95 deletions

View File

@ -9,7 +9,6 @@ import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.preference.PreferencesHelper import eu.kanade.tachiyomi.data.preference.PreferencesHelper
import eu.kanade.tachiyomi.databinding.ReaderGeneralSettingsBinding import eu.kanade.tachiyomi.databinding.ReaderGeneralSettingsBinding
import eu.kanade.tachiyomi.ui.reader.ReaderActivity import eu.kanade.tachiyomi.ui.reader.ReaderActivity
import eu.kanade.tachiyomi.util.preference.bindToIntPreference
import eu.kanade.tachiyomi.util.preference.bindToPreference import eu.kanade.tachiyomi.util.preference.bindToPreference
import uy.kohesive.injekt.injectLazy import uy.kohesive.injekt.injectLazy

View File

@ -13,7 +13,6 @@ import eu.kanade.tachiyomi.databinding.ReaderReadingModeSettingsBinding
import eu.kanade.tachiyomi.ui.reader.ReaderActivity import eu.kanade.tachiyomi.ui.reader.ReaderActivity
import eu.kanade.tachiyomi.ui.reader.viewer.pager.PagerViewer import eu.kanade.tachiyomi.ui.reader.viewer.pager.PagerViewer
import eu.kanade.tachiyomi.ui.reader.viewer.webtoon.WebtoonViewer 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.util.preference.bindToPreference
import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.launchIn
import uy.kohesive.injekt.injectLazy import uy.kohesive.injekt.injectLazy

View File

@ -8,7 +8,6 @@ import android.view.MenuItem
import android.widget.FrameLayout import android.widget.FrameLayout
import androidx.annotation.ArrayRes import androidx.annotation.ArrayRes
import androidx.appcompat.widget.PopupMenu import androidx.appcompat.widget.PopupMenu
import androidx.core.view.get
import com.tfcporciuncula.flow.Preference import com.tfcporciuncula.flow.Preference
import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.databinding.SpinnerPreferenceBinding import eu.kanade.tachiyomi.databinding.SpinnerPreferenceBinding
@ -53,12 +52,8 @@ class SpinnerPreference @JvmOverloads constructor(context: Context, attrs: Attri
} }
fun setSelection(selection: Int) { fun setSelection(selection: Int) {
popup?.menu?.get(selectedPosition)?.isCheckable = false
popup?.menu?.get(selectedPosition)?.isChecked = false
selectedPosition = selection selectedPosition = selection
binding.details.text = entries.getOrNull(selection).orEmpty() binding.details.text = entries.getOrNull(selection).orEmpty()
popup?.menu?.get(selectedPosition)?.isCheckable = true
popup?.menu?.get(selectedPosition)?.isChecked = true
} }
fun bindToPreference(pref: Preference<Int>, offset: Int = 0, block: ((Int) -> Unit)? = null) { fun bindToPreference(pref: Preference<Int>, offset: Int = 0, block: ((Int) -> Unit)? = null) {
@ -99,14 +94,14 @@ class SpinnerPreference @JvmOverloads constructor(context: Context, attrs: Attri
// Set a listener so we are notified if a menu item is clicked // Set a listener so we are notified if a menu item is clicked
popup.setOnMenuItemClickListener { menuItem -> popup.setOnMenuItemClickListener { menuItem ->
val pos = popup.menuClicked(menuItem) val pos = menuClicked(menuItem)
onItemSelectedListener?.invoke(pos) onItemSelectedListener?.invoke(pos)
true true
} }
// Set a listener so we are notified if a menu item is clicked // Set a listener so we are notified if a menu item is clicked
popup.setOnMenuItemClickListener { menuItem -> popup.setOnMenuItemClickListener { menuItem ->
val enumConstants = T::class.java.enumConstants val enumConstants = T::class.java.enumConstants
val pos = popup.menuClicked(menuItem) val pos = menuClicked(menuItem)
enumConstants?.get(pos)?.let { preference.set(it) } enumConstants?.get(pos)?.let { preference.set(it) }
true true
} }
@ -117,7 +112,7 @@ class SpinnerPreference @JvmOverloads constructor(context: Context, attrs: Attri
val popup = popup() val popup = popup()
// Set a listener so we are notified if a menu item is clicked // Set a listener so we are notified if a menu item is clicked
popup.setOnMenuItemClickListener { menuItem -> popup.setOnMenuItemClickListener { menuItem ->
val pos = popup.menuClicked(menuItem) val pos = menuClicked(menuItem)
preference.set(intValues[pos] ?: 0) preference.set(intValues[pos] ?: 0)
block?.invoke(pos) block?.invoke(pos)
true true
@ -129,7 +124,7 @@ class SpinnerPreference @JvmOverloads constructor(context: Context, attrs: Attri
val popup = popup() val popup = popup()
// Set a listener so we are notified if a menu item is clicked // Set a listener so we are notified if a menu item is clicked
popup.setOnMenuItemClickListener { menuItem -> popup.setOnMenuItemClickListener { menuItem ->
val pos = popup.menuClicked(menuItem) val pos = menuClicked(menuItem)
preference.set(pos + offset) preference.set(pos + offset)
block?.invoke(pos) block?.invoke(pos)
true true
@ -142,20 +137,16 @@ class SpinnerPreference @JvmOverloads constructor(context: Context, attrs: Attri
// Set a listener so we are notified if a menu item is clicked // Set a listener so we are notified if a menu item is clicked
popup.setOnMenuItemClickListener { menuItem -> popup.setOnMenuItemClickListener { menuItem ->
val pos = popup.menuClicked(menuItem) val pos = menuClicked(menuItem)
onItemSelectedListener?.invoke(pos) onItemSelectedListener?.invoke(pos)
true true
} }
return popup return popup
} }
fun PopupMenu.menuClicked(menuItem: MenuItem): Int { fun menuClicked(menuItem: MenuItem): Int {
val pos = menuItem.itemId val pos = menuItem.itemId
menu[selectedPosition].isCheckable = false
menu[selectedPosition].isChecked = false
setSelection(pos) setSelection(pos)
menu[pos].isCheckable = true
menu[pos].isChecked = true
return pos return pos
} }
@ -164,8 +155,6 @@ class SpinnerPreference @JvmOverloads constructor(context: Context, attrs: Attri
entries.forEachIndexed { index, entry -> entries.forEachIndexed { index, entry ->
popup.menu.add(0, index, 0, entry) popup.menu.add(0, index, 0, entry)
} }
popup.menu[selectedPosition].isCheckable = true
popup.menu[selectedPosition].isChecked = true
return popup return popup
} }
} }

View File

@ -1,10 +1,7 @@
package eu.kanade.tachiyomi.util.preference package eu.kanade.tachiyomi.util.preference
import android.widget.CompoundButton import android.widget.CompoundButton
import android.widget.Spinner
import androidx.annotation.ArrayRes
import com.tfcporciuncula.flow.Preference import com.tfcporciuncula.flow.Preference
import eu.kanade.tachiyomi.widget.IgnoreFirstSpinnerListener
/** /**
* Binds a checkbox or switch view with a boolean preference. * Binds a checkbox or switch view with a boolean preference.
@ -13,39 +10,3 @@ fun CompoundButton.bindToPreference(pref: Preference<Boolean>) {
isChecked = pref.get() isChecked = pref.get()
setOnCheckedChangeListener { _, isChecked -> pref.set(isChecked) } setOnCheckedChangeListener { _, isChecked -> pref.set(isChecked) }
} }
/**
* Binds a spinner to an int preference with an optional offset for the value.
*/
fun Spinner.bindToPreference(pref: Preference<Int>, offset: Int = 0) {
onItemSelectedListener = IgnoreFirstSpinnerListener { position ->
pref.set(position + offset)
}
setSelection(pref.get() - offset, false)
}
/**
* Binds a spinner to an enum preference.
*/
inline fun <reified T : Enum<T>> Spinner.bindToPreference(pref: Preference<T>) {
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 <string-array>
* of int values that will be parsed here and applied to the preference.
*/
fun Spinner.bindToIntPreference(pref: Preference<Int>, @ArrayRes intValuesResource: Int) {
val intValues = resources.getStringArray(intValuesResource).map { it.toIntOrNull() }
onItemSelectedListener = IgnoreFirstSpinnerListener { position ->
pref.set(intValues[position]!!)
}
setSelection(intValues.indexOf(pref.get()), false)
}

View File

@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="?attr/listPreferredItemHeightSmall" android:layout_height="?attr/listPreferredItemHeightSmall"
@ -23,6 +24,6 @@
android:id="@+id/expand_icon" android:id="@+id/expand_icon"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:tint="?attr/colorOnPrimary" /> app:tint="?attr/colorOnPrimary" />
</LinearLayout> </LinearLayout>

View File

@ -3,9 +3,7 @@
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent">
android:clipToPadding="false"
android:padding="16dp">
<LinearLayout <LinearLayout
android:layout_width="match_parent" android:layout_width="match_parent"
@ -30,12 +28,16 @@
android:id="@+id/show_page_number" android:id="@+id/show_page_number"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:paddingStart="16dp"
android:paddingEnd="16dp"
android:text="@string/pref_show_page_number" /> android:text="@string/pref_show_page_number" />
<com.google.android.material.switchmaterial.SwitchMaterial <com.google.android.material.switchmaterial.SwitchMaterial
android:id="@+id/fullscreen" android:id="@+id/fullscreen"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:paddingStart="16dp"
android:paddingEnd="16dp"
android:text="@string/pref_fullscreen" /> android:text="@string/pref_fullscreen" />
<com.google.android.material.switchmaterial.SwitchMaterial <com.google.android.material.switchmaterial.SwitchMaterial
@ -43,6 +45,8 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="@string/pref_cutout_short" android:text="@string/pref_cutout_short"
android:paddingStart="16dp"
android:paddingEnd="16dp"
android:visibility="gone" android:visibility="gone"
tools:visibility="visible" /> tools:visibility="visible" />
@ -50,24 +54,32 @@
android:id="@+id/keepscreen" android:id="@+id/keepscreen"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:paddingStart="16dp"
android:paddingEnd="16dp"
android:text="@string/pref_keep_screen_on" /> android:text="@string/pref_keep_screen_on" />
<com.google.android.material.switchmaterial.SwitchMaterial <com.google.android.material.switchmaterial.SwitchMaterial
android:id="@+id/long_tap" android:id="@+id/long_tap"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:paddingStart="16dp"
android:paddingEnd="16dp"
android:text="@string/pref_read_with_long_tap" /> android:text="@string/pref_read_with_long_tap" />
<com.google.android.material.switchmaterial.SwitchMaterial <com.google.android.material.switchmaterial.SwitchMaterial
android:id="@+id/always_show_chapter_transition" android:id="@+id/always_show_chapter_transition"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:paddingStart="16dp"
android:paddingEnd="16dp"
android:text="@string/pref_always_show_chapter_transition" /> android:text="@string/pref_always_show_chapter_transition" />
<com.google.android.material.switchmaterial.SwitchMaterial <com.google.android.material.switchmaterial.SwitchMaterial
android:id="@+id/page_transitions" android:id="@+id/page_transitions"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:paddingStart="16dp"
android:paddingEnd="16dp"
android:text="@string/pref_page_transitions" /> android:text="@string/pref_page_transitions" />
</LinearLayout> </LinearLayout>

View File

@ -10,12 +10,10 @@
android:id="@+id/pager_prefs" android:id="@+id/pager_prefs"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:text="@string/pager_viewer" android:text="@string/pager_viewer"
android:textColor="?attr/colorAccent" android:paddingStart="16dp"
android:textStyle="bold" android:paddingEnd="16dp"
app:layout_constraintStart_toStartOf="parent" android:textAppearance="@style/TextAppearance.Medium.SubHeading" />
app:layout_constraintTop_toTopOf="parent" />
<eu.kanade.tachiyomi.ui.reader.setting.SpinnerPreference <eu.kanade.tachiyomi.ui.reader.setting.SpinnerPreference
android:id="@+id/pager_nav" android:id="@+id/pager_nav"
@ -49,23 +47,26 @@
android:id="@+id/crop_borders" android:id="@+id/crop_borders"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="@string/pref_crop_borders" android:paddingStart="16dp"
app:layout_constraintTop_toBottomOf="@id/zoom_start" /> android:paddingEnd="16dp"
android:text="@string/pref_crop_borders" />
<com.google.android.material.switchmaterial.SwitchMaterial <com.google.android.material.switchmaterial.SwitchMaterial
android:id="@+id/dual_page_split" android:id="@+id/dual_page_split"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="@string/pref_dual_page_split" android:text="@string/pref_dual_page_split"
app:layout_constraintTop_toBottomOf="@id/crop_borders" /> android:paddingStart="16dp"
android:paddingEnd="16dp" />
<com.google.android.material.switchmaterial.SwitchMaterial <com.google.android.material.switchmaterial.SwitchMaterial
android:id="@+id/dual_page_invert" android:id="@+id/dual_page_invert"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:paddingStart="16dp"
android:paddingEnd="16dp"
android:text="@string/pref_dual_page_invert" android:text="@string/pref_dual_page_invert"
android:visibility="gone" android:visibility="gone"
app:layout_constraintTop_toBottomOf="@id/dual_page_split"
tools:visibility="visible" /> tools:visibility="visible" />
<androidx.constraintlayout.widget.Group <androidx.constraintlayout.widget.Group

View File

@ -3,9 +3,7 @@
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent">
android:clipToPadding="false"
android:padding="16dp">
<LinearLayout <LinearLayout
android:layout_width="match_parent" android:layout_width="match_parent"
@ -16,6 +14,7 @@
android:id="@+id/viewer" android:id="@+id/viewer"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginBottom="24dp"
android:entries="@array/viewers_selector" android:entries="@array/viewers_selector"
app:title="@string/pref_category_for_this_series" /> app:title="@string/pref_category_for_this_series" />
@ -26,9 +25,6 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:visibility="gone" android:visibility="gone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/viewer"
tools:visibility="visible" /> tools:visibility="visible" />
<!-- Webtoon preferences --> <!-- Webtoon preferences -->
@ -37,10 +33,7 @@
layout="@layout/reader_webtoon_settings" layout="@layout/reader_webtoon_settings"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:visibility="gone" android:visibility="gone" />
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/viewer" />
</LinearLayout> </LinearLayout>

View File

@ -10,12 +10,10 @@
android:id="@+id/webtoon_prefs" android:id="@+id/webtoon_prefs"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:text="@string/webtoon_viewer" android:text="@string/webtoon_viewer"
android:textColor="?attr/colorAccent" android:paddingStart="16dp"
android:textStyle="bold" android:paddingEnd="16dp"
app:layout_constraintStart_toStartOf="parent" android:textAppearance="@style/TextAppearance.Medium.SubHeading" />
app:layout_constraintTop_toTopOf="parent" />
<eu.kanade.tachiyomi.ui.reader.setting.SpinnerPreference <eu.kanade.tachiyomi.ui.reader.setting.SpinnerPreference
android:id="@+id/webtoon_nav" android:id="@+id/webtoon_nav"
@ -43,22 +41,25 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="@string/pref_crop_borders" android:text="@string/pref_crop_borders"
app:layout_constraintTop_toBottomOf="@+id/webtoon_side_padding" /> android:paddingStart="16dp"
android:paddingEnd="16dp" />
<com.google.android.material.switchmaterial.SwitchMaterial <com.google.android.material.switchmaterial.SwitchMaterial
android:id="@+id/dual_page_split" android:id="@+id/dual_page_split"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="@string/pref_dual_page_split" android:paddingStart="16dp"
app:layout_constraintTop_toBottomOf="@id/crop_borders_webtoon" /> android:paddingEnd="16dp"
android:text="@string/pref_dual_page_split" />
<com.google.android.material.switchmaterial.SwitchMaterial <com.google.android.material.switchmaterial.SwitchMaterial
android:id="@+id/dual_page_invert" android:id="@+id/dual_page_invert"
android:layout_width="match_parent" android:layout_width="match_parent"
android:paddingStart="16dp"
android:paddingEnd="16dp"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="@string/pref_dual_page_invert" android:text="@string/pref_dual_page_invert"
android:visibility="gone" android:visibility="gone"
app:layout_constraintTop_toBottomOf="@id/dual_page_split"
tools:visibility="visible" /> tools:visibility="visible" />
<androidx.constraintlayout.widget.Group <androidx.constraintlayout.widget.Group

View File

@ -3,15 +3,16 @@
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content"> android:layout_height="?attr/listPreferredItemHeightSmall"
android:background="?attr/selectableItemBackground"
android:paddingStart="16dp"
android:paddingEnd="16dp">
<com.google.android.material.textview.MaterialTextView <com.google.android.material.textview.MaterialTextView
android:id="@+id/title" android:id="@+id/title"
style="@style/TextAppearance.MaterialComponents.Body2" style="@style/TextAppearance.MaterialComponents.Body2"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:layout_marginBottom="12dp"
android:maxLines="1" android:maxLines="1"
android:textColor="?android:attr/textColorPrimary" android:textColor="?android:attr/textColorPrimary"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintBottom_toBottomOf="parent"
@ -27,19 +28,20 @@
android:maxLines="1" android:maxLines="1"
android:textColor="?android:attr/textColorSecondary" android:textColor="?android:attr/textColorSecondary"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toStartOf="@+id/dropdown_caret"
app:layout_constraintHorizontal_bias="0.0" app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toEndOf="@+id/start_barrier" app:layout_constraintStart_toEndOf="@+id/start_barrier"
app:layout_constraintTop_toTopOf="parent" app:layout_constraintTop_toTopOf="parent"
tools:text="Details" /> tools:text="Details" />
<ImageView <ImageView
android:id="@+id/dropdown_caret"
android:layout_width="24dp" android:layout_width="24dp"
android:layout_height="24dp" android:layout_height="24dp"
android:layout_marginStart="1dp" android:layout_marginStart="1dp"
android:src="@drawable/ic_expand_more_24dp" android:src="@drawable/ic_expand_more_24dp"
app:layout_constraintBottom_toBottomOf="@id/details" app:layout_constraintBottom_toBottomOf="@id/details"
app:layout_constraintStart_toEndOf="@id/details" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@id/details" app:layout_constraintTop_toTopOf="@id/details"
app:tint="?android:attr/textColorSecondary" app:tint="?android:attr/textColorSecondary"
tools:ignore="ContentDescription" /> tools:ignore="ContentDescription" />