import { AppKernel } from '@scripts/app/AppKernel'; import { UIViewID } from '@scripts/define/UIDefine'; import { addBtnClick } from '@scripts/utils/UIHelper'; import { _decorator, Node } from 'cc'; const { ccclass, property } = _decorator; import { UIPopup } from '@scripts/framework/ui/UIPopup'; import { MailItem } from './MailItem'; import VirtualList from '@scripts/components/virtualList/VirtualList'; @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(VirtualList) private scrollView: VirtualList = null; private _store: any = {}; protected async onBeforeOpen(_params: unknown): Promise { const res = await this._store.reqMailList(); return res; } protected onCreate(): void | Promise { this._store = AppKernel.stores.mailStore; 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 onOpen(): void | Promise { const list = this._store.mailList ?? []; this.scrollView.numItems = list.length; } onRenderMailItem(item: Node, index: number) { const list = this._store.mailList ?? []; const data = list[index]; const mailItem = item.getComponent(MailItem); mailItem.initItem(data); item.active = true; } private updateMailItems() { console.log('updateMailItems'); this.scrollView.numItems = this._store.mailList.length; if (this.scrollView.checkInited(false)) this.scrollView.updateAll(); } private onBtnCloseClick() { AppKernel.ui.close(UIViewID.Mail); } private async onBtnDeleteAllClick() { AppKernel.ui.dialog({ message: "Delete all selected mails? Cannot be undone.", callBack: async (res) => { if (res) { await this._store.reqMailDelete(0); this._store.reqMailList(); console.log("delete all", res); } return true; }, showClose: true, }) } private async onBtnClaimAllClick() { AppKernel.ui.dialog({ message: "Claim all mail rewards?", callBack: async (res) => { if (res) { await this._store.reqMailTake(0); this._store.reqMailList(); console.log("claim all", res); } return true; }, showClose: true, }) } }