Make PreferenceModel an interface
This commit is contained in:
parent
2f320c7239
commit
1c8846dc57
@ -6,10 +6,9 @@ import com.topjohnwu.magisk.di.Protected
|
|||||||
import com.topjohnwu.magisk.model.preference.PreferenceModel
|
import com.topjohnwu.magisk.model.preference.PreferenceModel
|
||||||
import com.topjohnwu.magisk.utils.inject
|
import com.topjohnwu.magisk.utils.inject
|
||||||
|
|
||||||
object KConfig : PreferenceModel() {
|
object KConfig : PreferenceModel {
|
||||||
|
|
||||||
override val context: Context by inject(Protected)
|
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())
|
private var internalUpdateChannel by preference(Config.Key.UPDATE_CHANNEL, STABLE.id.toString())
|
||||||
var useCustomTabs by preference("useCustomTabs", true)
|
var useCustomTabs by preference("useCustomTabs", true)
|
||||||
|
@ -16,7 +16,7 @@ class BooleanProperty(
|
|||||||
property: KProperty<*>
|
property: KProperty<*>
|
||||||
): Boolean {
|
): Boolean {
|
||||||
val prefName = name.trimEmptyToNull() ?: property.name
|
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(
|
override operator fun setValue(
|
||||||
|
@ -16,7 +16,7 @@ class FloatProperty(
|
|||||||
property: KProperty<*>
|
property: KProperty<*>
|
||||||
): Float {
|
): Float {
|
||||||
val prefName = name.trimEmptyToNull() ?: property.name
|
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(
|
override operator fun setValue(
|
||||||
|
@ -16,7 +16,7 @@ class IntProperty(
|
|||||||
property: KProperty<*>
|
property: KProperty<*>
|
||||||
): Int {
|
): Int {
|
||||||
val prefName = name.trimEmptyToNull() ?: property.name
|
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(
|
override operator fun setValue(
|
||||||
|
@ -16,7 +16,7 @@ class LongProperty(
|
|||||||
property: KProperty<*>
|
property: KProperty<*>
|
||||||
): Long {
|
): Long {
|
||||||
val prefName = name.trimEmptyToNull() ?: property.name
|
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(
|
override operator fun setValue(
|
||||||
|
@ -1,51 +1,53 @@
|
|||||||
package com.topjohnwu.magisk.model.preference
|
package com.topjohnwu.magisk.model.preference
|
||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import kotlin.properties.ReadWriteProperty
|
import android.content.SharedPreferences
|
||||||
|
|
||||||
abstract class PreferenceModel(
|
interface PreferenceModel {
|
||||||
private val commitPrefs: Boolean = false
|
|
||||||
) {
|
|
||||||
|
|
||||||
protected abstract val fileName: String
|
val context: Context
|
||||||
protected abstract 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,
|
name: String,
|
||||||
default: Boolean,
|
default: Boolean,
|
||||||
commit: Boolean = commitPrefs
|
commit: Boolean = commitPrefs
|
||||||
): ReadWriteProperty<PreferenceModel, Boolean> = BooleanProperty(name, default, commit)
|
) = BooleanProperty(name, default, commit)
|
||||||
|
|
||||||
protected fun preference(
|
fun preference(
|
||||||
name: String,
|
name: String,
|
||||||
default: Float,
|
default: Float,
|
||||||
commit: Boolean = commitPrefs
|
commit: Boolean = commitPrefs
|
||||||
): ReadWriteProperty<PreferenceModel, Float> = FloatProperty(name, default, commit)
|
) = FloatProperty(name, default, commit)
|
||||||
|
|
||||||
protected fun preference(
|
fun preference(
|
||||||
name: String,
|
name: String,
|
||||||
default: Int,
|
default: Int,
|
||||||
commit: Boolean = commitPrefs
|
commit: Boolean = commitPrefs
|
||||||
): ReadWriteProperty<PreferenceModel, Int> = IntProperty(name, default, commit)
|
) = IntProperty(name, default, commit)
|
||||||
|
|
||||||
protected fun preference(
|
fun preference(
|
||||||
name: String,
|
name: String,
|
||||||
default: Long,
|
default: Long,
|
||||||
commit: Boolean = commitPrefs
|
commit: Boolean = commitPrefs
|
||||||
): ReadWriteProperty<PreferenceModel, Long> = LongProperty(name, default, commit)
|
) = LongProperty(name, default, commit)
|
||||||
|
|
||||||
protected fun preference(
|
fun preference(
|
||||||
name: String,
|
name: String,
|
||||||
default: String,
|
default: String,
|
||||||
commit: Boolean = commitPrefs
|
commit: Boolean = commitPrefs
|
||||||
): ReadWriteProperty<PreferenceModel, String> = StringProperty(name, default, commit)
|
) = StringProperty(name, default, commit)
|
||||||
|
|
||||||
protected fun preference(
|
fun preference(
|
||||||
name: String,
|
name: String,
|
||||||
default: Set<String>,
|
default: Set<String>,
|
||||||
commit: Boolean = commitPrefs
|
commit: Boolean = commitPrefs
|
||||||
): ReadWriteProperty<PreferenceModel, Set<String>> = StringSetProperty(name, default, commit)
|
) = StringSetProperty(name, default, commit)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -16,7 +16,7 @@ class StringProperty(
|
|||||||
property: KProperty<*>
|
property: KProperty<*>
|
||||||
): String {
|
): String {
|
||||||
val prefName = name.trimEmptyToNull() ?: property.name
|
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(
|
override operator fun setValue(
|
||||||
|
@ -16,7 +16,7 @@ class StringSetProperty(
|
|||||||
property: KProperty<*>
|
property: KProperty<*>
|
||||||
): Set<String> {
|
): Set<String> {
|
||||||
val prefName = name.trimEmptyToNull() ?: property.name
|
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(
|
override operator fun setValue(
|
||||||
|
Loading…
Reference in New Issue
Block a user