Grid size preference

This commit is contained in:
Koitharu
2020-02-29 13:43:22 +02:00
parent ad201d2bcd
commit 5c3242c511
8 changed files with 63 additions and 3 deletions

View File

@@ -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>

View File

@@ -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)
}
}

View File

@@ -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?) {

View File

@@ -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)
}
}
}
}

View File

@@ -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)
}
}
}