Modify make YukiHookPrefsBridge not singleton to fix some duplicate assignment problems and change code note in documentation

This commit is contained in:
2023-04-17 04:40:35 +08:00
parent b41897fd58
commit f9cdcc508f
7 changed files with 28 additions and 42 deletions

View File

@@ -96,7 +96,7 @@ fun Context.prefs(name: String): YukiHookPrefsBridge
**Function Illustrate** **Function Illustrate**
> 获取 `YukiHookPrefsBridge` 对象。 > 创建 `YukiHookPrefsBridge` 对象。
可以同时在模块与 (Xposed) 宿主环境中使用。 可以同时在模块与 (Xposed) 宿主环境中使用。
@@ -116,7 +116,7 @@ fun Context.dataChannel(packageName: String): YukiHookDataChannel.NameSpace
**Function Illustrate** **Function Illustrate**
> 获取模块的数据通讯桥命名空间对象。 > 获取 `YukiHookDataChannel` 对象。
::: danger ::: danger

View File

@@ -252,7 +252,7 @@ val prefs: YukiHookPrefsBridge
**Function Illustrate** **Function Illustrate**
> 获得当前使用的存取数据对象缓存实例 > 创建 `YukiHookPrefsBridge` 对象
::: danger ::: danger
@@ -276,7 +276,7 @@ fun prefs(name: String): YukiHookPrefsBridge
**Function Illustrate** **Function Illustrate**
> 获得当前使用的存取数据对象缓存实例 > 创建 `YukiHookPrefsBridge` 对象
你可以通过 `name` 来自定义 Sp 存储的名称。 你可以通过 `name` 来自定义 Sp 存储的名称。
@@ -298,7 +298,7 @@ val dataChannel: YukiHookDataChannel.NameSpace
**Function Illustrate** **Function Illustrate**
> 获得当前使用的数据通讯桥命名空间对象。 > 获取 `YukiHookDataChannel` 对象。
::: danger ::: danger

View File

@@ -88,7 +88,7 @@ fun Context.prefs(name: String): YukiHookPrefsBridge
**功能描述** **功能描述**
> 获取 `YukiHookPrefsBridge` 对象。 > 创建 `YukiHookPrefsBridge` 对象。
可以同时在模块与 (Xposed) 宿主环境中使用。 可以同时在模块与 (Xposed) 宿主环境中使用。
@@ -108,7 +108,7 @@ fun Context.dataChannel(packageName: String): YukiHookDataChannel.NameSpace
**功能描述** **功能描述**
> 获取模块的数据通讯桥命名空间对象。 > 获取 `YukiHookDataChannel` 对象。
::: danger ::: danger

View File

@@ -244,7 +244,7 @@ val prefs: YukiHookPrefsBridge
**功能描述** **功能描述**
> 获得当前使用的存取数据对象缓存实例 > 创建 `YukiHookPrefsBridge` 对象
::: danger ::: danger
@@ -268,7 +268,7 @@ fun prefs(name: String): YukiHookPrefsBridge
**功能描述** **功能描述**
> 获得当前使用的存取数据对象缓存实例 > 创建 `YukiHookPrefsBridge` 对象
你可以通过 `name` 来自定义 Sp 存储的名称。 你可以通过 `name` 来自定义 Sp 存储的名称。
@@ -290,7 +290,7 @@ val dataChannel: YukiHookDataChannel.NameSpace
**功能描述** **功能描述**
> 获得当前使用的数据通讯桥命名空间对象。 > 获取 `YukiHookDataChannel` 对象。
::: danger ::: danger

View File

