diff --git a/docs-source/src/en/api/public/com/highcapable/yukihookapi/hook/xposed/prefs/YukiHookModulePrefs.md b/docs-source/src/en/api/public/com/highcapable/yukihookapi/hook/xposed/prefs/YukiHookModulePrefs.md index 7aff15c4..cb8493d8 100644 --- a/docs-source/src/en/api/public/com/highcapable/yukihookapi/hook/xposed/prefs/YukiHookModulePrefs.md +++ b/docs-source/src/en/api/public/com/highcapable/yukihookapi/hook/xposed/prefs/YukiHookModulePrefs.md @@ -274,185 +274,85 @@ fun all(): HashMap ::: -## remove - method - -```kotlin:no-line-numbers -fun remove(key: String) -``` +

remove - method

**Change Records** `v1.0` `first` -**Function Illustrate** +`v1.1.9` `deprecated` -> 移除全部包含 `key` 的存储数据。 +请转移到 `edit` 方法 -::: warning - -在 (Xposed) 宿主环境下只读,无法使用。 - -::: - -## remove - method - -```kotlin:no-line-numbers -inline fun remove(prefs: PrefsData) -``` - -**Change Records** - -`v1.0.67` `added` - -**Function Illustrate** - -> 移除 `PrefsData.key` 的存储数据。 - -::: warning - -在 (Xposed) 宿主环境下只读,无法使用。 - -::: - -## clear - method - -```kotlin:no-line-numbers -fun clear() -``` +

clear - method

**Change Records** `v1.0.77` `added` -**Function Illustrate** +`v1.1.9` `deprecated` -> 移除全部存储数据。 +请转移到 `edit` 方法 -::: warning - -在 (Xposed) 宿主环境下只读,无法使用。 - -::: - -## putString - method - -```kotlin:no-line-numbers -fun putString(key: String, value: String) -``` +

putString - method

**Change Records** `v1.0` `first` -**Function Illustrate** +`v1.1.9` `deprecated` -> 存储 `String` 键值。 +请转移到 `edit` 方法 -::: warning - -在 (Xposed) 宿主环境下只读,无法使用。 - -::: - -## putStringSet - method - -```kotlin:no-line-numbers -fun putStringSet(key: String, value: Set) -``` +

putStringSet - method

**Change Records** `v1.0.77` `added` -**Function Illustrate** +`v1.1.9` `deprecated` -> 存储 `Set` 键值。 +请转移到 `edit` 方法 -::: warning - -在 (Xposed) 宿主环境下只读,无法使用。 - -::: - -## putBoolean - method - -```kotlin:no-line-numbers -fun putBoolean(key: String, value: Boolean) -``` +

putBoolean - method

**Change Records** `v1.0` `first` -**Function Illustrate** +`v1.1.9` `deprecated` -> 存储 `Boolean` 键值。 +请转移到 `edit` 方法 -::: warning - -在 (Xposed) 宿主环境下只读,无法使用。 - -::: - -## putInt - method - -```kotlin:no-line-numbers -fun putInt(key: String, value: Int) -``` +

putInt - method

**Change Records** `v1.0` `first` -**Function Illustrate** +`v1.1.9` `deprecated` -> 存储 `Int` 键值。 +请转移到 `edit` 方法 -::: warning - -在 (Xposed) 宿主环境下只读,无法使用。 - -::: - -## putLong - method - -```kotlin:no-line-numbers -fun putLong(key: String, value: Long) -``` +

putLong - method

**Change Records** `v1.0` `first` -**Function Illustrate** +`v1.1.9` `deprecated` -> 存储 `Long` 键值。 +请转移到 `edit` 方法 -::: warning - -在 (Xposed) 宿主环境下只读,无法使用。 - -::: - -## putFloat - method - -```kotlin:no-line-numbers -fun putFloat(key: String, value: Float) -``` +

putFloat - method

**Change Records** `v1.0` `first` -**Function Illustrate** +`v1.1.9` `deprecated` -> 存储 `Float` 键值。 - -::: warning - -在 (Xposed) 宿主环境下只读,无法使用。 - -::: +请转移到 `edit` 方法 ## get - method @@ -468,19 +368,51 @@ inline fun get(prefs: PrefsData, value: T): T > 智能获取指定类型的键值。 -## put - method - -```kotlin:no-line-numbers -inline fun put(prefs: PrefsData, value: T) -``` +

