diff --git a/AnkiDroid/src/main/java/com/ichi2/anki/settings/Settings.kt b/AnkiDroid/src/main/java/com/ichi2/anki/settings/Settings.kt index 0aaa29dd3854..1fa6973f3a04 100644 --- a/AnkiDroid/src/main/java/com/ichi2/anki/settings/Settings.kt +++ b/AnkiDroid/src/main/java/com/ichi2/anki/settings/Settings.kt @@ -22,6 +22,8 @@ import com.ichi2.anki.AnkiDroidApp import com.ichi2.anki.BuildConfig import com.ichi2.anki.settings.enums.FrameStyle import com.ichi2.anki.settings.enums.SettingEnum +import kotlin.properties.ReadWriteProperty +import kotlin.reflect.KProperty object Settings { private val prefs by lazy { AnkiDroidApp.sharedPrefs() } @@ -72,13 +74,54 @@ object Settings { } ?: defaultValue } - // ****************************************** Sync ****************************************** // + // **************************************** Delegates *************************************** // - val isAutoSyncEnabled: Boolean - get() = getBoolean(SettingKey.AUTO_SYNC, false) + private fun booleanSetting( + key: String, + defaultValue: Boolean, + ): ReadWriteProperty = + object : ReadWriteProperty { + override fun getValue( + thisRef: Any?, + property: KProperty<*>, + ): Boolean = getBoolean(key, defaultValue) + + override fun setValue( + thisRef: Any?, + property: KProperty<*>, + value: Boolean, + ) { + putBoolean(key, value) + } + } + + private fun stringSetting( + key: String, + defaultValue: String, + ): ReadWriteProperty = + object : ReadWriteProperty { + override fun getValue( + thisRef: Any?, + property: KProperty<*>, + ): String = getString(key, defaultValue) ?: defaultValue + + override fun setValue( + thisRef: Any?, + property: KProperty<*>, + value: String, + ) { + putString(key, defaultValue) + } + } + + // ****************************************************************************************** // + // **************************************** Settings **************************************** // + // ****************************************************************************************** // + + // ****************************************** Sync ****************************************** // - val username: String - get() = getString(SettingKey.USERNAME, "") ?: "" + val isAutoSyncEnabled by booleanSetting(SettingKey.AUTO_SYNC, false) + val username by stringSetting(SettingKey.USERNAME, "") // **************************************** Reviewer **************************************** // @@ -92,10 +135,8 @@ object Settings { // **************************************** Advanced **************************************** // - var ankidroidDirectory: String - @SuppressLint("SdCardPath") - get() = getString(SettingKey.ANKIDROID_DIRECTORY, "/sdcard/AnkiDroid") ?: "" - set(value) = putString(SettingKey.ANKIDROID_DIRECTORY, value) + @delegate:SuppressLint("SdCardPath") + var ankidroidDirectory by stringSetting(SettingKey.ANKIDROID_DIRECTORY, "/sdcard/AnkiDroid") // ************************************* Developer options ********************************** //