From 1c8846dc573478abeca2bd1014a5c63e966a3e7d Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Sat, 8 Jun 2019 16:30:12 -0700 Subject: [PATCH] Make PreferenceModel an interface --- .../main/java/com/topjohnwu/magisk/KConfig.kt | 3 +- .../model/preference/BooleanProperty.kt | 2 +- .../magisk/model/preference/FloatProperty.kt | 2 +- .../magisk/model/preference/IntProperty.kt | 2 +- .../magisk/model/preference/LongProperty.kt | 2 +- .../model/preference/PreferenceModel.kt | 42 ++++++++++--------- .../magisk/model/preference/StringProperty.kt | 2 +- .../model/preference/StringSetProperty.kt | 2 +- 8 files changed, 29 insertions(+), 28 deletions(-) diff --git a/app/src/main/java/com/topjohnwu/magisk/KConfig.kt b/app/src/main/java/com/topjohnwu/magisk/KConfig.kt index 765e10c28..a481a935e 100644 --- a/app/src/main/java/com/topjohnwu/magisk/KConfig.kt +++ b/app/src/main/java/com/topjohnwu/magisk/KConfig.kt @@ -6,10 +6,9 @@ import com.topjohnwu.magisk.di.Protected import com.topjohnwu.magisk.model.preference.PreferenceModel import com.topjohnwu.magisk.utils.inject -object KConfig : PreferenceModel() { +object KConfig : PreferenceModel { override val context: Context by inject(Protected) - override val fileName: String = "${context.packageName}_preferences" private var internalUpdateChannel by preference(Config.Key.UPDATE_CHANNEL, STABLE.id.toString()) var useCustomTabs by preference("useCustomTabs", true) diff --git a/app/src/main/java/com/topjohnwu/magisk/model/preference/BooleanProperty.kt b/app/src/main/java/com/topjohnwu/magisk/model/preference/BooleanProperty.kt index 7b90fcb94..3acc80dd3 100644 --- a/app/src/main/java/com/topjohnwu/magisk/model/preference/BooleanProperty.kt +++ b/app/src/main/java/com/topjohnwu/magisk/model/preference/BooleanProperty.kt @@ -16,7 +16,7 @@ class BooleanProperty( property: KProperty<*> ): Boolean { val prefName = name.trimEmptyToNull() ?: property.name - return runCatching { thisRef.prefs.get(prefName, default) }.getOrNull() ?: default + return thisRef.prefs.get(prefName, default) } override operator fun setValue( diff --git a/app/src/main/java/com/topjohnwu/magisk/model/preference/FloatProperty.kt b/app/src/main/java/com/topjohnwu/magisk/model/preference/FloatProperty.kt index 2ed281f0f..46ab8cc29 100644 --- a/app/src/main/java/com/topjohnwu/magisk/model/preference/FloatProperty.kt +++ b/app/src/main/java/com/topjohnwu/magisk/model/preference/FloatProperty.kt @@ -16,7 +16,7 @@ class FloatProperty( property: KProperty<*> ): Float { val prefName = name.trimEmptyToNull() ?: property.name - return runCatching { thisRef.prefs.get(prefName, default) }.getOrNull() ?: default + return thisRef.prefs.get(prefName, default) } override operator fun setValue( diff --git a/app/src/main/java/com/topjohnwu/magisk/model/preference/IntProperty.kt b/app/src/main/java/com/topjohnwu/magisk/model/preference/IntProperty.kt index 795e26eeb..2189acea1 100644 --- a/app/src/main/java/com/topjohnwu/magisk/model/preference/IntProperty.kt +++ b/app/src/main/java/com/topjohnwu/magisk/model/preference/IntProperty.kt @@ -16,7 +16,7 @@ class IntProperty( property: KProperty<*> ): Int { val prefName = name.trimEmptyToNull() ?: property.name - return runCatching { thisRef.prefs.get(prefName, default) }.getOrNull() ?: default + return thisRef.prefs.get(prefName, default) } override operator fun setValue( diff --git a/app/src/main/java/com/topjohnwu/magisk/model/preference/LongProperty.kt b/app/src/main/java/com/topjohnwu/magisk/model/preference/LongProperty.kt index 6b6888b45..b97266182 100644 --- a/app/src/main/java/com/topjohnwu/magisk/model/preference/LongProperty.kt +++ b/app/src/main/java/com/topjohnwu/magisk/model/preference/LongProperty.kt @@ -16,7 +16,7 @@ class LongProperty( property: KProperty<*> ): Long { val prefName = name.trimEmptyToNull() ?: property.name - return runCatching { thisRef.prefs.get(prefName, default) }.getOrNull() ?: default + return thisRef.prefs.get(prefName, default) } override operator fun setValue( diff --git a/app/src/main/java/com/topjohnwu/magisk/model/preference/PreferenceModel.kt b/app/src/main/java/com/topjohnwu/magisk/model/preference/PreferenceModel.kt index f999f2259..d751baa98 100644 --- a/app/src/main/java/com/topjohnwu/magisk/model/preference/PreferenceModel.kt +++ b/app/src/main/java/com/topjohnwu/magisk/model/preference/PreferenceModel.kt @@ -1,51 +1,53 @@ package com.topjohnwu.magisk.model.preference import android.content.Context -import kotlin.properties.ReadWriteProperty +import android.content.SharedPreferences -abstract class PreferenceModel( - private val commitPrefs: Boolean = false -) { +interface PreferenceModel { - protected abstract val fileName: String - protected abstract val context: Context + val context: Context - internal val prefs get() = context.getSharedPreferences(fileName, Context.MODE_PRIVATE) + val fileName: String + get() = "${context.packageName}_preferences" + val commitPrefs: Boolean + get() = false + val prefs: SharedPreferences + get() = context.getSharedPreferences(fileName, Context.MODE_PRIVATE) - protected fun preference( + fun preference( name: String, default: Boolean, commit: Boolean = commitPrefs - ): ReadWriteProperty = BooleanProperty(name, default, commit) + ) = BooleanProperty(name, default, commit) - protected fun preference( + fun preference( name: String, default: Float, commit: Boolean = commitPrefs - ): ReadWriteProperty = FloatProperty(name, default, commit) + ) = FloatProperty(name, default, commit) - protected fun preference( + fun preference( name: String, default: Int, commit: Boolean = commitPrefs - ): ReadWriteProperty = IntProperty(name, default, commit) + ) = IntProperty(name, default, commit) - protected fun preference( + fun preference( name: String, default: Long, commit: Boolean = commitPrefs - ): ReadWriteProperty = LongProperty(name, default, commit) + ) = LongProperty(name, default, commit) - protected fun preference( + fun preference( name: String, default: String, commit: Boolean = commitPrefs - ): ReadWriteProperty = StringProperty(name, default, commit) + ) = StringProperty(name, default, commit) - protected fun preference( + fun preference( name: String, default: Set, commit: Boolean = commitPrefs - ): ReadWriteProperty> = StringSetProperty(name, default, commit) + ) = StringSetProperty(name, default, commit) -} \ No newline at end of file +} diff --git a/app/src/main/java/com/topjohnwu/magisk/model/preference/StringProperty.kt b/app/src/main/java/com/topjohnwu/magisk/model/preference/StringProperty.kt index df0bc4a9c..52e2d44ef 100644 --- a/app/src/main/java/com/topjohnwu/magisk/model/preference/StringProperty.kt +++ b/app/src/main/java/com/topjohnwu/magisk/model/preference/StringProperty.kt @@ -16,7 +16,7 @@ class StringProperty( property: KProperty<*> ): String { val prefName = name.trimEmptyToNull() ?: property.name - return runCatching { thisRef.prefs.get(prefName, default) }.getOrNull() ?: default + return thisRef.prefs.get(prefName, default) } override operator fun setValue( diff --git a/app/src/main/java/com/topjohnwu/magisk/model/preference/StringSetProperty.kt b/app/src/main/java/com/topjohnwu/magisk/model/preference/StringSetProperty.kt index ba9952bbe..d513940c7 100644 --- a/app/src/main/java/com/topjohnwu/magisk/model/preference/StringSetProperty.kt +++ b/app/src/main/java/com/topjohnwu/magisk/model/preference/StringSetProperty.kt @@ -16,7 +16,7 @@ class StringSetProperty( property: KProperty<*> ): Set { val prefName = name.trimEmptyToNull() ?: property.name - return runCatching { thisRef.prefs.get(prefName, default) }.getOrNull() ?: default + return thisRef.prefs.get(prefName, default) } override operator fun setValue(