put - method

**Change Records** `v1.0.67` `added` +`v1.1.9` `deprecated` + +请转移到 `edit` 方法 + +## edit - method + +```kotlin:no-line-numbers +fun edit(): Editor +``` + +**Change Records** + +`v1.1.9` `added` + **Function Illustrate** -> 智能存储指定类型的键值。 +> 创建新的 `Editor`。 + +::: warning + +在 (Xposed) 宿主环境下只读,无法使用。 + +::: + +## edit - method + +```kotlin:no-line-numbers +fun edit(initiate: Editor.() -> Unit) +``` + +**Change Records** + +`v1.1.9` `added` + +**Function Illustrate** + +> 创建新的 `Editor`。 + +自动调用 `Editor.apply` 方法。 ::: warning @@ -508,4 +440,194 @@ fun clearCache() 下次将从 `XSharedPreferences` 重新读取。 -在 (Xposed) 宿主环境中使用。 \ No newline at end of file +在 (Xposed) 宿主环境中使用。 + +## Editor - class + +```kotlin:no-line-numbers +inner class Editor internal constructor() +``` + +**Change Records** + +`v1.1.9` `added` + +**Function Illustrate** + +> `YukiHookModulePrefs` 的存储代理类。 + +请使用 `edit` 方法来获取 `Editor`。 + +::: warning + +在 (Xposed) 宿主环境下只读,无法使用。 + +::: + +### remove - method + +```kotlin:no-line-numbers +fun remove(key: String): Editor +``` + +**Change Records** + +`v1.1.9` `added` + +**Function Illustrate** + +> 移除全部包含 `key` 的存储数据。 + +### remove - method + +```kotlin:no-line-numbers +inline fun remove(prefs: PrefsData): Editor +``` + +**Change Records** + +`v1.1.9` `added` + +**Function Illustrate** + +> 移除 `PrefsData.key` 的存储数据。 + +### clear - method + +```kotlin:no-line-numbers +fun clear(): Editor +``` + +**Change Records** + +`v1.1.9` `added` + +**Function Illustrate** + +> 移除全部存储数据。 + +### putString - method + +```kotlin:no-line-numbers +fun putString(key: String, value: String): Editor +``` + +**Change Records** + +`v1.1.9` `added` + +**Function Illustrate** + +> 存储 `String` 键值。 + +### putStringSet - method + +```kotlin:no-line-numbers +fun putStringSet(key: String, value: Set): Editor +``` + +**Change Records** + +`v1.1.9` `added` + +**Function Illustrate** + +> 存储 `Set` 键值。 + +### putBoolean - method + +```kotlin:no-line-numbers +fun putBoolean(key: String, value: Boolean): Editor +``` + +**Change Records** + +`v1.1.9` `added` + +**Function Illustrate** + +> 存储 `Boolean` 键值。 + +### putInt - method + +```kotlin:no-line-numbers +fun putInt(key: String, value: Int): Editor +``` + +**Change Records** + +`v1.1.9` `added` + +**Function Illustrate** + +> 存储 `Int` 键值。 + +### putLong - method + +```kotlin:no-line-numbers +fun putLong(key: String, value: Long): Editor +``` + +**Change Records** + +`v1.1.9` `added` + +**Function Illustrate** + +> 存储 `Long` 键值。 + +### putFloat - method + +```kotlin:no-line-numbers +fun putFloat(key: String, value: Float): Editor +``` + +**Change Records** + +`v1.1.9` `added` + +**Function Illustrate** + +> 存储 `Float` 键值。 + +### put - method + +```kotlin:no-line-numbers +inline fun put(prefs: PrefsData, value: T): Editor +``` + +**Change Records** + +`v1.1.9` `added` + +**Function Illustrate** + +> 智能存储指定类型的键值。 + +### commit - method + +```kotlin:no-line-numbers +fun commit(): Boolean +``` + +**Change Records** + +`v1.1.9` `added` + +**Function Illustrate** + +> 提交更改 (同步)。 + +### apply - method + +```kotlin:no-line-numbers +fun apply() +``` + +**Change Records** + +`v1.1.9` `added` + +**Function Illustrate** + +> 提交更改 (异步)。 \ No newline at end of file diff --git a/docs-source/src/zh-cn/api/public/com/highcapable/yukihookapi/hook/xposed/prefs/YukiHookModulePrefs.md b/docs-source/src/zh-cn/api/public/com/highcapable/yukihookapi/hook/xposed/prefs/YukiHookModulePrefs.md index 13c2dadb..95f44209 100644 --- a/docs-source/src/zh-cn/api/public/com/highcapable/yukihookapi/hook/xposed/prefs/YukiHookModulePrefs.md +++ b/docs-source/src/zh-cn/api/public/com/highcapable/yukihookapi/hook/xposed/prefs/YukiHookModulePrefs.md @@ -266,185 +266,85 @@ fun all(): HashMap ::: -## remove - method - -```kotlin:no-line-numbers -fun remove(key: String) -``` +