@@ -97,7 +97,7 @@ val Context.modulePrefs get() = prefs()
fun Context.modulePrefs(name: String) = prefs(name) fun Context.modulePrefs(name: String) = prefs(name)
/** /**
* 获取 [YukiHookPrefsBridge] 对象 * 创建 [YukiHookPrefsBridge] 对象
* *
* 可以同时在模块与 (Xposed) 宿主环境中使用 * 可以同时在模块与 (Xposed) 宿主环境中使用
* *
@@ -107,10 +107,10 @@ fun Context.modulePrefs(name: String) = prefs(name)
* @param name 自定义 Sp 存储名称 - 默认空 * @param name 自定义 Sp 存储名称 - 默认空
* @return [YukiHookPrefsBridge] * @return [YukiHookPrefsBridge]
*/ */
fun Context.prefs(name: String = "") = YukiHookPrefsBridge.instance(context = this).let { if (name.isNotBlank()) it.name(name) else it } fun Context.prefs(name: String = "") = YukiHookPrefsBridge.from(context = this).let { if (name.isNotBlank()) it.name(name) else it }
/** /**
* 获取模块的数据通讯桥命名空间对象 * 获取 [YukiHookDataChannel] 对象
* *
* - ❗只能在模块环境使用此功能 - 其它环境下使用将不起作用 * - ❗只能在模块环境使用此功能 - 其它环境下使用将不起作用
* @param packageName 目标 Hook APP (宿主) 包名 * @param packageName 目标 Hook APP (宿主) 包名

View File

@@ -178,15 +178,15 @@ open class PackageParam internal constructor(@PublishedApi internal var wrapper:
else YukiXposedModule.dynamicModuleAppResources) ?: error("Current Hook Framework not support moduleAppResources") else YukiXposedModule.dynamicModuleAppResources) ?: error("Current Hook Framework not support moduleAppResources")
/** /**
* 获得当前使用的存取数据对象缓存实例 * 创建 [YukiHookPrefsBridge] 对象
* *
* - ❗作为 Hook API 装载时无法使用 - 会抛出异常 * - ❗作为 Hook API 装载时无法使用 - 会抛出异常
* @return [YukiHookPrefsBridge] * @return [YukiHookPrefsBridge]
*/ */
val prefs get() = YukiHookPrefsBridge.instance() val prefs get() = YukiHookPrefsBridge.from()
/** /**
* 获得当前使用的存取数据对象缓存实例 * 创建 [YukiHookPrefsBridge] 对象
* *
* - ❗作为 Hook API 装载时无法使用 - 会抛出异常 * - ❗作为 Hook API 装载时无法使用 - 会抛出异常
* @param name 自定义 Sp 存储名称 * @param name 自定义 Sp 存储名称
@@ -195,7 +195,7 @@ open class PackageParam internal constructor(@PublishedApi internal var wrapper:
fun prefs(name: String) = prefs.name(name) fun prefs(name: String) = prefs.name(name)
/** /**
* 获得当前使用的数据通讯桥命名空间对象 * 获取 [YukiHookDataChannel] 对象
* *
* - ❗作为 Hook API 装载时无法使用 - 会抛出异常 * - ❗作为 Hook API 装载时无法使用 - 会抛出异常
* @return [YukiHookDataChannel.NameSpace] * @return [YukiHookDataChannel.NameSpace]

View File

@@ -64,9 +64,6 @@ class YukiHookPrefsBridge private constructor(private var context: Context? = nu
/** 当前是否为 (Xposed) 宿主环境 */ /** 当前是否为 (Xposed) 宿主环境 */
private val isXposedEnvironment = YukiXposedModule.isXposedEnvironment private val isXposedEnvironment = YukiXposedModule.isXposedEnvironment
/** 当前 [YukiHookPrefsBridge] 单例 */
private var instance: YukiHookPrefsBridge? = null
/** 当前缓存的 [XSharedPreferencesDelegate] 实例数组 */ /** 当前缓存的 [XSharedPreferencesDelegate] 实例数组 */
private val xPrefs = HashMap<String, XSharedPreferencesDelegate>() private val xPrefs = HashMap<String, XSharedPreferencesDelegate>()
@@ -74,12 +71,11 @@ class YukiHookPrefsBridge private constructor(private var context: Context? = nu
private val sPrefs = HashMap<String, SharedPreferences>() private val sPrefs = HashMap<String, SharedPreferences>()
/** /**
* 获取 [YukiHookPrefsBridge] 单例 * 创建 [YukiHookPrefsBridge] 对象
* @param context 实例 - (Xposed) 宿主环境为空 * @param context 实例 - (Xposed) 宿主环境为空
* @return [YukiHookPrefsBridge] * @return [YukiHookPrefsBridge]
*/ */
internal fun instance(context: Context? = null) = internal fun from(context: Context? = null) = YukiHookPrefsBridge(context)
instance?.apply { if (context != null) this.context = context } ?: YukiHookPrefsBridge(context).apply { instance = this }
/** /**
* 设置全局可读可写 * 设置全局可读可写
@@ -302,7 +298,7 @@ class YukiHookPrefsBridge private constructor(private var context: Context? = nu
else resetCacheSet { currentXsp.getString(key, value) ?: value } else resetCacheSet { currentXsp.getString(key, value) ?: value }
else currentSp.getString(key, value) ?: value).let { else currentSp.getString(key, value) ?: value).let {
makeWorldReadable() makeWorldReadable()
resetNativeSet { it } it
} }
/** /**
@@ -327,7 +323,7 @@ class YukiHookPrefsBridge private constructor(private var context: Context? = nu
else resetCacheSet { currentXsp.getStringSet(key, value) ?: value } else resetCacheSet { currentXsp.getStringSet(key, value) ?: value }
else currentSp.getStringSet(key, value) ?: value).let { else currentSp.getStringSet(key, value) ?: value).let {
makeWorldReadable() makeWorldReadable()
resetNativeSet { it } it
} }
/** /**
@@ -352,7 +348,7 @@ class YukiHookPrefsBridge private constructor(private var context: Context? = nu
else resetCacheSet { currentXsp.getBoolean(key, value) } else resetCacheSet { currentXsp.getBoolean(key, value) }
else currentSp.getBoolean(key, value)).let { else currentSp.getBoolean(key, value)).let {
makeWorldReadable() makeWorldReadable()
resetNativeSet { it } it
} }
/** /**
@@ -377,7 +373,7 @@ class YukiHookPrefsBridge private constructor(private var context: Context? = nu
else resetCacheSet { currentXsp.getInt(key, value) } else resetCacheSet { currentXsp.getInt(key, value) }
else currentSp.getInt(key, value)).let { else currentSp.getInt(key, value)).let {
makeWorldReadable() makeWorldReadable()
resetNativeSet { it } it
} }
/** /**
@@ -402,7 +398,7 @@ class YukiHookPrefsBridge private constructor(private var context: Context? = nu
else resetCacheSet { currentXsp.getFloat(key, value) } else resetCacheSet { currentXsp.getFloat(key, value) }
else currentSp.getFloat(key, value)).let { else currentSp.getFloat(key, value)).let {
makeWorldReadable() makeWorldReadable()
resetNativeSet { it } it
} }
/** /**
@@ -427,7 +423,7 @@ class YukiHookPrefsBridge private constructor(private var context: Context? = nu
else resetCacheSet { currentXsp.getLong(key, value) } else resetCacheSet { currentXsp.getLong(key, value) }
else currentSp.getLong(key, value)).let { else currentSp.getLong(key, value)).let {
makeWorldReadable() makeWorldReadable()
resetNativeSet { it } it
} }
/** /**
@@ -641,16 +637,6 @@ class YukiHookPrefsBridge private constructor(private var context: Context? = nu
return result() return result()
} }
/**
* 恢复 [isUsingNativeStorage] 为默认状态
* @param result 回调方法体的结果
* @return [T]
*/
private inline fun <T> resetNativeSet(result: () -> T): T {
isUsingNativeStorage = false
return result()
}
/** /**
* [YukiHookPrefsBridge] 的存储代理类 * [YukiHookPrefsBridge] 的存储代理类
* *
@@ -800,10 +786,10 @@ class YukiHookPrefsBridge private constructor(private var context: Context? = nu
* 提交更改 (同步) * 提交更改 (同步)
* @return [Boolean] 是否成功 * @return [Boolean] 是否成功
*/ */
fun commit() = resetNativeSet { editor?.commit()?.also { makeWorldReadable() } ?: false } fun commit() = editor?.commit()?.also { makeWorldReadable() } ?: false
/** 提交更改 (异步) */ /** 提交更改 (异步) */
fun apply() = resetNativeSet { editor?.apply().also { makeWorldReadable() } ?: Unit } fun apply() = editor?.apply().also { makeWorldReadable() } ?: Unit
/** /**
* 仅在模块环境执行 * 仅在模块环境执行