MailMailPanelCtrl.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /// #pkgName FGUI包名
  2. /// #panelName UIPanel名字
  3. /// #UIName = $"{#pkgName}{#panelName}" UIKey名字
  4. /// 该脚本由模板创建
  5. /// created by cb 2024
  6. using FairyGUI;
  7. using FL.Data;
  8. using FL.Data.Items;
  9. using FL.Network;
  10. using System.Collections.Generic;
  11. using XGame.Database;
  12. using XGame.Framework.UI;
  13. namespace FL.FGUI
  14. {
  15. public struct IMailRwdItem
  16. {
  17. public IItemBase Item;
  18. public bool IsGet;
  19. }
  20. /// <summary>
  21. /// UI逻辑处理类
  22. /// </summary>
  23. /// <typeparam name=""></typeparam>
  24. public partial class MailMailPanelCtrl : UIController<MailMailPanelVM>
  25. {
  26. private List<MailInfo> _mailList = new List<MailInfo>();
  27. private List<IMailRwdItem> _mailRwdList;
  28. private bool _isOpenMail;// 是否打开单封邮件
  29. private string _mailId;// 当前打开的邮件id
  30. protected override void OnEnable(object intent)
  31. {
  32. AddUIListenres();
  33. Init();
  34. _isOpenMail = false;
  35. ShowUI();
  36. }
  37. protected override void OnDisable()
  38. {
  39. RemoveUIListenres();
  40. }
  41. #region UI事件
  42. private void AddUIListenres()
  43. {
  44. VM.AllReadBtn.onClick.Add(OnClickAllReadBtn);
  45. VM.DeleteReadBtn.onClick.Add(OnClickDeleteReadBtn);
  46. VM.BackMainBtn.onClick.Add(OnClickBackMainBtn);
  47. VM.AllGetBtn.onClick.Add(OnClickAllGetBtn);
  48. VM.ReadBtn.onClick.Add(OnClickReadBtn);
  49. VM.GetBtn.onClick.Add(OnClickGetBtn);
  50. VM.DeleteBtn.onClick.Add(OnClickDeleteBtn);
  51. Context.AddListener(EventDefine.UpMailList, UpMailList);
  52. VM.MailList.ItemClickEvent += OnClcikMailListItem;
  53. }
  54. private void RemoveUIListenres()
  55. {
  56. VM.AllReadBtn.onClick.Remove(OnClickAllReadBtn);
  57. VM.DeleteReadBtn.onClick.Remove(OnClickDeleteReadBtn);
  58. VM.BackMainBtn.onClick.Remove(OnClickBackMainBtn);
  59. VM.AllGetBtn.onClick.Remove(OnClickAllGetBtn);
  60. VM.ReadBtn.onClick.Remove(OnClickReadBtn);
  61. VM.GetBtn.onClick.Remove(OnClickGetBtn);
  62. VM.DeleteBtn.onClick.Remove(OnClickDeleteBtn);
  63. Context.RemoveListener(EventDefine.UpMailList, UpMailList);
  64. VM.MailList.ItemClickEvent -= OnClcikMailListItem;
  65. }
  66. private void Init()
  67. {
  68. if (_mailList == null) _mailList = new List<MailInfo>();
  69. if (_mailRwdList == null) _mailRwdList = new List<IMailRwdItem>();
  70. }
  71. private void ShowUI()
  72. {
  73. bool canGet = false;
  74. bool canRead = false;
  75. _mailList.Clear();
  76. MailInfo mailInfo = null;
  77. foreach (var mail in MailData.Instance.mailInfo)
  78. {
  79. if (mail.Value.rts <= 0)
  80. {
  81. if (mail.Value.items.Count > 0)
  82. {
  83. canGet = true;
  84. }
  85. else
  86. {
  87. canRead = true;
  88. }
  89. }
  90. if (_isOpenMail && _mailId == mail.Value.id)
  91. {
  92. mailInfo = mail.Value;
  93. }
  94. long nowTime = Context.Time.GetNowTime() / 1000;
  95. if (mail.Value.ets >= nowTime)
  96. {
  97. _mailList.Add(mail.Value);
  98. }
  99. }
  100. VM.AllReadBtn.visible = !_isOpenMail && canRead;// 一键已读
  101. VM.DeleteReadBtn.visible = !_isOpenMail && !canRead;// 删除已读
  102. VM.AllGetBtn.visible = !_isOpenMail;// 全部领取
  103. VM.AllGetBtn.enabled = canGet;// 全部领取
  104. VM.MailDetailsGrp.visible = _isOpenMail && mailInfo != null;// 是否打开单封邮件
  105. VM.MailList.visible = !VM.MailDetailsGrp.visible;// 邮件列表隐藏
  106. if (_isOpenMail && mailInfo != null)
  107. {
  108. VM.BackMainBtn.visible = _isOpenMail;// 返回邮件列表
  109. bool hasItem = mailInfo.items.Count > 0;
  110. bool singleCanGet = mailInfo.rts <= 0 && hasItem;
  111. bool singleCanRead = mailInfo.rts <= 0 && !hasItem;
  112. VM.ReadBtn.visible = _isOpenMail && singleCanRead;// 已读
  113. VM.GetBtn.visible = _isOpenMail && singleCanGet;// 领取
  114. VM.DeleteBtn.visible = _isOpenMail && !singleCanGet && !singleCanRead;// 删除
  115. VM.TitleLabel.text = mailInfo.title;
  116. VM.TimeLabel.text = mailInfo.ets.ToTimeYMDHM();
  117. VM.ContentLabel.text = mailInfo.content;
  118. VM.ItemGrp.visible = hasItem;
  119. if (hasItem)
  120. {
  121. _mailRwdList.Clear();
  122. for (int i = 0; i < mailInfo.items.Count; i++)
  123. {
  124. IMailRwdItem mailRwdItem = new IMailRwdItem()
  125. {
  126. Item = ItemService.Instance.GetItemBase(mailInfo.items[i].id, mailInfo.items[i].count, mailInfo.items[i].kind),
  127. IsGet = mailInfo.rts > 0,
  128. };
  129. _mailRwdList.Add(mailRwdItem);
  130. }
  131. ShowItemsUI(_mailRwdList);
  132. }
  133. }
  134. SetMailList(_mailList);
  135. }
  136. // 打开单封邮件
  137. private void OpenSingleMail(string mailId)
  138. {
  139. _isOpenMail = true;
  140. _mailId = mailId;
  141. ShowUI();
  142. }
  143. //刷新邮件列表
  144. private void UpMailList(int eventId, object args)
  145. {
  146. ShowUI();
  147. }
  148. //刷新邮件列表
  149. private void SetMailList(List<MailInfo> dataList)
  150. {
  151. VM.MailList.BindDatas(dataList);
  152. UpMailCount(dataList.Count);
  153. VM.EmptyLabel.visible = dataList.Count == 0;
  154. }
  155. // 邮件数量
  156. private void UpMailCount(int num)
  157. {
  158. VM.MailCountLabel.text = string.Format(StringDefine.MailCountLabel, num, KeyValue.mailMax);
  159. }
  160. //单封邮件内的奖励
  161. private void ShowItemsUI(List<IMailRwdItem> dataList)
  162. {
  163. VM.ItemList.BindDatas(dataList);
  164. }
  165. //点击邮件列表
  166. private void OnClcikMailListItem(int index)
  167. {
  168. string mailId = _mailList[index].id;
  169. OpenSingleMail(mailId);
  170. }
  171. //一键已读
  172. private void OnClickAllReadBtn(EventContext context)
  173. {
  174. MailService.Instance.RequestMailReadAll();
  175. }
  176. //删除已读
  177. private void OnClickDeleteReadBtn(EventContext context)
  178. {
  179. MailService.Instance.RequestMailDelAll();
  180. }
  181. //返回
  182. private void OnClickBackMainBtn(EventContext context)
  183. {
  184. _isOpenMail = false;
  185. ShowUI();
  186. }
  187. //一键领取
  188. private void OnClickAllGetBtn(EventContext context)
  189. {
  190. MailService.Instance.RequestMailRwdAll();
  191. }
  192. //已读
  193. private void OnClickReadBtn(EventContext context)
  194. {
  195. MailService.Instance.RequestMailRwd(_mailId);
  196. }
  197. //领取
  198. private void OnClickGetBtn(EventContext context)
  199. {
  200. MailService.Instance.RequestMailRwd(_mailId);
  201. }
  202. //删除
  203. private void OnClickDeleteBtn(EventContext context)
  204. {
  205. _isOpenMail = false;
  206. VM.MailDetailsGrp.visible = false;
  207. VM.MailList.visible = true;
  208. MailService.Instance.RequestMailDel(_mailId);
  209. }
  210. #endregion
  211. }
  212. }