mirror of
https://github.com/topjohnwu/Magisk.git
synced 2025-02-26 07:37:23 +00:00
Removed Kotpref and replaced it with PreferenceModel
This commit is contained in:
parent
28efded624
commit
d1dfda405f
@ -59,7 +59,6 @@ dependencies {
|
|||||||
implementation 'com.jakewharton.timber:timber:4.7.1'
|
implementation 'com.jakewharton.timber:timber:4.7.1'
|
||||||
implementation 'com.github.skoumalcz:teanity:0.3.3'
|
implementation 'com.github.skoumalcz:teanity:0.3.3'
|
||||||
implementation 'com.ncapdevi:frag-nav:3.2.0'
|
implementation 'com.ncapdevi:frag-nav:3.2.0'
|
||||||
implementation 'com.chibatching.kotpref:kotpref:2.8.0'
|
|
||||||
|
|
||||||
def vMarkwon = '3.0.1'
|
def vMarkwon = '3.0.1'
|
||||||
implementation "ru.noties.markwon:core:${vMarkwon}"
|
implementation "ru.noties.markwon:core:${vMarkwon}"
|
||||||
|
@ -10,7 +10,6 @@ import android.os.Build
|
|||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import androidx.appcompat.app.AppCompatDelegate
|
import androidx.appcompat.app.AppCompatDelegate
|
||||||
import androidx.multidex.MultiDex
|
import androidx.multidex.MultiDex
|
||||||
import com.chibatching.kotpref.Kotpref
|
|
||||||
import com.topjohnwu.magisk.di.koinModules
|
import com.topjohnwu.magisk.di.koinModules
|
||||||
import com.topjohnwu.magisk.utils.LocaleManager
|
import com.topjohnwu.magisk.utils.LocaleManager
|
||||||
import com.topjohnwu.magisk.utils.RootUtils
|
import com.topjohnwu.magisk.utils.RootUtils
|
||||||
@ -29,12 +28,6 @@ open class App : Application(), Application.ActivityLifecycleCallbacks {
|
|||||||
@Volatile
|
@Volatile
|
||||||
private var foreground: Activity? = null
|
private var foreground: Activity? = null
|
||||||
|
|
||||||
override fun onCreate() {
|
|
||||||
super.onCreate()
|
|
||||||
|
|
||||||
Kotpref.init(this)
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun attachBaseContext(base: Context) {
|
override fun attachBaseContext(base: Context) {
|
||||||
super.attachBaseContext(base)
|
super.attachBaseContext(base)
|
||||||
if (BuildConfig.DEBUG)
|
if (BuildConfig.DEBUG)
|
||||||
|
@ -1,23 +1,26 @@
|
|||||||
package com.topjohnwu.magisk
|
package com.topjohnwu.magisk
|
||||||
|
|
||||||
import com.chibatching.kotpref.ContextProvider
|
import android.content.Context
|
||||||
import com.chibatching.kotpref.KotprefModel
|
|
||||||
import com.topjohnwu.magisk.KConfig.UpdateChannel.STABLE
|
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<ContextProvider>()) {
|
object KConfig : PreferenceModel() {
|
||||||
override val kotprefName: String = "${context.packageName}_preferences"
|
|
||||||
|
|
||||||
private var internalUpdateChannel by intPref(STABLE.id, "updateChannel")
|
override val context: Context by inject(Protected)
|
||||||
var useCustomTabs by booleanPref(true, "useCustomTabs")
|
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
|
@JvmStatic
|
||||||
var customUpdateChannel by stringPref("", "custom_channel")
|
var customUpdateChannel by preference(Config.Key.CUSTOM_CHANNEL, "")
|
||||||
|
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
var updateChannel: UpdateChannel
|
var updateChannel: UpdateChannel
|
||||||
get() = UpdateChannel.byId(internalUpdateChannel)
|
get() = UpdateChannel.byId(internalUpdateChannel.toIntOrNull() ?: STABLE.id)
|
||||||
set(value) {
|
set(value) {
|
||||||
internalUpdateChannel = value.id
|
internalUpdateChannel = value.id.toString()
|
||||||
}
|
}
|
||||||
|
|
||||||
internal const val DEFAULT_CHANNEL = "topjohnwu/magisk_files"
|
internal const val DEFAULT_CHANNEL = "topjohnwu/magisk_files"
|
||||||
|
@ -2,7 +2,6 @@ package com.topjohnwu.magisk.di
|
|||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import androidx.preference.PreferenceManager
|
import androidx.preference.PreferenceManager
|
||||||
import com.chibatching.kotpref.ContextProvider
|
|
||||||
import com.skoumal.teanity.rxbus.RxBus
|
import com.skoumal.teanity.rxbus.RxBus
|
||||||
import com.topjohnwu.magisk.App
|
import com.topjohnwu.magisk.App
|
||||||
import org.koin.dsl.module
|
import org.koin.dsl.module
|
||||||
@ -16,11 +15,4 @@ val applicationModule = module {
|
|||||||
factory(Protected) { get<App>().protectedContext }
|
factory(Protected) { get<App>().protectedContext }
|
||||||
single(SUTimeout) { get<Context>(Protected).getSharedPreferences("su_timeout", 0) }
|
single(SUTimeout) { get<Context>(Protected).getSharedPreferences("su_timeout", 0) }
|
||||||
single { PreferenceManager.getDefaultSharedPreferences(get<Context>(Protected)) }
|
single { PreferenceManager.getDefaultSharedPreferences(get<Context>(Protected)) }
|
||||||
single { createContextProvider(get(Protected)) as ContextProvider }
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun createContextProvider(context: Context) = object : ContextProvider {
|
|
||||||
override fun getApplicationContext(): Context {
|
|
||||||
return context
|
|
||||||
}
|
|
||||||
}
|
}
|
@ -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<PreferenceModel, Boolean> {
|
||||||
|
|
||||||
|
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) }
|
||||||
|
}
|
||||||
|
}
|
@ -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<PreferenceModel, Float> {
|
||||||
|
|
||||||
|
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) }
|
||||||
|
}
|
||||||
|
}
|
@ -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<PreferenceModel, Int> {
|
||||||
|
|
||||||
|
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) }
|
||||||
|
}
|
||||||
|
}
|
@ -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<PreferenceModel, Long> {
|
||||||
|
|
||||||
|
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) }
|
||||||
|
}
|
||||||
|
}
|
@ -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<PreferenceModel, Boolean> = BooleanProperty(name, default, commit)
|
||||||
|
|
||||||
|
protected fun preference(
|
||||||
|
name: String,
|
||||||
|
default: Float,
|
||||||
|
commit: Boolean = commitPrefs
|
||||||
|
): ReadWriteProperty<PreferenceModel, Float> = FloatProperty(name, default, commit)
|
||||||
|
|
||||||
|
protected fun preference(
|
||||||
|
name: String,
|
||||||
|
default: Int,
|
||||||
|
commit: Boolean = commitPrefs
|
||||||
|
): ReadWriteProperty<PreferenceModel, Int> = IntProperty(name, default, commit)
|
||||||
|
|
||||||
|
protected fun preference(
|
||||||
|
name: String,
|
||||||
|
default: Long,
|
||||||
|
commit: Boolean = commitPrefs
|
||||||
|
): ReadWriteProperty<PreferenceModel, Long> = LongProperty(name, default, commit)
|
||||||
|
|
||||||
|
protected fun preference(
|
||||||
|
name: String,
|
||||||
|
default: String,
|
||||||
|
commit: Boolean = commitPrefs
|
||||||
|
): ReadWriteProperty<PreferenceModel, String> = StringProperty(name, default, commit)
|
||||||
|
|
||||||
|
protected fun preference(
|
||||||
|
name: String,
|
||||||
|
default: Set<String>,
|
||||||
|
commit: Boolean = commitPrefs
|
||||||
|
): ReadWriteProperty<PreferenceModel, Set<String>> = StringSetProperty(name, default, commit)
|
||||||
|
|
||||||
|
}
|
@ -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<String>) = 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<String>) = getStringSet(name, value) ?: value
|
||||||
|
|
||||||
|
}
|
@ -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<PreferenceModel, String> {
|
||||||
|
|
||||||
|
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) }
|
||||||
|
}
|
||||||
|
}
|
@ -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<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 runCatching { thisRef.prefs.get(prefName, default) }.getOrNull() ?: 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) }
|
||||||
|
}
|
||||||
|
}
|
@ -19,3 +19,5 @@ fun Int.res(vararg args: Any): String {
|
|||||||
val resources: Resources by inject()
|
val resources: Resources by inject()
|
||||||
return resources.getString(this, *args)
|
return resources.getString(this, *args)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun String.trimEmptyToNull(): String? = if (isBlank()) null else this
|
Loading…
x
Reference in New Issue
Block a user