remove - method

**变更记录** `v1.0` `添加` -**功能描述** +`v1.1.9` `作废` -> 移除全部包含 `key` 的存储数据。 +请转移到 `edit` 方法 -::: warning - -在 (Xposed) 宿主环境下只读,无法使用。 - -::: - -## remove - method - -```kotlin:no-line-numbers -inline fun remove(prefs: PrefsData) -``` - -**变更记录** - -`v1.0.67` `新增` - -**功能描述** - -> 移除 `PrefsData.key` 的存储数据。 - -::: warning - -在 (Xposed) 宿主环境下只读,无法使用。 - -::: - -## clear - method - -```kotlin:no-line-numbers -fun clear() -``` +

clear - method

**变更记录** `v1.0.77` `新增` -**功能描述** +`v1.1.9` `作废` -> 移除全部存储数据。 +请转移到 `edit` 方法 -::: warning - -在 (Xposed) 宿主环境下只读,无法使用。 - -::: - -## putString - method - -```kotlin:no-line-numbers -fun putString(key: String, value: String) -``` +

putString - method

**变更记录** `v1.0` `添加` -**功能描述** +`v1.1.9` `作废` -> 存储 `String` 键值。 +请转移到 `edit` 方法 -::: warning - -在 (Xposed) 宿主环境下只读,无法使用。 - -::: - -## putStringSet - method - -```kotlin:no-line-numbers -fun putStringSet(key: String, value: Set) -``` +

putStringSet - method

**变更记录** `v1.0.77` `新增` -**功能描述** +`v1.1.9` `作废` -> 存储 `Set` 键值。 +请转移到 `edit` 方法 -::: warning - -在 (Xposed) 宿主环境下只读,无法使用。 - -::: - -## putBoolean - method - -```kotlin:no-line-numbers -fun putBoolean(key: String, value: Boolean) -``` +

putBoolean - method

**变更记录** `v1.0` `添加` -**功能描述** +`v1.1.9` `作废` -> 存储 `Boolean` 键值。 +请转移到 `edit` 方法 -::: warning - -在 (Xposed) 宿主环境下只读,无法使用。 - -::: - -## putInt - method - -```kotlin:no-line-numbers -fun putInt(key: String, value: Int) -``` +

putInt - method

**变更记录** `v1.0` `添加` -**功能描述** +`v1.1.9` `作废` -> 存储 `Int` 键值。 +请转移到 `edit` 方法 -::: warning - -在 (Xposed) 宿主环境下只读,无法使用。 - -::: - -## putLong - method - -```kotlin:no-line-numbers -fun putLong(key: String, value: Long) -``` +

putLong - method

**变更记录** `v1.0` `添加` -**功能描述** +`v1.1.9` `作废` -> 存储 `Long` 键值。 +请转移到 `edit` 方法 -::: warning - -在 (Xposed) 宿主环境下只读,无法使用。 - -::: - -## putFloat - method - -```kotlin:no-line-numbers -fun putFloat(key: String, value: Float) -``` +

putFloat - method

**变更记录** `v1.0` `添加` -**功能描述** +`v1.1.9` `作废` -> 存储 `Float` 键值。 - -::: warning - -在 (Xposed) 宿主环境下只读,无法使用。 - -::: +请转移到 `edit` 方法 ## get - method @@ -460,19 +360,51 @@ inline fun get(prefs: PrefsData, value: T): T > 智能获取指定类型的键值。 -## put - method - -```kotlin:no-line-numbers -inline fun put(prefs: PrefsData, value: T) -``` +

