import { BusinessError } from '@kit.BasicServicesKit'; import { ComponentContent, promptAction } from '@kit.ArkUI'; import { UIContext } from '@ohos.arkui.UIContext'; import { BasicType, YTLog } from '../../../../Index'; import { yTDoubleConfirm } from '../components/YtDoubleConfirm'; import { AgreePrivacy } from '../components/AgreePrivacy'; interface InitOption { context: UIContext; options: promptAction.BaseDialogOptions; } class Params implements BasicType { src?: ResourceStr acSrc?: ResourceStr text?: string index?: number number?: number | undefined message?: string click?: () => void constructor(item: BasicType) { this.src = item.src; this.text = item.text; this.index = item.index; this.number = item.number; this.click = item.click; this.message = item.message; this.acSrc = item.acSrc; } } export class YTToast { private static declare instance: YTToast private declare ctx: UIContext; private declare contentNode: ComponentContent | null; private declare options: promptAction.BaseDialogOptions; private maskColor: string = '#80000000' static getInstance() { if (!YTToast.instance) { YTToast.instance = new YTToast() } return YTToast.instance } init(initOption: InitOption) { this.ctx = initOption.context; initOption.options.maskColor = this.maskColor this.options = initOption.options; } setContext(context: UIContext) { this.ctx = context; } setContentNode(node: ComponentContent) { this.contentNode = node; } setOptions(options: promptAction.BaseDialogOptions) { this.options = options; } openToast(builder: WrappedBuilder<[ BasicType ]>, item: BasicType) { this.contentNode = new ComponentContent(this.ctx, builder, new Params(item)); this.ctx.getPromptAction() .openCustomDialog(this.contentNode, this.options) .then(() => { console.info('OpenCustomDialog complete.') }) .catch((error: BusinessError) => { let message = (error as BusinessError).message; let code = (error as BusinessError).code; console.error(`OpenCustomDialog args error code is ${code}, message is ${message}`); }) } doubleConfirm(item: BasicType) { this.openToast(wrapBuilder(yTDoubleConfirm), item) } agreePrivacy(item: BasicType) { this.openToast(wrapBuilder(AgreePrivacy), item) } hide() { try { if (this.contentNode !== null) { this.ctx.getPromptAction() .closeCustomDialog(this.contentNode) .then(() => { this.contentNode = null console.info('CloseCustomDialog complete.') }) .catch((error: BusinessError) => { let message = (error as BusinessError).message; let code = (error as BusinessError).code; console.error(`CloseCustomDialog args error code is ${code}, message is ${message}`); }) } } catch (e) { YTLog.warn(e) } } updateDialog(options: promptAction.BaseDialogOptions) { if (this.contentNode !== null) { this.ctx.getPromptAction() .updateCustomDialog(this.contentNode, options) .then(() => { console.info('UpdateCustomDialog complete.') }) .catch((error: BusinessError) => { let message = (error as BusinessError).message; let code = (error as BusinessError).code; console.error(`UpdateCustomDialog args error code is ${code}, message is ${message}`); }) } } }