Grid size preference
This commit is contained in:
@@ -7,6 +7,7 @@ import androidx.appcompat.app.AppCompatDelegate
|
||||
import androidx.preference.PreferenceManager
|
||||
import org.koitharu.kotatsu.R
|
||||
import org.koitharu.kotatsu.utils.delegates.prefs.EnumPreferenceDelegate
|
||||
import org.koitharu.kotatsu.utils.delegates.prefs.IntPreferenceDelegate
|
||||
import org.koitharu.kotatsu.utils.delegates.prefs.NullableStringPreferenceDelegate
|
||||
import org.koitharu.kotatsu.utils.delegates.prefs.StringIntPreferenceDelegate
|
||||
|
||||
@@ -29,6 +30,11 @@ class AppSettings private constructor(resources: Resources, private val prefs: S
|
||||
AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM
|
||||
)
|
||||
|
||||
val gridSize by IntPreferenceDelegate(
|
||||
resources.getString(R.string.key_grid_size),
|
||||
100
|
||||
)
|
||||
|
||||
private var sourcesOrderStr by NullableStringPreferenceDelegate(resources.getString(R.string.key_sources_order))
|
||||
|
||||
var sourcesOrder: List<Int>
|
||||
|
||||
@@ -175,6 +175,7 @@ abstract class MangaListFragment<E> : BaseFragment(R.layout.fragment_list), Mang
|
||||
override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences, key: String) {
|
||||
when (key) {
|
||||
getString(R.string.key_list_mode) -> initListMode(settings.listMode)
|
||||
getString(R.string.key_grid_size) -> UiUtils.SpanCountResolver.update(recyclerView)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -7,6 +7,7 @@ import androidx.appcompat.app.AppCompatDelegate
|
||||
import androidx.collection.arrayMapOf
|
||||
import androidx.preference.ListPreference
|
||||
import androidx.preference.Preference
|
||||
import androidx.preference.SeekBarPreference
|
||||
import org.koitharu.kotatsu.R
|
||||
import org.koitharu.kotatsu.core.prefs.ListMode
|
||||
import org.koitharu.kotatsu.ui.common.BasePreferenceFragment
|
||||
@@ -20,7 +21,15 @@ class AppearanceSettingsFragment : BasePreferenceFragment(R.string.appearance),
|
||||
|
||||
findPreference<Preference>(R.string.key_list_mode)?.summary =
|
||||
listModes[settings.listMode]?.let(::getString)
|
||||
findPreference<ListPreference>(R.string.key_theme)?.summaryProvider = ListPreference.SimpleSummaryProvider.getInstance()
|
||||
findPreference<ListPreference>(R.string.key_theme)?.summaryProvider =
|
||||
ListPreference.SimpleSummaryProvider.getInstance()
|
||||
findPreference<SeekBarPreference>(R.string.key_grid_size)?.run {
|
||||
summary = "%d%%".format(value)
|
||||
setOnPreferenceChangeListener { preference, newValue ->
|
||||
preference.summary = "%d%%".format(newValue)
|
||||
true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||
|
||||
@@ -7,6 +7,8 @@ import androidx.annotation.ColorInt
|
||||
import androidx.recyclerview.widget.GridLayoutManager
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import org.koitharu.kotatsu.R
|
||||
import org.koitharu.kotatsu.core.prefs.AppSettings
|
||||
import org.koitharu.kotatsu.utils.ext.measureWidth
|
||||
import kotlin.math.abs
|
||||
import kotlin.math.roundToInt
|
||||
|
||||
@@ -24,7 +26,8 @@ object UiUtils {
|
||||
|
||||
@JvmStatic
|
||||
fun resolveGridSpanCount(context: Context, width: Int = 0): Int {
|
||||
val cellWidth = context.resources.getDimensionPixelSize(R.dimen.preferred_grid_width)
|
||||
val scaleFactor = AppSettings(context).gridSize / 100f
|
||||
val cellWidth = context.resources.getDimension(R.dimen.preferred_grid_width) * scaleFactor
|
||||
val screenWidth = (if (width <= 0) {
|
||||
context.resources.displayMetrics.widthPixels
|
||||
} else width).toDouble()
|
||||
@@ -49,5 +52,12 @@ object UiUtils {
|
||||
resolveGridSpanCount(rv.context, width)
|
||||
}
|
||||
|
||||
fun update(rv: RecyclerView) {
|
||||
val width = rv.measureWidth()
|
||||
if (width > 0) {
|
||||
(rv.layoutManager as? GridLayoutManager)?.spanCount =
|
||||
resolveGridSpanCount(rv.context, width)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package org.koitharu.kotatsu.utils.delegates.prefs
|
||||
|
||||
import android.content.SharedPreferences
|
||||
import androidx.core.content.edit
|
||||
import kotlin.properties.ReadWriteProperty
|
||||
import kotlin.reflect.KProperty
|
||||
|
||||
class IntPreferenceDelegate(private val key: String, private val defValue: Int) :
|
||||
ReadWriteProperty<SharedPreferences, Int> {
|
||||
|
||||
override fun getValue(thisRef: SharedPreferences, property: KProperty<*>): Int {
|
||||
return thisRef.getInt(key, defValue) ?: defValue
|
||||
}
|
||||
|
||||
override fun setValue(thisRef: SharedPreferences, property: KProperty<*>, value: Int) {
|
||||
thisRef.edit {
|
||||
putInt(key, value)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@
|
||||
<string name="key_theme">theme</string>
|
||||
<string name="key_sources_order">sources_order</string>
|
||||
<string name="key_pages_cache_clear">pages_cache_clear</string>
|
||||
<string name="key_grid_size">grid_size</string>
|
||||
<string-array name="values_theme">
|
||||
<item>-1</item>
|
||||
<item>1</item>
|
||||
|
||||
@@ -84,4 +84,5 @@
|
||||
<string name="webtoon">Webtoon</string>
|
||||
<string name="read_mode">Read mode</string>
|
||||
<string name="put_items_below_to_disable_it">Put items below to disable it</string>
|
||||
<string name="grid_size">Grid size</string>
|
||||
</resources>
|
||||
@@ -9,13 +9,25 @@
|
||||
android:entryValues="@array/values_theme"
|
||||
android:key="@string/key_theme"
|
||||
android:title="@string/theme"
|
||||
app:allowDividerBelow="true"
|
||||
app:iconSpaceReserved="false" />
|
||||
|
||||
<Preference
|
||||
android:key="@string/key_list_mode"
|
||||
android:persistent="false"
|
||||
android:title="@string/list_mode"
|
||||
app:allowDividerAbove="true"
|
||||
app:iconSpaceReserved="false" />
|
||||
|
||||
<SeekBarPreference
|
||||
android:key="@string/key_grid_size"
|
||||
android:title="@string/grid_size"
|
||||
app:iconSpaceReserved="false"
|
||||
app:defaultValue="100"
|
||||
app:min="50"
|
||||
app:showSeekBarValue="false"
|
||||
app:updatesContinuously="true"
|
||||
app:seekBarIncrement="10"
|
||||
app:allowDividerBelow="true"
|
||||
android:max="150" />
|
||||
|
||||
</PreferenceScreen>
|
||||
Reference in New Issue
Block a user