From b25e60779c78f18edd32a14826bd699ba9bb7f5e Mon Sep 17 00:00:00 2001 From: ltdong Date: Fri, 21 Jan 2022 00:13:35 +0800 Subject: [PATCH] replace storage with preference Signed-off-by: ltdong --- api/@ohos.data.preference.d.ts | 218 +++++++++++++++++++++++++++++++++ api/@ohos.data.storage.d.ts | 20 ++- 2 files changed, 235 insertions(+), 3 deletions(-) create mode 100644 api/@ohos.data.preference.d.ts diff --git a/api/@ohos.data.preference.d.ts b/api/@ohos.data.preference.d.ts new file mode 100644 index 0000000000..6d53b3a3d9 --- /dev/null +++ b/api/@ohos.data.preference.d.ts @@ -0,0 +1,218 @@ +/* +* 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'; + +/** + * Provides interfaces to obtain and modify preference data. + * + * @name preference + * @since 8 + * @sysCap SystemCapability.Data#DATA_APPDATAMGR + * @devices phone, tablet + */ +declare namespace preference { + /** + * Obtains a {@link Preference} instance matching a specified preference file name. + * + *

The {@link references} instance loads all data of the preference file and + * resides in the memory. You can use removePreferenceFromCache to remove the instance from the memory. + * + * @param path Indicates the path of preference file stored. + * @return Returns the {@link Preference} instance matching the specified preference file name. + * @throws BusinessError if invoked failed + * @since 8 + */ + function getPreferenceSync(path: string): Preference; + function getPreference(path: string, callback: AsyncCallback): void; + function getPreference(path: string): Promise; + + /** + * Deletes a {@link Preference} instance matching a specified preference file name + * from the cache which is performed by removePreferenceFromCache and deletes the + * preference file. + * + *

When deleting the {@link Preference} 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 path Indicates the path of preference file + * @throws BusinessError if invoked failed + * @since 8 + */ + function deletePreferenceSync(path: string): void; + function deletePreference(path: string, callback: AsyncCallback): void; + function deletePreference(path: string): Promise; + + /** + * Deletes a {@link Preference} instance matching a specified preference file name + * from the cache. + * + *

When deleting the {@link Preference} 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 path Indicates the path of preference file. + * @throws BusinessError if invoked failed + * @since 8 + */ + function removePreferenceFromCacheSync(path: string): void; + function removePreferenceFromCache(path: string, callback: AsyncCallback): void; + function removePreferenceFromCache(path: string): Promise; + + /** + * Provides interfaces to obtain and modify preference data. + * + *

The preference data is stored in a file, which matches only one {@link Preference} instance in the memory. + * You can use getPreference to obtain the {@link Preference} instance matching + * the file that stores preference data, and use emovePreferenceFromCache + * to remove the {@link Preference} instance from the memory. + * + * @sysCap SystemCapability.Data#DATA_APPDATAMGR + * @devices phone, tablet + * @since 8 + */ + interface Preference { + /** + * Obtains the value of a preference 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 preference. 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 8 + */ + getSync(key: string, defValue: ValueType): ValueType; + get(key: string, defValue: ValueType, callback: AsyncCallback): void; + get(key: string, defValue: ValueType): Promise; + + /** + * Checks whether the {@link Preference} object has a preference matching a specified key. + * + * @param key Indicates the key of the preference to check for. + * @return Returns {@code true} if the {@link Preference} object has a preference with the specified key; + * returns {@code false} otherwise. + * @throws BusinessError if invoked failed + * @since 8 + */ + hasSync(key: string): boolean; + has(key: string, callback: AsyncCallback): boolean; + has(key: string): Promise; + + /** + * Sets an int value for the key in the {@link Preference} object. + * + *

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

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

You can call the {@link #flush} method to save the {@link Preference} object to the + * file. + * + * @throws BusinessError if invoked failed + * @since 8 + */ + clearSync(): void; + clear(callback: AsyncCallback): void; + clear(): Promise; + + /** + * Saves the {@link Preference} object to the file. + * + * @throws BusinessError if invoked failed + * @since 8 + */ + flushSync(): void; + flush(callback: AsyncCallback): void; + flush(): Promise; + + /** + * Registers an observer to listen for the change of a {@link Preference} object. + * + * @param callback Indicates the callback when preference changes. + * @throws BusinessError if invoked failed + * @since 8 + */ + on(type: 'change', callback: Callback): void; + + /** + * Unregisters an existing observer. + * + * @param callback Indicates the registered callback. + * @throws BusinessError if invoked failed + * @since 8 + */ + off(type: 'change', callback: Callback): void; + } + + /** + * Indicates possible value types + */ + type ValueType = number | string | boolean; + + /** + * Define the change data information object. + * + * @sysCap SystemCapability.Data#DATA_APPDATAMGR + * @devices phone, tablet + * @since 8 + */ + interface PreferenceObserver { + /** + * Indicates which key changes + */ + key: string; + } + + /** + * 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 preference; \ No newline at end of file diff --git a/api/@ohos.data.storage.d.ts b/api/@ohos.data.storage.d.ts index 937ce3ad4a..11f9ed6ef1 100644 --- a/api/@ohos.data.storage.d.ts +++ b/api/@ohos.data.storage.d.ts @@ -21,6 +21,7 @@ import { AsyncCallback, Callback } from './basic'; * @since 5 * @sysCap SystemCapability.Data#DATA_APPDATAMGR * @devices phone, tablet + * @deprecated */ declare namespace storage { /** @@ -33,6 +34,7 @@ declare namespace storage { * @return Returns the {@link Storage} instance matching the specified storage file name. * @throws BusinessError if invoked failed * @since 5 + * @deprecated */ function getStorageSync(path: string): Storage; function getStorage(path: string, callback: AsyncCallback): void; @@ -50,6 +52,7 @@ declare namespace storage { * @param path Indicates the path of storage file * @throws BusinessError if invoked failed * @since 5 + * @deprecated */ function deleteStorageSync(path: string): void; function deleteStorage(path: string, callback: AsyncCallback): void; @@ -66,6 +69,7 @@ declare namespace storage { * @param path Indicates the path of storage file. * @throws BusinessError if invoked failed * @since 5 + * @deprecated */ function removeStorageFromCacheSync(path: string): void; function removeStorageFromCache(path: string, callback: AsyncCallback): void; @@ -82,6 +86,7 @@ declare namespace storage { * @sysCap SystemCapability.Data#DATA_APPDATAMGR * @devices phone, tablet * @since 5 + * @deprecated */ interface Storage { /** @@ -94,19 +99,21 @@ declare namespace storage { * @return Returns the value matching the specified key if it is found; returns the default value otherwise. * @throws BusinessError if invoked failed * @since 5 + * @deprecated */ getSync(key: string, defValue: ValueType): ValueType; get(key: string, defValue: ValueType, callback: AsyncCallback): void; get(key: string, defValue: ValueType): Promise; /** - * Checks whether the {@link Storage} object contains a storage matching a specified key. + * Checks whether the {@link Storage} object has a storage matching a specified key. * * @param key Indicates the key of the storage to check for. - * @return Returns {@code true} if the {@link Storage} object contains a storage with the specified key; + * @return Returns {@code true} if the {@link Storage} object has a storage with the specified key; * returns {@code false} otherwise. * @throws BusinessError if invoked failed * @since 5 + * @deprecated */ hasSync(key: string): boolean; has(key: string, callback: AsyncCallback): boolean; @@ -123,6 +130,7 @@ declare namespace storage { * MAX_KEY_LENGTH. * @throws BusinessError if invoked failed * @since 5 + * @deprecated */ putSync(key: string, value: ValueType): void; put(key: string, value: ValueType, callback: AsyncCallback): void; @@ -138,6 +146,7 @@ declare namespace storage { * MAX_KEY_LENGTH. * @throws BusinessError if invoked failed * @since 5 + * @deprecated */ deleteSync(key: string): void; delete(key: string, callback: AsyncCallback): void; @@ -151,16 +160,18 @@ declare namespace storage { * * @throws BusinessError if invoked failed * @since 5 + * @deprecated */ clearSync(): void; clear(callback: AsyncCallback): void; clear(): Promise; /** - * Asynchronously saves the {@link Storage} object to the file. + * Saves the {@link Storage} object to the file. * * @throws BusinessError if invoked failed * @since 5 + * @deprecated */ flushSync(): void; flush(callback: AsyncCallback): void; @@ -172,6 +183,7 @@ declare namespace storage { * @param callback Indicates the callback when storage changes. * @throws BusinessError if invoked failed * @since 5 + * @deprecated */ on(type: 'change', callback: Callback): void; @@ -181,6 +193,7 @@ declare namespace storage { * @param callback Indicates the registered callback. * @throws BusinessError if invoked failed * @since 5 + * @deprecated */ off(type: 'change', callback: Callback): void; } @@ -196,6 +209,7 @@ declare namespace storage { * @sysCap SystemCapability.Data#DATA_APPDATAMGR * @devices phone, tablet * @since 5 + * @deprecated */ interface StorageObserver { /** -- Gitee