diff --git a/entry/src/main/ets/common/database/PreferencesUtil.ets b/entry/src/main/ets/common/database/PreferencesUtil.ets index ab2f0117ec3727e5c18518d992b13eda1312c8a0..6d7cb585ba16b8bedd3400d0bf1765409cfbc3df 100644 --- a/entry/src/main/ets/common/database/PreferencesUtil.ets +++ b/entry/src/main/ets/common/database/PreferencesUtil.ets @@ -13,6 +13,7 @@ * limitations under the License. */ +import { BusinessError } from '@kit.BasicServicesKit'; import { preferences } from '@kit.ArkData'; import { GlobalContext } from '../utils/GlobalContext'; import Logger from '../utils/Logger'; @@ -26,38 +27,52 @@ const KEY_APP_FONT_SIZE = 'appFontSize'; */ export class PreferencesUtil { createFontPreferences(context: Context) { - let fontPreferences: Function = (() => { - let preference: Promise = preferences.getPreferences(context, PREFERENCES_NAME); - return preference; - }); - GlobalContext.getContext().setObject('getFontPreferences', fontPreferences); + preferences.getPreferences(context, PREFERENCES_NAME).then((pref: preferences.Preferences) => { + GlobalContext.getContext().setObject('getFontPreferences', pref); + }).catch((error: BusinessError) => { + Logger.error(TAG, `getPreferences err, code: ${error.code}, mesage: ${error.message}`); + }) } saveDefaultFontSize(fontSize: number) { - let getFontPreferences: Function = GlobalContext.getContext().getObject('getFontPreferences') as Function; - getFontPreferences().then((preferences: preferences.Preferences) => { - preferences.has(KEY_APP_FONT_SIZE).then(async (isExist: boolean) => { - Logger.info(TAG, 'preferences has changeFontSize is ' + isExist); - if (!isExist) { - await preferences.put(KEY_APP_FONT_SIZE, fontSize); - preferences.flush(); - } + try { + let getFontPreferences: Function = GlobalContext.getContext().getObject('getFontPreferences') as Function; + getFontPreferences().then((preferences: preferences.Preferences) => { + preferences.has(KEY_APP_FONT_SIZE).then(async (isExist: boolean) => { + Logger.info(TAG, 'preferences has changeFontSize is ' + isExist); + if (!isExist) { + await preferences.put(KEY_APP_FONT_SIZE, fontSize); + preferences.flush().catch((error: BusinessError) => { + Logger.error(TAG, `flush err, code: ${error.code}, mesage: ${error.message}`); + }); + } + }).catch((err: Error) => { + Logger.error(TAG, 'Has the value failed with err: ' + err); + }); }).catch((err: Error) => { - Logger.error(TAG, 'Has the value failed with err: ' + err); + Logger.error(TAG, 'Get the preferences failed, err: ' + err); }); - }).catch((err: Error) => { - Logger.error(TAG, 'Get the preferences failed, err: ' + err); - }); + } catch (err) { + let error = err as BusinessError; + Logger.error(TAG, `saveDefaultFontSize err, code: ${error.code}, mesage: ${error.message}`); + } } saveChangeFontSize(fontSize: number) { let getFontPreferences: Function = GlobalContext.getContext().getObject('getFontPreferences') as Function; - getFontPreferences().then(async (preferences: preferences.Preferences) => { - await preferences.put(KEY_APP_FONT_SIZE, fontSize); - preferences.flush(); - }).catch((err: Error) => { - Logger.error(TAG, 'put the preferences failed, err: ' + err); - }); + try { + getFontPreferences().then(async (preferences: preferences.Preferences) => { + await preferences.put(KEY_APP_FONT_SIZE, fontSize); + preferences.flush().catch((error: BusinessError) => { + Logger.error(TAG, `getFontPreferences err, code: ${error.code}, mesage: ${error.message}`); + }); + }).catch((err: Error) => { + Logger.error(TAG, 'put the preferences failed, err: ' + err); + }); + } catch (err) { + let error = err as BusinessError; + Logger.error(TAG, `saveChangeFontSize err, code: ${error.code}, mesage: ${error.message}`); + } } async getChangeFontSize() { @@ -71,7 +86,9 @@ export class PreferencesUtil { async deleteChangeFontSize() { let getFontPreferences: Function = GlobalContext.getContext().getObject('getFontPreferences') as Function; const preferences: preferences.Preferences = await getFontPreferences(); - let deleteValue = preferences.delete(KEY_APP_FONT_SIZE); + let deleteValue = preferences.delete(KEY_APP_FONT_SIZE).catch((error: BusinessError) => { + Logger.error(TAG, `delete err, code: ${error.code}, mesage: ${error.message}`); + }); deleteValue.then(() => { Logger.info(TAG, 'Succeeded in deleting the key appFontSize.'); }).catch((err: Error) => {