FollowManager.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. using UnityEngine;
  2. using UnityEngine.Events;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using LitJson;
  6. public class FollowManager
  7. {
  8. private List<Follower> followList;
  9. private Dictionary<int, Follower> followDict;
  10. private List<Follower> beFollowedList;
  11. private Dictionary<int, Follower> beFollowedDict;
  12. public UnityEvent InitCompletion;
  13. public IntegerEvent FollowAdded;
  14. public ObjectEvent FollowDeleted;
  15. private bool m_Initializing = false;
  16. private bool m_Initialized = false;
  17. public bool initialized
  18. {
  19. get{
  20. return m_Initialized;
  21. }
  22. }
  23. public FollowManager()
  24. {
  25. followList = new List<Follower> ();
  26. followDict = new Dictionary<int, Follower> ();
  27. beFollowedList = new List<Follower> ();
  28. beFollowedDict = new Dictionary<int, Follower> ();
  29. InitCompletion = new UnityEvent ();
  30. FollowAdded = new IntegerEvent ();
  31. FollowDeleted = new ObjectEvent ();
  32. }
  33. public void Init()
  34. {
  35. if (m_Initialized) {
  36. InitCompletion.Invoke ();
  37. return;
  38. }
  39. m_Initializing = true;
  40. FollowRequest.GetFollowerList ().ResultEvent.AddListener ((bool success, JsonData data)=>{
  41. m_Initializing = false;
  42. if(success)
  43. {
  44. int myUserId = Session.GetInstance().myUserData.id;
  45. for(int i=0; i<data.Count; i++)
  46. {
  47. JsonData o = data[i];
  48. int me = JsonUtil.ToInt(o["me"]);
  49. int target = JsonUtil.ToInt(o["target"]);
  50. if(me == myUserId)
  51. {
  52. AddFollow(target);
  53. }
  54. else if(target == myUserId)
  55. {
  56. AddBefollow(me);
  57. }
  58. }
  59. m_Initialized = true;
  60. InitCompletion.Invoke();
  61. }
  62. });
  63. }
  64. public void Follow(int userId, CallBackUtil.BoolCallBack callBack)
  65. {
  66. FollowRequest.Follow (userId).ResultEvent.AddListener((bool success, JsonData data)=>{
  67. if(success)
  68. {
  69. int targetId = JsonUtil.ToInt(data);
  70. AddFollow(targetId);
  71. AlertPanel.Show(Language.GetStr("UserPanel", "followed").Replace("%NICK%", UserCache.GetNick(targetId)));
  72. }
  73. callBack(success);
  74. });
  75. }
  76. private void AddFollow(int userId)
  77. {
  78. if (IsFollow (userId))
  79. return;
  80. Follower follower = new Follower ();
  81. follower.userId = userId;
  82. followList.Add (follower);
  83. followDict.Add (userId, follower);
  84. FollowAdded.Invoke (userId);
  85. }
  86. public void Unfollow(int userId, CallBackUtil.BoolCallBack callBack)
  87. {
  88. FollowRequest.Unfollow (userId).ResultEvent.AddListener((bool success, JsonData data)=>{
  89. if(success)
  90. {
  91. int targetId = JsonUtil.ToInt(data);
  92. DeleteFollow(targetId);
  93. AlertPanel.Show(Language.GetStr("UserPanel", "unfollowed").Replace("%NICK%", UserCache.GetNick(targetId)));
  94. }
  95. callBack(success);
  96. });
  97. }
  98. public void DeleteFollow(int userId)
  99. {
  100. if (IsFollow (userId)) {
  101. Follower follower = followDict [userId];
  102. followList.Remove (follower);
  103. followDict.Remove (userId);
  104. FollowDeleted.Invoke (follower);
  105. }
  106. }
  107. public bool IsFollow(int userId)
  108. {
  109. return followDict.ContainsKey (userId);
  110. }
  111. public List<Follower> GetFollowList()
  112. {
  113. return followList;
  114. }
  115. public Follower GetFollower(int userId)
  116. {
  117. Follower follower = null;
  118. followDict.TryGetValue (userId, out follower);
  119. if (follower == null)
  120. beFollowedDict.TryGetValue (userId, out follower);
  121. return follower;
  122. }
  123. public bool IsBeFollowed(int userId)
  124. {
  125. return beFollowedDict.ContainsKey (userId);
  126. }
  127. public void AddBefollow(int userId)
  128. {
  129. if (IsBeFollowed (userId))
  130. return;
  131. Follower follower = new Follower ();
  132. follower.userId = userId;
  133. beFollowedList.Add (follower);
  134. beFollowedDict.Add (userId, follower);
  135. }
  136. public void DeleteBefollow(int userId)
  137. {
  138. if (IsBeFollowed (userId)) {
  139. Follower follower = beFollowedDict [userId];
  140. beFollowedList.Remove (follower);
  141. beFollowedDict.Remove (userId);
  142. }
  143. }
  144. private List<ChatMsg> tempMsgList;
  145. public void AddMsg(ChatMsg chatMsg)
  146. {
  147. if (!initialized) {
  148. if (tempMsgList == null)
  149. tempMsgList = new List<ChatMsg> ();
  150. tempMsgList.Add (chatMsg);
  151. if(!m_Initializing)
  152. {
  153. InitCompletion.AddListener (OnSelfInit);
  154. Init ();
  155. }
  156. return;
  157. }
  158. }
  159. private void OnSelfInit()
  160. {
  161. InitCompletion.RemoveListener (OnSelfInit);
  162. if(tempMsgList != null)
  163. {
  164. for(int i=0; i<tempMsgList.Count; i++)
  165. {
  166. AddMsg (tempMsgList [i]);
  167. }
  168. tempMsgList = null;
  169. }
  170. }
  171. private static FollowManager instance;
  172. public static FollowManager GetInstance()
  173. {
  174. if(instance == null)
  175. {
  176. instance = new FollowManager ();
  177. }
  178. return instance;
  179. }
  180. }