-
-
Notifications
You must be signed in to change notification settings - Fork 45
Replace setting sliders with steppers where useful #963
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,19 @@ | ||
| package com.philkes.notallyx.presentation.activity.main.fragment.settings | ||
|
|
||
| import android.annotation.SuppressLint | ||
| import android.content.Context | ||
| import android.hardware.biometrics.BiometricManager | ||
| import android.net.Uri | ||
| import android.os.Build | ||
| import android.os.Handler | ||
| import android.os.Looper | ||
| import android.text.method.PasswordTransformationMethod | ||
| import android.view.LayoutInflater | ||
| import android.view.MotionEvent | ||
| import android.view.View | ||
| import android.view.inputmethod.EditorInfo | ||
| import androidx.core.view.isVisible | ||
| import androidx.core.widget.doAfterTextChanged | ||
| import androidx.documentfile.provider.DocumentFile | ||
| import com.google.android.material.color.DynamicColors | ||
| import com.google.android.material.dialog.MaterialAlertDialogBuilder | ||
|
|
@@ -23,11 +29,14 @@ import com.philkes.notallyx.databinding.DialogSelectionBoxBinding | |
| import com.philkes.notallyx.databinding.DialogTextInputBinding | ||
| import com.philkes.notallyx.databinding.PreferenceBinding | ||
| import com.philkes.notallyx.databinding.PreferenceSeekbarBinding | ||
| import com.philkes.notallyx.databinding.PreferenceStepperBinding | ||
| import com.philkes.notallyx.presentation.checkedTag | ||
| import com.philkes.notallyx.presentation.hideKeyboard | ||
| import com.philkes.notallyx.presentation.select | ||
| import com.philkes.notallyx.presentation.setCancelButton | ||
| import com.philkes.notallyx.presentation.setTextSizeSp | ||
| import com.philkes.notallyx.presentation.showAndFocus | ||
| import com.philkes.notallyx.presentation.showKeyboard | ||
| import com.philkes.notallyx.presentation.showToast | ||
| import com.philkes.notallyx.presentation.view.misc.MenuDialog | ||
| import com.philkes.notallyx.presentation.viewmodel.BaseNoteModel | ||
|
|
@@ -501,43 +510,157 @@ fun PreferenceSeekbarBinding.setupTextSizePreference( | |
| } | ||
| } | ||
|
|
||
| fun PreferenceSeekbarBinding.setupAutoSaveIdleTime( | ||
| preference: IntPreference, | ||
| @SuppressLint("ClickableViewAccessibility") | ||
| fun PreferenceStepperBinding.setup( | ||
| value: Int, | ||
| titleResId: Int, | ||
| min: Int, | ||
| max: Int, | ||
| context: Context, | ||
| value: Int = preference.value, | ||
| enabled: Boolean = true, | ||
| labelFormatter: ((Int) -> String)? = null, | ||
| onChange: (newValue: Int) -> Unit, | ||
| ) { | ||
| Slider.apply { | ||
| setLabelFormatter { sliderValue -> | ||
| if (sliderValue == -1f) { | ||
| context.getString(R.string.disabled) | ||
| } else "${sliderValue.toInt()}s" | ||
| val initialValue = value.coerceIn(min, max) | ||
| Title.setText(titleResId) | ||
| ValueInput.setText(labelFormatter?.invoke(initialValue) ?: initialValue.toString()) | ||
| ValueInput.isEnabled = enabled | ||
|
|
||
| fun updateButtons(value: Int) { | ||
| MinusButton.isEnabled = enabled && value > min | ||
| PlusButton.isEnabled = enabled && value < max | ||
| } | ||
|
|
||
| updateButtons(initialValue) | ||
|
|
||
| val handler = Handler(Looper.getMainLooper()) | ||
| fun updateValue(increment: Int, commit: Boolean): Boolean { | ||
| val newValue = (ValueInput.tag as? Int ?: initialValue) + increment | ||
| val valid = newValue in min..max | ||
| if (valid) { | ||
| ValueInput.tag = newValue | ||
| ValueInput.setText(labelFormatter?.invoke(newValue) ?: newValue.toString()) | ||
| updateButtons(newValue) | ||
| if (commit) { | ||
| onChange(newValue) | ||
| } | ||
| } | ||
| ValueInput.clearFocus() | ||
| return valid | ||
|
Crustack marked this conversation as resolved.
|
||
| } | ||
|
|
||
| fun stepperRunnable(isIncrement: Boolean) = | ||
| object : Runnable { | ||
| override fun run() { | ||
| if (updateValue(if (isIncrement) 1 else -1, false)) { | ||
| handler.postDelayed(this, 100) | ||
| } | ||
| } | ||
| } | ||
| addOnChangeListener { _, value, _ -> | ||
| if (value == -1f) { | ||
| setAlpha(0.6f) // Reduce opacity to make it look disabled | ||
| var runnable: Runnable? = null | ||
| val startAutoIncrement = { isIncrement: Boolean -> | ||
| runnable?.let { handler.removeCallbacks(it) } | ||
| runnable = | ||
| if (isIncrement) stepperRunnable(isIncrement = true) | ||
| else stepperRunnable(isIncrement = false) | ||
| handler.postDelayed(runnable!!, 100) | ||
| } | ||
|
|
||
| MinusButton.apply { | ||
| setOnClickListener { updateValue(-1, true) } | ||
| setOnLongClickListener { | ||
| startAutoIncrement(false) | ||
| true | ||
| } | ||
| setOnTouchListener { _, event -> | ||
| if ( | ||
| runnable != null && | ||
| (event.action == MotionEvent.ACTION_UP || | ||
| event.action == MotionEvent.ACTION_CANCEL) | ||
| ) { | ||
| handler.removeCallbacks(runnable!!) | ||
| onChange(ValueInput.tag as? Int ?: value) | ||
| } | ||
| false | ||
| } | ||
| } | ||
|
|
||
| PlusButton.apply { | ||
| setOnClickListener { updateValue(1, true) } | ||
| setOnLongClickListener { | ||
| startAutoIncrement(true) | ||
| true | ||
| } | ||
| setOnTouchListener { _, event -> | ||
| if ( | ||
| runnable != null && | ||
| (event.action == MotionEvent.ACTION_UP || | ||
| event.action == MotionEvent.ACTION_CANCEL) | ||
| ) { | ||
| handler.removeCallbacks(runnable!!) | ||
| onChange(ValueInput.tag as? Int ?: value) | ||
| } | ||
| false | ||
| } | ||
| } | ||
|
Comment on lines
+560
to
+605
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't commit twice on a normal tap. The touch listener saves on every button release, and the click listener saves again immediately after. For settings with side effects, that means duplicated work on every tap; Suggested fix- fun stepperRunnable(isIncrement: Boolean) =
+ var didAutoRepeat = false
+ fun stepperRunnable(isIncrement: Boolean) =
object : Runnable {
override fun run() {
if (updateValue(if (isIncrement) 1 else -1, false)) {
+ didAutoRepeat = true
handler.postDelayed(this, 100)
}
}
}
var runnable: Runnable? = null
val startAutoIncrement = { isIncrement: Boolean ->
+ didAutoRepeat = false
runnable?.let { handler.removeCallbacks(it) }
runnable =
if (isIncrement) stepperRunnable(isIncrement = true)
else stepperRunnable(isIncrement = false)
handler.postDelayed(runnable!!, 100)
@@
MinusButton.setOnTouchListener { _, event ->
if (event.action == MotionEvent.ACTION_UP || event.action == MotionEvent.ACTION_CANCEL) {
runnable?.let { handler.removeCallbacks(it) }
- onChange(ValueInput.tag as? Int ?: value)
+ if (didAutoRepeat) {
+ didAutoRepeat = false
+ onChange(ValueInput.tag as? Int ?: value)
+ }
}
false
}
@@
PlusButton.setOnTouchListener { _, event ->
if (event.action == MotionEvent.ACTION_UP || event.action == MotionEvent.ACTION_CANCEL) {
runnable?.let { handler.removeCallbacks(it) }
- onChange(ValueInput.tag as? Int ?: value)
+ if (didAutoRepeat) {
+ didAutoRepeat = false
+ onChange(ValueInput.tag as? Int ?: value)
+ }
}
false
}🤖 Prompt for AI Agents |
||
|
|
||
| ValueInput.apply { | ||
| tag = value | ||
| doAfterTextChanged { text -> | ||
| if (ValueInput.hasFocus()) { | ||
| val newValue = text.toString().toIntOrNull() | ||
| if (newValue != null) { | ||
| val clampedValue = newValue.coerceIn(min, max) | ||
| if (clampedValue != ValueInput.tag) { | ||
| ValueInput.tag = clampedValue | ||
| updateButtons(clampedValue) | ||
| } | ||
| } | ||
| } | ||
| } | ||
| setOnFocusChangeListener { _, hasFocus -> | ||
| if (hasFocus) { | ||
| val currentValue = ValueInput.tag as? Int ?: initialValue | ||
| val text = currentValue.toString() | ||
| ValueInput.setText(text) | ||
| ValueInput.setSelection(text.length) | ||
| context.showKeyboard(ValueInput) | ||
| } else { | ||
| setAlpha(1f) // Restore normal appearance | ||
| val currentValue = ValueInput.tag as? Int ?: initialValue | ||
| ValueInput.setText(labelFormatter?.invoke(currentValue) ?: currentValue.toString()) | ||
| updateButtons(currentValue) | ||
| onChange(currentValue) | ||
| } | ||
| } | ||
| setOnEditorActionListener { v, actionId, _ -> | ||
| if (actionId == EditorInfo.IME_ACTION_DONE || actionId == EditorInfo.IME_ACTION_NEXT) { | ||
| context.hideKeyboard(ValueInput) | ||
| v.clearFocus() // This triggers the OnFocusChangeListener logic | ||
| true | ||
| } else { | ||
| false | ||
| } | ||
| } | ||
| } | ||
| setup(preference, context, value, onChange = onChange) | ||
| } | ||
|
|
||
| fun PreferenceSeekbarBinding.setupAutoEmptyBin( | ||
| fun PreferenceStepperBinding.setup( | ||
| preference: IntPreference, | ||
| context: Context, | ||
| value: Int = preference.value, | ||
| labelFormatter: ((Int) -> String)? = null, | ||
| onChange: (newValue: Int) -> Unit, | ||
| ) { | ||
| Slider.apply { | ||
| setLabelFormatter { sliderValue -> | ||
| if (sliderValue == 0f) { | ||
| context.getString(R.string.disabled) | ||
| } else "${sliderValue.toInt()} ${context.getString(R.string.days)}" | ||
| } | ||
| setup( | ||
| value, | ||
| preference.titleResId!!, | ||
| preference.min, | ||
| preference.max, | ||
| context, | ||
| labelFormatter = labelFormatter, | ||
| ) { newValue -> | ||
| onChange(newValue) | ||
| } | ||
| setup(preference, context, value, R.string.auto_remove_deleted_notes_hint, onChange) | ||
| } | ||
|
|
||
| fun PreferenceBinding.setupStartView( | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| <vector xmlns:android="http://schemas.android.com/apk/res/android" | ||
| android:width="24dp" | ||
| android:height="24dp" | ||
| android:viewportWidth="960" | ||
| android:viewportHeight="960" | ||
| android:tint="?attr/colorControlNormal"> | ||
| <path | ||
| android:fillColor="@android:color/white" | ||
| android:pathData="M200,520L200,440L760,440L760,520L200,520Z"/> | ||
| </vector> |
Uh oh!
There was an error while loading. Please reload this page.