| | 1 | | using System.Collections.Generic; |
| | 2 | | using System.Linq; |
| | 3 | | using DCL.Interface; |
| | 4 | |
|
| | 5 | | public class FriendsTabView : FriendsTabViewBase |
| | 6 | | { |
| | 7 | | private const int CREATION_AMOUNT_PER_FRAME = 5; |
| | 8 | |
|
| 23 | 9 | | public EntryList onlineFriendsList = new EntryList(); |
| 23 | 10 | | public EntryList offlineFriendsList = new EntryList(); |
| | 11 | | public event System.Action<FriendEntry> OnWhisper; |
| | 12 | | public event System.Action<string> OnDeleteConfirmation; |
| | 13 | |
|
| | 14 | | private string lastProcessedFriend; |
| 23 | 15 | | internal readonly Dictionary<string, FriendEntryBase.Model> creationQueue = new Dictionary<string, FriendEntryBase.M |
| | 16 | |
|
| | 17 | | public override void Initialize(FriendsHUDView owner, int preinstantiatedEntries) |
| | 18 | | { |
| 22 | 19 | | base.Initialize(owner, preinstantiatedEntries); |
| | 20 | |
|
| 22 | 21 | | onlineFriendsList.toggleTextPrefix = "ONLINE"; |
| 22 | 22 | | offlineFriendsList.toggleTextPrefix = "OFFLINE"; |
| | 23 | |
|
| 22 | 24 | | if (ChatController.i != null) |
| | 25 | | { |
| 0 | 26 | | ChatController.i.OnAddMessage -= ChatController_OnAddMessage; |
| 0 | 27 | | ChatController.i.OnAddMessage += ChatController_OnAddMessage; |
| | 28 | | } |
| 22 | 29 | | } |
| | 30 | |
|
| | 31 | | public override void OnDestroy() |
| | 32 | | { |
| 22 | 33 | | base.OnDestroy(); |
| | 34 | |
|
| 22 | 35 | | if (ChatController.i != null) |
| 0 | 36 | | ChatController.i.OnAddMessage -= ChatController_OnAddMessage; |
| 22 | 37 | | } |
| | 38 | |
|
| | 39 | | protected override bool CreateEntry(string userId) |
| | 40 | | { |
| 19 | 41 | | if (!base.CreateEntry(userId)) |
| 0 | 42 | | return false; |
| | 43 | |
|
| 19 | 44 | | var entry = GetEntry(userId) as FriendEntry; |
| | 45 | |
|
| 20 | 46 | | entry.OnWhisperClick += (x) => OnWhisper?.Invoke(x); |
| 19 | 47 | | entry.OnJumpInClick += (x) => this.owner.OnCloseButtonPressed(); |
| | 48 | |
|
| 19 | 49 | | return true; |
| | 50 | | } |
| | 51 | |
|
| | 52 | | public override bool RemoveEntry(string userId) |
| | 53 | | { |
| 4 | 54 | | if (!base.RemoveEntry(userId)) |
| 0 | 55 | | return false; |
| | 56 | |
|
| 4 | 57 | | offlineFriendsList.Remove(userId); |
| 4 | 58 | | onlineFriendsList.Remove(userId); |
| 4 | 59 | | offlineFriendsList.RemoveLastTimestamp(userId); |
| 4 | 60 | | onlineFriendsList.RemoveLastTimestamp(userId); |
| 4 | 61 | | return true; |
| | 62 | | } |
| | 63 | |
|
| | 64 | | public override bool UpdateEntry(string userId, FriendEntryBase.Model model) |
| | 65 | | { |
| 19 | 66 | | if (!base.UpdateEntry(userId, model)) |
| | 67 | | { |
| | 68 | | //Replace the queued model for creation for the updated one. |
| 0 | 69 | | if (creationQueue.ContainsKey(userId)) |
| 0 | 70 | | creationQueue[userId] = model; |
| 0 | 71 | | return false; |
| | 72 | | } |
| | 73 | |
|
| 19 | 74 | | var entry = entries[userId]; |
| | 75 | |
|
| 19 | 76 | | if (model.status == PresenceStatus.ONLINE) |
| | 77 | | { |
| 5 | 78 | | offlineFriendsList.Remove(userId); |
| 5 | 79 | | onlineFriendsList.Add(userId, entry); |
| | 80 | |
|
| 5 | 81 | | var removedTimestamp = offlineFriendsList.RemoveLastTimestamp(userId); |
| 5 | 82 | | onlineFriendsList.AddOrUpdateLastTimestamp(removedTimestamp); |
| 5 | 83 | | } |
| | 84 | | else |
| | 85 | | { |
| 14 | 86 | | onlineFriendsList.Remove(userId); |
| 14 | 87 | | offlineFriendsList.Add(userId, entry); |
| | 88 | |
|
| 14 | 89 | | var removedTimestamp = onlineFriendsList.RemoveLastTimestamp(userId); |
| 14 | 90 | | offlineFriendsList.AddOrUpdateLastTimestamp(removedTimestamp); |
| | 91 | | } |
| | 92 | |
|
| 19 | 93 | | return true; |
| | 94 | | } |
| | 95 | |
|
| | 96 | | protected override void OnPressDeleteButton(string userId) |
| | 97 | | { |
| 1 | 98 | | if (string.IsNullOrEmpty(userId)) |
| 0 | 99 | | return; |
| | 100 | |
|
| 1 | 101 | | FriendEntryBase friendEntryToDelete = GetEntry(userId); |
| 1 | 102 | | if (friendEntryToDelete != null) |
| | 103 | | { |
| 1 | 104 | | confirmationDialog.SetText($"Are you sure you want to delete {friendEntryToDelete.model.userName} as a frien |
| 1 | 105 | | confirmationDialog.Show(() => |
| | 106 | | { |
| 1 | 107 | | RemoveEntry(userId); |
| 1 | 108 | | OnDeleteConfirmation?.Invoke(userId); |
| 1 | 109 | | }); |
| | 110 | | } |
| 1 | 111 | | } |
| | 112 | |
|
| | 113 | | private void ChatController_OnAddMessage(ChatMessage message) |
| | 114 | | { |
| 0 | 115 | | if (message.messageType != ChatMessage.Type.PRIVATE) |
| 0 | 116 | | return; |
| | 117 | |
|
| 0 | 118 | | FriendEntryBase friend = GetEntry(message.sender != UserProfile.GetOwnUserProfile().userId |
| | 119 | | ? message.sender |
| | 120 | | : message.recipient); |
| | 121 | |
|
| 0 | 122 | | if (friend == null) |
| 0 | 123 | | return; |
| | 124 | |
|
| 0 | 125 | | bool reorderFriendEntries = false; |
| | 126 | |
|
| 0 | 127 | | if (friend.userId != lastProcessedFriend) |
| | 128 | | { |
| 0 | 129 | | lastProcessedFriend = friend.userId; |
| 0 | 130 | | reorderFriendEntries = true; |
| | 131 | | } |
| | 132 | |
|
| 0 | 133 | | LastFriendTimestampModel timestampToUpdate = new LastFriendTimestampModel |
| | 134 | | { |
| | 135 | | userId = friend.userId, |
| | 136 | | lastMessageTimestamp = message.timestamp |
| | 137 | | }; |
| | 138 | |
|
| | 139 | | // Each time a private message is received (or sent by the player), we sort the online and offline lists by time |
| 0 | 140 | | if (friend.model.status == PresenceStatus.ONLINE) |
| | 141 | | { |
| 0 | 142 | | onlineFriendsList.AddOrUpdateLastTimestamp(timestampToUpdate, reorderFriendEntries); |
| 0 | 143 | | } |
| | 144 | | else |
| | 145 | | { |
| 0 | 146 | | offlineFriendsList.AddOrUpdateLastTimestamp(timestampToUpdate, reorderFriendEntries); |
| | 147 | | } |
| | 148 | |
|
| 0 | 149 | | lastProcessedFriend = friend.userId; |
| 0 | 150 | | } |
| | 151 | |
|
| | 152 | | public void CreateOrUpdateEntryDeferred(string userId, FriendEntryBase.Model model) |
| | 153 | | { |
| 20 | 154 | | if (creationQueue.ContainsKey(userId)) |
| 0 | 155 | | creationQueue[userId] = model; |
| | 156 | | else |
| 20 | 157 | | creationQueue.Add(userId, model); |
| 20 | 158 | | } |
| | 159 | |
|
| | 160 | | /// <summary> |
| | 161 | | /// To avoid having hiccups when a player with dozens of friends load into the game |
| | 162 | | /// we deferred the entries instantiation to multiple frames |
| | 163 | | /// </summary> |
| | 164 | | protected override void UpdateLayout() |
| | 165 | | { |
| 65 | 166 | | if (creationQueue.Count == 0) |
| | 167 | | { |
| 50 | 168 | | base.UpdateLayout(); |
| 50 | 169 | | return; |
| | 170 | | } |
| | 171 | |
|
| 68 | 172 | | for (int i = 0; i < CREATION_AMOUNT_PER_FRAME && creationQueue.Count != 0; i++) |
| | 173 | | { |
| 19 | 174 | | var pair = creationQueue.FirstOrDefault(); |
| 19 | 175 | | creationQueue.Remove(pair.Key); |
| 19 | 176 | | CreateEntry(pair.Key); |
| 19 | 177 | | UpdateEntry(pair.Key, pair.Value); |
| | 178 | | } |
| | 179 | |
|
| | 180 | | //If we have creations to process we avoid reconstructing the layout until we are done |
| 15 | 181 | | if (creationQueue.Count != 0) |
| | 182 | | { |
| 0 | 183 | | layoutIsDirty = false; |
| | 184 | | } |
| | 185 | |
|
| 15 | 186 | | base.UpdateLayout(); |
| 15 | 187 | | } |
| | 188 | | } |