From ab97abedf9906c474f4c4515b42206806e7260c8 Mon Sep 17 00:00:00 2001 From: xuqm Date: Thu, 10 Apr 2025 19:46:57 +0800 Subject: [PATCH] =?UTF-8?q?feat(basic):=20=E5=AE=9E=E7=8E=B0=E8=87=AA?= =?UTF-8?q?=E5=AE=9A=E4=B9=89=20AlertDialog=20=E5=B9=B6=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E4=B8=8A=E4=B8=8B=E6=96=87=E7=AE=A1=E7=90=86-=20=E6=96=B0?= =?UTF-8?q?=E5=A2=9E=E8=87=AA=E5=AE=9A=E4=B9=89=20AlertDialog=20=E5=8A=9F?= =?UTF-8?q?=E8=83=BD=EF=BC=8C=E6=8F=90=E4=BE=9B=E6=9B=B4=E7=81=B5=E6=B4=BB?= =?UTF-8?q?=E7=9A=84=E5=BC=B9=E7=AA=97=E6=A0=B7=E5=BC=8F=E5=92=8C=E4=BA=A4?= =?UTF-8?q?=E4=BA=92=20-=20=E5=AE=9E=E7=8E=B0=E5=85=A8=E5=B1=80=20UI=20?= =?UTF-8?q?=E4=B8=8A=E4=B8=8B=E6=96=87=E7=AE=A1=E7=90=86=EF=BC=8C=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E8=B7=A8=E7=BB=84=E4=BB=B6=E5=BC=B9=E7=AA=97=E6=98=BE?= =?UTF-8?q?=E7=A4=BA=20-=20=E4=BC=98=E5=8C=96=20SafeView=20=E7=BB=84?= =?UTF-8?q?=E4=BB=B6=EF=BC=8C=E8=87=AA=E5=8A=A8=E7=AE=A1=E7=90=86=20UI=20?= =?UTF-8?q?=E4=B8=8A=E4=B8=8B=E6=96=87=E7=94=9F=E5=91=BD=E5=91=A8=E6=9C=9F?= =?UTF-8?q?=20-=20=E8=B0=83=E6=95=B4=E9=BB=98=E8=AE=A4=E5=BC=B9=E7=AA=97?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=EF=BC=8C=E6=8F=90=E9=AB=98=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E4=BD=93=E9=AA=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/ets/ContextConfig.ets | 13 +++++ src/main/ets/utils/ToolsHelper.ets | 94 +++++++++++++++++++++++------- src/main/ets/view/SafeView.ets | 8 +++ 3 files changed, 94 insertions(+), 21 deletions(-) diff --git a/src/main/ets/ContextConfig.ets b/src/main/ets/ContextConfig.ets index a773a71..552d3a9 100644 --- a/src/main/ets/ContextConfig.ets +++ b/src/main/ets/ContextConfig.ets @@ -7,6 +7,7 @@ import { WindowHelper } from './utils/WindowHelper'; declare namespace globalThis { let _brushEngineContext: common.UIAbilityContext; + let _uiUIContext: UIContext[]; }; export class GlobalContext { @@ -18,5 +19,17 @@ export class GlobalContext { globalThis._brushEngineContext = context; WindowHelper.windowClass = context.windowStage.getMainWindowSync() } + 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 { + return globalThis._uiUIContext[globalThis._uiUIContext.length-1]; + } } diff --git a/src/main/ets/utils/ToolsHelper.ets b/src/main/ets/utils/ToolsHelper.ets index b7a8e53..d7b4bd3 100644 --- a/src/main/ets/utils/ToolsHelper.ets +++ b/src/main/ets/utils/ToolsHelper.ets @@ -4,7 +4,18 @@ import { buffer, HashMap, util } from '@kit.ArkTS'; import { DeviceInfo } from '../bean/DeviceInfo'; import { common } from '@kit.AbilityKit'; import { md5_hex } from '../util/md5'; -import { LogHelper } from '../../../../Index'; +import { GlobalContext, LogHelper } from '../../../../Index'; +import { ComponentContent } from '@kit.ArkUI'; + +class AlertBean { + options: AlertOptions + dialogTag: string + + constructor(options: AlertOptions, dialogTag: string) { + this.options = options + this.dialogTag = dialogTag + } +} export interface Btn { text?: string | Resource; @@ -128,6 +139,30 @@ function customDialogBuilder(option: ListOptions, dialogTag: string) { }.justifyContent(FlexAlign.Start) } +@Builder +function alertDialogBuilder(options: AlertBean) { + + Column() { + Text('---------') + .fontSize(16) + .fontColor('#00BE87') + .textAlign(TextAlign.Center) + .width('100%') + .height(60) + .backgroundColor('#F0FFFA') + .borderRadius({ + topLeft: 10, + topRight: 10 + }) + .onClick(() => { + if (ToolsHelper.mapAlertDialog.get(options.dialogTag)) { + ToolsHelper.closeAlertDialog(ToolsHelper.mapAlertDialog.get(options.dialogTag)) + ToolsHelper.mapAlertDialog.remove(options.dialogTag) + } + }) + }.justifyContent(FlexAlign.Start) +} + interface ThrottleInterface { startTime: number; //调用的时间 timeoutNumber: number; //setTimeout的句柄 @@ -187,31 +222,47 @@ export class ToolsHelper { return true } - /**kio9 + static closeAlertDialog(content: ComponentContent) { + try { + GlobalContext.getUiContext().getPromptAction().closeCustomDialog(content) + } catch (err) { + } + } + + /** * 弹出Alert弹窗 * @param options */ static showAlertDialog(options: AlertOptions) { - try { - promptAction.showDialog({ - alignment: DialogAlignment.Center, - title: options.title, - message: options.msg, - buttons: [{ - text: options.action?.text ?? "确定", - color: options.action?.color ?? "#000000", - }] - }) - .then(() => { - options.action?.onClick && options.action?.onClick() + const dialogTag = ToolsHelper.getUuid() + const ui = GlobalContext.getUiContext() + const c = new ComponentContent(ui, wrapBuilder(alertDialogBuilder), new AlertBean(options, dialogTag)) + ui.getPromptAction().openCustomDialog(c,{ + autoCancel: false + }).then(() => { + ToolsHelper.mapAlertDialog.set(dialogTag, c) + }).catch(() => { + try { + promptAction.showDialog({ + alignment: DialogAlignment.Center, + title: options.title, + message: options.msg, + buttons: [{ + text: options.action?.text ?? "确定", + color: options.action?.color ?? "#000000", + }] }) - .catch((err: Error) => { - // ToolsHelper.showMessage(err.message) - }) - } catch (error) { - let message = (error as BusinessError).message - // ToolsHelper.showMessage(message) - } + .then(() => { + options.action?.onClick && options.action?.onClick() + }) + .catch((err: Error) => { + ToolsHelper.showMessage(err.message) + }) + } catch (error) { + let message = (error as BusinessError).message + ToolsHelper.showMessage(message) + } + }) } /** @@ -252,6 +303,7 @@ export class ToolsHelper { } public static mapDialog = new HashMap() + public static mapAlertDialog = new HashMap>() /** * 弹出List弹窗 diff --git a/src/main/ets/view/SafeView.ets b/src/main/ets/view/SafeView.ets index 2705c68..80d2c70 100644 --- a/src/main/ets/view/SafeView.ets +++ b/src/main/ets/view/SafeView.ets @@ -1,5 +1,6 @@ import { WindowHelper } from '../utils/WindowHelper'; import { inputMethod } from '@kit.IMEKit'; +import { GlobalContext } from '../ContextConfig'; export interface TitleBarBtn { text?: string | Resource; @@ -47,6 +48,13 @@ export struct SafeView { // 设置导航栏底下标题 @Prop bottomTitleText: ResourceStr + aboutToAppear(): void { + GlobalContext.pushUIContext(this.getUIContext()) + } + aboutToDisappear(): void { + GlobalContext.popUIContext() + } + @Builder doNothingBuilder() { }