| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- 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<boolean> {
- const mailStore = AppKernel.stores.mailStore;
- const res = await mailStore.reqMailList();
- return res;
- }
- // protected onOpen(_params: unknown): void {
- // this.updateMailItems();
- // }
- protected onCreate(): void | Promise<void> {
- 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 onBeforeClose(_reason: UICloseReason): boolean | Promise<boolean> {
- AppKernel.events.off('mail:update', this.updateMailItems, this);
- return true;
- }
- 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;
- }
- // 弹窗销毁,清空池子释放资源
- protected onDestroy(): void {
- this._mailItemPool.clear();
- }
- 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,
- })
- }
- }
|