From a8640f52efdd9c876a5a090397eabc441e0b46a6 Mon Sep 17 00:00:00 2001 From: vvb2060 Date: Sat, 15 Jan 2022 19:52:13 +0800 Subject: [PATCH] Merge into one file --- .../magisk/data/preference/BooleanProperty.kt | 30 ---- .../magisk/data/preference/FloatProperty.kt | 30 ---- .../magisk/data/preference/IntProperty.kt | 30 ---- .../magisk/data/preference/LongProperty.kt | 30 ---- .../magisk/data/preference/Property.kt | 147 ++++++++++++++++++ .../magisk/data/preference/StringProperty.kt | 30 ---- .../data/preference/StringSetProperty.kt | 30 ---- 7 files changed, 147 insertions(+), 180 deletions(-) delete mode 100644 app/src/main/java/com/topjohnwu/magisk/data/preference/BooleanProperty.kt delete mode 100644 app/src/main/java/com/topjohnwu/magisk/data/preference/FloatProperty.kt delete mode 100644 app/src/main/java/com/topjohnwu/magisk/data/preference/IntProperty.kt delete mode 100644 app/src/main/java/com/topjohnwu/magisk/data/preference/LongProperty.kt delete mode 100644 app/src/main/java/com/topjohnwu/magisk/data/preference/StringProperty.kt delete mode 100644 app/src/main/java/com/topjohnwu/magisk/data/preference/StringSetProperty.kt diff --git a/app/src/main/java/com/topjohnwu/magisk/data/preference/BooleanProperty.kt b/app/src/main/java/com/topjohnwu/magisk/data/preference/BooleanProperty.kt deleted file mode 100644 index 3ac22fd02..000000000 --- a/app/src/main/java/com/topjohnwu/magisk/data/preference/BooleanProperty.kt +++ /dev/null @@ -1,30 +0,0 @@ -package com.topjohnwu.magisk.data.preference - -import androidx.core.content.edit -import com.topjohnwu.magisk.ktx.trimEmptyToNull -import kotlin.properties.ReadWriteProperty -import kotlin.reflect.KProperty - -class BooleanProperty( - private val name: String, - private val default: Boolean, - private val commit: Boolean -) : Property(), ReadWriteProperty { - - override operator fun getValue( - thisRef: PreferenceModel, - property: KProperty<*> - ): Boolean { - val prefName = name.trimEmptyToNull() ?: property.name - return thisRef.prefs.get(prefName, default) - } - - override operator fun setValue( - thisRef: PreferenceModel, - property: KProperty<*>, - value: Boolean - ) { - val prefName = name.trimEmptyToNull() ?: property.name - thisRef.prefs.edit(commit) { put(prefName, value) } - } -} diff --git a/app/src/main/java/com/topjohnwu/magisk/data/preference/FloatProperty.kt b/app/src/main/java/com/topjohnwu/magisk/data/preference/FloatProperty.kt deleted file mode 100644 index aeef0de7c..000000000 --- a/app/src/main/java/com/topjohnwu/magisk/data/preference/FloatProperty.kt +++ /dev/null @@ -1,30 +0,0 @@ -package com.topjohnwu.magisk.data.preference - -import androidx.core.content.edit -import com.topjohnwu.magisk.ktx.trimEmptyToNull -import kotlin.properties.ReadWriteProperty -import kotlin.reflect.KProperty - -class FloatProperty( - private val name: String, - private val default: Float, - private val commit: Boolean -) : Property(), ReadWriteProperty { - - override operator fun getValue( - thisRef: PreferenceModel, - property: KProperty<*> - ): Float { - val prefName = name.trimEmptyToNull() ?: property.name - return thisRef.prefs.get(prefName, default) - } - - override operator fun setValue( - thisRef: PreferenceModel, - property: KProperty<*>, - value: Float - ) { - val prefName = name.trimEmptyToNull() ?: property.name - thisRef.prefs.edit(commit) { put(prefName, value) } - } -} diff --git a/app/src/main/java/com/topjohnwu/magisk/data/preference/IntProperty.kt b/app/src/main/java/com/topjohnwu/magisk/data/preference/IntProperty.kt deleted file mode 100644 index a22dade7b..000000000 --- a/app/src/main/java/com/topjohnwu/magisk/data/preference/IntProperty.kt +++ /dev/null @@ -1,30 +0,0 @@ -package com.topjohnwu.magisk.data.preference - -import androidx.core.content.edit -import com.topjohnwu.magisk.ktx.trimEmptyToNull -import kotlin.properties.ReadWriteProperty -import kotlin.reflect.KProperty - -class IntProperty( - private val name: String, - private val default: Int, - private val commit: Boolean -) : Property(), ReadWriteProperty { - - override operator fun getValue( - thisRef: PreferenceModel, - property: KProperty<*> - ): Int { - val prefName = name.trimEmptyToNull() ?: property.name - return thisRef.prefs.get(prefName, default) - } - - override operator fun setValue( - thisRef: PreferenceModel, - property: KProperty<*>, - value: Int - ) { - val prefName = name.trimEmptyToNull() ?: property.name - thisRef.prefs.edit(commit) { put(prefName, value) } - } -} diff --git a/app/src/main/java/com/topjohnwu/magisk/data/preference/LongProperty.kt b/app/src/main/java/com/topjohnwu/magisk/data/preference/LongProperty.kt deleted file mode 100644 index f01239463..000000000 --- a/app/src/main/java/com/topjohnwu/magisk/data/preference/LongProperty.kt +++ /dev/null @@ -1,30 +0,0 @@ -package com.topjohnwu.magisk.data.preference - -import androidx.core.content.edit -import com.topjohnwu.magisk.ktx.trimEmptyToNull -import kotlin.properties.ReadWriteProperty -import kotlin.reflect.KProperty - -class LongProperty( - private val name: String, - private val default: Long, - private val commit: Boolean -) : Property(), ReadWriteProperty { - - override operator fun getValue( - thisRef: PreferenceModel, - property: KProperty<*> - ): Long { - val prefName = name.trimEmptyToNull() ?: property.name - return thisRef.prefs.get(prefName, default) - } - - override operator fun setValue( - thisRef: PreferenceModel, - property: KProperty<*>, - value: Long - ) { - val prefName = name.trimEmptyToNull() ?: property.name - thisRef.prefs.edit(commit) { put(prefName, value) } - } -} diff --git a/app/src/main/java/com/topjohnwu/magisk/data/preference/Property.kt b/app/src/main/java/com/topjohnwu/magisk/data/preference/Property.kt index e1948df26..df8075139 100644 --- a/app/src/main/java/com/topjohnwu/magisk/data/preference/Property.kt +++ b/app/src/main/java/com/topjohnwu/magisk/data/preference/Property.kt @@ -1,6 +1,9 @@ package com.topjohnwu.magisk.data.preference import android.content.SharedPreferences +import androidx.core.content.edit +import kotlin.properties.ReadWriteProperty +import kotlin.reflect.KProperty abstract class Property { @@ -19,3 +22,147 @@ abstract class Property { fun SharedPreferences.get(name: String, value: Set) = getStringSet(name, value) ?: value } + +class BooleanProperty( + private val name: String, + private val default: Boolean, + private val commit: Boolean +) : Property(), ReadWriteProperty { + + override operator fun getValue( + thisRef: PreferenceModel, + property: KProperty<*> + ): Boolean { + val prefName = name.ifBlank { property.name } + return thisRef.prefs.get(prefName, default) + } + + override operator fun setValue( + thisRef: PreferenceModel, + property: KProperty<*>, + value: Boolean + ) { + val prefName = name.ifBlank { property.name } + thisRef.prefs.edit(commit) { put(prefName, value) } + } +} + +class FloatProperty( + private val name: String, + private val default: Float, + private val commit: Boolean +) : Property(), ReadWriteProperty { + + override operator fun getValue( + thisRef: PreferenceModel, + property: KProperty<*> + ): Float { + val prefName = name.ifBlank { property.name } + return thisRef.prefs.get(prefName, default) + } + + override operator fun setValue( + thisRef: PreferenceModel, + property: KProperty<*>, + value: Float + ) { + val prefName = name.ifBlank { property.name } + thisRef.prefs.edit(commit) { put(prefName, value) } + } +} + +class IntProperty( + private val name: String, + private val default: Int, + private val commit: Boolean +) : Property(), ReadWriteProperty { + + override operator fun getValue( + thisRef: PreferenceModel, + property: KProperty<*> + ): Int { + val prefName = name.ifBlank { property.name } + return thisRef.prefs.get(prefName, default) + } + + override operator fun setValue( + thisRef: PreferenceModel, + property: KProperty<*>, + value: Int + ) { + val prefName = name.ifBlank { property.name } + thisRef.prefs.edit(commit) { put(prefName, value) } + } +} + +class LongProperty( + private val name: String, + private val default: Long, + private val commit: Boolean +) : Property(), ReadWriteProperty { + + override operator fun getValue( + thisRef: PreferenceModel, + property: KProperty<*> + ): Long { + val prefName = name.ifBlank { property.name } + return thisRef.prefs.get(prefName, default) + } + + override operator fun setValue( + thisRef: PreferenceModel, + property: KProperty<*>, + value: Long + ) { + val prefName = name.ifBlank { property.name } + thisRef.prefs.edit(commit) { put(prefName, value) } + } +} + +class StringProperty( + private val name: String, + private val default: String, + private val commit: Boolean +) : Property(), ReadWriteProperty { + + override operator fun getValue( + thisRef: PreferenceModel, + property: KProperty<*> + ): String { + val prefName = name.ifBlank { property.name } + return thisRef.prefs.get(prefName, default) + } + + override operator fun setValue( + thisRef: PreferenceModel, + property: KProperty<*>, + value: String + ) { + val prefName = name.ifBlank { property.name } + thisRef.prefs.edit(commit) { put(prefName, value) } + } +} + +class StringSetProperty( + private val name: String, + private val default: Set, + private val commit: Boolean +) : Property(), ReadWriteProperty> { + + override operator fun getValue( + thisRef: PreferenceModel, + property: KProperty<*> + ): Set { + val prefName = name.ifBlank { property.name } + return thisRef.prefs.get(prefName, default) + } + + override operator fun setValue( + thisRef: PreferenceModel, + property: KProperty<*>, + value: Set + ) { + val prefName = name.ifBlank { property.name } + thisRef.prefs.edit(commit) { put(prefName, value) } + } +} diff --git a/app/src/main/java/com/topjohnwu/magisk/data/preference/StringProperty.kt b/app/src/main/java/com/topjohnwu/magisk/data/preference/StringProperty.kt deleted file mode 100644 index 02d589b98..000000000 --- a/app/src/main/java/com/topjohnwu/magisk/data/preference/StringProperty.kt +++ /dev/null @@ -1,30 +0,0 @@ -package com.topjohnwu.magisk.data.preference - -import androidx.core.content.edit -import com.topjohnwu.magisk.ktx.trimEmptyToNull -import kotlin.properties.ReadWriteProperty -import kotlin.reflect.KProperty - -class StringProperty( - private val name: String, - private val default: String, - private val commit: Boolean -) : Property(), ReadWriteProperty { - - override operator fun getValue( - thisRef: PreferenceModel, - property: KProperty<*> - ): String { - val prefName = name.trimEmptyToNull() ?: property.name - return thisRef.prefs.get(prefName, default) - } - - override operator fun setValue( - thisRef: PreferenceModel, - property: KProperty<*>, - value: String - ) { - val prefName = name.trimEmptyToNull() ?: property.name - thisRef.prefs.edit(commit) { put(prefName, value) } - } -} diff --git a/app/src/main/java/com/topjohnwu/magisk/data/preference/StringSetProperty.kt b/app/src/main/java/com/topjohnwu/magisk/data/preference/StringSetProperty.kt deleted file mode 100644 index 22fc6a8da..000000000 --- a/app/src/main/java/com/topjohnwu/magisk/data/preference/StringSetProperty.kt +++ /dev/null @@ -1,30 +0,0 @@ -package com.topjohnwu.magisk.data.preference - -import androidx.core.content.edit -import com.topjohnwu.magisk.ktx.trimEmptyToNull -import kotlin.properties.ReadWriteProperty -import kotlin.reflect.KProperty - -class StringSetProperty( - private val name: String, - private val default: Set, - private val commit: Boolean -) : Property(), ReadWriteProperty> { - - override operator fun getValue( - thisRef: PreferenceModel, - property: KProperty<*> - ): Set { - val prefName = name.trimEmptyToNull() ?: property.name - return thisRef.prefs.get(prefName, default) - } - - override operator fun setValue( - thisRef: PreferenceModel, - property: KProperty<*>, - value: Set - ) { - val prefName = name.trimEmptyToNull() ?: property.name - thisRef.prefs.edit(commit) { put(prefName, value) } - } -}