Merge into one file

This commit is contained in:
vvb2060 2022-01-15 19:52:13 +08:00 committed by John Wu
parent 0f4e44c38f
commit a8640f52ef
7 changed files with 147 additions and 180 deletions

View File

@ -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<PreferenceModel, Boolean> {
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) }
}
}

View File

@ -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<PreferenceModel, Float> {
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) }
}
}

View File

@ -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<PreferenceModel, Int> {
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) }
}
}

View File

@ -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<PreferenceModel, Long> {
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) }
}
}

View File

@ -1,6 +1,9 @@
package com.topjohnwu.magisk.data.preference package com.topjohnwu.magisk.data.preference
import android.content.SharedPreferences import android.content.SharedPreferences
import androidx.core.content.edit
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty
abstract class Property { abstract class Property {
@ -19,3 +22,147 @@ abstract class Property {
fun SharedPreferences.get(name: String, value: Set<String>) = getStringSet(name, value) ?: value fun SharedPreferences.get(name: String, value: Set<String>) = getStringSet(name, value) ?: value
} }
class BooleanProperty(
private val name: String,
private val default: Boolean,
private val commit: Boolean
) : Property(), ReadWriteProperty<PreferenceModel, Boolean> {
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<PreferenceModel, Float> {
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<PreferenceModel, Int> {
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<PreferenceModel, Long> {
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<PreferenceModel, String> {
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<String>,
private val commit: Boolean
) : Property(), ReadWriteProperty<PreferenceModel, Set<String>> {
override operator fun getValue(
thisRef: PreferenceModel,
property: KProperty<*>
): Set<String> {
val prefName = name.ifBlank { property.name }
return thisRef.prefs.get(prefName, default)
}
override operator fun setValue(
thisRef: PreferenceModel,
property: KProperty<*>,
value: Set<String>
) {
val prefName = name.ifBlank { property.name }
thisRef.prefs.edit(commit) { put(prefName, value) }
}
}

View File

@ -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<PreferenceModel, String> {
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) }
}
}

View File

@ -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<String>,
private val commit: Boolean
) : Property(), ReadWriteProperty<PreferenceModel, Set<String>> {
override operator fun getValue(
thisRef: PreferenceModel,
property: KProperty<*>
): Set<String> {
val prefName = name.trimEmptyToNull() ?: property.name
return thisRef.prefs.get(prefName, default)
}
override operator fun setValue(
thisRef: PreferenceModel,
property: KProperty<*>,
value: Set<String>
) {
val prefName = name.trimEmptyToNull() ?: property.name
thisRef.prefs.edit(commit) { put(prefName, value) }
}
}