HarmonyOS实战:首页多弹窗顺序弹出终极解决方案

背景

随着应用软件功能的不断增加,应用程序软件首页成为弹窗的重灾区,不仅有升级弹窗,还有积分弹窗,签到,引导等各种弹窗。为了彻底解弹窗问题,本文将使用设计模式解决这个痛点。

设计模式

本方案采用责任链设计模式和建造者设计模式,通过将不同的弹窗添加到弹窗处理类,然后按显示顺序。

实现方案

  1. 先定义基础弹窗接口 DialogIntercept,统一弹窗的行为。intercept() 方法用于执行下一个弹窗。show ()方法用于判断当前弹窗是否显示,这里同时支持异步接口请求返回的判断。
export interface DialogIntercept{
    
  intercept(dialogChain:DialogChain):void

  show():boolean |Promise<boolean>
}

2. 提供一个弹窗处理类DialogChain,用于处理多个弹窗的执行逻辑,将弹窗依次添加到 chainList 中保存,然后执行proceed()方法开始显示弹窗,同时提供一个proceedNext() 方法用于直接跳过当前弹窗,由于代码较多,此处省略了部分代码。

/**
 * 处理弹窗执行
 */
export class DialogChain {
  private index: number = 0
  private chainList: ArrayList<DialogIntercept> = new ArrayList()

  addIntercept(dialogIntercept: DialogIntercept): DialogChain {
    if (!this.chainList.has(dialogIntercept)) {
      this.chainList.add(dialogIntercept)
    }
    return this
  }

  /**
   * 不执行当前弹窗,可以直接跳过
   */
  proceedNext() {
    ++this.index
    this.proceed()
  }

  /**
   * 调用继续执行下一步
   */
  proceed() {
    if (this.index >= 0 && this.index < this.chainList.length) {
      let dialogIntercept = this.chainList[this.index]
       let show = dialogIntercept.show()
       if (typeof show === 'boolean' && show) { {
        ..........
      } else if (show instanceof Promise) {
       ...........
      } else {
       ........
      }

    } else {
      this.index = 0

    }
  }
}

3. 自定义弹窗实现DialogIntercept 接口,通过show()方法的返回值决定当前弹窗是否弹出,如签到弹窗肯定是每天弹出,可以根据条件 直接返还 true。或者是礼物弹窗,当接口查询到还有是否有未领取的礼物来决定弹窗的是否弹出。这里简单测试一下。 4. 分别定义弹窗 DialogA,DialogB,DialogC,实现接口DialogIntercept。

export class DialogA implements DialogIntercept {
  uiContext: UIContext
  contentNode?: ComponentContent<DialogParams>
  promptAction?: PromptAction

  constructor(uiContext: UIContext) {
    this.uiContext = uiContext;
    this.promptAction = this.uiContext.getPromptAction();
  }

  intercept(dialogChain: DialogChain): void {

    let params = new DialogParams()
    params.callBack = () => {
      this.promptAction?.closeCustomDialog(this.contentNode)
      dialogChain.proceed()
    }
    // UI展示的Node
    this.contentNode = new ComponentContent(this.uiContext, wrapBuilder(DialogABuild), params);

    // 打开弹窗
    this.promptAction?.openCustomDialog(
      this.contentNode,
      {
        isModal: true,
        autoCancel: true,
        alignment: DialogAlignment.Center
      }
    )

  }

  show(): boolean | Promise<boolean> {
    return true
  }
}

@Builder
function DialogABuild(params: DialogParams) {
  // 封装后的UI
  DialogView({ eventModel: params,content:"恭喜您,获得300万积分,请及时领取!",confirmBtnContent:"领取",cancelBtnContent:"取消" })
}

export class DialogParams {
  callBack = () => {
  }
}

5. 将三个弹窗添加到弹窗管理类,然后依次执行弹窗。

private dialogChain = new DialogChain()

 this.dialogChain
      .addIntercept(new DialogA(this.getUIContext()))
      .addIntercept(new DialogB(this.getUIContext()))
      .addIntercept(new DialogC(this.getUIContext()))
 //开始执行弹窗
 this.dialogChain.proceed()

6. 实现效果如下:

阅读全文
下载说明:
1、本站所有资源均从互联网上收集整理而来,仅供学习交流之用,因此不包含技术服务请大家谅解!
2、本站不提供任何实质性的付费和支付资源,所有需要积分下载的资源均为网站运营赞助费用或者线下劳务费用!
3、本站所有资源仅用于学习及研究使用,您必须在下载后的24小时内删除所下载资源,切勿用于商业用途,否则由此引发的法律纠纷及连带责任本站和发布者概不承担!
4、本站站内提供的所有可下载资源,本站保证未做任何负面改动(不包含修复bug和完善功能等正面优化或二次开发),但本站不保证资源的准确性、安全性和完整性,用户下载后自行斟酌,我们以交流学习为目的,并不是所有的源码都100%无错或无bug!如有链接无法下载、失效或广告,请联系客服处理!
5、本站资源除标明原创外均来自网络整理,版权归原作者或本站特约原创作者所有,如侵犯到您的合法权益,请立即告知本站,本站将及时予与删除并致以最深的歉意!
6、如果您也有好的资源或教程,您可以投稿发布,成功分享后有站币奖励和额外收入!
7、如果您喜欢该资源,请支持官方正版资源,以得到更好的正版服务!
8、请您认真阅读上述内容,注册本站用户或下载本站资源即您同意上述内容!
原文链接:https://www.1024c.cn/archives/22007,转载请注明出处。
0

评论0

显示验证码
没有账号?注册  忘记密码?