using UnityEngine; using UnityEngine.Events; using System.Collections; using System.Collections.Generic; using LitJson; public class FollowManager { private List followList; private Dictionary followDict; private List beFollowedList; private Dictionary beFollowedDict; public UnityEvent InitCompletion; public IntegerEvent FollowAdded; public ObjectEvent FollowDeleted; private bool m_Initializing = false; private bool m_Initialized = false; public bool initialized { get{ return m_Initialized; } } public FollowManager() { followList = new List (); followDict = new Dictionary (); beFollowedList = new List (); beFollowedDict = new Dictionary (); InitCompletion = new UnityEvent (); FollowAdded = new IntegerEvent (); FollowDeleted = new ObjectEvent (); } public void Init() { if (m_Initialized) { InitCompletion.Invoke (); return; } m_Initializing = true; FollowRequest.GetFollowerList ().ResultEvent.AddListener ((bool success, JsonData data)=>{ m_Initializing = false; if(success) { int myUserId = Session.GetInstance().myUserData.id; for(int i=0; i{ if(success) { int targetId = JsonUtil.ToInt(data); AddFollow(targetId); AlertPanel.Show(Language.GetStr("UserPanel", "followed").Replace("%NICK%", UserCache.GetNick(targetId))); } callBack(success); }); } private void AddFollow(int userId) { if (IsFollow (userId)) return; Follower follower = new Follower (); follower.userId = userId; followList.Add (follower); followDict.Add (userId, follower); FollowAdded.Invoke (userId); } public void Unfollow(int userId, CallBackUtil.BoolCallBack callBack) { FollowRequest.Unfollow (userId).ResultEvent.AddListener((bool success, JsonData data)=>{ if(success) { int targetId = JsonUtil.ToInt(data); DeleteFollow(targetId); AlertPanel.Show(Language.GetStr("UserPanel", "unfollowed").Replace("%NICK%", UserCache.GetNick(targetId))); } callBack(success); }); } public void DeleteFollow(int userId) { if (IsFollow (userId)) { Follower follower = followDict [userId]; followList.Remove (follower); followDict.Remove (userId); FollowDeleted.Invoke (follower); } } public bool IsFollow(int userId) { return followDict.ContainsKey (userId); } public List GetFollowList() { return followList; } public Follower GetFollower(int userId) { Follower follower = null; followDict.TryGetValue (userId, out follower); if (follower == null) beFollowedDict.TryGetValue (userId, out follower); return follower; } public bool IsBeFollowed(int userId) { return beFollowedDict.ContainsKey (userId); } public void AddBefollow(int userId) { if (IsBeFollowed (userId)) return; Follower follower = new Follower (); follower.userId = userId; beFollowedList.Add (follower); beFollowedDict.Add (userId, follower); } public void DeleteBefollow(int userId) { if (IsBeFollowed (userId)) { Follower follower = beFollowedDict [userId]; beFollowedList.Remove (follower); beFollowedDict.Remove (userId); } } private List tempMsgList; public void AddMsg(ChatMsg chatMsg) { if (!initialized) { if (tempMsgList == null) tempMsgList = new List (); tempMsgList.Add (chatMsg); if(!m_Initializing) { InitCompletion.AddListener (OnSelfInit); Init (); } return; } } private void OnSelfInit() { InitCompletion.RemoveListener (OnSelfInit); if(tempMsgList != null) { for(int i=0; i