| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- 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<T> implements BasicType<T> {
- src?: ResourceStr
- acSrc?: ResourceStr
- text?: string
- index?: number
- number?: number | undefined
- message?: string
- click?: () => void
- constructor(item: BasicType<undefined>) {
- 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 declare ctx: UIContext;
- private declare contentNode: ComponentContent<Object> | null;
- private declare options: promptAction.BaseDialogOptions;
- private maskColor: string = '#80000000'
- 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<Object>) {
- this.contentNode = node;
- }
- setOptions(options: promptAction.BaseDialogOptions) {
- this.options = options;
- }
- openToast(builder: WrappedBuilder<[
- BasicType<undefined>
- ]>, item: BasicType<undefined>) {
- this.contentNode =
- new ComponentContent(this.ctx, builder, new Params<undefined>(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<undefined>) {
- this.openToast(wrapBuilder(yTDoubleConfirm), item)
- }
- agreePrivacy(item: BasicType<undefined>) {
- 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}`);
- })
- }
- }
- }
- export const yTToast = new YTToast()
|