Deduplicate settings sanitizers
This commit is contained in:
@@ -62,6 +62,17 @@ data class NavButtonConfig(
|
||||
val longPressDurationMs: Int,
|
||||
)
|
||||
|
||||
internal fun sanitizeSideArrowRepeatSpeed(stepsPerSecond: Float): Float {
|
||||
return if (stepsPerSecond.isFinite()) {
|
||||
stepsPerSecond.coerceIn(
|
||||
NavButtonSettingsStore.MIN_SIDE_ARROW_REPEAT_STEPS_PER_SECOND,
|
||||
NavButtonSettingsStore.MAX_SIDE_ARROW_REPEAT_STEPS_PER_SECOND,
|
||||
)
|
||||
} else {
|
||||
NavButtonSettingsStore.DEFAULT_SIDE_ARROW_REPEAT_STEPS_PER_SECOND
|
||||
}
|
||||
}
|
||||
|
||||
class NavButtonSettingsStore(context: Context) {
|
||||
private val localPreferences = context.applicationContext.getSharedPreferences(
|
||||
PREFERENCES_NAME,
|
||||
@@ -82,12 +93,10 @@ class NavButtonSettingsStore(context: Context) {
|
||||
longPressAction = NavButtonAction.fromStorageValue(
|
||||
preferences.getString(longPressActionKey(buttonId), null),
|
||||
),
|
||||
longPressDurationMs = sanitizeDuration(
|
||||
preferences.getInt(
|
||||
longPressDurationKey(buttonId),
|
||||
defaultLongPressDurationMs(),
|
||||
),
|
||||
),
|
||||
longPressDurationMs = preferences.getInt(
|
||||
longPressDurationKey(buttonId),
|
||||
defaultLongPressDurationMs(),
|
||||
).coerceLongPressDuration(),
|
||||
)
|
||||
}
|
||||
|
||||
@@ -105,7 +114,7 @@ class NavButtonSettingsStore(context: Context) {
|
||||
|
||||
fun setLongPressDurationMs(buttonId: NavButtonId, durationMs: Int) {
|
||||
persistChanges {
|
||||
putInt(longPressDurationKey(buttonId), sanitizeDuration(durationMs))
|
||||
putInt(longPressDurationKey(buttonId), durationMs.coerceLongPressDuration())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -150,37 +159,20 @@ class NavButtonSettingsStore(context: Context) {
|
||||
}
|
||||
|
||||
fun getSideArrowLongPressDurationMs(): Int {
|
||||
return sanitizeDuration(
|
||||
currentPreferences().getInt(
|
||||
KEY_SIDE_ARROW_LONG_PRESS_DURATION_MS,
|
||||
defaultLongPressDurationMs(),
|
||||
),
|
||||
)
|
||||
return currentPreferences().getInt(
|
||||
KEY_SIDE_ARROW_LONG_PRESS_DURATION_MS,
|
||||
defaultLongPressDurationMs(),
|
||||
).coerceLongPressDuration()
|
||||
}
|
||||
|
||||
fun setSideArrowLongPressDurationMs(durationMs: Int) {
|
||||
persistChanges {
|
||||
putInt(KEY_SIDE_ARROW_LONG_PRESS_DURATION_MS, sanitizeDuration(durationMs))
|
||||
putInt(KEY_SIDE_ARROW_LONG_PRESS_DURATION_MS, durationMs.coerceLongPressDuration())
|
||||
}
|
||||
}
|
||||
|
||||
fun defaultLongPressDurationMs(): Int {
|
||||
return sanitizeDuration(ViewConfiguration.getLongPressTimeout())
|
||||
}
|
||||
|
||||
fun sanitizeDuration(durationMs: Int): Int {
|
||||
return durationMs.coerceIn(MIN_DURATION_MS, MAX_DURATION_MS)
|
||||
}
|
||||
|
||||
fun sanitizeSideArrowRepeatSpeed(stepsPerSecond: Float): Float {
|
||||
return if (stepsPerSecond.isFinite()) {
|
||||
stepsPerSecond.coerceIn(
|
||||
MIN_SIDE_ARROW_REPEAT_STEPS_PER_SECOND,
|
||||
MAX_SIDE_ARROW_REPEAT_STEPS_PER_SECOND,
|
||||
)
|
||||
} else {
|
||||
DEFAULT_SIDE_ARROW_REPEAT_STEPS_PER_SECOND
|
||||
}
|
||||
return ViewConfiguration.getLongPressTimeout().coerceLongPressDuration()
|
||||
}
|
||||
|
||||
private fun currentPreferences(): SharedPreferences {
|
||||
@@ -223,7 +215,6 @@ class NavButtonSettingsStore(context: Context) {
|
||||
private const val TAG = "NavButtons"
|
||||
const val PREFERENCES_NAME = "nav_button_settings"
|
||||
const val ACTION_SETTINGS_CHANGED = "se.ajpanton.navbuttons.SETTINGS_CHANGED"
|
||||
const val EXTRA_SIDE_ARROWS_ENABLED = "side_arrows_enabled"
|
||||
const val KEY_PRESS_ACTION = "press_action"
|
||||
const val KEY_LONG_PRESS_ACTION = "long_press_action"
|
||||
const val KEY_LONG_PRESS_DURATION_MS = "long_press_duration_ms"
|
||||
@@ -343,7 +334,13 @@ class NavButtonSettingsStore(context: Context) {
|
||||
|
||||
for (key in durationKeys) {
|
||||
if (source.contains(key)) {
|
||||
editor.putInt(key, source.getInt(key, sanitizeDuration(ViewConfiguration.getLongPressTimeout())))
|
||||
editor.putInt(
|
||||
key,
|
||||
source.getInt(
|
||||
key,
|
||||
ViewConfiguration.getLongPressTimeout().coerceLongPressDuration(),
|
||||
).coerceLongPressDuration(),
|
||||
)
|
||||
} else {
|
||||
editor.remove(key)
|
||||
}
|
||||
@@ -384,9 +381,5 @@ class NavButtonSettingsStore(context: Context) {
|
||||
|
||||
editor.apply()
|
||||
}
|
||||
|
||||
private fun sanitizeDuration(durationMs: Int): Int {
|
||||
return durationMs.coerceIn(MIN_DURATION_MS, MAX_DURATION_MS)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -387,7 +387,7 @@ class SettingsActivity : Activity() {
|
||||
speedPlusButton,
|
||||
NavButtonSettingsStore.SIDE_ARROW_REPEAT_SPEED_STEP,
|
||||
parse = ::parseRepeatSpeed,
|
||||
sanitize = settingsStore::sanitizeSideArrowRepeatSpeed,
|
||||
sanitize = ::sanitizeSideArrowRepeatSpeed,
|
||||
format = ::formatRepeatSpeed,
|
||||
persist = settingsStore::setSideArrowRepeatStepsPerSecond,
|
||||
)
|
||||
@@ -475,7 +475,7 @@ class SettingsActivity : Activity() {
|
||||
plusButton,
|
||||
NavButtonSettingsStore.DURATION_STEP_MS.toFloat(),
|
||||
parse = { it.toIntOrNull()?.toFloat() },
|
||||
sanitize = { settingsStore.sanitizeDuration(it.toInt()).toFloat() },
|
||||
sanitize = { it.toInt().coerceLongPressDuration().toFloat() },
|
||||
format = { it.toInt().toString() },
|
||||
persist = { persist(it.toInt()) },
|
||||
)
|
||||
|
||||
@@ -211,14 +211,4 @@ internal class SettingsRepository(
|
||||
)
|
||||
}
|
||||
|
||||
private fun sanitizeSideArrowRepeatSpeed(stepsPerSecond: Float): Float {
|
||||
return if (stepsPerSecond.isFinite()) {
|
||||
stepsPerSecond.coerceIn(
|
||||
NavButtonSettingsStore.MIN_SIDE_ARROW_REPEAT_STEPS_PER_SECOND,
|
||||
NavButtonSettingsStore.MAX_SIDE_ARROW_REPEAT_STEPS_PER_SECOND,
|
||||
)
|
||||
} else {
|
||||
NavButtonSettingsStore.DEFAULT_SIDE_ARROW_REPEAT_STEPS_PER_SECOND
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user