Improve Cards layout number controls

This commit is contained in:
ajp_anton
2026-08-30 23:41:58 +00:00
parent 55de8f7aa9
commit 08f8764c77
3 changed files with 52 additions and 29 deletions
@@ -2,10 +2,8 @@ package se.ajpanton.notificationsmaster
import android.os.Bundle
import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import android.widget.Button
import android.widget.SeekBar
import android.widget.Spinner
import android.widget.TextView
import androidx.fragment.app.Fragment
import com.google.android.material.switchmaterial.SwitchMaterial
@@ -50,10 +48,10 @@ class CardsLayoutFragment : Fragment(R.layout.fragment_cards_layout) {
currentSettings: (VisibilityPolicy) -> CardsGridSettings,
update: (VisibilityPolicy, CardsGridSettings) -> VisibilityPolicy,
) {
controls.rows.bindNumbers(0..5, initial.maxRows) { value ->
controls.rows.bind(0..5, initial.maxRows) { value ->
save(currentSettings, update) { it.copy(maxRows = value) }
}
controls.icons.bindNumbers(1..15, initial.maxIconsPerRow) { value ->
controls.icons.bind(1..15, initial.maxIconsPerRow) { value ->
save(currentSettings, update) { it.copy(maxIconsPerRow = value) }
}
controls.even.isChecked = initial.evenDistribution
@@ -90,48 +88,57 @@ class CardsLayoutFragment : Fragment(R.layout.fragment_cards_layout) {
}
}
private fun Spinner.bindNumbers(range: IntRange, selected: Int, changed: (Int) -> Unit) {
private fun NumberStepper.bind(range: IntRange, selected: Int, changed: (Int) -> Unit) {
var current = selected
adapter = ArrayAdapter(
requireContext(),
android.R.layout.simple_spinner_dropdown_item,
range.map(Int::toString),
)
setSelection(selected - range.first, false)
onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
override fun onNothingSelected(parent: AdapterView<*>?) = Unit
override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
val value = range.first + position
if (value != current) {
current = value
changed(value)
fun show() {
value.text = current.toString()
minus.isEnabled = current > range.first
plus.isEnabled = current < range.last
}
fun step(amount: Int) {
val next = (current + amount).coerceIn(range)
if (next != current) {
current = next
show()
changed(next)
}
}
minus.setOnClickListener { step(-1) }
plus.setOnClickListener { step(1) }
show()
}
private fun controls(lockscreen: Boolean): Controls {
val b = binding!!
return if (lockscreen) b.lockscreenSettings.run {
Controls(
lockscreenMaxRows,
lockscreenMaxIcons,
NumberStepper(lockscreenMaxRowsMinus, lockscreenMaxRows, lockscreenMaxRowsPlus),
NumberStepper(lockscreenMaxIconsMinus, lockscreenMaxIcons, lockscreenMaxIconsPlus),
lockscreenEvenDistribution,
lockscreenLimitWidth,
lockscreenIconHeight,
lockscreenIconHeightValue,
)
} else b.aodSettings.run {
Controls(aodMaxRows, aodMaxIcons, aodEvenDistribution, aodLimitWidth, aodIconHeight, aodIconHeightValue)
Controls(
NumberStepper(aodMaxRowsMinus, aodMaxRows, aodMaxRowsPlus),
NumberStepper(aodMaxIconsMinus, aodMaxIcons, aodMaxIconsPlus),
aodEvenDistribution,
aodLimitWidth,
aodIconHeight,
aodIconHeightValue,
)
}
}
private data class Controls(
val rows: Spinner,
val icons: Spinner,
val rows: NumberStepper,
val icons: NumberStepper,
val even: SwitchMaterial,
val limit: SwitchMaterial,
val height: SeekBar,
val heightValue: TextView,
)
private data class NumberStepper(val minus: Button, val value: TextView, val plus: Button)
}
@@ -5,9 +5,17 @@
android:orientation="vertical">
<TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="12dp" android:text="Max rows" />
<Spinner android:id="@+id/aod_max_rows" android:layout_width="wrap_content" android:layout_height="wrap_content" android:popupBackground="@drawable/dropdown_popup_background" />
<LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center_vertical" android:orientation="horizontal">
<com.google.android.material.button.MaterialButton android:id="@+id/aod_max_rows_minus" style="?attr/materialButtonOutlinedStyle" android:layout_width="48dp" android:layout_height="wrap_content" android:contentDescription="Decrease max rows" android:text="" />
<TextView android:id="@+id/aod_max_rows" android:layout_width="48dp" android:layout_height="wrap_content" android:gravity="center" android:textAppearance="?attr/textAppearanceBody1" />
<com.google.android.material.button.MaterialButton android:id="@+id/aod_max_rows_plus" style="?attr/materialButtonOutlinedStyle" android:layout_width="48dp" android:layout_height="wrap_content" android:contentDescription="Increase max rows" android:text="+" />
</LinearLayout>
<TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="8dp" android:text="Max icons per row" />
<Spinner android:id="@+id/aod_max_icons" android:layout_width="wrap_content" android:layout_height="wrap_content" android:popupBackground="@drawable/dropdown_popup_background" />
<LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center_vertical" android:orientation="horizontal">
<com.google.android.material.button.MaterialButton android:id="@+id/aod_max_icons_minus" style="?attr/materialButtonOutlinedStyle" android:layout_width="48dp" android:layout_height="wrap_content" android:contentDescription="Decrease max icons per row" android:text="" />
<TextView android:id="@+id/aod_max_icons" android:layout_width="48dp" android:layout_height="wrap_content" android:gravity="center" android:textAppearance="?attr/textAppearanceBody1" />
<com.google.android.material.button.MaterialButton android:id="@+id/aod_max_icons_plus" style="?attr/materialButtonOutlinedStyle" android:layout_width="48dp" android:layout_height="wrap_content" android:contentDescription="Increase max icons per row" android:text="+" />
</LinearLayout>
<com.google.android.material.switchmaterial.SwitchMaterial android:id="@+id/aod_even_distribution" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="8dp" android:text="Even distribution" />
<com.google.android.material.switchmaterial.SwitchMaterial android:id="@+id/aod_limit_width" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Limit to screen width" />
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="8dp" android:gravity="center_vertical" android:orientation="horizontal">
@@ -5,9 +5,17 @@
android:orientation="vertical">
<TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="12dp" android:text="Max rows" />
<Spinner android:id="@+id/lockscreen_max_rows" android:layout_width="wrap_content" android:layout_height="wrap_content" android:popupBackground="@drawable/dropdown_popup_background" />
<LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center_vertical" android:orientation="horizontal">
<com.google.android.material.button.MaterialButton android:id="@+id/lockscreen_max_rows_minus" style="?attr/materialButtonOutlinedStyle" android:layout_width="48dp" android:layout_height="wrap_content" android:contentDescription="Decrease max rows" android:text="" />
<TextView android:id="@+id/lockscreen_max_rows" android:layout_width="48dp" android:layout_height="wrap_content" android:gravity="center" android:textAppearance="?attr/textAppearanceBody1" />
<com.google.android.material.button.MaterialButton android:id="@+id/lockscreen_max_rows_plus" style="?attr/materialButtonOutlinedStyle" android:layout_width="48dp" android:layout_height="wrap_content" android:contentDescription="Increase max rows" android:text="+" />
</LinearLayout>
<TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="8dp" android:text="Max icons per row" />
<Spinner android:id="@+id/lockscreen_max_icons" android:layout_width="wrap_content" android:layout_height="wrap_content" android:popupBackground="@drawable/dropdown_popup_background" />
<LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center_vertical" android:orientation="horizontal">
<com.google.android.material.button.MaterialButton android:id="@+id/lockscreen_max_icons_minus" style="?attr/materialButtonOutlinedStyle" android:layout_width="48dp" android:layout_height="wrap_content" android:contentDescription="Decrease max icons per row" android:text="" />
<TextView android:id="@+id/lockscreen_max_icons" android:layout_width="48dp" android:layout_height="wrap_content" android:gravity="center" android:textAppearance="?attr/textAppearanceBody1" />
<com.google.android.material.button.MaterialButton android:id="@+id/lockscreen_max_icons_plus" style="?attr/materialButtonOutlinedStyle" android:layout_width="48dp" android:layout_height="wrap_content" android:contentDescription="Increase max icons per row" android:text="+" />
</LinearLayout>
<com.google.android.material.switchmaterial.SwitchMaterial android:id="@+id/lockscreen_even_distribution" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="8dp" android:text="Even distribution" />
<com.google.android.material.switchmaterial.SwitchMaterial android:id="@+id/lockscreen_limit_width" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Limit to screen width" />
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="8dp" android:gravity="center_vertical" android:orientation="horizontal">