Skip to content

Commit 11333a8

Browse files
committed
refactor: add intPref util
1 parent 8f58505 commit 11333a8

File tree

1 file changed

+26
-0
lines changed
  • AnkiDroid/src/main/java/com/ichi2/anki/settings

1 file changed

+26
-0
lines changed

AnkiDroid/src/main/java/com/ichi2/anki/settings/Prefs.kt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,12 @@ object Prefs {
7575
defValue: Int,
7676
): Int = sharedPrefs.getInt(key(keyResId), defValue)
7777

78+
@VisibleForTesting
79+
fun putInt(
80+
@StringRes keyResId: Int,
81+
defValue: Int,
82+
) = sharedPrefs.edit { putInt(key(keyResId), defValue) }
83+
7884
@VisibleForTesting
7985
fun <E> getEnum(
8086
@StringRes keyResId: Int,
@@ -129,6 +135,26 @@ object Prefs {
129135
}
130136
}
131137

138+
@VisibleForTesting
139+
fun intPref(
140+
@StringRes keyResId: Int,
141+
defaultValue: Int,
142+
): ReadWriteProperty<Any, Int> =
143+
object : ReadWriteProperty<Any?, Int> {
144+
override fun getValue(
145+
thisRef: Any?,
146+
property: KProperty<*>,
147+
): Int = getInt(keyResId, defaultValue)
148+
149+
override fun setValue(
150+
thisRef: Any?,
151+
property: KProperty<*>,
152+
value: Int,
153+
) {
154+
putInt(keyResId, value)
155+
}
156+
}
157+
132158
// ****************************************************************************************** //
133159
// **************************************** Settings **************************************** //
134160
// ****************************************************************************************** //

0 commit comments

Comments
 (0)