From d339e60740672946479dc3c64a87b8a7f08f71d6 Mon Sep 17 00:00:00 2001 From: hdw Date: Tue, 6 May 2025 18:15:26 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BA=9F=E5=BC=83api=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/ets/entryability/EntryAbility.ets | 1 + entry/src/main/ets/pages/DeletePage.ets | 6 ++-- entry/src/main/ets/pages/DetailPage.ets | 2 +- entry/src/main/ets/pages/EditPage.ets | 30 +++++++++---------- entry/src/main/ets/pages/ListPage.ets | 2 +- entry/src/main/ets/view/DeviceDialog.ets | 2 +- .../ets/viewmodel/DeletePageViewModel.ets | 6 ++-- .../ets/viewmodel/DetailPageViewModel.ets | 14 +++++---- .../main/ets/viewmodel/EditPageViewModel.ets | 8 +++-- .../main/ets/viewmodel/ListPageViewModel.ets | 7 +++-- .../src/main/ets/viewmodel/PageViewModel.ets | 3 +- 11 files changed, 45 insertions(+), 36 deletions(-) diff --git a/entry/src/main/ets/entryability/EntryAbility.ets b/entry/src/main/ets/entryability/EntryAbility.ets index 4b598cb..bcf8337 100644 --- a/entry/src/main/ets/entryability/EntryAbility.ets +++ b/entry/src/main/ets/entryability/EntryAbility.ets @@ -62,6 +62,7 @@ export default class EntryAbility extends UIAbility { } hilog.isLoggable(0x0000, TAG, hilog.LogLevel.INFO); Logger.info(TAG, `Succeeded in loading the content. Data: ${JSON.stringify(data)}`); + AppStorage.setOrCreate('uiContext', windowStage.getMainWindowSync().getUIContext()); }); } diff --git a/entry/src/main/ets/pages/DeletePage.ets b/entry/src/main/ets/pages/DeletePage.ets index f5c1578..e6c32ff 100644 --- a/entry/src/main/ets/pages/DeletePage.ets +++ b/entry/src/main/ets/pages/DeletePage.ets @@ -96,11 +96,11 @@ struct DeletePage { Logger.error(TAG, `Fail to delete Batch, code message is ${JSON.stringify(err)}`); return; } - promptAction.showToast({ + this.getUIContext().getPromptAction().showToast({ message: $r('app.string.prompt_message_deleted'), duration: CommonConstants.PROMPT_DURATION }); - router.pushUrl({ + this.getUIContext().getRouter().pushUrl({ url: CommonConstants.LIST_PAGE_URL }).catch((err: BusinessError) => { Logger.error(TAG, `pushUrl failed, code message is ${JSON.stringify(err)}`); @@ -163,7 +163,7 @@ struct DeletePage { .width($r('app.float.icon_close_size')) .margin({ right: $r('app.float.icon_close_margin_right') }) .onClick(() => { - router.back(); + this.getUIContext().getRouter().back(); }) Text($r('app.string.contacts_select')) .fontColor($r('app.color.navigation_title')) diff --git a/entry/src/main/ets/pages/DetailPage.ets b/entry/src/main/ets/pages/DetailPage.ets index 7dfa19a..ac25964 100644 --- a/entry/src/main/ets/pages/DetailPage.ets +++ b/entry/src/main/ets/pages/DetailPage.ets @@ -64,7 +64,7 @@ struct ContactsDetail { * Initialize detail page data. */ initializeData(): void { - let params = router.getParams() as Record; + let params = this.getUIContext().getRouter().getParams() as Record; // Get contact details. this.contactsDataBase.get(params.key as string, (err: BusinessError, data) => { if (err) { diff --git a/entry/src/main/ets/pages/EditPage.ets b/entry/src/main/ets/pages/EditPage.ets index 197c34e..a3929ca 100644 --- a/entry/src/main/ets/pages/EditPage.ets +++ b/entry/src/main/ets/pages/EditPage.ets @@ -31,10 +31,10 @@ struct EditPage { @State remarks: string = ''; private isEdit: boolean = false; private editPageViewModel: EditPageViewModel = new EditPageViewModel(); - private textInputContext = getContext(this) as common.UIAbilityContext; + private textInputContext = this.getUIContext().getHostContext() as common.UIAbilityContext; aboutToAppear() { - let params = router.getParams() as Record; + let params = this.getUIContext().getRouter().getParams() as Record; this.isEdit = params.isEdit as boolean; GlobalContext.getContext().setObject('FirstInTo', false); this.initializeData(); @@ -47,22 +47,22 @@ struct EditPage { * @param value Changed Data. */ contactInfoChange(key: Resource, value: string) { - let context = getContext(this) as common.UIAbilityContext; + let context = this.getUIContext().getHostContext() as common.UIAbilityContext; let contactKey = context.resourceManager.getStringSync(key); switch (contactKey) { - case getContext(this).resourceManager.getStringSync(CommonConstants.CONTACTS_DETAIL_NAME): + case context.resourceManager.getStringSync(CommonConstants.CONTACTS_DETAIL_NAME): this.name = value; break; - case getContext(this).resourceManager.getStringSync(CommonConstants.CONTACTS_DETAIL_ADDRESS): + case context.resourceManager.getStringSync(CommonConstants.CONTACTS_DETAIL_ADDRESS): this.address = value; break; - case getContext(this).resourceManager.getStringSync(CommonConstants.CONTACTS_DETAIL_TEL): + case context.resourceManager.getStringSync(CommonConstants.CONTACTS_DETAIL_TEL): this.telephony = value; break; - case getContext(this).resourceManager.getStringSync(CommonConstants.CONTACTS_DETAIL_EMAIL): + case context.resourceManager.getStringSync(CommonConstants.CONTACTS_DETAIL_EMAIL): this.email = value; break; - case getContext(this).resourceManager.getStringSync(CommonConstants.CONTACTS_DETAIL_REMARKS): + case context.resourceManager.getStringSync(CommonConstants.CONTACTS_DETAIL_REMARKS): this.remarks = value; break; default: @@ -75,8 +75,8 @@ struct EditPage { */ initializeData(): void { // Check whether the page is an editing page. - if (router.getParams() && this.isEdit) { - let params = router.getParams() as Record; + if (this.getUIContext().getRouter().getParams() && this.isEdit) { + let params = this.getUIContext().getRouter().getParams() as Record; this.name = params.name as string; this.address = params.address as string; this.telephony = params.telephony as string; @@ -188,7 +188,7 @@ struct EditPage { .backgroundColor(Color.White) .fontSize($r('app.float.item_font_size')) .fontWeight(FontWeight.Regular) - .enabled(this.textInputContext.resourceManager.getStringSync(key) === getContext(this).resourceManager.getStringSync(CommonConstants.CONTACTS_DETAIL_NAME) + .enabled(this.textInputContext.resourceManager.getStringSync(key) === this.getUIContext().getHostContext()!.resourceManager.getStringSync(CommonConstants.CONTACTS_DETAIL_NAME) && this.isEdit ? false : true) .onChange((value) => { this.contactInfoChange(key, value); @@ -212,9 +212,9 @@ struct EditPage { getInputType(key: Resource): InputType { switch (this.textInputContext.resourceManager.getStringSync(key)) { - case getContext(this).resourceManager.getStringSync(CommonConstants.CONTACTS_DETAIL_TEL): + case this.getUIContext().getHostContext()!.resourceManager.getStringSync(CommonConstants.CONTACTS_DETAIL_TEL): return InputType.PhoneNumber; - case getContext(this).resourceManager.getStringSync(CommonConstants.CONTACTS_DETAIL_EMAIL): + case this.getUIContext().getHostContext()!.resourceManager.getStringSync(CommonConstants.CONTACTS_DETAIL_EMAIL): return InputType.Email; default: return InputType.Normal; @@ -223,9 +223,9 @@ struct EditPage { getMaxLength(key: Resource): number { switch (this.textInputContext.resourceManager.getStringSync(key)) { - case getContext(this).resourceManager.getStringSync(CommonConstants.CONTACTS_DETAIL_NAME): + case this.getUIContext().getHostContext()!.resourceManager.getStringSync(CommonConstants.CONTACTS_DETAIL_NAME): return CommonConstants.CONTACTS_NAME_MAX_LENGTH; - case getContext(this).resourceManager.getStringSync(CommonConstants.CONTACTS_DETAIL_TEL): + case this.getUIContext().getHostContext()!.resourceManager.getStringSync(CommonConstants.CONTACTS_DETAIL_TEL): return CommonConstants.CONTACTS_TEL_MAX_LENGTH; default: return CommonConstants.CONTACTS_DETAIL_MAX; diff --git a/entry/src/main/ets/pages/ListPage.ets b/entry/src/main/ets/pages/ListPage.ets index e8807ba..37986b0 100644 --- a/entry/src/main/ets/pages/ListPage.ets +++ b/entry/src/main/ets/pages/ListPage.ets @@ -104,7 +104,7 @@ struct ListPage { onPageShow() { // Access the listPage page to obtain the list data. this.getAllData(); - router.clear(); + this.getUIContext().getRouter().clear(); } aboutToAppear() { diff --git a/entry/src/main/ets/view/DeviceDialog.ets b/entry/src/main/ets/view/DeviceDialog.ets index 2f1c94b..572739e 100644 --- a/entry/src/main/ets/view/DeviceDialog.ets +++ b/entry/src/main/ets/view/DeviceDialog.ets @@ -149,7 +149,7 @@ export default struct DeviceListDialogComponent { .height($r('app.float.button_line_height')) .onClick(() => { if (CommonConstants.INVALID_INDEX === this.selectedIndex) { - promptAction.showToast({ + this.getUIContext().getPromptAction().showToast({ message: $r('app.string.please_select_device') }); } else { diff --git a/entry/src/main/ets/viewmodel/DeletePageViewModel.ets b/entry/src/main/ets/viewmodel/DeletePageViewModel.ets index d7f9245..cf5ada7 100644 --- a/entry/src/main/ets/viewmodel/DeletePageViewModel.ets +++ b/entry/src/main/ets/viewmodel/DeletePageViewModel.ets @@ -16,12 +16,14 @@ import { promptAction } from '@kit.ArkUI'; import CommonConstants from '../common/constants/CommonConstants'; +const uiContext: UIContext | undefined = AppStorage.get('uiContext'); + export class DeletePageViewModel { /** * No contact to be deleted is selected. */ unCheckedContact(): void { - promptAction.showToast({ + uiContext!.getPromptAction().showToast({ message: $r('app.string.prompt_delete'), duration: CommonConstants.PROMPT_DURATION }); @@ -31,7 +33,7 @@ export class DeletePageViewModel { * Button for canceling the deletion dialog box. */ cancelDialog(): void { - promptAction.showToast({ + uiContext!.getPromptAction().showToast({ message: $r('app.string.delete_cancel_text'), duration: CommonConstants.PROMPT_DURATION }); diff --git a/entry/src/main/ets/viewmodel/DetailPageViewModel.ets b/entry/src/main/ets/viewmodel/DetailPageViewModel.ets index 47fbb2d..b9c791a 100644 --- a/entry/src/main/ets/viewmodel/DetailPageViewModel.ets +++ b/entry/src/main/ets/viewmodel/DetailPageViewModel.ets @@ -21,6 +21,8 @@ import Logger from '../common/util/Logger'; import { ContactsDataBase } from '../common/database/ContactsDataBase'; import { GlobalContext } from '../common/util/GlobalContext'; +const uiContext: UIContext | undefined = AppStorage.get('uiContext'); + export class DetailPageViewModel { contactsDataBase = GlobalContext.getContext().getObject('contactsDataBase') as ContactsDataBase; @@ -28,7 +30,7 @@ export class DetailPageViewModel { * Button for canceling the deletion dialog box. */ cancelDialog(): void { - promptAction.showToast({ + uiContext?.getPromptAction().showToast({ message: $r('app.string.delete_cancel_text'), duration: CommonConstants.PROMPT_DURATION }); @@ -42,11 +44,11 @@ export class DetailPageViewModel { try { // This interface is used to delete the information about a specified contact. this.contactsDataBase.delete(contactsKey, () => { - promptAction.showToast({ + uiContext?.getPromptAction().showToast({ message: $r('app.string.prompt_message_deleted'), duration: CommonConstants.PROMPT_DURATION }); - router.pushUrl({ + uiContext?.getRouter().pushUrl({ url: CommonConstants.LIST_PAGE_URL }).catch((err: BusinessError) => { Logger.error(`pushUrl failed, code message is ${JSON.stringify(err)}`); @@ -73,11 +75,11 @@ export class DetailPageViewModel { } } if (GlobalContext.getContext().getObject('FirstInTo')) { - router.pushUrl(routerParameter).catch((err: BusinessError) => { + uiContext?.getRouter().pushUrl(routerParameter).catch((err: BusinessError) => { Logger.error(`PushUrl failed, code message is ${JSON.stringify(err)}`); }); } else { - router.replaceUrl(routerParameter).catch((err: BusinessError) => { + uiContext?.getRouter().replaceUrl(routerParameter).catch((err: BusinessError) => { Logger.error(`ReplaceUrl failed, code message is ${JSON.stringify(err)}`); }); } @@ -87,7 +89,7 @@ export class DetailPageViewModel { * Redirect list page. */ redirectListPage(): void { - router.pushUrl({ + uiContext?.getRouter().pushUrl({ url: CommonConstants.LIST_PAGE_URL }).catch((err: BusinessError) => { Logger.error(`pushUrl failed, error message is ${JSON.stringify(err)}`); diff --git a/entry/src/main/ets/viewmodel/EditPageViewModel.ets b/entry/src/main/ets/viewmodel/EditPageViewModel.ets index d590f47..7942e91 100644 --- a/entry/src/main/ets/viewmodel/EditPageViewModel.ets +++ b/entry/src/main/ets/viewmodel/EditPageViewModel.ets @@ -22,6 +22,8 @@ import Logger from '../common/util/Logger' import { ContactsDataBase } from '../common/database/ContactsDataBase'; import { GlobalContext } from '../common/util/GlobalContext'; +const uiContext: UIContext | undefined = AppStorage.get('uiContext'); + export class EditPageViewModel { contactsDataBase = GlobalContext.getContext().getObject('contactsDataBase') as ContactsDataBase; @@ -32,7 +34,7 @@ export class EditPageViewModel { */ commonRouter(url: string, name: string): void { let contactsKey = CommonConstants.CONTACTS_DATABASE_KEY + name; - router.pushUrl({ + uiContext!.getRouter().pushUrl({ url: url, params: { key: contactsKey @@ -50,14 +52,14 @@ export class EditPageViewModel { let contactsKey = CommonConstants.CONTACTS_DATABASE_KEY + contactData.name; // Save the contact information. this.contactsDataBase.save(contactsKey, JSON.stringify(contactData)); - router.replaceUrl({ + uiContext!.getRouter().replaceUrl({ url: CommonConstants.PAGE_DETAIL_URL, params: { key: contactsKey } }).catch((err: BusinessError) => { Logger.error(`pushUrl failed, code message is ${JSON.stringify(err)}`); }); } else { - promptAction.showToast({ + uiContext!.getPromptAction().showToast({ message: $r('app.string.contact_name'), duration: CommonConstants.PROMPT_DURATION }); diff --git a/entry/src/main/ets/viewmodel/ListPageViewModel.ets b/entry/src/main/ets/viewmodel/ListPageViewModel.ets index e75e54b..dac4276 100644 --- a/entry/src/main/ets/viewmodel/ListPageViewModel.ets +++ b/entry/src/main/ets/viewmodel/ListPageViewModel.ets @@ -23,6 +23,7 @@ import { ContactsDataBase } from '../common/database/ContactsDataBase'; import { GlobalContext } from '../common/util/GlobalContext'; const TAG: string = 'ListPageViewModel'; +const uiContext: UIContext | undefined = AppStorage.get('uiContext'); export class ListPageViewModel { startAbilityCallBack: (key: string) => void = () => { @@ -31,7 +32,7 @@ export class ListPageViewModel { async startAbility(deviceId: string | undefined) { Logger.info(TAG, `startAbility deviceId: ${deviceId}`); - let context = getContext(this) as common.UIAbilityContext; + let context = uiContext!.getHostContext() as common.UIAbilityContext; let want: Want = { bundleName: CommonConstants.BUNDLE_NAME, abilityName: CommonConstants.ENTRY_ABILITY, @@ -47,7 +48,7 @@ export class ListPageViewModel { * Button for redirecting to the add page. */ redirectAddPage(): void { - router.pushUrl({ + uiContext!.getRouter().pushUrl({ url: CommonConstants.ADD_EDIT_URL, params: { isEdit: false @@ -61,7 +62,7 @@ export class ListPageViewModel { * Button for redirecting to the delete page. */ redirectDeletePage(): void { - router.pushUrl({ + uiContext!.getRouter().pushUrl({ url: CommonConstants.DELETE_PAGE_URL }).catch((err: BusinessError) => { Logger.error(`pushUrl failed, code message is ${JSON.stringify(err)}`); diff --git a/entry/src/main/ets/viewmodel/PageViewModel.ets b/entry/src/main/ets/viewmodel/PageViewModel.ets index 1470d51..d106a89 100644 --- a/entry/src/main/ets/viewmodel/PageViewModel.ets +++ b/entry/src/main/ets/viewmodel/PageViewModel.ets @@ -22,6 +22,7 @@ import CommonConstants from '../common/constants/CommonConstants'; import { ListItemData } from './ListItemData'; const TAG: string = 'PageViewModel'; +const uiContext: UIContext | undefined = AppStorage.get('uiContext'); /** * Interface for batch delete page and contacts Home page. @@ -94,7 +95,7 @@ export class PageViewModel { */ redirectDetailPage(item: ListItemData): void { let contactsKey = CommonConstants.CONTACTS_DATABASE_KEY + item.name; - router.pushUrl({ + uiContext!.getRouter().pushUrl({ url: CommonConstants.PAGE_DETAIL_URL, params: { key: contactsKey -- Gitee