123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244 |
- using UnityEngine;
- using UnityEngine.Events;
- using System.Collections;
- using System.Collections.Generic;
- using LitJson;
- public class FollowManager
- {
- private List<Follower> followList;
- private Dictionary<int, Follower> followDict;
- private List<Follower> beFollowedList;
- private Dictionary<int, Follower> 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<Follower> ();
- followDict = new Dictionary<int, Follower> ();
- beFollowedList = new List<Follower> ();
- beFollowedDict = new Dictionary<int, Follower> ();
- 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<data.Count; i++)
- {
- JsonData o = data[i];
- int me = JsonUtil.ToInt(o["me"]);
- int target = JsonUtil.ToInt(o["target"]);
- if(me == myUserId)
- {
- AddFollow(target);
- }
- else if(target == myUserId)
- {
- AddBefollow(me);
- }
- }
- m_Initialized = true;
- InitCompletion.Invoke();
- }
- });
- }
- public void Follow(int userId, CallBackUtil.BoolCallBack callBack)
- {
- FollowRequest.Follow (userId).ResultEvent.AddListener((bool success, JsonData data)=>{
- 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<Follower> 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<ChatMsg> tempMsgList;
- public void AddMsg(ChatMsg chatMsg)
- {
- if (!initialized) {
- if (tempMsgList == null)
- tempMsgList = new List<ChatMsg> ();
- 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<tempMsgList.Count; i++)
- {
- AddMsg (tempMsgList [i]);
- }
- tempMsgList = null;
- }
- }
- private static FollowManager instance;
- public static FollowManager GetInstance()
- {
- if(instance == null)
- {
- instance = new FollowManager ();
- }
- return instance;
- }
- }
|