Mail.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import { AppKernel } from '@scripts/app/AppKernel';
  2. import { UIViewID } from '@scripts/define/UIDefine';
  3. import { UIView } from '@scripts/framework/ui/UIView';
  4. import { addBtnClick } from '@scripts/utils/UIHelper';
  5. import { _decorator, Component, instantiate, Node, Prefab, NodePool } from 'cc';
  6. const { ccclass, property } = _decorator;
  7. import { UIPopup } from '@scripts/framework/ui/UIPopup';
  8. import { MailItem } from './MailItem';
  9. import { UICloseReason } from '@scripts/framework/ui/UILayer';
  10. @ccclass('Mail')
  11. export class Mail extends UIPopup {
  12. readonly viewId: UIViewID = UIViewID.Mail;
  13. @property(Node)
  14. private btn_close: Node = null;
  15. @property(Node)
  16. private btn_deleteAll: Node = null;
  17. @property(Node)
  18. private btn_claimAll: Node = null;
  19. @property(Prefab)
  20. private mail_item: Prefab = null;
  21. @property(Node)
  22. private mail_content_layout: Node = null;
  23. private _mailList: any[] = [];
  24. private _mailItemPool: NodePool = new NodePool();
  25. private _usedItems: Node[] = [];
  26. protected async onBeforeOpen(_params: unknown): Promise<boolean> {
  27. const mailStore = AppKernel.stores.mailStore;
  28. const res = await mailStore.reqMailList();
  29. return res;
  30. }
  31. // protected onOpen(_params: unknown): void {
  32. // this.updateMailItems();
  33. // }
  34. protected onCreate(): void | Promise<void> {
  35. AppKernel.events.on('mail:update', this.updateMailItems, this);
  36. addBtnClick(this.btn_close, this.onBtnCloseClick, this);
  37. addBtnClick(this.btn_deleteAll, this.onBtnDeleteAllClick, this);
  38. addBtnClick(this.btn_claimAll, this.onBtnClaimAllClick, this);
  39. }
  40. protected onBeforeClose(_reason: UICloseReason): boolean | Promise<boolean> {
  41. AppKernel.events.off('mail:update', this.updateMailItems, this);
  42. return true;
  43. }
  44. private updateMailItems() {
  45. console.log('updateMailItems');
  46. this._mailList = AppKernel.stores.mailStore.mailList;
  47. const newMailList = this._mailList ?? [];
  48. const layout = this.mail_content_layout;
  49. if (!layout) return;
  50. // 将当前正在使用的item全部回收进对象池
  51. for (const node of this._usedItems) {
  52. this._mailItemPool.put(node);
  53. }
  54. this._usedItems.length = 0;
  55. for (const mailData of newMailList) {
  56. let itemNode: Node;
  57. if (this._mailItemPool.size() > 0) {
  58. itemNode = this._mailItemPool.get();
  59. } else {
  60. itemNode = instantiate(this.mail_item);
  61. }
  62. itemNode.active = true;
  63. itemNode.parent = layout;
  64. const mailItemCmp = itemNode.getComponent(MailItem);
  65. if (mailItemCmp) {
  66. mailItemCmp.initItem(mailData);
  67. }
  68. this._usedItems.push(itemNode);
  69. }
  70. }
  71. // 弹窗关闭,回收节点
  72. protected async onAfterClose() {
  73. for (const node of this._usedItems) {
  74. this._mailItemPool.put(node);
  75. }
  76. this._usedItems.length = 0;
  77. }
  78. // 弹窗销毁,清空池子释放资源
  79. protected onDestroy(): void {
  80. this._mailItemPool.clear();
  81. }
  82. private onBtnCloseClick() {
  83. AppKernel.ui.close(UIViewID.Mail);
  84. }
  85. private async onBtnDeleteAllClick() {
  86. const { mailStore } = AppKernel.stores;
  87. AppKernel.ui.dialog({
  88. message: "Delete all selected mails? Cannot be undone.",
  89. callBack: async (res) => {
  90. if (res) {
  91. await mailStore.reqMailDelete(0);
  92. mailStore.reqMailList();
  93. console.log("delete all", res);
  94. }
  95. return true;
  96. },
  97. showClose: true,
  98. })
  99. }
  100. private async onBtnClaimAllClick() {
  101. const { mailStore } = AppKernel.stores;
  102. AppKernel.ui.dialog({
  103. message: "Claim all mail rewards?",
  104. callBack: async (res) => {
  105. if (res) {
  106. await mailStore.reqMailTake(0);
  107. mailStore.reqMailList();
  108. console.log("claim all", res);
  109. }
  110. return true;
  111. },
  112. showClose: true,
  113. })
  114. }
  115. }