FriendView.cs 11 KB

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