From e8f0a74636c3a54688f0e4d37d0c717ea2b46470 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E5=8B=A4=E6=B0=91?= Date: Mon, 12 May 2025 12:21:16 +0800 Subject: [PATCH] =?UTF-8?q?refactor(context):=20=E9=87=8D=E6=9E=84?= =?UTF-8?q?=E4=B8=8A=E4=B8=8B=E6=96=87=E7=AE=A1=E7=90=86=E5=B9=B6=E4=BC=98?= =?UTF-8?q?=E5=8C=96=E7=9B=B8=E5=85=B3=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit -移除 AppAbility 中的冗余导入 - 调整 MiniAppManager 中的导入顺序 - 重构 GlobalContext 类,使用单个 UIContext 实例替代数组- 优化 SafeView 组件,移除不必要的生命周期方法 -改进 ToolsHelper.showMessage 方法,支持在不同上下文中显示提示信息- 调整 CertDown 页面的逻辑,优化证书下载流程 --- src/main/ets/ContextConfig.ets | 28 +++++++++++++++------------- src/main/ets/utils/ToolsHelper.ets | 18 +++++++++++++----- src/main/ets/view/SafeView.ets | 7 ------- 3 files changed, 28 insertions(+), 25 deletions(-) diff --git a/src/main/ets/ContextConfig.ets b/src/main/ets/ContextConfig.ets index 7425757..cece028 100644 --- a/src/main/ets/ContextConfig.ets +++ b/src/main/ets/ContextConfig.ets @@ -4,10 +4,11 @@ import type common from '@ohos.app.ability.common'; import { WindowHelper } from './utils/WindowHelper'; +import { BusinessError } from '@kit.BasicServicesKit'; declare namespace globalThis { let _brushEngineContext: common.UIAbilityContext; - let _uiUIContext: UIContext[]; + let _uiUIContext: UIContext; }; export class GlobalContext { @@ -17,22 +18,23 @@ export class GlobalContext { static setContext(context: common.UIAbilityContext): void { globalThis._brushEngineContext = context; - WindowHelper.windowClass = context.windowStage.getMainWindowSync() + + context.windowStage.getMainWindow((err: BusinessError, data) => { + let errCode: number = err.code; + if (errCode) { + console.error(`Failed to obtain the main window. Cause code: ${err.code}, message: ${err.message}`); + return; + } + WindowHelper.windowClass = data; + globalThis._uiUIContext = WindowHelper.windowClass.getUIContext(); + }); } - static pushUIContext(context: UIContext): void { - if (!globalThis._uiUIContext) { - globalThis._uiUIContext = [] - } - globalThis._uiUIContext.push(context); - } - static popUIContext(): void { - globalThis._uiUIContext.splice(globalThis._uiUIContext.length-1,1) - } - static getUiContext(): UIContext|undefined { + + static getUiContext(): UIContext | undefined { if (globalThis._uiUIContext === undefined) { return undefined; } - return globalThis._uiUIContext[globalThis._uiUIContext.length-1]; + return globalThis._uiUIContext; } } diff --git a/src/main/ets/utils/ToolsHelper.ets b/src/main/ets/utils/ToolsHelper.ets index c8c1586..9b118cf 100644 --- a/src/main/ets/utils/ToolsHelper.ets +++ b/src/main/ets/utils/ToolsHelper.ets @@ -261,11 +261,19 @@ export class ToolsHelper { * @param msg */ static showMessage(msg: string) { - console.info(msg); - promptAction.showToast({ - message: msg, - duration: 1500 - }); + + const pa = GlobalContext.getUiContext()?.getPromptAction() + if (pa) { + pa.showToast({ + message: msg, + duration: 1500 + }); + } else { + promptAction.showToast({ + message: msg, + duration: 1500 + }); + } } diff --git a/src/main/ets/view/SafeView.ets b/src/main/ets/view/SafeView.ets index 2596e40..db845a5 100644 --- a/src/main/ets/view/SafeView.ets +++ b/src/main/ets/view/SafeView.ets @@ -48,13 +48,6 @@ export struct SafeView { // 设置导航栏底下标题 @Prop bottomTitleText: ResourceStr - aboutToAppear(): void { - GlobalContext.pushUIContext(this.getUIContext()) - } - aboutToDisappear(): void { - GlobalContext.popUIContext() - } - @Builder doNothingBuilder() { }