From d1dfda405fa2f6ab34111d6135362d0bfa832146 Mon Sep 17 00:00:00 2001 From: Viktor De Pasquale Date: Mon, 3 Jun 2019 16:35:36 +0200 Subject: [PATCH] Removed Kotpref and replaced it with PreferenceModel --- app/build.gradle | 1 - app/src/main/java/com/topjohnwu/magisk/App.kt | 7 --- .../main/java/com/topjohnwu/magisk/KConfig.kt | 23 +++++---- .../topjohnwu/magisk/di/ApplicationModule.kt | 8 --- .../model/preference/BooleanProperty.kt | 30 +++++++++++ .../magisk/model/preference/FloatProperty.kt | 30 +++++++++++ .../magisk/model/preference/IntProperty.kt | 30 +++++++++++ .../magisk/model/preference/LongProperty.kt | 30 +++++++++++ .../model/preference/PreferenceModel.kt | 51 +++++++++++++++++++ .../magisk/model/preference/Property.kt | 21 ++++++++ .../magisk/model/preference/StringProperty.kt | 30 +++++++++++ .../model/preference/StringSetProperty.kt | 30 +++++++++++ .../com/topjohnwu/magisk/utils/XString.kt | 4 +- 13 files changed, 268 insertions(+), 27 deletions(-) create mode 100644 app/src/main/java/com/topjohnwu/magisk/model/preference/BooleanProperty.kt create mode 100644 app/src/main/java/com/topjohnwu/magisk/model/preference/FloatProperty.kt create mode 100644 app/src/main/java/com/topjohnwu/magisk/model/preference/IntProperty.kt create mode 100644 app/src/main/java/com/topjohnwu/magisk/model/preference/LongProperty.kt create mode 100644 app/src/main/java/com/topjohnwu/magisk/model/preference/PreferenceModel.kt create mode 100644 app/src/main/java/com/topjohnwu/magisk/model/preference/Property.kt create mode 100644 app/src/main/java/com/topjohnwu/magisk/model/preference/StringProperty.kt create mode 100644 app/src/main/java/com/topjohnwu/magisk/model/preference/StringSetProperty.kt diff --git a/app/build.gradle b/app/build.gradle index 9d764198e..ee97d1d6e 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -59,7 +59,6 @@ dependencies { implementation 'com.jakewharton.timber:timber:4.7.1' implementation 'com.github.skoumalcz:teanity:0.3.3' implementation 'com.ncapdevi:frag-nav:3.2.0' - implementation 'com.chibatching.kotpref:kotpref:2.8.0' def vMarkwon = '3.0.1' implementation "ru.noties.markwon:core:${vMarkwon}" diff --git a/app/src/main/java/com/topjohnwu/magisk/App.kt b/app/src/main/java/com/topjohnwu/magisk/App.kt index c7edc7503..a82606e3d 100644 --- a/app/src/main/java/com/topjohnwu/magisk/App.kt +++ b/app/src/main/java/com/topjohnwu/magisk/App.kt @@ -10,7 +10,6 @@ import android.os.Build import android.os.Bundle import androidx.appcompat.app.AppCompatDelegate import androidx.multidex.MultiDex -import com.chibatching.kotpref.Kotpref import com.topjohnwu.magisk.di.koinModules import com.topjohnwu.magisk.utils.LocaleManager import com.topjohnwu.magisk.utils.RootUtils @@ -29,12 +28,6 @@ open class App : Application(), Application.ActivityLifecycleCallbacks { @Volatile private var foreground: Activity? = null - override fun onCreate() { - super.onCreate() - - Kotpref.init(this) - } - override fun attachBaseContext(base: Context) { super.attachBaseContext(base) if (BuildConfig.DEBUG) diff --git a/app/src/main/java/com/topjohnwu/magisk/KConfig.kt b/app/src/main/java/com/topjohnwu/magisk/KConfig.kt index b0103b51e..765e10c28 100644 --- a/app/src/main/java/com/topjohnwu/magisk/KConfig.kt +++ b/app/src/main/java/com/topjohnwu/magisk/KConfig.kt @@ -1,23 +1,26 @@ package com.topjohnwu.magisk -import com.chibatching.kotpref.ContextProvider -import com.chibatching.kotpref.KotprefModel +import android.content.Context import com.topjohnwu.magisk.KConfig.UpdateChannel.STABLE -import com.topjohnwu.magisk.utils.get +import com.topjohnwu.magisk.di.Protected +import com.topjohnwu.magisk.model.preference.PreferenceModel +import com.topjohnwu.magisk.utils.inject -object KConfig : KotprefModel(get()) { - override val kotprefName: String = "${context.packageName}_preferences" +object KConfig : PreferenceModel() { - private var internalUpdateChannel by intPref(STABLE.id, "updateChannel") - var useCustomTabs by booleanPref(true, "useCustomTabs") + 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) @JvmStatic - var customUpdateChannel by stringPref("", "custom_channel") + var customUpdateChannel by preference(Config.Key.CUSTOM_CHANNEL, "") @JvmStatic var updateChannel: UpdateChannel - get() = UpdateChannel.byId(internalUpdateChannel) + get() = UpdateChannel.byId(internalUpdateChannel.toIntOrNull() ?: STABLE.id) set(value) { - internalUpdateChannel = value.id + internalUpdateChannel = value.id.toString() } internal const val DEFAULT_CHANNEL = "topjohnwu/magisk_files" diff --git a/app/src/main/java/com/topjohnwu/magisk/di/ApplicationModule.kt b/app/src/main/java/com/topjohnwu/magisk/di/ApplicationModule.kt index d66f7a337..ebdfd1025 100644 --- a/app/src/main/java/com/topjohnwu/magisk/di/ApplicationModule.kt +++ b/app/src/main/java/com/topjohnwu/magisk/di/ApplicationModule.kt @@ -2,7 +2,6 @@ package com.topjohnwu.magisk.di import android.content.Context import androidx.preference.PreferenceManager -import com.chibatching.kotpref.ContextProvider import com.skoumal.teanity.rxbus.RxBus import com.topjohnwu.magisk.App import org.koin.dsl.module @@ -16,11 +15,4 @@ val applicationModule = module { factory(Protected) { get().protectedContext } single(SUTimeout) { get(Protected).getSharedPreferences("su_timeout", 0) } single { PreferenceManager.getDefaultSharedPreferences(get(Protected)) } - single { createContextProvider(get(Protected)) as ContextProvider } -} - -private fun createContextProvider(context: Context) = object : ContextProvider { - override fun getApplicationContext(): Context { - return context - } } \ No newline at end of file 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 new file mode 100644 index 000000000..7b90fcb94 --- /dev/null +++ b/app/src/main/java/com/topjohnwu/magisk/model/preference/BooleanProperty.kt @@ -0,0 +1,30 @@ +package com.topjohnwu.magisk.model.preference + +import androidx.core.content.edit +import com.topjohnwu.magisk.utils.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 runCatching { thisRef.prefs.get(prefName, default) }.getOrNull() ?: default + } + + override operator fun setValue( + thisRef: PreferenceModel, + property: KProperty<*>, + value: Boolean + ) { + val prefName = name.trimEmptyToNull() ?: property.name + thisRef.prefs.edit(commit) { put(prefName, value) } + } +} \ No newline at end of file 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 new file mode 100644 index 000000000..2ed281f0f --- /dev/null +++ b/app/src/main/java/com/topjohnwu/magisk/model/preference/FloatProperty.kt @@ -0,0 +1,30 @@ +package com.topjohnwu.magisk.model.preference + +import androidx.core.content.edit +import com.topjohnwu.magisk.utils.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 runCatching { thisRef.prefs.get(prefName, default) }.getOrNull() ?: default + } + + override operator fun setValue( + thisRef: PreferenceModel, + property: KProperty<*>, + value: Float + ) { + val prefName = name.trimEmptyToNull() ?: property.name + thisRef.prefs.edit(commit) { put(prefName, value) } + } +} \ No newline at end of file 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 new file mode 100644 index 000000000..795e26eeb --- /dev/null +++ b/app/src/main/java/com/topjohnwu/magisk/model/preference/IntProperty.kt @@ -0,0 +1,30 @@ +package com.topjohnwu.magisk.model.preference + +import androidx.core.content.edit +import com.topjohnwu.magisk.utils.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 runCatching { thisRef.prefs.get(prefName, default) }.getOrNull() ?: default + } + + override operator fun setValue( + thisRef: PreferenceModel, + property: KProperty<*>, + value: Int + ) { + val prefName = name.trimEmptyToNull() ?: property.name + thisRef.prefs.edit(commit) { put(prefName, value) } + } +} \ No newline at end of file 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 new file mode 100644 index 000000000..6b6888b45 --- /dev/null +++ b/app/src/main/java/com/topjohnwu/magisk/model/preference/LongProperty.kt @@ -0,0 +1,30 @@ +package com.topjohnwu.magisk.model.preference + +import androidx.core.content.edit +import com.topjohnwu.magisk.utils.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 runCatching { thisRef.prefs.get(prefName, default) }.getOrNull() ?: default + } + + override operator fun setValue( + thisRef: PreferenceModel, + property: KProperty<*>, + value: Long + ) { + val prefName = name.trimEmptyToNull() ?: property.name + thisRef.prefs.edit(commit) { put(prefName, value) } + } +} \ No newline at end of file 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 new file mode 100644 index 000000000..f999f2259 --- /dev/null +++ b/app/src/main/java/com/topjohnwu/magisk/model/preference/PreferenceModel.kt @@ -0,0 +1,51 @@ +package com.topjohnwu.magisk.model.preference + +import android.content.Context +import kotlin.properties.ReadWriteProperty + +abstract class PreferenceModel( + private val commitPrefs: Boolean = false +) { + + protected abstract val fileName: String + protected abstract val context: Context + + internal val prefs get() = context.getSharedPreferences(fileName, Context.MODE_PRIVATE) + + protected fun preference( + name: String, + default: Boolean, + commit: Boolean = commitPrefs + ): ReadWriteProperty = BooleanProperty(name, default, commit) + + protected fun preference( + name: String, + default: Float, + commit: Boolean = commitPrefs + ): ReadWriteProperty = FloatProperty(name, default, commit) + + protected fun preference( + name: String, + default: Int, + commit: Boolean = commitPrefs + ): ReadWriteProperty = IntProperty(name, default, commit) + + protected fun preference( + name: String, + default: Long, + commit: Boolean = commitPrefs + ): ReadWriteProperty = LongProperty(name, default, commit) + + protected fun preference( + name: String, + default: String, + commit: Boolean = commitPrefs + ): ReadWriteProperty = StringProperty(name, default, commit) + + protected fun preference( + name: String, + default: Set, + commit: Boolean = commitPrefs + ): ReadWriteProperty> = StringSetProperty(name, default, commit) + +} \ No newline at end of file diff --git a/app/src/main/java/com/topjohnwu/magisk/model/preference/Property.kt b/app/src/main/java/com/topjohnwu/magisk/model/preference/Property.kt new file mode 100644 index 000000000..0b2e07628 --- /dev/null +++ b/app/src/main/java/com/topjohnwu/magisk/model/preference/Property.kt @@ -0,0 +1,21 @@ +package com.topjohnwu.magisk.model.preference + +import android.content.SharedPreferences + +abstract class Property { + + fun SharedPreferences.Editor.put(name: String, value: Boolean) = putBoolean(name, value) + fun SharedPreferences.Editor.put(name: String, value: Float) = putFloat(name, value) + fun SharedPreferences.Editor.put(name: String, value: Int) = putInt(name, value) + fun SharedPreferences.Editor.put(name: String, value: Long) = putLong(name, value) + fun SharedPreferences.Editor.put(name: String, value: String) = putString(name, value) + fun SharedPreferences.Editor.put(name: String, value: Set) = putStringSet(name, value) + + fun SharedPreferences.get(name: String, value: Boolean) = getBoolean(name, value) + fun SharedPreferences.get(name: String, value: Float) = getFloat(name, value) + fun SharedPreferences.get(name: String, value: Int) = getInt(name, value) + fun SharedPreferences.get(name: String, value: Long) = getLong(name, value) + fun SharedPreferences.get(name: String, value: String) = getString(name, value) ?: value + fun SharedPreferences.get(name: String, value: Set) = getStringSet(name, value) ?: value + +} \ 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 new file mode 100644 index 000000000..df0bc4a9c --- /dev/null +++ b/app/src/main/java/com/topjohnwu/magisk/model/preference/StringProperty.kt @@ -0,0 +1,30 @@ +package com.topjohnwu.magisk.model.preference + +import androidx.core.content.edit +import com.topjohnwu.magisk.utils.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 runCatching { thisRef.prefs.get(prefName, default) }.getOrNull() ?: default + } + + override operator fun setValue( + thisRef: PreferenceModel, + property: KProperty<*>, + value: String + ) { + val prefName = name.trimEmptyToNull() ?: property.name + thisRef.prefs.edit(commit) { put(prefName, value) } + } +} \ No newline at end of file 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 new file mode 100644 index 000000000..ba9952bbe --- /dev/null +++ b/app/src/main/java/com/topjohnwu/magisk/model/preference/StringSetProperty.kt @@ -0,0 +1,30 @@ +package com.topjohnwu.magisk.model.preference + +import androidx.core.content.edit +import com.topjohnwu.magisk.utils.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 runCatching { thisRef.prefs.get(prefName, default) }.getOrNull() ?: default + } + + override operator fun setValue( + thisRef: PreferenceModel, + property: KProperty<*>, + value: Set + ) { + val prefName = name.trimEmptyToNull() ?: property.name + thisRef.prefs.edit(commit) { put(prefName, value) } + } +} \ No newline at end of file diff --git a/app/src/main/java/com/topjohnwu/magisk/utils/XString.kt b/app/src/main/java/com/topjohnwu/magisk/utils/XString.kt index 3551d6c60..cd1a1bc64 100644 --- a/app/src/main/java/com/topjohnwu/magisk/utils/XString.kt +++ b/app/src/main/java/com/topjohnwu/magisk/utils/XString.kt @@ -18,4 +18,6 @@ fun StringBuilder.appendIf(condition: Boolean, builder: StringBuilder.() -> Unit fun Int.res(vararg args: Any): String { val resources: Resources by inject() return resources.getString(this, *args) -} \ No newline at end of file +} + +fun String.trimEmptyToNull(): String? = if (isBlank()) null else this \ No newline at end of file