Make PreferenceModel an interface

This commit is contained in:
topjohnwu 2019-06-08 16:30:12 -07:00
parent 2f320c7239
commit 1c8846dc57
8 changed files with 29 additions and 28 deletions

View File

@ -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)

View File

@ -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(

View File

@ -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(

View File

@ -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(

View File

@ -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(

View File

@ -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<PreferenceModel, Boolean> = BooleanProperty(name, default, commit)
) = BooleanProperty(name, default, commit)
protected fun preference(
fun preference(
name: String,
default: Float,
commit: Boolean = commitPrefs
): ReadWriteProperty<PreferenceModel, Float> = FloatProperty(name, default, commit)
) = FloatProperty(name, default, commit)
protected fun preference(
fun preference(
name: String,
default: Int,
commit: Boolean = commitPrefs
): ReadWriteProperty<PreferenceModel, Int> = IntProperty(name, default, commit)
) = IntProperty(name, default, commit)
protected fun preference(
fun preference(
name: String,
default: Long,
commit: Boolean = commitPrefs
): ReadWriteProperty<PreferenceModel, Long> = LongProperty(name, default, commit)
) = LongProperty(name, default, commit)
protected fun preference(
fun preference(
name: String,
default: String,
commit: Boolean = commitPrefs
): ReadWriteProperty<PreferenceModel, String> = StringProperty(name, default, commit)
) = StringProperty(name, default, commit)
protected fun preference(
fun preference(
name: String,
default: Set<String>,
commit: Boolean = commitPrefs
): ReadWriteProperty<PreferenceModel, Set<String>> = StringSetProperty(name, default, commit)
) = StringSetProperty(name, default, commit)
}
}

View File

@ -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(

View File

@ -16,7 +16,7 @@ class StringSetProperty(
property: KProperty<*>
): Set<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(