Mail.ts 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import { AppKernel } from '@scripts/app/AppKernel';
  2. import { UIViewID } from '@scripts/define/UIDefine';
  3. import { addBtnClick } from '@scripts/utils/UIHelper';
  4. import { _decorator, Node } from 'cc';
  5. const { ccclass, property } = _decorator;
  6. import { UIPopup } from '@scripts/framework/ui/UIPopup';
  7. import { MailItem } from './MailItem';
  8. import VirtualList from '@scripts/components/virtualList/VirtualList';
  9. @ccclass('Mail')
  10. export class Mail extends UIPopup {
  11. readonly viewId: UIViewID = UIViewID.Mail;
  12. @property(Node)
  13. private btn_close: Node = null;
  14. @property(Node)
  15. private btn_deleteAll: Node = null;
  16. @property(Node)
  17. private btn_claimAll: Node = null;
  18. @property(VirtualList)
  19. private scrollView: VirtualList = null;
  20. private _store: any = {};
  21. protected async onBeforeOpen(_params: unknown): Promise<boolean> {
  22. const res = await this._store.reqMailList();
  23. return res;
  24. }
  25. protected onCreate(): void | Promise<void> {
  26. this._store = AppKernel.stores.mailStore;
  27. AppKernel.events.on('mail:update', this.updateMailItems, this);
  28. addBtnClick(this.btn_close, this.onBtnCloseClick, this);
  29. addBtnClick(this.btn_deleteAll, this.onBtnDeleteAllClick, this);
  30. addBtnClick(this.btn_claimAll, this.onBtnClaimAllClick, this);
  31. }
  32. protected onOpen(): void | Promise<void> {
  33. const list = this._store.mailList ?? [];
  34. this.scrollView.numItems = list.length;
  35. }
  36. onRenderMailItem(item: Node, index: number) {
  37. const list = this._store.mailList ?? [];
  38. const data = list[index];
  39. const mailItem = item.getComponent(MailItem);
  40. mailItem.initItem(data);
  41. item.active = true;
  42. }
  43. private updateMailItems() {
  44. console.log('updateMailItems');
  45. this.scrollView.numItems = this._store.mailList.length;
  46. if (this.scrollView.checkInited(false)) this.scrollView.updateAll();
  47. }
  48. private onBtnCloseClick() {
  49. AppKernel.ui.close(UIViewID.Mail);
  50. }
  51. private async onBtnDeleteAllClick() {
  52. AppKernel.ui.dialog({
  53. message: "Delete all selected mails? Cannot be undone.",
  54. callBack: async (res) => {
  55. if (res) {
  56. await this._store.reqMailDelete(0);
  57. this._store.reqMailList();
  58. console.log("delete all", res);
  59. }
  60. return true;
  61. },
  62. showClose: true,
  63. })
  64. }
  65. private async onBtnClaimAllClick() {
  66. AppKernel.ui.dialog({
  67. message: "Claim all mail rewards?",
  68. callBack: async (res) => {
  69. if (res) {
  70. await this._store.reqMailTake(0);
  71. this._store.reqMailList();
  72. console.log("claim all", res);
  73. }
  74. return true;
  75. },
  76. showClose: true,
  77. })
  78. }
  79. }