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