FriendView.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. using UnityEngine;
  2. using FairyGUI;
  3. using UI.Friend;
  4. namespace GFGGame
  5. {
  6. public class FriendView : BaseView
  7. {
  8. private UI_FriendUI _ui;
  9. private GameObject _scenePrefab;
  10. private GameObject _sceneObject;
  11. private long _friendRoleId;
  12. public override void Dispose()
  13. {
  14. if (_sceneObject != null)
  15. {
  16. GameObject.Destroy(_sceneObject);
  17. _sceneObject = null;
  18. }
  19. if (_ui != null)
  20. {
  21. _ui.Dispose();
  22. _ui = null;
  23. }
  24. base.Dispose();
  25. }
  26. protected override void Init()
  27. {
  28. base.Init();
  29. packageName = UI_FriendUI.PACKAGE_NAME;
  30. _ui = UI_FriendUI.Create();
  31. viewCom = _ui.target;
  32. isfullScreen = true;
  33. _scenePrefab = GFGAsset.Load<GameObject>(ResPathUtil.GetPrefabPath("SceneFriend"));
  34. _ui.m_list.SetVirtual();
  35. _ui.m_list.itemRenderer = RenderListItem;
  36. _ui.m_list.onClickItem.Add(OnListItemClick);
  37. _ui.m_btnInfo.onClick.Add(OnBtnInfoClick);
  38. _ui.m_btnDelete.onClick.Add(OnBtnDeleteClick);
  39. _ui.m_btnAdd.onClick.Add(OnBtnAddClick);
  40. _ui.m_btnSendAll.onClick.Add(OnBtnSendAllClick);
  41. }
  42. protected override void OnInit()
  43. {
  44. base.OnInit();
  45. _ui.m_btnBack.onClick.Add(OnHide);
  46. }
  47. protected override void AddEventListener()
  48. {
  49. base.AddEventListener();
  50. EventAgent.AddEventListener(ConstMessage.FRIEND_REFRESH, RefreshView);
  51. EventAgent.AddEventListener(ConstMessage.FRIEND_REMOVE, RefreshRemoveFriend);
  52. EventAgent.AddEventListener(ConstMessage.FRIEND_ADD, RefreshAddFriend);
  53. EventAgent.AddEventListener(ConstMessage.RED_CHANGE, UpdateRedDot);
  54. }
  55. protected override void OnShown()
  56. {
  57. base.OnShown();
  58. _friendRoleId = this.viewData == null ? 0 : (long)this.viewData;
  59. if (_sceneObject == null)
  60. {
  61. CustomSuitData customSuit = CustomSuitDataManager.GetSuitList(CustomSuitDataManager.currentIndex);
  62. _sceneObject = GameObject.Instantiate(_scenePrefab);
  63. EquipDataCache.cacher.setSceneObj(_sceneObject);
  64. EquipDataCache.cacher.AddOrRemove(customSuit.bg, true);
  65. }
  66. FriendDataManager.Instance.UpdateFriendList(true);
  67. _ui.m_list.numItems = FriendDataManager.Instance.FriendDatas.Count;
  68. if (_ui.m_list.numItems > 0)
  69. {
  70. if (_friendRoleId > 0)
  71. {
  72. for (int i = 0; i < FriendDataManager.Instance.FriendDatas.Count; i++)
  73. {
  74. if (_friendRoleId == FriendDataManager.Instance.FriendDatas[i].roleInfo.roleId)
  75. {
  76. _ui.m_list.selectedIndex = i;
  77. _ui.m_list.ScrollToView(i);
  78. ReqFriendDetialInfo(i);
  79. }
  80. }
  81. }
  82. else
  83. {
  84. _ui.m_list.selectedIndex = 0;
  85. ReqFriendDetialInfo(0);
  86. }
  87. }
  88. else
  89. {
  90. ReqFriendDetialInfo(-1);
  91. }
  92. RefreshView();
  93. UpdateRedDot();
  94. }
  95. protected override void OnHide()
  96. {
  97. base.OnHide();
  98. if (_sceneObject != null)
  99. {
  100. GameObject.Destroy(_sceneObject);
  101. _sceneObject = null;
  102. }
  103. _ui.m_list.numItems = 0;
  104. ViewManager.GoBackFrom(typeof(FriendView).FullName);
  105. }
  106. protected override void RemoveEventListener()
  107. {
  108. base.RemoveEventListener();
  109. EventAgent.RemoveEventListener(ConstMessage.FRIEND_REFRESH, RefreshView);
  110. EventAgent.RemoveEventListener(ConstMessage.FRIEND_REMOVE, RefreshRemoveFriend);
  111. EventAgent.RemoveEventListener(ConstMessage.FRIEND_ADD, RefreshAddFriend);
  112. EventAgent.RemoveEventListener(ConstMessage.RED_CHANGE, UpdateRedDot);
  113. }
  114. private void RefreshView()
  115. {
  116. _ui.m_list.RefreshVirtualList();
  117. _ui.m_txtCount.text = string.Format("好友数:{0}/{1}", _ui.m_list.numItems, GlobalCfgArray.globalCfg.maxFriendCount);
  118. _ui.m_btnSendAll.text = RedDotDataManager.Instance.GetFriendGiftRed() ? "一键领取并赠送" : "一键赠送";
  119. }
  120. private void RenderListItem(int index, GObject obj)
  121. {
  122. FriendInfoData friendInfo = FriendDataManager.Instance.FriendDatas[index];
  123. UI_ListItem item = UI_ListItem.Proxy(obj);
  124. item.m_txtName.text = friendInfo.roleInfo.roleName;
  125. item.m_txtLvl.text = friendInfo.roleInfo.roleLv.ToString();
  126. item.m_c2.selectedIndex = friendInfo.roleInfo.offlineTimeSec == 0 ? 0 : 1;
  127. item.m_c1.selectedIndex = FriendDataManager.Instance.GetGiftState(friendInfo.roleInfo.roleId);
  128. if (item.m_btnSend.data == null)
  129. {
  130. item.m_btnSend.onClick.Add(OnClickBtnSend);
  131. }
  132. item.m_btnSend.data = friendInfo;
  133. item.target.data = index;
  134. UI_ListItem.ProxyEnd();
  135. }
  136. private void OnClickBtnSend(EventContext context)
  137. {
  138. GObject item = context.sender as GObject;
  139. FriendInfoData friendInfo = item.data as FriendInfoData;
  140. if (friendInfo.takeGiftState == ConstBonusStatus.CAN_GET)
  141. {
  142. // if (FriendDataManager.Instance.Count >= GlobalCfgArray.globalCfg.maxGetPowerCount)
  143. // {
  144. // PromptController.Instance.ShowFloatTextPrompt("今日体力已全部领取");
  145. // return;
  146. // }
  147. FriendSProxy.ReqTakeGiftFromFriend(friendInfo.roleInfo.roleId).Coroutine();
  148. }
  149. else if (friendInfo.giveGiftState == (int)ConstGiveGiftStatus.CanGave)
  150. {
  151. FriendSProxy.ReqGiveGiftToFriend(friendInfo.roleInfo.roleId).Coroutine();
  152. }
  153. }
  154. private void OnListItemClick(EventContext context)
  155. {
  156. GObject item = context.data as GObject;
  157. int index = (int)item.data;
  158. ReqFriendDetialInfo(index);
  159. }
  160. private void OnBtnSendAllClick()
  161. {
  162. if (RedDotDataManager.Instance.GetFriendGiftRed() && FriendDataManager.Instance.Count < GlobalCfgArray.globalCfg.maxGetPowerCount)
  163. {
  164. FriendSProxy.ReqTakeGiftFromAllFriend().Coroutine();
  165. }
  166. else
  167. {
  168. FriendSProxy.ReqGiveGiftToAllFriend().Coroutine();
  169. }
  170. }
  171. private void OnBtnDeleteClick()
  172. {
  173. if (_ui.m_list.numItems == 0)
  174. {
  175. PromptController.Instance.ShowFloatTextPrompt("暂无好友可删除");
  176. return;
  177. }
  178. AlertUI.Show("是否删除好友?").SetLeftButton(true).SetRightButton(true, "确定", (object data) =>
  179. {
  180. int index = _ui.m_list.selectedIndex;
  181. long roleId = FriendDataManager.Instance.FriendDatas[index].roleInfo.roleId;
  182. FriendSProxy.ReqDeleteFriend(roleId).Coroutine();
  183. });
  184. }
  185. private void RefreshRemoveFriend()
  186. {
  187. int index = _ui.m_list.selectedIndex;
  188. _ui.m_list.numItems = FriendDataManager.Instance.FriendDatas.Count;
  189. if (FriendDataManager.Instance.FriendDatas.Count > 0)
  190. {
  191. if (index >= FriendDataManager.Instance.FriendDatas.Count)
  192. {
  193. index = FriendDataManager.Instance.FriendDatas.Count - 1;//选中最后一个
  194. }
  195. _ui.m_list.selectedIndex = index;
  196. }
  197. else
  198. {
  199. index = -1;//无好友
  200. }
  201. ReqFriendDetialInfo(index);
  202. _ui.m_txtCount.text = string.Format("好友数:{0}/{1}", _ui.m_list.numItems, GlobalCfgArray.globalCfg.maxFriendCount);
  203. }
  204. private void RefreshAddFriend()
  205. {
  206. int selectedIndex = 0;
  207. if (_ui.m_list.numItems > 0)
  208. {
  209. int childIndex = _ui.m_list.ItemIndexToChildIndex(_ui.m_list.selectedIndex);
  210. GButton item = _ui.m_list.GetChildAt(childIndex).asButton.GetChild("btnSend").asButton;
  211. FriendInfoData friendInfo = item.data as FriendInfoData;
  212. selectedIndex = FriendDataManager.Instance.FriendDatas.IndexOf(friendInfo);
  213. }
  214. _ui.m_list.numItems = FriendDataManager.Instance.FriendDatas.Count;
  215. _ui.m_list.selectedIndex = selectedIndex;
  216. ReqFriendDetialInfo(selectedIndex);
  217. _ui.m_txtCount.text = string.Format("好友数:{0}/{1}", _ui.m_list.numItems, GlobalCfgArray.globalCfg.maxFriendCount);
  218. }
  219. private async void ReqFriendDetialInfo(int index)
  220. {
  221. if (index >= 0)
  222. {
  223. _friendRoleId = FriendDataManager.Instance.FriendDatas[index].roleInfo.roleId;
  224. OtherRoleInfoDetailData roleInfoDetail = await RoleInfoSProxy.ReqOtherRoleDetailInfo(_friendRoleId);
  225. if (roleInfoDetail != null)
  226. {
  227. UpdateScene(roleInfoDetail.customSuitData);
  228. }
  229. }
  230. else
  231. {
  232. CustomSuitData customSuit = CustomSuitDataManager.GetSuitList(CustomSuitDataManager.currentIndex);
  233. UpdateScene(customSuit);
  234. }
  235. }
  236. private void UpdateScene(CustomSuitData suitSavedData)
  237. {
  238. if (suitSavedData != null)
  239. {
  240. EquipDataCache.cacher.PutOnSuitMemory(suitSavedData);
  241. _ui.m_txtName.text = string.Format("我的套装{0}", NumberUtil.GetChiniseNumberText(suitSavedData.pos + 1));
  242. }
  243. else
  244. {
  245. EquipDataCache.cacher.PutOnDefaultSuitSaved();
  246. _ui.m_txtName.text = string.Format("我的套装{0}", NumberUtil.GetChiniseNumberText(1));
  247. }
  248. }
  249. private void OnBtnInfoClick()
  250. {
  251. if (_friendRoleId == 0)
  252. {
  253. ViewManager.Show<RoleInfoView>(null, new object[] { typeof(FriendView).FullName, _friendRoleId });
  254. }
  255. else
  256. {
  257. FriendInfoData friendInfoData = FriendDataManager.Instance.GetFriendDataById(_friendRoleId);
  258. ViewManager.Show<OtherRoleInfoView>(new object[] { friendInfoData.roleInfo }, new object[] { typeof(FriendView).FullName, _friendRoleId });
  259. }
  260. }
  261. private void OnBtnAddClick()
  262. {
  263. ViewManager.Show<FriendAddView>();
  264. }
  265. private void UpdateRedDot()
  266. {
  267. RedDotController.Instance.SetComRedDot(_ui.m_btnAdd, RedDotDataManager.Instance.GetFriendApplyRed());
  268. RedDotController.Instance.SetComRedDot(_ui.m_btnSendAll, RedDotDataManager.Instance.GetFriendGiftRed());
  269. RefreshView();
  270. }
  271. }
  272. }