From 02bf3f68d505ecd6a457018d5289470e688f3a40 Mon Sep 17 00:00:00 2001 From: WX1377357 Date: Thu, 5 Jun 2025 11:03:43 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=BA=9F=E5=BC=83api=E6=9B=B4=E6=96=B0?= =?UTF-8?q?=E4=B8=ADuiContext=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- entry/src/main/ets/entryability/EntryAbility.ets | 1 - entry/src/main/ets/pages/DeletePage.ets | 4 ++-- entry/src/main/ets/pages/DetailPage.ets | 9 +++++---- entry/src/main/ets/pages/EditPage.ets | 5 +++-- entry/src/main/ets/pages/ListPage.ets | 8 ++++---- entry/src/main/ets/view/ListAreaComponent.ets | 2 +- entry/src/main/ets/viewmodel/DeletePageViewModel.ets | 5 ++--- entry/src/main/ets/viewmodel/DetailPageViewModel.ets | 9 ++++----- entry/src/main/ets/viewmodel/EditPageViewModel.ets | 5 ++--- entry/src/main/ets/viewmodel/ListPageViewModel.ets | 11 +++++------ entry/src/main/ets/viewmodel/PageViewModel.ets | 3 +-- 11 files changed, 29 insertions(+), 33 deletions(-) diff --git a/entry/src/main/ets/entryability/EntryAbility.ets b/entry/src/main/ets/entryability/EntryAbility.ets index bcf8337..4b598cb 100644 --- a/entry/src/main/ets/entryability/EntryAbility.ets +++ b/entry/src/main/ets/entryability/EntryAbility.ets @@ -62,7 +62,6 @@ 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 e6c32ff..6e7ea8a 100644 --- a/entry/src/main/ets/pages/DeletePage.ets +++ b/entry/src/main/ets/pages/DeletePage.ets @@ -42,7 +42,7 @@ struct DeletePage { dialogController: CustomDialogController = new CustomDialogController({ builder: DeleteDialog({ cancel: () => { - this.deletePageViewModel.cancelDialog(); + this.deletePageViewModel.cancelDialog(this.getUIContext()); }, confirm: () => { this.batchDeleteButton(); @@ -141,7 +141,7 @@ struct DeletePage { } }, rightClickEvent: () => { - this.count === 0 ? this.deletePageViewModel.unCheckedContact() : this.dialogController.open(); + this.count === 0 ? this.deletePageViewModel.unCheckedContact(this.getUIContext()) : this.dialogController.open(); }, pageId: CommonConstants.DELETE_PAGE_ID, leftIcon: $r('app.media.ic_public_select'), diff --git a/entry/src/main/ets/pages/DetailPage.ets b/entry/src/main/ets/pages/DetailPage.ets index ac25964..7c3be94 100644 --- a/entry/src/main/ets/pages/DetailPage.ets +++ b/entry/src/main/ets/pages/DetailPage.ets @@ -40,10 +40,10 @@ struct ContactsDetail { dialogController: CustomDialogController = new CustomDialogController({ builder: DeleteDialog({ cancel: () => { - this.detailPageViewModel.cancelDialog(); + this.detailPageViewModel.cancelDialog(this.getUIContext()); }, confirm: () => { - this.detailPageViewModel.deleteContactButton(this.name); + this.detailPageViewModel.deleteContactButton(this.name, this.getUIContext()); }, promptMessage: $r('app.string.delete_dialog_text') }), @@ -98,7 +98,8 @@ struct ContactsDetail { this.address, this.telephony, this.email, - this.remarks + this.remarks, + this.getUIContext() ); }, rightClickEvent: () => { @@ -124,7 +125,7 @@ struct ContactsDetail { .width($r('app.float.edit_icon_size')) .height($r('app.float.edit_icon_size')) .onClick(() => { - this.detailPageViewModel.redirectListPage(); + this.detailPageViewModel.redirectListPage(this.getUIContext()); }) } .layoutWeight(CommonConstants.WEIGHT) diff --git a/entry/src/main/ets/pages/EditPage.ets b/entry/src/main/ets/pages/EditPage.ets index a3929ca..2a5cb88 100644 --- a/entry/src/main/ets/pages/EditPage.ets +++ b/entry/src/main/ets/pages/EditPage.ets @@ -137,7 +137,7 @@ struct EditPage { .margin({ right: $r('app.float.close_icon_margin') }) .onClick(() => { let url = this.isEdit ? CommonConstants.PAGE_DETAIL_URL : CommonConstants.LIST_PAGE_URL; - this.editPageViewModel.commonRouter(url, this.name); + this.editPageViewModel.commonRouter(url, this.name, this.getUIContext()); }) Text(this.isEdit ? $r('app.string.edit_title') : $r('app.string.add_contacts_text')) .width(CommonConstants.PERCENTAGE_MAX) @@ -157,7 +157,8 @@ struct EditPage { .justifyContent(FlexAlign.End) .onClick(() => { this.editPageViewModel.saveInfo( - new ContactData(this.name, this.address, this.telephony, this.email, this.remarks) + new ContactData(this.name, this.address, this.telephony, this.email, this.remarks), + this.getUIContext() ); }) } diff --git a/entry/src/main/ets/pages/ListPage.ets b/entry/src/main/ets/pages/ListPage.ets index 37986b0..71645f6 100644 --- a/entry/src/main/ets/pages/ListPage.ets +++ b/entry/src/main/ets/pages/ListPage.ets @@ -67,7 +67,7 @@ struct ListPage { if (this.selectedIndex !== undefined && (this.remoteDeviceModel === null || this.remoteDeviceModel.discoverList.length <= 0)) { Logger.info(TAG, `continue unauthed device: ${JSON.stringify(this.deviceList)}`); - this.listPageViewModel.startAbility(this.deviceList[this.selectedIndex].networkId); + this.listPageViewModel.startAbility(this.deviceList[this.selectedIndex].networkId,this.getUIContext()); this.clearSelectState(); return; } @@ -80,7 +80,7 @@ struct ListPage { && this.selectedIndex !== undefined) { for (let i = 0; i < this.remoteDeviceModel.deviceList!.length; i++) { if (this.remoteDeviceModel.deviceList![i].deviceName === this.deviceList[this.selectedIndex].deviceName) { - this.listPageViewModel.startAbility(this.remoteDeviceModel.deviceList![i].networkId); + this.listPageViewModel.startAbility(this.remoteDeviceModel.deviceList![i].networkId,this.getUIContext()); } } } @@ -208,7 +208,7 @@ struct ListPage { .height($r('app.float.empty_view_size')) .margin({ right: $r('app.float.image_spacing_margin') }) .onClick(() => { - this.listPageViewModel.redirectAddPage(); + this.listPageViewModel.redirectAddPage(this.getUIContext()); }) Image($r('app.media.ic_more')) .width($r('app.float.empty_view_size')) @@ -330,7 +330,7 @@ struct ListPage { left: $r('app.float.delete_padding_left') }) .onClick(() => { - this.listPageViewModel.redirectDeletePage(); + this.listPageViewModel.redirectDeletePage(this.getUIContext()); }) Divider() .height($r('app.float.divider_height')) diff --git a/entry/src/main/ets/view/ListAreaComponent.ets b/entry/src/main/ets/view/ListAreaComponent.ets index 36ca96c..62b8030 100644 --- a/entry/src/main/ets/view/ListAreaComponent.ets +++ b/entry/src/main/ets/view/ListAreaComponent.ets @@ -30,7 +30,7 @@ export struct ListAreaComponent { ListItemComponent({ itemInfo: item }) } .onClick(() => { - PageViewModel.redirectDetailPage(item); + PageViewModel.redirectDetailPage(item, this.getUIContext()); }) }, (item: ListItemData) => JSON.stringify(item)) } diff --git a/entry/src/main/ets/viewmodel/DeletePageViewModel.ets b/entry/src/main/ets/viewmodel/DeletePageViewModel.ets index cf5ada7..0a7e1bf 100644 --- a/entry/src/main/ets/viewmodel/DeletePageViewModel.ets +++ b/entry/src/main/ets/viewmodel/DeletePageViewModel.ets @@ -16,13 +16,12 @@ 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 { + unCheckedContact(uiContext:UIContext): void { uiContext!.getPromptAction().showToast({ message: $r('app.string.prompt_delete'), duration: CommonConstants.PROMPT_DURATION @@ -32,7 +31,7 @@ export class DeletePageViewModel { /** * Button for canceling the deletion dialog box. */ - cancelDialog(): void { + cancelDialog(uiContext:UIContext): void { 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 b9c791a..6f54a73 100644 --- a/entry/src/main/ets/viewmodel/DetailPageViewModel.ets +++ b/entry/src/main/ets/viewmodel/DetailPageViewModel.ets @@ -21,7 +21,6 @@ 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; @@ -29,7 +28,7 @@ export class DetailPageViewModel { /** * Button for canceling the deletion dialog box. */ - cancelDialog(): void { + cancelDialog(uiContext:UIContext): void { uiContext?.getPromptAction().showToast({ message: $r('app.string.delete_cancel_text'), duration: CommonConstants.PROMPT_DURATION @@ -39,7 +38,7 @@ export class DetailPageViewModel { /** * Delete contact button. */ - deleteContactButton(name: string): void { + deleteContactButton(name: string, uiContext:UIContext): void { let contactsKey = CommonConstants.CONTACTS_DATABASE_KEY + name; try { // This interface is used to delete the information about a specified contact. @@ -62,7 +61,7 @@ export class DetailPageViewModel { /** * Button for redirecting to the details page. */ - redirectEditPage(name: string, address: string, telephony: string, email: string, remarks: string): void { + redirectEditPage(name: string, address: string, telephony: string, email: string, remarks: string, uiContext:UIContext): void { let routerParameter: router.RouterOptions = { url: CommonConstants.ADD_EDIT_URL, params: { @@ -88,7 +87,7 @@ export class DetailPageViewModel { /** * Redirect list page. */ - redirectListPage(): void { + redirectListPage(uiContext:UIContext): void { uiContext?.getRouter().pushUrl({ url: CommonConstants.LIST_PAGE_URL }).catch((err: BusinessError) => { diff --git a/entry/src/main/ets/viewmodel/EditPageViewModel.ets b/entry/src/main/ets/viewmodel/EditPageViewModel.ets index 7942e91..099ce58 100644 --- a/entry/src/main/ets/viewmodel/EditPageViewModel.ets +++ b/entry/src/main/ets/viewmodel/EditPageViewModel.ets @@ -22,7 +22,6 @@ 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 +31,7 @@ export class EditPageViewModel { * * @param url Indicates the route address. */ - commonRouter(url: string, name: string): void { + commonRouter(url: string, name: string, uiContext:UIContext): void { let contactsKey = CommonConstants.CONTACTS_DATABASE_KEY + name; uiContext!.getRouter().pushUrl({ url: url, @@ -47,7 +46,7 @@ export class EditPageViewModel { /** * Save contact information. */ - saveInfo(contactData: ContactData): void { + saveInfo(contactData: ContactData, uiContext:UIContext): void { if (contactData.name !== '') { let contactsKey = CommonConstants.CONTACTS_DATABASE_KEY + contactData.name; // Save the contact information. diff --git a/entry/src/main/ets/viewmodel/ListPageViewModel.ets b/entry/src/main/ets/viewmodel/ListPageViewModel.ets index dac4276..be22131 100644 --- a/entry/src/main/ets/viewmodel/ListPageViewModel.ets +++ b/entry/src/main/ets/viewmodel/ListPageViewModel.ets @@ -23,16 +23,15 @@ 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 = () => { }; contactsDataBase = GlobalContext.getContext().getObject('contactsDataBase') as ContactsDataBase; - async startAbility(deviceId: string | undefined) { + async startAbility(deviceId: string | undefined, uiContext:UIContext) { Logger.info(TAG, `startAbility deviceId: ${deviceId}`); - let context = uiContext!.getHostContext() as common.UIAbilityContext; + let context = uiContext.getHostContext() as common.UIAbilityContext; let want: Want = { bundleName: CommonConstants.BUNDLE_NAME, abilityName: CommonConstants.ENTRY_ABILITY, @@ -47,8 +46,8 @@ export class ListPageViewModel { /** * Button for redirecting to the add page. */ - redirectAddPage(): void { - uiContext!.getRouter().pushUrl({ + redirectAddPage(uiContext:UIContext): void { + uiContext.getRouter().pushUrl({ url: CommonConstants.ADD_EDIT_URL, params: { isEdit: false @@ -61,7 +60,7 @@ export class ListPageViewModel { /** * Button for redirecting to the delete page. */ - redirectDeletePage(): void { + redirectDeletePage(uiContext:UIContext): void { uiContext!.getRouter().pushUrl({ url: CommonConstants.DELETE_PAGE_URL }).catch((err: BusinessError) => { diff --git a/entry/src/main/ets/viewmodel/PageViewModel.ets b/entry/src/main/ets/viewmodel/PageViewModel.ets index d106a89..2733e6c 100644 --- a/entry/src/main/ets/viewmodel/PageViewModel.ets +++ b/entry/src/main/ets/viewmodel/PageViewModel.ets @@ -22,7 +22,6 @@ 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. @@ -93,7 +92,7 @@ export class PageViewModel { * * @param item List Information. */ - redirectDetailPage(item: ListItemData): void { + redirectDetailPage(item: ListItemData, uiContext:UIContext): void { let contactsKey = CommonConstants.CONTACTS_DATABASE_KEY + item.name; uiContext!.getRouter().pushUrl({ url: CommonConstants.PAGE_DETAIL_URL, -- Gitee