Bind the margin ratio as a float preference and rename variables

- Fixed the floatListPreference that was used in the SettingsReaderController
- Created new extension for binding float preferences in the ReaderSettingsSheet
- Renamed the ratio variables to include the `webtoon` naming
This commit is contained in:
John Leehey 2019-12-02 14:19:22 -08:00
parent 4014c48c62
commit f14af7cf83
8 changed files with 39 additions and 26 deletions

View File

@ -51,7 +51,7 @@ object PreferenceKeys {
const val readWithVolumeKeysInverted = "reader_volume_keys_inverted" const val readWithVolumeKeysInverted = "reader_volume_keys_inverted"
const val webtoonMarginRatio = "margin_ratio" const val marginRatioWebtoon = "margin_ratio_webtoon"
const val portraitColumns = "pref_library_columns_portrait_key" const val portraitColumns = "pref_library_columns_portrait_key"

View File

@ -71,6 +71,8 @@ class PreferencesHelper(val context: Context) {
fun cropBordersWebtoon() = rxPrefs.getBoolean(Keys.cropBordersWebtoon, false) fun cropBordersWebtoon() = rxPrefs.getBoolean(Keys.cropBordersWebtoon, false)
fun marginRatioWebtoon() = rxPrefs.getFloat(Keys.marginRatioWebtoon, 0f)
fun readWithTapping() = rxPrefs.getBoolean(Keys.readWithTapping, true) fun readWithTapping() = rxPrefs.getBoolean(Keys.readWithTapping, true)
fun readWithLongTap() = rxPrefs.getBoolean(Keys.readWithLongTap, true) fun readWithLongTap() = rxPrefs.getBoolean(Keys.readWithLongTap, true)
@ -79,8 +81,6 @@ class PreferencesHelper(val context: Context) {
fun readWithVolumeKeysInverted() = rxPrefs.getBoolean(Keys.readWithVolumeKeysInverted, false) fun readWithVolumeKeysInverted() = rxPrefs.getBoolean(Keys.readWithVolumeKeysInverted, false)
fun marginRatio() = rxPrefs.getInteger(Keys.webtoonMarginRatio, 0)
fun portraitColumns() = rxPrefs.getInteger(Keys.portraitColumns, 0) fun portraitColumns() = rxPrefs.getInteger(Keys.portraitColumns, 0)
fun landscapeColumns() = rxPrefs.getInteger(Keys.landscapeColumns, 0) fun landscapeColumns() = rxPrefs.getInteger(Keys.landscapeColumns, 0)

View File

@ -1,6 +1,7 @@
package eu.kanade.tachiyomi.ui.reader package eu.kanade.tachiyomi.ui.reader
import android.os.Bundle import android.os.Bundle
import android.support.annotation.ArrayRes
import android.support.design.widget.BottomSheetDialog import android.support.design.widget.BottomSheetDialog
import android.support.v4.widget.NestedScrollView import android.support.v4.widget.NestedScrollView
import android.widget.CompoundButton import android.widget.CompoundButton
@ -82,7 +83,7 @@ class ReaderSettingsSheet(private val activity: ReaderActivity) : BottomSheetDia
private fun initWebtoonPreferences() { private fun initWebtoonPreferences() {
webtoon_prefs_group.visible() webtoon_prefs_group.visible()
crop_borders_webtoon.bindToPreference(preferences.cropBordersWebtoon()) crop_borders_webtoon.bindToPreference(preferences.cropBordersWebtoon())
margin_ratio_webtoon.bindToPreference(preferences.marginRatio()) margin_ratio_webtoon.bindToFloatPreference(preferences.marginRatioWebtoon(), R.array.webtoon_margin_ratio_values)
} }
/** /**
@ -103,4 +104,17 @@ class ReaderSettingsSheet(private val activity: ReaderActivity) : BottomSheetDia
setSelection(pref.getOrDefault() - offset, false) setSelection(pref.getOrDefault() - offset, false)
} }
/**
* Binds a spinner to a float preference. The position of the spinner item must
* correlate with the [floatValues] resource item (in arrays.xml), which is a <string-array>
* of float values that will be parsed here and applied to the preference.
*/
private fun Spinner.bindToFloatPreference(pref: Preference<Float>, @ArrayRes floatValuesResource: Int) {
val floatValues = resources.getStringArray(floatValuesResource).map { it.toFloatOrNull() }
onItemSelectedListener = IgnoreFirstSpinnerListener { position ->
pref.set(floatValues[position])
}
setSelection(floatValues.indexOf(pref.getOrDefault()), false)
}
} }

View File

@ -56,22 +56,8 @@ class WebtoonConfig(preferences: PreferencesHelper = Injekt.get()) {
preferences.readWithVolumeKeysInverted() preferences.readWithVolumeKeysInverted()
.register({ volumeKeysInverted = it }) .register({ volumeKeysInverted = it })
preferences.marginRatio() preferences.marginRatioWebtoon()
.register({ marginFromPreference(it) }, { imagePropertyChangedListener?.invoke() }) .register({ marginRatio = it }, { imagePropertyChangedListener?.invoke() })
}
private fun marginFromPreference(position: Int) {
marginRatio = when (position) {
1 -> PageMargin.TEN_PERCENT
2 -> PageMargin.TWENTY_FIVE_PERCENT
else -> PageMargin.NO_MARGIN
}
}
object PageMargin {
const val NO_MARGIN = 0f
const val TEN_PERCENT = 0.1f
const val TWENTY_FIVE_PERCENT = 0.25f
} }
fun unsubscribe() { fun unsubscribe() {

View File

@ -114,11 +114,12 @@ class SettingsReaderController : SettingsController() {
} }
floatListPreference { floatListPreference {
key = Keys.webtoonMarginRatio key = Keys.marginRatioWebtoon
titleRes = R.string.pref_reader_theme titleRes = R.string.pref_reader_margin
entriesRes = arrayOf(R.string.webtoon_margin_ratio_0, entriesRes = arrayOf(R.string.webtoon_margin_ratio_0,
R.string.webtoon_margin_ratio_10, R.string.webtoon_margin_ratio_25) R.string.webtoon_margin_ratio_10, R.string.webtoon_margin_ratio_15,
entryValues = arrayOf("0", "1", "2") R.string.webtoon_margin_ratio_20, R.string.webtoon_margin_ratio_25)
entryValues = arrayOf("0", "0.1", "0.15", "0.2", "0.25")
defaultValue = "0" defaultValue = "0"
summary = "%s" summary = "%s"
} }

View File

@ -254,7 +254,7 @@
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="16dp" android:layout_marginTop="16dp"
android:entries="@array/margin_ratio" android:entries="@array/webtoon_margin_ratio"
app:layout_constraintLeft_toRightOf="@id/verticalcenter" app:layout_constraintLeft_toRightOf="@id/verticalcenter"
app:layout_constraintRight_toRightOf="@id/spinner_end" app:layout_constraintRight_toRightOf="@id/spinner_end"
app:layout_constraintTop_toBottomOf="@id/crop_borders_webtoon"/> app:layout_constraintTop_toBottomOf="@id/crop_borders_webtoon"/>

View File

@ -53,12 +53,22 @@
<item>@string/scale_type_smart_fit</item> <item>@string/scale_type_smart_fit</item>
</string-array> </string-array>
<string-array name="margin_ratio"> <string-array name="webtoon_margin_ratio">
<item>@string/webtoon_margin_ratio_0</item> <item>@string/webtoon_margin_ratio_0</item>
<item>@string/webtoon_margin_ratio_10</item> <item>@string/webtoon_margin_ratio_10</item>
<item>@string/webtoon_margin_ratio_15</item>
<item>@string/webtoon_margin_ratio_20</item>
<item>@string/webtoon_margin_ratio_25</item> <item>@string/webtoon_margin_ratio_25</item>
</string-array> </string-array>
<string-array name="webtoon_margin_ratio_values">
<item>0.0</item>
<item>0.1</item>
<item>0.15</item>
<item>0.2</item>
<item>0.25</item>
</string-array>
<string-array name="image_scale_type_values"> <string-array name="image_scale_type_values">
<item>1</item> <item>1</item>
<item>2</item> <item>2</item>

View File

@ -233,6 +233,8 @@
<string name="color_filter_a_value">A</string> <string name="color_filter_a_value">A</string>
<string name="webtoon_margin_ratio_0">No margin</string> <string name="webtoon_margin_ratio_0">No margin</string>
<string name="webtoon_margin_ratio_10">10%</string> <string name="webtoon_margin_ratio_10">10%</string>
<string name="webtoon_margin_ratio_15">15%</string>
<string name="webtoon_margin_ratio_20">20%</string>
<string name="webtoon_margin_ratio_25">25%</string> <string name="webtoon_margin_ratio_25">25%</string>