| | 1 | | using System.Collections.Generic; |
| | 2 | | using System.Linq; |
| | 3 | | using DCL.Social.Friends; |
| | 4 | | using UnityEngine; |
| | 5 | |
|
| | 6 | | public class RealmTracker |
| | 7 | | { |
| 0 | 8 | | HashSet<TrackedRealmInfo> realmListeners = new HashSet<TrackedRealmInfo>(); |
| | 9 | |
|
| 0 | 10 | | public UserProfile profile { private set; get; } |
| 0 | 11 | | public UserStatus status { private set; get; } |
| | 12 | | Color backgroundColor; |
| | 13 | |
|
| 0 | 14 | | public RealmTracker(string userId, Color backgroundColor) |
| | 15 | | { |
| 0 | 16 | | profile = UserProfileController.userProfilesCatalog.Get(userId); |
| 0 | 17 | | this.backgroundColor = backgroundColor; |
| 0 | 18 | | } |
| | 19 | |
|
| 0 | 20 | | public void SetStatus(UserStatus newStatus) { status = newStatus; } |
| | 21 | |
|
| | 22 | | public void AddListener(TrackedRealmInfo listener) |
| | 23 | | { |
| 0 | 24 | | listener.OnListenerDisposed += OnListenerDisposed; |
| 0 | 25 | | realmListeners.Add(listener); |
| 0 | 26 | | listener.OnFriendAdded(profile, backgroundColor); |
| 0 | 27 | | } |
| | 28 | |
|
| | 29 | | public void RemoveListener(TrackedRealmInfo listener) |
| | 30 | | { |
| 0 | 31 | | OnListenerRemoved(listener); |
| 0 | 32 | | realmListeners.Remove(listener); |
| 0 | 33 | | } |
| | 34 | |
|
| 0 | 35 | | public bool HasListeners() { return realmListeners.Count > 0; } |
| | 36 | |
|
| | 37 | | public bool HasChangedRealm(string serverName) |
| | 38 | | { |
| 0 | 39 | | if (realmListeners.Count > 0 && realmListeners.First().ContainRealm(serverName)) |
| | 40 | | { |
| 0 | 41 | | return false; |
| | 42 | | } |
| | 43 | |
|
| 0 | 44 | | return true; |
| | 45 | | } |
| | 46 | |
|
| | 47 | | public void RemoveAllListeners() |
| | 48 | | { |
| 0 | 49 | | if (!HasListeners()) |
| | 50 | | { |
| 0 | 51 | | return; |
| | 52 | | } |
| | 53 | |
|
| 0 | 54 | | using (var listenerIterator = realmListeners.GetEnumerator()) |
| | 55 | | { |
| 0 | 56 | | while (listenerIterator.MoveNext()) |
| | 57 | | { |
| 0 | 58 | | OnListenerRemoved(listenerIterator.Current); |
| | 59 | | } |
| 0 | 60 | | } |
| | 61 | |
|
| 0 | 62 | | realmListeners.Clear(); |
| 0 | 63 | | } |
| | 64 | |
|
| | 65 | | public bool IsOnline() |
| | 66 | | { |
| 0 | 67 | | if (status.presence != PresenceStatus.ONLINE) |
| 0 | 68 | | return false; |
| 0 | 69 | | if (status.realm == null) |
| 0 | 70 | | return false; |
| 0 | 71 | | return !string.IsNullOrEmpty(status.realm.serverName); |
| | 72 | | } |
| | 73 | |
|
| | 74 | | void OnListenerDisposed(TrackedRealmInfo listener) |
| | 75 | | { |
| 0 | 76 | | listener.OnListenerDisposed -= OnListenerDisposed; |
| 0 | 77 | | realmListeners.Remove(listener); |
| 0 | 78 | | } |
| | 79 | |
|
| | 80 | | void OnListenerRemoved(TrackedRealmInfo listener) |
| | 81 | | { |
| 0 | 82 | | listener.OnListenerDisposed -= OnListenerDisposed; |
| 0 | 83 | | listener.OnFriendRemoved(profile); |
| 0 | 84 | | } |
| | 85 | | } |