背景
隨著應(yīng)用軟件功能的不斷增加,應(yīng)用程序軟件首頁(yè)成為彈窗的重災(zāi)區(qū),不僅有升級(jí)彈窗,還有積分彈窗,簽到,引導(dǎo)等各種彈窗。為了徹底解彈窗問題,本文將使用設(shè)計(jì)模式解決這個(gè)痛點(diǎn)。
設(shè)計(jì)模式
本方案采用責(zé)任鏈設(shè)計(jì)模式和建造者設(shè)計(jì)模式,通過將不同的彈窗添加到彈窗處理類,然后按顯示順序。
實(shí)現(xiàn)方案
- 先定義基礎(chǔ)彈窗接口 DialogIntercept,統(tǒng)一彈窗的行為。intercept() 方法用于執(zhí)行下一個(gè)彈窗。show ()方法用于判斷當(dāng)前彈窗是否顯示,這里同時(shí)支持異步接口請(qǐng)求返回的判斷。
export interface DialogIntercept{
intercept(dialogChain:DialogChain):void
show():boolean |Promise< boolean >
}
- 提供一個(gè)彈窗處理類DialogChain,用于處理多個(gè)彈窗的執(zhí)行邏輯,將彈窗依次添加到 chainList 中保存,然后執(zhí)行proceed()方法開始顯示彈窗,同時(shí)提供一個(gè)proceedNext() 方法用于直接跳過當(dāng)前彈窗,由于代碼較多,此處省略了部分代碼。
/**
* 處理彈窗執(zhí)行
*/
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
}
/**
* 不執(zhí)行當(dāng)前彈窗,可以直接跳過
*/
proceedNext() {
++this.index
this.proceed()
}
/**
* 調(diào)用繼續(xù)執(zhí)行下一步
*/
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
}
}
}
- 自定義彈窗實(shí)現(xiàn)DialogIntercept 接口,通過show()方法的返回值決定當(dāng)前彈窗是否彈出,如簽到彈窗肯定是每天彈出,可以根據(jù)條件 直接返還 true。或者是禮物彈窗,當(dāng)接口查詢到還有是否有未領(lǐng)取的禮物來(lái)決定彈窗的是否彈出。這里簡(jiǎn)單測(cè)試一下。
- 分別定義彈窗 DialogA,DialogB,DialogC,實(shí)現(xiàn)接口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萬(wàn)積分,請(qǐng)及時(shí)領(lǐng)取!",confirmBtnContent:"領(lǐng)取",cancelBtnContent:"取消" })
}
export class DialogParams {
callBack = () = > {
}
}
- 將三個(gè)彈窗添加到彈窗管理類,然后依次執(zhí)行彈窗。
private dialogChain = new DialogChain()
this.dialogChain
.addIntercept(new DialogA(this.getUIContext()))
.addIntercept(new DialogB(this.getUIContext()))
.addIntercept(new DialogC(this.getUIContext()))
//開始執(zhí)行彈窗
this.dialogChain.proceed()
- 實(shí)現(xiàn)效果如下:

審核編輯 黃宇
-
HarmonyOS
+關(guān)注
關(guān)注
80文章
2144瀏覽量
35258
發(fā)布評(píng)論請(qǐng)先 登錄
QCon·上海站HarmonyOS開發(fā)者技術(shù)分論壇:共探鴻蒙開發(fā)新機(jī)遇
HarmonyOSAI編程編譯報(bào)錯(cuò)智能分析
分享---超聲波焊接機(jī)設(shè)備實(shí)現(xiàn)告 \"警彈窗\" 效果的簡(jiǎn)單方法
直流電機(jī)EMC整改:從干擾源到解決方案的實(shí)戰(zhàn)指南
鴻蒙非侵入式彈窗新解法,企查查正式開源“QuickDialog”彈窗組件庫(kù)
HarmonyOS AI輔助編程工具(CodeGenie)報(bào)錯(cuò)分析
《仿盒馬》app開發(fā)技術(shù)分享-- 分類模塊頂部導(dǎo)航列表彈窗(16)
鴻蒙5開發(fā)寶藏案例分享---體驗(yàn)流暢的首頁(yè)信息流
HarmonyOS5云服務(wù)技術(shù)分享--賬號(hào)登錄文章整理
如何在KaihongOS操作系統(tǒng)上寫一個(gè)彈窗組件
DialogHub上線OpenHarmony開源社區(qū),高效開發(fā)鴻蒙應(yīng)用彈窗
軟通動(dòng)力亮相華為HarmonyOS Connect伙伴峰會(huì)
鴻蒙原生頁(yè)面高性能解決方案上線OpenHarmony社區(qū) 助力打造高性能原生應(yīng)用
HarmonyOS開發(fā)指導(dǎo)類文檔更新速遞(上)
ShiMeta鴻蒙多屏同步拼接解決方案

HarmonyOS實(shí)戰(zhàn):首頁(yè)多彈窗順序彈出終極解決方案
評(píng)論