Skip to content

Commit

Permalink
refactor(Settings): add delegates
Browse files Browse the repository at this point in the history
they guarantee that the getter and the setter use the same key. Also, they reduce the boilerplate of writing a simple setter

more delegates can be added later
  • Loading branch information
BrayanDSO committed Jan 14, 2025
1 parent bd507bb commit c222513
Showing 1 changed file with 50 additions and 9 deletions.
59 changes: 50 additions & 9 deletions AnkiDroid/src/main/java/com/ichi2/anki/settings/Settings.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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() }
Expand Down Expand Up @@ -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<Any?, Boolean> =
object : ReadWriteProperty<Any?, Boolean> {
override fun getValue(
thisRef: Any?,
property: KProperty<*>,
): Boolean = prefs.getBoolean(key, defaultValue)

override fun setValue(
thisRef: Any?,
property: KProperty<*>,
value: Boolean,
) {
prefs.edit { putBoolean(key, value) }
}
}

private fun stringSetting(
key: String,
defaultValue: String,
): ReadWriteProperty<Any?, String> =
object : ReadWriteProperty<Any?, String> {
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 **************************************** //

Expand All @@ -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 ********************************** //

Expand Down

0 comments on commit c222513

Please sign in to comment.