Mail.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 onDestroy(): void {
  41. AppKernel.events.off('mail:update', this.updateMailItems, this);
  42. this._mailItemPool.clear();
  43. super.onDestroy();
  44. }
  45. private updateMailItems() {
  46. console.log('updateMailItems');
  47. this._mailList = AppKernel.stores.mailStore.mailList;
  48. const newMailList = this._mailList ?? [];
  49. const layout = this.mail_content_layout;
  50. if (!layout) return;
  51. // 将当前正在使用的item全部回收进对象池
  52. for (const node of this._usedItems) {
  53. this._mailItemPool.put(node);
  54. }
  55. this._usedItems.length = 0;
  56. for (const mailData of newMailList) {
  57. let itemNode: Node;
  58. if (this._mailItemPool.size() > 0) {
  59. itemNode = this._mailItemPool.get();
  60. } else {
  61. itemNode = instantiate(this.mail_item);
  62. }
  63. itemNode.active = true;
  64. itemNode.parent = layout;
  65. const mailItemCmp = itemNode.getComponent(MailItem);
  66. if (mailItemCmp) {
  67. mailItemCmp.initItem(mailData);
  68. }
  69. this._usedItems.push(itemNode);
  70. }
  71. }
  72. // 弹窗关闭,回收节点
  73. protected async onAfterClose() {
  74. for (const node of this._usedItems) {
  75. this._mailItemPool.put(node);
  76. }
  77. this._usedItems.length = 0;
  78. }
  79. private onBtnCloseClick() {
  80. AppKernel.ui.close(UIViewID.Mail);
  81. }
  82. private async onBtnDeleteAllClick() {
  83. const { mailStore } = AppKernel.stores;
  84. AppKernel.ui.dialog({
  85. message: "Delete all selected mails? Cannot be undone.",
  86. callBack: async (res) => {
  87. if (res) {
  88. await mailStore.reqMailDelete(0);
  89. mailStore.reqMailList();
  90. console.log("delete all", res);
  91. }
  92. return true;
  93. },
  94. showClose: true,
  95. })
  96. }
  97. private async onBtnClaimAllClick() {
  98. const { mailStore } = AppKernel.stores;
  99. AppKernel.ui.dialog({
  100. message: "Claim all mail rewards?",
  101. callBack: async (res) => {
  102. if (res) {
  103. await mailStore.reqMailTake(0);
  104. mailStore.reqMailList();
  105. console.log("claim all", res);
  106. }
  107. return true;
  108. },
  109. showClose: true,
  110. })
  111. }
  112. }