put - method

**变更记录** `v1.0.67` `新增` +`v1.1.9` `作废` + +请转移到 `edit` 方法 + +## edit - method + +```kotlin:no-line-numbers +fun edit(): Editor +``` + +**变更记录** + +`v1.1.9` `新增` + **功能描述** -> 智能存储指定类型的键值。 +> 创建新的 `Editor`。 + +::: warning + +在 (Xposed) 宿主环境下只读,无法使用。 + +::: + +## edit - method + +```kotlin:no-line-numbers +fun edit(initiate: Editor.() -> Unit) +``` + +**变更记录** + +`v1.1.9` `新增` + +**功能描述** + +> 创建新的 `Editor`。 + +自动调用 `Editor.apply` 方法。 ::: warning @@ -500,4 +432,194 @@ fun clearCache() 下次将从 `XSharedPreferences` 重新读取。 -在 (Xposed) 宿主环境中使用。 \ No newline at end of file +在 (Xposed) 宿主环境中使用。 + +## Editor - class + +```kotlin:no-line-numbers +inner class Editor internal constructor() +``` + +**变更记录** + +`v1.1.9` `新增` + +**功能描述** + +> `YukiHookModulePrefs` 的存储代理类。 + +请使用 `edit` 方法来获取 `Editor`。 + +::: warning + +在 (Xposed) 宿主环境下只读,无法使用。 + +::: + +### remove - method + +```kotlin:no-line-numbers +fun remove(key: String): Editor +``` + +**变更记录** + +`v1.1.9` `新增` + +**功能描述** + +> 移除全部包含 `key` 的存储数据。 + +### remove - method + +```kotlin:no-line-numbers +inline fun remove(prefs: PrefsData): Editor +``` + +**变更记录** + +`v1.1.9` `新增` + +**功能描述** + +> 移除 `PrefsData.key` 的存储数据。 + +### clear - method + +```kotlin:no-line-numbers +fun clear(): Editor +``` + +**变更记录** + +`v1.1.9` `新增` + +**功能描述** + +> 移除全部存储数据。 + +### putString - method + +```kotlin:no-line-numbers +fun putString(key: String, value: String): Editor +``` + +**变更记录** + +`v1.1.9` `新增` + +**功能描述** + +> 存储 `String` 键值。 + +### putStringSet - method + +```kotlin:no-line-numbers +fun putStringSet(key: String, value: Set): Editor +``` + +**变更记录** + +`v1.1.9` `新增` + +**功能描述** + +> 存储 `Set` 键值。 + +### putBoolean - method + +```kotlin:no-line-numbers +fun putBoolean(key: String, value: Boolean): Editor +``` + +**变更记录** + +`v1.1.9` `新增` + +**功能描述** + +> 存储 `Boolean` 键值。 + +### putInt - method + +```kotlin:no-line-numbers +fun putInt(key: String, value: Int): Editor +``` + +**变更记录** + +`v1.1.9` `新增` + +**功能描述** + +> 存储 `Int` 键值。 + +### putLong - method + +```kotlin:no-line-numbers +fun putLong(key: String, value: Long): Editor +``` + +**变更记录** + +`v1.1.9` `新增` + +**功能描述** + +> 存储 `Long` 键值。 + +### putFloat - method + +```kotlin:no-line-numbers +fun putFloat(key: String, value: Float): Editor +``` + +**变更记录** + +`v1.1.9` `新增` + +**功能描述** + +> 存储 `Float` 键值。 + +### put - method + +```kotlin:no-line-numbers +inline fun put(prefs: PrefsData, value: T): Editor +``` + +**变更记录** + +`v1.1.9` `新增` + +**功能描述** + +> 智能存储指定类型的键值。 + +### commit - method + +```kotlin:no-line-numbers +fun commit(): Boolean +``` + +**变更记录** + +`v1.1.9` `新增` + +**功能描述** + +> 提交更改 (同步)。 + +### apply - method + +```kotlin:no-line-numbers +fun apply() +``` + +**变更记录** + +`v1.1.9` `新增` + +**功能描述** + +> 提交更改 (异步)。 \ No newline at end of file diff --git a/yukihookapi/src/main/java/com/highcapable/yukihookapi/hook/xposed/prefs/YukiHookModulePrefs.kt b/yukihookapi/src/main/java/com/highcapable/yukihookapi/hook/xposed/prefs/YukiHookModulePrefs.kt index 19572f4d..1a34588e 100644 --- a/yukihookapi/src/main/java/com/highcapable/yukihookapi/hook/xposed/prefs/YukiHookModulePrefs.kt +++ b/yukihookapi/src/main/java/com/highcapable/yukihookapi/hook/xposed/prefs/YukiHookModulePrefs.kt @@ -398,6 +398,33 @@ class YukiHookModulePrefs private constructor(private var context: Context? = nu it } + /** + * 智能获取指定类型的键值 + * @param prefs 键值实例 + * @param value 默认值 - 未指定默认为 [prefs] 中的 [PrefsData.value] + * @return [T] 只能是 [String]、[Set]<[String]>、[Int]、[Float]、[Long]、[Boolean] + */ + inline fun get(prefs: PrefsData, value: T = prefs.value): T = getPrefsData(prefs.key, value) as T + + /** + * 智能获取指定类型的键值 + * + * 封装方法以调用内联方法 + * @param key 键值 + * @param value 默认值 + * @return [Any] + */ + @PublishedApi + internal fun getPrefsData(key: String, value: Any?): Any = when (value) { + is String -> getString(key, value) + is Set<*> -> getStringSet(key, value as? Set ?: error("Key-Value type ${value.javaClass.name} is not allowed")) + is Int -> getInt(key, value) + is Float -> getFloat(key, value) + is Long -> getLong(key, value) + is Boolean -> getBoolean(key, value) + else -> error("Key-Value type ${value?.javaClass?.name} is not allowed") + } + /** * 判断当前是否包含 [key] 键值的数据 * @@ -426,193 +453,138 @@ class YukiHookModulePrefs private constructor(private var context: Context? = nu /** * 移除全部包含 [key] 的存储数据 * - * - 在模块环境中使用 + * - ❗此方法已弃用 - 在之后的版本中将直接被删除 * - * - ❗在 (Xposed) 宿主环境下只读 - 无法使用 + * - ❗请现在转移到 [edit] 方法 * @param key 键值名称 */ - fun remove(key: String) = moduleEnvironment { - sPrefs.edit().remove(key).apply() - makeWorldReadable() - } + @Deprecated(message = "此方法因为性能问题已被作废,请转移到新用法", ReplaceWith("edit { remove(key) }")) + fun remove(key: String) = edit { remove(key) } /** * 移除 [PrefsData.key] 的存储数据 * - * - 在模块环境中使用 + * - ❗此方法已弃用 - 在之后的版本中将直接被删除 * - * - ❗在 (Xposed) 宿主环境下只读 - 无法使用 + * - ❗请现在转移到 [edit] 方法 * @param prefs 键值实例 */ - inline fun remove(prefs: PrefsData) = remove(prefs.key) + @Deprecated(message = "此方法因为性能问题已被作废,请转移到新用法", ReplaceWith("edit { remove(prefs) }")) + inline fun remove(prefs: PrefsData) = edit { remove(prefs) } /** * 移除全部存储数据 * - * - 在模块环境中使用 + * - ❗此方法已弃用 - 在之后的版本中将直接被删除 * - * - ❗在 (Xposed) 宿主环境下只读 - 无法使用 + * - ❗请现在转移到 [edit] 方法 */ - fun clear() = moduleEnvironment { - sPrefs.edit().clear().apply() - makeWorldReadable() - } + @Deprecated(message = "此方法因为性能问题已被作废,请转移到新用法", ReplaceWith("edit { clear() }")) + fun clear() = edit { clear() } /** * 存储 [String] 键值 * - * - 建议使用 [PrefsData] 创建模板并使用 [put] 存储数据 + * - ❗此方法已弃用 - 在之后的版本中将直接被删除 * - * - 在模块环境中使用 - * - * - ❗在 (Xposed) 宿主环境下只读 - 无法使用 + * - ❗请现在转移到 [edit] 方法 * @param key 键值名称 * @param value 键值数据 */ - fun putString(key: String, value: String) = moduleEnvironment { - sPrefs.edit().putString(key, value).apply() - makeWorldReadable() - } + @Deprecated(message = "此方法因为性能问题已被作废,请转移到新用法", ReplaceWith("edit { putString(key, value) }")) + fun putString(key: String, value: String) = edit { putString(key, value) } /** * 存储 [Set]<[String]> 键值 * - * - 建议使用 [PrefsData] 创建模板并使用 [put] 存储数据 + * - ❗此方法已弃用 - 在之后的版本中将直接被删除 * - * - 在模块环境中使用 - * - * - ❗在 (Xposed) 宿主环境下只读 - 无法使用 + * - ❗请现在转移到 [edit] 方法 * @param key 键值名称 * @param value 键值数据 */ - fun putStringSet(key: String, value: Set) = moduleEnvironment { - sPrefs.edit().putStringSet(key, value).apply() - makeWorldReadable() - } + @Deprecated(message = "此方法因为性能问题已被作废,请转移到新用法", ReplaceWith("edit { putStringSet(key, value) }")) + fun putStringSet(key: String, value: Set) = edit { putStringSet(key, value) } /** * 存储 [Boolean] 键值 * - * - 建议使用 [PrefsData] 创建模板并使用 [put] 存储数据 + * - ❗此方法已弃用 - 在之后的版本中将直接被删除 * - * - 在模块环境中使用 - * - * - ❗在 (Xposed) 宿主环境下只读 - 无法使用 + * - ❗请现在转移到 [edit] 方法 * @param key 键值名称 * @param value 键值数据 */ - fun putBoolean(key: String, value: Boolean) = moduleEnvironment { - sPrefs.edit().putBoolean(key, value).apply() - makeWorldReadable() - } + @Deprecated(message = "此方法因为性能问题已被作废,请转移到新用法", ReplaceWith("edit { putBoolean(key, value) }")) + fun putBoolean(key: String, value: Boolean) = edit { putBoolean(key, value) } /** * 存储 [Int] 键值 * - * - 建议使用 [PrefsData] 创建模板并使用 [put] 存储数据 + * - ❗此方法已弃用 - 在之后的版本中将直接被删除 * - * - 在模块环境中使用 - * - * - ❗在 (Xposed) 宿主环境下只读 - 无法使用 + * - ❗请现在转移到 [edit] 方法 * @param key 键值名称 * @param value 键值数据 */ - fun putInt(key: String, value: Int) = moduleEnvironment { - sPrefs.edit().putInt(key, value).apply() - makeWorldReadable() - } + @Deprecated(message = "此方法因为性能问题已被作废,请转移到新用法", ReplaceWith("edit { putInt(key, value) }")) + fun putInt(key: String, value: Int) = edit { putInt(key, value) } /** * 存储 [Float] 键值 * - * - 建议使用 [PrefsData] 创建模板并使用 [put] 存储数据 + * - ❗此方法已弃用 - 在之后的版本中将直接被删除 * - * - 在模块环境中使用 - * - * - ❗在 (Xposed) 宿主环境下只读 - 无法使用 + * - ❗请现在转移到 [edit] 方法 * @param key 键值名称 * @param value 键值数据 */ - fun putFloat(key: String, value: Float) = moduleEnvironment { - sPrefs.edit().putFloat(key, value).apply() - makeWorldReadable() - } + @Deprecated(message = "此方法因为性能问题已被作废,请转移到新用法", ReplaceWith("edit { putFloat(key, value) }")) + fun putFloat(key: String, value: Float) = edit { putFloat(key, value) } /** * 存储 [Long] 键值 * - * - 建议使用 [PrefsData] 创建模板并使用 [put] 存储数据 + * - ❗此方法已弃用 - 在之后的版本中将直接被删除 * - * - 在模块环境中使用 - * - * - ❗在 (Xposed) 宿主环境下只读 - 无法使用 + * - ❗请现在转移到 [edit] 方法 * @param key 键值名称 * @param value 键值数据 */ - fun putLong(key: String, value: Long) = moduleEnvironment { - sPrefs.edit().putLong(key, value).apply() - makeWorldReadable() - } - - /** - * 智能获取指定类型的键值 - * @param prefs 键值实例 - * @param value 默认值 - 未指定默认为 [prefs] 中的 [PrefsData.value] - * @return [T] 只能是 [String]、[Set]<[String]>、[Int]、[Float]、[Long]、[Boolean] - */ - inline fun get(prefs: PrefsData, value: T = prefs.value): T = getPrefsData(prefs.key, value) as T + @Deprecated(message = "此方法因为性能问题已被作废,请转移到新用法", ReplaceWith("edit { putLong(key, value) }")) + fun putLong(key: String, value: Long) = edit { putLong(key, value) } /** * 智能存储指定类型的键值 * - * - 在模块环境中使用 + * - ❗此方法已弃用 - 在之后的版本中将直接被删除 * - * - ❗在 (Xposed) 宿主环境下只读 - 无法使用 - * @param prefs 键值实例 - * @param value 要存储的值 - 只能是 [String]、[Set]<[String]>、[Int]、[Float]、[Long]、[Boolean] + * - ❗请现在转移到 [edit] 方法 */ - inline fun put(prefs: PrefsData, value: T) = putPrefsData(prefs.key, value) + @Deprecated(message = "此方法因为性能问题已被作废,请转移到新用法", ReplaceWith("edit { put(prefs, value) }")) + inline fun put(prefs: PrefsData, value: T) = edit { put(prefs, value) } /** - * 智能获取指定类型的键值 - * - * 封装方法以调用内联方法 - * @param key 键值 - * @param value 默认值 - * @return [Any] - */ - @PublishedApi - internal fun getPrefsData(key: String, value: Any?): Any = when (value) { - is String -> getString(key, value) - is Set<*> -> getStringSet(key, value as? Set ?: error("Key-Value type ${value.javaClass.name} is not allowed")) - is Int -> getInt(key, value) - is Float -> getFloat(key, value) - is Long -> getLong(key, value) - is Boolean -> getBoolean(key, value) - else -> error("Key-Value type ${value?.javaClass?.name} is not allowed") - } - - /** - * 智能存储指定类型的键值 - * - * 封装方法以调用内联方法 + * 创建新的 [Editor] * * - 在模块环境中使用 * * - ❗在 (Xposed) 宿主环境下只读 - 无法使用 - * @param key 键值 - * @param value 要存储的值 - 只能是 [String]、[Set]<[String]>、[Int]、[Float]、[Long]、[Boolean] + * @return [Editor] */ - @PublishedApi - internal fun putPrefsData(key: String, value: Any?) = when (value) { - is String -> putString(key, value) - is Set<*> -> putStringSet(key, value as? Set ?: error("Key-Value type ${value.javaClass.name} is not allowed")) - is Int -> putInt(key, value) - is Float -> putFloat(key, value) - is Long -> putLong(key, value) - is Boolean -> putBoolean(key, value) - else -> error("Key-Value type ${value?.javaClass?.name} is not allowed") - } + fun edit() = Editor() + + /** + * 创建新的 [Editor] + * + * 自动调用 [Editor.apply] 方法 + * + * - 在模块环境中使用 + * + * - ❗在 (Xposed) 宿主环境下只读 - 无法使用 + * @param initiate 方法体 + */ + fun edit(initiate: Editor.() -> Unit = {}) = edit().apply(initiate).apply() /** * 清除 [XSharedPreferences] 中缓存的键值数据 @@ -638,13 +610,170 @@ class YukiHookModulePrefs private constructor(private var context: Context? = nu } /** - * 仅在模块环境执行 + * [YukiHookModulePrefs] 的存储代理类 * - * 非模块环境使用会打印警告信息 - * @param callback 在模块环境执行 + * - ❗请使用 [edit] 方法来获取 [Editor] + * + * - 在模块环境中使用 + * + * - ❗在 (Xposed) 宿主环境下只读 - 无法使用 */ - private inline fun moduleEnvironment(callback: () -> Unit) { - if (isXposedEnvironment.not()) callback() - else yLoggerW(msg = "YukiHookModulePrefs write operation not allowed in Xposed Environment") + inner class Editor internal constructor() { + + /** 创建新的存储代理类 */ + private var editor = runCatching { sPrefs.edit() }.getOrNull() + + /** + * 移除全部包含 [key] 的存储数据 + * @param key 键值名称 + * @return [Editor] + */ + fun remove(key: String) = moduleEnvironment { + editor?.remove(key) + makeWorldReadable() + } + + /** + * 移除 [PrefsData.key] 的存储数据 + * @param prefs 键值实例 + * @return [Editor] + */ + inline fun remove(prefs: PrefsData) = remove(prefs.key) + + /** + * 移除全部存储数据 + * @return [Editor] + */ + fun clear() = moduleEnvironment { + editor?.clear() + makeWorldReadable() + } + + /** + * 存储 [String] 键值 + * + * - 建议使用 [PrefsData] 创建模板并使用 [put] 存储数据 + * @param key 键值名称 + * @param value 键值数据 + * @return [Editor] + */ + fun putString(key: String, value: String) = moduleEnvironment { + editor?.putString(key, value) + makeWorldReadable() + } + + /** + * 存储 [Set]<[String]> 键值 + * + * - 建议使用 [PrefsData] 创建模板并使用 [put] 存储数据 + * @param key 键值名称 + * @param value 键值数据 + * @return [Editor] + */ + fun putStringSet(key: String, value: Set) = moduleEnvironment { + editor?.putStringSet(key, value) + makeWorldReadable() + } + + /** + * 存储 [Boolean] 键值 + * + * - 建议使用 [PrefsData] 创建模板并使用 [put] 存储数据 + * @param key 键值名称 + * @param value 键值数据 + * @return [Editor] + */ + fun putBoolean(key: String, value: Boolean) = moduleEnvironment { + editor?.putBoolean(key, value) + makeWorldReadable() + } + + /** + * 存储 [Int] 键值 + * + * - 建议使用 [PrefsData] 创建模板并使用 [put] 存储数据 + * @param key 键值名称 + * @param value 键值数据 + * @return [Editor] + */ + fun putInt(key: String, value: Int) = moduleEnvironment { + editor?.putInt(key, value) + makeWorldReadable() + } + + /** + * 存储 [Float] 键值 + * + * - 建议使用 [PrefsData] 创建模板并使用 [put] 存储数据 + * @param key 键值名称 + * @param value 键值数据 + * @return [Editor] + */ + fun putFloat(key: String, value: Float) = moduleEnvironment { + editor?.putFloat(key, value) + makeWorldReadable() + } + + /** + * 存储 [Long] 键值 + * + * - 建议使用 [PrefsData] 创建模板并使用 [put] 存储数据 + * @param key 键值名称 + * @param value 键值数据 + * @return [Editor] + */ + fun putLong(key: String, value: Long) = moduleEnvironment { + editor?.putLong(key, value) + makeWorldReadable() + } + + /** + * 智能存储指定类型的键值 + * @param prefs 键值实例 + * @param value 要存储的值 - 只能是 [String]、[Set]<[String]>、[Int]、[Float]、[Long]、[Boolean] + * @return [Editor] + */ + inline fun put(prefs: PrefsData, value: T) = putPrefsData(prefs.key, value) + + /** + * 智能存储指定类型的键值 + * + * 封装方法以调用内联方法 + * @param key 键值 + * @param value 要存储的值 - 只能是 [String]、[Set]<[String]>、[Int]、[Float]、[Long]、[Boolean] + * @return [Editor] + */ + @PublishedApi + internal fun putPrefsData(key: String, value: Any?) = when (value) { + is String -> putString(key, value) + is Set<*> -> putStringSet(key, value as? Set ?: error("Key-Value type ${value.javaClass.name} is not allowed")) + is Int -> putInt(key, value) + is Float -> putFloat(key, value) + is Long -> putLong(key, value) + is Boolean -> putBoolean(key, value) + else -> error("Key-Value type ${value?.javaClass?.name} is not allowed") + } + + /** + * 提交更改 (同步) + * @return [Boolean] 是否成功 + */ + fun commit() = editor?.commit()?.also { makeWorldReadable() } ?: false + + /** 提交更改 (异步) */ + fun apply() = editor?.apply().also { makeWorldReadable() } ?: Unit + + /** + * 仅在模块环境执行 + * + * 非模块环境使用会打印警告信息 + * @param callback 在模块环境执行 + * @return [Editor] + */ + private inline fun moduleEnvironment(callback: () -> Unit): Editor { + if (isXposedEnvironment.not()) callback() + else yLoggerW(msg = "YukiHookModulePrefs.Editor not allowed in Xposed Environment") + return this + } } } \ No newline at end of file