import { AppKernel } from '@scripts/app/AppKernel'; import { UIViewID } from '@scripts/define/UIDefine'; import { UIView } from '@scripts/framework/ui/UIView'; import { addBtnClick } from '@scripts/utils/UIHelper'; import { _decorator, Component, instantiate, Node, Prefab, NodePool } from 'cc'; const { ccclass, property } = _decorator; import { UIPopup } from '@scripts/framework/ui/UIPopup'; import { MailItem } from './MailItem'; import { UICloseReason } from '@scripts/framework/ui/UILayer'; @ccclass('Mail') export class Mail extends UIPopup { readonly viewId: UIViewID = UIViewID.Mail; @property(Node) private btn_close: Node = null; @property(Node) private btn_deleteAll: Node = null; @property(Node) private btn_claimAll: Node = null; @property(Prefab) private mail_item: Prefab = null; @property(Node) private mail_content_layout: Node = null; private _mailList: any[] = []; private _mailItemPool: NodePool = new NodePool(); private _usedItems: Node[] = []; protected async onBeforeOpen(_params: unknown): Promise { const mailStore = AppKernel.stores.mailStore; const res = await mailStore.reqMailList(); return res; } // protected onOpen(_params: unknown): void { // this.updateMailItems(); // } protected onCreate(): void | Promise { AppKernel.events.on('mail:update', this.updateMailItems, this); addBtnClick(this.btn_close, this.onBtnCloseClick, this); addBtnClick(this.btn_deleteAll, this.onBtnDeleteAllClick, this); addBtnClick(this.btn_claimAll, this.onBtnClaimAllClick, this); } protected onDestroy(): void { AppKernel.events.off('mail:update', this.updateMailItems, this); this._mailItemPool.clear(); super.onDestroy(); } private updateMailItems() { console.log('updateMailItems'); this._mailList = AppKernel.stores.mailStore.mailList; const newMailList = this._mailList ?? []; const layout = this.mail_content_layout; if (!layout) return; // 将当前正在使用的item全部回收进对象池 for (const node of this._usedItems) { this._mailItemPool.put(node); } this._usedItems.length = 0; for (const mailData of newMailList) { let itemNode: Node; if (this._mailItemPool.size() > 0) { itemNode = this._mailItemPool.get(); } else { itemNode = instantiate(this.mail_item); } itemNode.active = true; itemNode.parent = layout; const mailItemCmp = itemNode.getComponent(MailItem); if (mailItemCmp) { mailItemCmp.initItem(mailData); } this._usedItems.push(itemNode); } } // 弹窗关闭,回收节点 protected async onAfterClose() { for (const node of this._usedItems) { this._mailItemPool.put(node); } this._usedItems.length = 0; } private onBtnCloseClick() { AppKernel.ui.close(UIViewID.Mail); } private async onBtnDeleteAllClick() { const { mailStore } = AppKernel.stores; AppKernel.ui.dialog({ message: "Delete all selected mails? Cannot be undone.", callBack: async (res) => { if (res) { await mailStore.reqMailDelete(0); mailStore.reqMailList(); console.log("delete all", res); } return true; }, showClose: true, }) } private async onBtnClaimAllClick() { const { mailStore } = AppKernel.stores; AppKernel.ui.dialog({ message: "Claim all mail rewards?", callBack: async (res) => { if (res) { await mailStore.reqMailTake(0); mailStore.reqMailList(); console.log("claim all", res); } return true; }, showClose: true, }) } }