From 27cda272cf81781932647d8d7725853976710778 Mon Sep 17 00:00:00 2001 From: PaDoBoo Date: Fri, 15 Apr 2022 16:35:36 +0800 Subject: [PATCH 1/4] =?UTF-8?q?data.preferences=E6=8E=A5=E5=8F=A3API9?= =?UTF-8?q?=E4=B8=A2=E5=A4=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: PaDoBoo --- api/@ohos.data.preferences.d.ts | 199 ++++++++++++++++++++++++++++++++ 1 file changed, 199 insertions(+) create mode 100644 api/@ohos.data.preferences.d.ts diff --git a/api/@ohos.data.preferences.d.ts b/api/@ohos.data.preferences.d.ts new file mode 100644 index 0000000000..000599ed5d --- /dev/null +++ b/api/@ohos.data.preferences.d.ts @@ -0,0 +1,199 @@ +/* +* Copyright (c) 2022 Huawei Device Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ +import { AsyncCallback, Callback } from './basic'; +import Context from "./application/Context"; + +/** + * Provides interfaces to obtain and modify preferences data. + * + * @name preferences + * @since 9 + * @syscap SystemCapability.DistributedDataManager.Preferences.Core + * + */ +declare namespace preferences { + /** + * Obtains a {@link Preferences} instance matching a specified preferences file name. + * + *

The {@link references} instance loads all data of the preferences file and + * resides in the memory. You can use removePreferencesFromCache to remove the instance from the memory. + * + * @param context Indicates the context of application or capability. + * @param name Indicates the preferences file name. + * @return Returns the {@link Preferences} instance matching the specified preferences file name. + * @throws BusinessError if invoked failed + * @since 9 + */ + function getPreferences(context: Context, name: string, callback: AsyncCallback): void; + function getPreferences(context: Context, name: string): Promise; + + /** + * Deletes a {@link Preferences} instance matching a specified preferences file name + * from the cache which is performed by removePreferencesFromCache and deletes the + * preferences file. + * + *

When deleting the {@link Preferences} instance, you must release all references + * of the instance. In addition, do not use the instance to perform data operations. Otherwise, data inconsistency + * will occur. + * + * @param context Indicates the context of application or capability. + * @param name Indicates the preferences file name. + * @throws BusinessError if invoked failed + * @since 9 + */ + function deletePreferences(context: Context, name: string, callback: AsyncCallback): void; + function deletePreferences(context: Context, name: string): Promise; + + /** + * Deletes a {@link Preferences} instance matching a specified preferences file name + * from the cache. + * + *

When deleting the {@link Preferences} instance, you must release all references + * of the instance. In addition, do not use the instance to perform data operations. Otherwise, data inconsistency + * will occur. + * + * @param context Indicates the context of application or capability. + * @param name Indicates the preferences file name. + * @throws BusinessError if invoked failed + * @since 9 + */ + function removePreferencesFromCache(context: Context, name: string, callback: AsyncCallback): void; + function removePreferencesFromCache(context: Context, name: string): Promise; + + /** + * Provides interfaces to obtain and modify preferences data. + * + *

The preferences data is stored in a file, which matches only one {@link Preferences} instance in the memory. + * You can use getPreferences to obtain the {@link Preferences} instance matching + * the file that stores preferences data, and use emovePreferencesFromCache + * to remove the {@link Preferences} instance from the memory. + * + * @syscap SystemCapability.DistributedDataManager.Preferences.Core + * + * @since 9 + */ + interface Preferences { + /** + * Obtains the value of a preferences in the int format. + * + *

If the value is {@code null} or not in the int format, the default value is returned. + * + * @param key Indicates the key of the preferences. It cannot be {@code null} or empty. + * @param defValue Indicates the default value to return. + * @return Returns the value matching the specified key if it is found; returns the default value otherwise. + * @throws BusinessError if invoked failed + * @since 9 + */ + get(key: string, defValue: ValueType, callback: AsyncCallback): void; + get(key: string, defValue: ValueType): Promise; + + /** + * Checks whether the {@link Preferences} object contains a preferences matching a specified key. + * + * @param key Indicates the key of the preferences to check for. + * @return Returns {@code true} if the {@link Preferences} object contains a preferences with the specified key; + * returns {@code false} otherwise. + * @throws BusinessError if invoked failed + * @since 9 + */ + has(key: string, callback: AsyncCallback): boolean; + has(key: string): Promise; + + /** + * Sets an int value for the key in the {@link Preferences} object. + * + *

You can call the {@link #flush} method to save the {@link Preferences} object to the + * file. + * + * @param key Indicates the key of the preferences to modify. It cannot be {@code null} or empty. + * @param value Indicates the value of the preferences. + * MAX_KEY_LENGTH. + * @throws BusinessError if invoked failed + * @since 9 + */ + put(key: string, value: ValueType, callback: AsyncCallback): void; + put(key: string, value: ValueType): Promise; + + /** + * Deletes the preferences with a specified key from the {@link Preferences} object. + * + *

You can call the {@link #flush} method to save the {@link Preferences} object to the + * file. + * + * @param key Indicates the key of the preferences to delete. It cannot be {@code null} or empty. + * MAX_KEY_LENGTH. + * @throws BusinessError if invoked failed + * @since 9 + */ + delete(key: string, callback: AsyncCallback): void; + delete(key: string): Promise; + + /** + * Clears all preferences from the {@link Preferences} object. + * + *

You can call the {@link #flush} method to save the {@link Preferences} object to the + * file. + * + * @throws BusinessError if invoked failed + * @since 9 + */ + clear(callback: AsyncCallback): void; + clear(): Promise; + + /** + * Asynchronously saves the {@link Preferences} object to the file. + * + * @throws BusinessError if invoked failed + * @since 9 + */ + flush(callback: AsyncCallback): void; + flush(): Promise; + + /** + * Registers an observer to listen for the change of a {@link Preferences} object. + * + * @param callback Indicates the callback when preferences changes. + * @throws BusinessError if invoked failed + * @since 9 + */ + on(type: 'change', callback: Callback<{ key: string }>): void; + + /** + * Unregisters an existing observer. + * + * @param callback Indicates the registered callback. + * @throws BusinessError if invoked failed + * @since 9 + */ + off(type: 'change', callback: Callback<{ key: string }>): void; + } + + /** + * Indicates possible value types + */ + type ValueType = number | string | boolean; + + /** + * Indicates the maximum length of a key (80 characters). + */ + const MAX_KEY_LENGTH: 80; + + /** + * Indicates the maximum length of a string (8192 characters). + */ + const MAX_VALUE_LENGTH: 8192; +} + +export default preferences; -- Gitee From 8538cad5536385e237a4953b1fc19c1177ac11f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BB=E5=B9=B3?= Date: Fri, 15 Apr 2022 08:58:11 +0000 Subject: [PATCH 2/4] Update api/@ohos.data.preferences.d.ts --- api/@ohos.data.preferences.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/@ohos.data.preferences.d.ts b/api/@ohos.data.preferences.d.ts index 000599ed5d..274caabf60 100644 --- a/api/@ohos.data.preferences.d.ts +++ b/api/@ohos.data.preferences.d.ts @@ -177,7 +177,7 @@ declare namespace preferences { * @throws BusinessError if invoked failed * @since 9 */ - off(type: 'change', callback: Callback<{ key: string }>): void; + off(type: 'change', callback?: Callback<{ key: string }>): void; } /** -- Gitee From 3d09fe91ec10e74e9a2e617c7b70c07dcb4ee292 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BB=E5=B9=B3?= Date: Fri, 15 Apr 2022 09:01:30 +0000 Subject: [PATCH 3/4] Update api/@ohos.data.preferences.d.ts --- api/@ohos.data.preferences.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/@ohos.data.preferences.d.ts b/api/@ohos.data.preferences.d.ts index 274caabf60..2d5a313f68 100644 --- a/api/@ohos.data.preferences.d.ts +++ b/api/@ohos.data.preferences.d.ts @@ -108,7 +108,7 @@ declare namespace preferences { * @throws BusinessError if invoked failed * @since 9 */ - has(key: string, callback: AsyncCallback): boolean; + has(key: string, callback: AsyncCallback): void; has(key: string): Promise; /** -- Gitee From 2cd7e3fb36a806536934eb438f2d714dde85ae15 Mon Sep 17 00:00:00 2001 From: PaDoBoo Date: Fri, 15 Apr 2022 17:09:47 +0800 Subject: [PATCH 4/4] Update api/@ohos.data.preferences.d.ts Signed-off-by: PaDoBoo --- api/@ohos.data.preferences.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/@ohos.data.preferences.d.ts b/api/@ohos.data.preferences.d.ts index 274caabf60..2d5a313f68 100644 --- a/api/@ohos.data.preferences.d.ts +++ b/api/@ohos.data.preferences.d.ts @@ -108,7 +108,7 @@ declare namespace preferences { * @throws BusinessError if invoked failed * @since 9 */ - has(key: string, callback: AsyncCallback): boolean; + has(key: string, callback: AsyncCallback): void; has(key: string): Promise; /** -- Gitee