FriendView.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. using Newtonsoft.Json;
  7. using Newtonsoft.Json.Linq;
  8. /* 界面-好友 */
  9. public class FriendView : JCUnityLib.ViewBase, MenuBackInterface
  10. {
  11. [SerializeField] GameObject myFriendBox;
  12. [SerializeField] GameObject friendRequestBox;
  13. [SerializeField] GameObject friendRecommendBox;
  14. [SerializeField] GameObject searchPlayerBox;
  15. void Awake()
  16. {
  17. InitBtnTabs();
  18. InitMyFriendBox();
  19. InitFriendRequestBox();
  20. InitFriendRecommendBox();
  21. InitSearchPlayerBox();
  22. }
  23. [NonSerialized] public int firstBtnTabsIndex = 0;
  24. public override void OnShowView(object[] args)
  25. {
  26. if (args.Length == 2 && (string)args[0] == "setFirstBtnTabsIndex")
  27. firstBtnTabsIndex = (int)args[1];
  28. }
  29. void Start()
  30. {
  31. PersistenHandler.ins?.menuBackCtr.views.Add(this);
  32. if (UserPlayer.ins != null) {
  33. UserPlayer.ins.tempData.onUpdate += onUserPlayerTempDataUpdate;
  34. onUserPlayerTempDataUpdate();
  35. }
  36. SetBtnTabSelected(btnTabs[firstBtnTabsIndex]);
  37. }
  38. void OnDestroy() {
  39. PersistenHandler.ins?.menuBackCtr.views.Remove(this);
  40. if (UserPlayer.ins != null) {
  41. UserPlayer.ins.tempData.onUpdate -= onUserPlayerTempDataUpdate;
  42. }
  43. if (_hasDeleteAnyFriends || _hasAcceptAnyFriends) {
  44. HomeView.ins?.RefreshFriendBarAccordingCurrentBar();
  45. }
  46. }
  47. public bool OnMenuBack() {
  48. ViewMgr.Instance.DestroyView<FriendView>();
  49. return true;
  50. }
  51. void onUserPlayerTempDataUpdate() {
  52. try {
  53. var data = UserPlayer.ins.tempData;
  54. btnTabs[1].transform.Find("FriendTip").gameObject.SetActive(data.hasFriendRequest);
  55. } catch (System.Exception e) {
  56. Debug.LogError(e.Message);
  57. Debug.LogError(e.StackTrace);
  58. }
  59. }
  60. #region myFriend
  61. void InitMyFriendBox() {
  62. myFriendBox.SetActive(false);
  63. GetMyFriendItemPrefab().SetActive(false);
  64. }
  65. Transform GetMyFriendItemParent() {
  66. return myFriendBox.transform.Find("ScrollView/Viewport/Content");
  67. }
  68. GameObject GetMyFriendItemPrefab() {
  69. return GetMyFriendItemParent().Find("Item").gameObject;
  70. }
  71. //是否删除过好友,该记录可帮助提醒homeView的好友栏刷新
  72. bool _hasDeleteAnyFriends = false;
  73. void EnterMyFriendBox() {
  74. myFriendBox.SetActive(true);
  75. for (int i = 1; i < GetMyFriendItemParent().childCount; i++) {
  76. Destroy(GetMyFriendItemParent().GetChild(i).gameObject);
  77. }
  78. Action<JArray> cb = delegate(JArray list) {
  79. if (list.Count > 0) {
  80. foreach (var itemInfo in list) {
  81. int friendID = itemInfo.Value<int>("friendID");
  82. int avatarID = itemInfo.Value<int>("avatarID");
  83. string avatarUrl = itemInfo.Value<string>("avatarUrl");
  84. string nickname = itemInfo.Value<string>("nickname");
  85. bool online = itemInfo.Value<bool>("online");
  86. long offlineTime = itemInfo.Value<long>("offlineTime");
  87. GameObject o = GameObject.Instantiate(GetMyFriendItemPrefab(), GetMyFriendItemParent());
  88. o.SetActive(true);
  89. o.name = friendID.ToString();
  90. RoleMgr.SetAvatarToImage(
  91. o.transform.Find("Avatar/Sprite").GetComponent<Image>(),
  92. avatarID, avatarUrl);
  93. o.transform.Find("Name").GetComponent<Text>().text = nickname;
  94. o.transform.Find("BG0").gameObject.SetActive(!online);
  95. o.transform.Find("BG1").gameObject.SetActive(online);
  96. Text onlineTip = o.transform.Find("OnlineTip").GetComponent<Text>();
  97. onlineTip.text = TimeUtil.GetOfflineTimeStr(offlineTime, online);
  98. onlineTip.color = online ? Color.green : Color.gray;
  99. Button btnDelete = o.transform.Find("BtnDelete").GetComponent<Button>();
  100. btnDelete.onClick.RemoveAllListeners();
  101. btnDelete.onClick.AddListener(delegate() {
  102. AudioMgr.ins.PlayBtn();
  103. Action<bool> cb = delegate(bool res) {
  104. if (res) {
  105. _hasDeleteAnyFriends = true;
  106. Destroy(o);
  107. }
  108. };
  109. UserPlayer.ins.call("friendComp.deleteMyFriend", new object[]{friendID}, cb);
  110. });
  111. }
  112. } else {
  113. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("好友列表为空"));
  114. }
  115. HomeView.ins?.RenderFriendList(list);
  116. };
  117. UserPlayer.ins.call("friendComp.getMyFriends", null, cb);
  118. PopupMgr.ins.ClearAllTip();
  119. }
  120. #endregion
  121. #region friendRequest
  122. void InitFriendRequestBox() {
  123. friendRequestBox.SetActive(false);
  124. GetFriendRequestItemPrefab().SetActive(false);
  125. }
  126. Transform GetFriendRequestItemParent() {
  127. return friendRequestBox.transform.Find("ScrollView/Viewport/Content");
  128. }
  129. GameObject GetFriendRequestItemPrefab() {
  130. return GetFriendRequestItemParent().Find("Item").gameObject;
  131. }
  132. //是否同意过好友添加请求,该记录可帮助提醒homeView的好友栏刷新
  133. bool _hasAcceptAnyFriends = false;
  134. void EnterFriendRequestBox() {
  135. friendRequestBox.SetActive(true);
  136. for (int i = 1; i < GetFriendRequestItemParent().childCount; i++) {
  137. Destroy(GetFriendRequestItemParent().GetChild(i).gameObject);
  138. }
  139. Action<JArray> cb = delegate(JArray list) {
  140. if (list.Count > 0) {
  141. foreach (var itemInfo in list) {
  142. int otherID = itemInfo.Value<int>("otherID");
  143. int avatarID = itemInfo.Value<int>("avatarID");
  144. string avatarUrl = itemInfo.Value<string>("avatarUrl");
  145. string nickname = itemInfo.Value<string>("nickname");
  146. bool online = itemInfo.Value<bool>("online");
  147. long offlineTime = itemInfo.Value<long>("offlineTime");
  148. GameObject o = GameObject.Instantiate(GetFriendRequestItemPrefab(), GetFriendRequestItemParent());
  149. o.SetActive(true);
  150. o.name = otherID.ToString();
  151. RoleMgr.SetAvatarToImage(
  152. o.transform.Find("Avatar/Sprite").GetComponent<Image>(),
  153. avatarID, avatarUrl);
  154. o.transform.Find("Name").GetComponent<Text>().text = nickname;
  155. o.transform.Find("BG0").gameObject.SetActive(!online);
  156. o.transform.Find("BG1").gameObject.SetActive(online);
  157. Text onlineTip = o.transform.Find("OnlineTip").GetComponent<Text>();
  158. onlineTip.text = TimeUtil.GetOfflineTimeStr(offlineTime, online);
  159. onlineTip.color = online ? Color.green : Color.gray;
  160. Button btnNo = o.transform.Find("BtnNo").GetComponent<Button>();
  161. btnNo.onClick.RemoveAllListeners();
  162. btnNo.onClick.AddListener(delegate() {
  163. AudioMgr.ins.PlayBtn();
  164. UserPlayer.ins.call("friendComp.decideAddFriend", new object[]{false, otherID});
  165. Destroy(o);
  166. UserPlayer.ins.tempData.hasFriendRequest = GetFriendRequestItemParent().childCount - 1 > 1;
  167. });
  168. Button btnYes = o.transform.Find("BtnYes").GetComponent<Button>();
  169. btnYes.onClick.RemoveAllListeners();
  170. btnYes.onClick.AddListener(delegate() {
  171. AudioMgr.ins.PlayBtn();
  172. UserPlayer.ins.call("friendComp.decideAddFriend", new object[]{true, otherID});
  173. Destroy(o);
  174. _hasAcceptAnyFriends = true;
  175. UserPlayer.ins.tempData.hasFriendRequest = GetFriendRequestItemParent().childCount - 1 > 1;
  176. });
  177. }
  178. } else {
  179. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("好友请求列表为空"));
  180. }
  181. };
  182. UserPlayer.ins.call("friendComp.getFriendRequestList", null, cb);
  183. PopupMgr.ins.ClearAllTip();
  184. }
  185. #endregion
  186. #region friendRecommend
  187. void InitFriendRecommendBox() {
  188. friendRecommendBox.SetActive(false);
  189. GetFriendRecommendItemPrefab().SetActive(false);
  190. }
  191. Transform GetFriendRecommendItemParent() {
  192. return friendRecommendBox.transform.Find("ScrollView/Viewport/Content");
  193. }
  194. GameObject GetFriendRecommendItemPrefab() {
  195. return GetFriendRecommendItemParent().Find("Item").gameObject;
  196. }
  197. void EnterFriendRecommendBox() {
  198. friendRecommendBox.SetActive(true);
  199. for (int i = 1; i < GetFriendRecommendItemParent().childCount; i++) {
  200. Destroy(GetFriendRecommendItemParent().GetChild(i).gameObject);
  201. }
  202. Action<JArray> cb = delegate(JArray list) {
  203. for (int i = 1; i < GetFriendRecommendItemParent().childCount; i++) {
  204. Destroy(GetFriendRecommendItemParent().GetChild(i).gameObject);
  205. }
  206. if (list.Count > 0) {
  207. foreach (var itemInfo in list) {
  208. int userID = itemInfo.Value<int>("id");
  209. int avatarID = itemInfo.Value<int>("avatarID");
  210. string avatarUrl = itemInfo.Value<string>("avatarUrl");
  211. string nickname = itemInfo.Value<string>("nickname");
  212. bool online = itemInfo.Value<bool>("online");
  213. long offlineTime = itemInfo.Value<long>("offlineTime");
  214. int playCount = itemInfo.Value<int>("playCount");
  215. GameObject o = GameObject.Instantiate(GetFriendRecommendItemPrefab(), GetFriendRecommendItemParent());
  216. o.SetActive(true);
  217. o.name = userID.ToString();
  218. RoleMgr.SetAvatarToImage(
  219. o.transform.Find("Avatar/Sprite").GetComponent<Image>(),
  220. avatarID, avatarUrl
  221. );
  222. o.transform.Find("Name").GetComponent<Text>().text = nickname;
  223. o.transform.Find("PlayCount").GetComponent<Text>().text =
  224. String.Format(
  225. TextAutoLanguage2.GetTextByKey("friend_record_play-again-count"), playCount
  226. );
  227. o.transform.Find("BG0").gameObject.SetActive(!online);
  228. o.transform.Find("BG1").gameObject.SetActive(online);
  229. Text onlineTip = o.transform.Find("OnlineTip").GetComponent<Text>();
  230. onlineTip.text = TimeUtil.GetOfflineTimeStr(offlineTime, online);
  231. onlineTip.color = online ? Color.green : Color.gray;
  232. Button btnYes = o.transform.Find("BtnAdd").GetComponent<Button>();
  233. btnYes.onClick.RemoveAllListeners();
  234. btnYes.onClick.AddListener(delegate() {
  235. AudioMgr.ins.PlayBtn();
  236. UserPlayer.ins.call("friendComp.requestAddFriend", userID);
  237. btnYes.interactable = false;
  238. });
  239. }
  240. } else {
  241. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("没有可推荐的好友"));
  242. }
  243. };
  244. UserPlayer.ins.call("friendComp.getFriendRecommendList", null, cb);
  245. PopupMgr.ins.ClearAllTip();
  246. }
  247. #endregion
  248. #region searchPlayer
  249. void InitSearchPlayerBox() {
  250. searchPlayerBox.SetActive(false);
  251. GetSearchPlayerItemPrefab().SetActive(false);
  252. }
  253. Transform GetSearchPlayerItemParent() {
  254. return searchPlayerBox.transform.Find("ScrollView/Viewport/Content");
  255. }
  256. GameObject GetSearchPlayerItemPrefab() {
  257. return GetSearchPlayerItemParent().Find("Item").gameObject;
  258. }
  259. void EnterSearchPlayerBox() {
  260. searchPlayerBox.SetActive(true);
  261. PopupMgr.ins.ClearAllTip();
  262. }
  263. InputField GetSearchInputField() {
  264. return searchPlayerBox.transform.Find("SearchBox/InputField").GetComponent<InputField>();
  265. }
  266. long _lastSearchTime = 0;
  267. public void btnEvent_SearchPlayer() {
  268. AudioMgr.ins.PlayBtn();
  269. if (JCUnityLib.TimeUtils.GetTimestamp() - _lastSearchTime < 3000) {
  270. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("操作过于频繁"));
  271. return;
  272. } else {
  273. _lastSearchTime = JCUnityLib.TimeUtils.GetTimestamp();
  274. }
  275. string searchText = GetSearchInputField().text;
  276. Action<JArray> cb = delegate(JArray list) {
  277. for (int i = 1; i < GetSearchPlayerItemParent().childCount; i++) {
  278. Destroy(GetSearchPlayerItemParent().GetChild(i).gameObject);
  279. }
  280. if (list.Count > 0) {
  281. foreach (var itemInfo in list) {
  282. int userID = itemInfo.Value<int>("id");
  283. int avatarID = itemInfo.Value<int>("avatarID");
  284. string avatarUrl = itemInfo.Value<string>("avatarUrl");
  285. string nickname = itemInfo.Value<string>("nickname");
  286. bool online = itemInfo.Value<bool>("online");
  287. long offlineTime = itemInfo.Value<long>("offlineTime");
  288. GameObject o = GameObject.Instantiate(GetSearchPlayerItemPrefab(), GetSearchPlayerItemParent());
  289. o.SetActive(true);
  290. o.name = userID.ToString();
  291. RoleMgr.SetAvatarToImage(
  292. o.transform.Find("Avatar/Sprite").GetComponent<Image>(),
  293. avatarID, avatarUrl
  294. );
  295. o.transform.Find("Name").GetComponent<Text>().text = nickname;
  296. o.transform.Find("BG0").gameObject.SetActive(!online);
  297. o.transform.Find("BG1").gameObject.SetActive(online);
  298. Text onlineTip = o.transform.Find("OnlineTip").GetComponent<Text>();
  299. onlineTip.text = TimeUtil.GetOfflineTimeStr(offlineTime, online);
  300. onlineTip.color = online ? Color.green : Color.gray;
  301. Button btnYes = o.transform.Find("BtnAdd").GetComponent<Button>();
  302. btnYes.onClick.RemoveAllListeners();
  303. btnYes.onClick.AddListener(delegate() {
  304. AudioMgr.ins.PlayBtn();
  305. UserPlayer.ins.call("friendComp.requestAddFriend", userID);
  306. btnYes.interactable = false;
  307. });
  308. }
  309. } else {
  310. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("没有搜索到目标玩家"));
  311. }
  312. };
  313. UserPlayer.ins.call("friendComp.searchPlayersByID", new object[]{searchText}, cb);
  314. }
  315. #endregion
  316. void HideAllBox() {
  317. myFriendBox.SetActive(false);
  318. friendRequestBox.SetActive(false);
  319. friendRecommendBox.SetActive(false);
  320. searchPlayerBox.SetActive(false);
  321. }
  322. public void Back() {
  323. AudioMgr.ins.PlayBtn();
  324. ViewMgr.Instance.DestroyView<FriendView>();
  325. }
  326. #region 左栏条目切换
  327. [SerializeField] Sprite[] btnTabTextures;
  328. Button[] btnTabs = {null, null, null, null};
  329. void InitBtnTabs() {
  330. btnTabs[0] = this.transform.Find("LineV/BtnTab0").GetComponent<Button>();
  331. btnTabs[1] =this.transform.Find("LineV/BtnTab1").GetComponent<Button>();
  332. btnTabs[2] =this.transform.Find("LineV/BtnTab2").GetComponent<Button>();
  333. btnTabs[3] =this.transform.Find("LineV/BtnTab3").GetComponent<Button>();
  334. for (int i = 0; i < btnTabs.Length; i++) {
  335. Button btnTab = btnTabs[i];
  336. int btnTabIndex = i;
  337. btnTab.onClick.AddListener(() => {
  338. AudioMgr.ins.PlayBtn();
  339. if (!IsBtnTabSelected(btnTab)) {
  340. SetBtnTabSelected(btnTab);
  341. }
  342. });
  343. }
  344. }
  345. bool IsBtnTabSelected(Button btn) {
  346. Image img = btn.GetComponent<Image>();
  347. return img.sprite.name.Equals(btnTabTextures[1].name);
  348. }
  349. void SetBtnTabSelected(Button btn) {
  350. foreach (var item in btnTabs) {
  351. Image img = item.GetComponent<Image>();
  352. img.sprite = item == btn ? btnTabTextures[1] : btnTabTextures[0];
  353. }
  354. int index = Array.IndexOf(btnTabs, btn);
  355. HandleBtnTabSelectedLogic(index);
  356. }
  357. void HandleBtnTabSelectedLogic(int btnIndex) {
  358. HideAllBox();
  359. if (btnIndex == 0) {
  360. EnterMyFriendBox();
  361. } else if (btnIndex == 1) {
  362. EnterFriendRequestBox();
  363. } else if (btnIndex == 2) {
  364. EnterFriendRecommendBox();
  365. } else if (btnIndex == 3) {
  366. EnterSearchPlayerBox();
  367. }
  368. }
  369. #endregion
  370. }