| | 1 | | using Newtonsoft.Json; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Linq; |
| | 4 | | using DCL.Helpers; |
| | 5 | | using UnityEngine; |
| | 6 | |
|
| | 7 | | public class ChatHeadGroupView : MonoBehaviour |
| | 8 | | { |
| | 9 | | const int MAX_GROUP_SIZE = 5; |
| | 10 | | const string CHAT_HEAD_PATH = "ChatHead"; |
| | 11 | | const string PLAYER_PREFS_LATEST_OPEN_CHATS = "LatestOpenChats"; |
| | 12 | |
|
| | 13 | | public event System.Action<TaskbarButton> OnHeadToggleOn; |
| | 14 | | public event System.Action<TaskbarButton> OnHeadToggleOff; |
| | 15 | |
|
| | 16 | | public Transform container; |
| | 17 | |
|
| | 18 | | [System.NonSerialized] |
| 5 | 19 | | public List<ChatHeadButton> chatHeads = new List<ChatHeadButton>(); |
| | 20 | |
|
| | 21 | | private IChatController chatController; |
| | 22 | | private IFriendsController friendsController; |
| | 23 | | private ulong rendererStateTimeMark; |
| | 24 | | private RectTransform contentParentRT; |
| | 25 | |
|
| | 26 | | public void Initialize(IChatController chatController, IFriendsController friendsController) |
| | 27 | | { |
| 4 | 28 | | this.chatController = chatController; |
| 4 | 29 | | this.friendsController = friendsController; |
| | 30 | |
|
| 4 | 31 | | if (chatController != null) |
| 3 | 32 | | chatController.OnAddMessage += ChatController_OnAddMessage; |
| | 33 | |
|
| 4 | 34 | | if (friendsController != null) |
| | 35 | | { |
| 1 | 36 | | friendsController.OnUpdateFriendship += FriendsController_OnUpdateFriendship; |
| 1 | 37 | | friendsController.OnUpdateUserStatus += FriendsController_OnUpdateUserStatus; |
| 1 | 38 | | friendsController.OnInitialized += FriendsController_OnInitialized; |
| | 39 | | } |
| | 40 | |
|
| 4 | 41 | | CommonScriptableObjects.rendererState.OnChange -= RendererState_OnChange; |
| 4 | 42 | | CommonScriptableObjects.rendererState.OnChange += RendererState_OnChange; |
| 4 | 43 | | } |
| | 44 | |
|
| | 45 | | private void Start() |
| | 46 | | { |
| 4 | 47 | | contentParentRT = container.parent as RectTransform; |
| 4 | 48 | | gameObject.SetActive(false); |
| 4 | 49 | | } |
| | 50 | |
|
| | 51 | | private void Update() |
| | 52 | | { |
| | 53 | | // NOTE: Update is only enabled when SetParentContainerAsDirty is called |
| 0 | 54 | | DCL.Helpers.Utils.ForceUpdateLayout(contentParentRT, delayed: true); |
| 0 | 55 | | this.enabled = false; |
| 0 | 56 | | } |
| | 57 | |
|
| | 58 | | private void FriendsController_OnUpdateFriendship(string id, FriendshipAction action) |
| | 59 | | { |
| 0 | 60 | | if (action != FriendshipAction.NONE) |
| 0 | 61 | | return; |
| | 62 | |
|
| 0 | 63 | | RemoveChatHead(id); |
| 0 | 64 | | } |
| | 65 | |
|
| | 66 | | private void FriendsController_OnUpdateUserStatus(string userId, FriendsController.UserStatus userStatus) |
| | 67 | | { |
| 0 | 68 | | ChatHeadButton updatedChatHead = chatHeads.FirstOrDefault(ch => ch.profile.userId == userId); |
| 0 | 69 | | if (updatedChatHead != null) |
| 0 | 70 | | updatedChatHead.SetOnlineStatus(userStatus.presence == PresenceStatus.ONLINE); |
| 0 | 71 | | } |
| | 72 | |
|
| | 73 | | private void FriendsController_OnInitialized() |
| | 74 | | { |
| | 75 | | // Load the chat heads from local storage just after FriendsController has been initialized |
| 0 | 76 | | LoadLatestOpenChats(); |
| 0 | 77 | | friendsController.OnInitialized -= FriendsController_OnInitialized; |
| 0 | 78 | | } |
| | 79 | |
|
| | 80 | | private void RendererState_OnChange(bool current, bool previous) |
| | 81 | | { |
| 0 | 82 | | rendererStateTimeMark = (ulong) System.DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(); |
| 0 | 83 | | CommonScriptableObjects.rendererState.OnChange -= RendererState_OnChange; |
| 0 | 84 | | } |
| | 85 | |
|
| | 86 | | private void OnDestroy() |
| | 87 | | { |
| 4 | 88 | | if (chatController != null) |
| 3 | 89 | | chatController.OnAddMessage -= ChatController_OnAddMessage; |
| | 90 | |
|
| 4 | 91 | | if (friendsController != null) |
| | 92 | | { |
| 1 | 93 | | friendsController.OnUpdateFriendship -= FriendsController_OnUpdateFriendship; |
| 1 | 94 | | friendsController.OnUpdateUserStatus -= FriendsController_OnUpdateUserStatus; |
| 1 | 95 | | friendsController.OnInitialized -= FriendsController_OnInitialized; |
| | 96 | | } |
| | 97 | |
|
| 4 | 98 | | CommonScriptableObjects.rendererState.OnChange -= RendererState_OnChange; |
| 4 | 99 | | } |
| | 100 | |
|
| | 101 | | private void ChatController_OnAddMessage(DCL.Interface.ChatMessage obj) |
| | 102 | | { |
| 1 | 103 | | if (!CommonScriptableObjects.rendererState.Get() || |
| | 104 | | obj.messageType != DCL.Interface.ChatMessage.Type.PRIVATE || |
| | 105 | | obj.timestamp < rendererStateTimeMark) |
| 0 | 106 | | return; |
| | 107 | |
|
| 1 | 108 | | var ownProfile = UserProfile.GetOwnUserProfile(); |
| | 109 | |
|
| 1 | 110 | | string userId = string.Empty; |
| | 111 | |
|
| 1 | 112 | | if (obj.sender != ownProfile.userId) |
| 1 | 113 | | userId = obj.sender; |
| 0 | 114 | | else if (obj.recipient != ownProfile.userId) |
| 0 | 115 | | userId = obj.recipient; |
| | 116 | |
|
| 1 | 117 | | if (!string.IsNullOrEmpty(userId)) |
| | 118 | | { |
| 1 | 119 | | AddChatHead(userId, obj.timestamp); |
| | 120 | | } |
| 1 | 121 | | } |
| | 122 | |
|
| 2 | 123 | | private void OnToggleOn(TaskbarButton head) { OnHeadToggleOn?.Invoke(head); } |
| | 124 | |
|
| | 125 | | private void OnToggleOff(TaskbarButton head) |
| | 126 | | { |
| 1 | 127 | | if (!(head is ChatHeadButton)) |
| 0 | 128 | | return; |
| | 129 | |
|
| 1 | 130 | | OnHeadToggleOff?.Invoke(head); |
| 1 | 131 | | } |
| | 132 | |
|
| | 133 | | private void SortChatHeads() |
| | 134 | | { |
| 2 | 135 | | chatHeads = chatHeads.OrderByDescending((x) => x.lastTimestamp).ToList(); |
| | 136 | |
|
| 4 | 137 | | for (int i = 0; i < chatHeads.Count; i++) |
| | 138 | | { |
| 1 | 139 | | chatHeads[i].transform.SetSiblingIndex(i); |
| | 140 | | } |
| 1 | 141 | | } |
| | 142 | |
|
| | 143 | | internal ChatHeadButton AddChatHead(string userId, ulong timestamp, bool saveStatusInStorage = true, bool forceLayou |
| | 144 | | { |
| 1 | 145 | | var existingHead = chatHeads.FirstOrDefault(x => x.profile.userId == userId); |
| | 146 | |
|
| 1 | 147 | | if (existingHead != null) |
| | 148 | | { |
| 0 | 149 | | existingHead.lastTimestamp = timestamp; |
| 0 | 150 | | SortChatHeads(); |
| | 151 | |
|
| 0 | 152 | | if (saveStatusInStorage) |
| | 153 | | { |
| 0 | 154 | | LatestOpenChatsList.Model existingHeadInStorage = CommonScriptableObjects.latestOpenChats.GetList().Firs |
| 0 | 155 | | if (existingHeadInStorage != null) |
| | 156 | | { |
| 0 | 157 | | existingHeadInStorage.lastTimestamp = timestamp; |
| 0 | 158 | | SaveLatestOpenChats(); |
| | 159 | | } |
| | 160 | | } |
| | 161 | |
|
| 0 | 162 | | return existingHead; |
| | 163 | | } |
| | 164 | |
|
| 1 | 165 | | if (!gameObject.activeSelf) |
| | 166 | | { |
| 0 | 167 | | gameObject.SetActive(true); |
| | 168 | | } |
| | 169 | |
|
| 1 | 170 | | GameObject prefab = Resources.Load(CHAT_HEAD_PATH) as GameObject; |
| 1 | 171 | | GameObject instance = Instantiate(prefab, container); |
| 1 | 172 | | ChatHeadButton chatHead = instance.GetComponent<ChatHeadButton>(); |
| | 173 | |
|
| 1 | 174 | | chatHead.Initialize(UserProfileController.userProfilesCatalog.Get(userId)); |
| 1 | 175 | | chatHead.lastTimestamp = timestamp; |
| 1 | 176 | | chatHead.OnToggleOn += OnToggleOn; |
| 1 | 177 | | chatHead.OnToggleOff += OnToggleOff; |
| | 178 | |
|
| 1 | 179 | | if (friendsController != null && |
| | 180 | | friendsController.GetFriends().TryGetValue(userId, out FriendsController.UserStatus friendStatus)) |
| | 181 | | { |
| 0 | 182 | | chatHead.SetOnlineStatus(friendStatus.presence == PresenceStatus.ONLINE); |
| | 183 | | } |
| | 184 | |
|
| 1 | 185 | | var animator = chatHead.GetComponent<ShowHideAnimator>(); |
| | 186 | |
|
| 1 | 187 | | if (animator != null) |
| 1 | 188 | | animator.Show(); |
| | 189 | |
|
| 1 | 190 | | chatHeads.Add(chatHead); |
| 1 | 191 | | SortChatHeads(); |
| | 192 | |
|
| 1 | 193 | | if (saveStatusInStorage) |
| | 194 | | { |
| 1 | 195 | | CommonScriptableObjects.latestOpenChats.Add(new LatestOpenChatsList.Model { userId = userId, lastTimestamp = |
| 1 | 196 | | SaveLatestOpenChats(); |
| | 197 | | } |
| | 198 | |
|
| 1 | 199 | | if (chatHeads.Count > MAX_GROUP_SIZE) |
| | 200 | | { |
| 0 | 201 | | var lastChatHead = chatHeads[chatHeads.Count - 1]; |
| 0 | 202 | | RemoveChatHead(lastChatHead, saveStatusInStorage, forceLayoutUpdate: false); |
| | 203 | | } |
| | 204 | |
|
| 1 | 205 | | if (forceLayoutUpdate) |
| | 206 | | { |
| 1 | 207 | | SetParentContainerAsDirty(); |
| | 208 | | } |
| | 209 | |
|
| 1 | 210 | | return chatHead; |
| | 211 | | } |
| | 212 | |
|
| | 213 | | internal void ClearChatHeads() |
| | 214 | | { |
| 0 | 215 | | var chatHeadsToRemove = chatHeads.ToArray(); |
| | 216 | |
|
| 0 | 217 | | for (int i = 0; i < chatHeadsToRemove.Length; i++) |
| | 218 | | { |
| 0 | 219 | | RemoveChatHead(chatHeadsToRemove[i], forceLayoutUpdate: false); |
| | 220 | | } |
| | 221 | |
|
| 0 | 222 | | SetParentContainerAsDirty(); |
| 0 | 223 | | } |
| | 224 | |
|
| 0 | 225 | | internal void RemoveChatHead(string userId, bool forceLayoutUpdate = true) { RemoveChatHead(chatHeads.FirstOrDefault |
| | 226 | |
|
| | 227 | | internal void RemoveChatHead(ChatHeadButton chatHead, bool saveStatusInStorage = true, bool forceLayoutUpdate = true |
| | 228 | | { |
| 0 | 229 | | if (chatHead == null) |
| 0 | 230 | | return; |
| | 231 | |
|
| 0 | 232 | | var animator = chatHead.GetComponent<ShowHideAnimator>(); |
| | 233 | |
|
| 0 | 234 | | if (animator != null) |
| | 235 | | { |
| 0 | 236 | | animator.OnWillFinishHide -= Animator_OnWillFinishHide; |
| 0 | 237 | | animator.OnWillFinishHide += Animator_OnWillFinishHide; |
| 0 | 238 | | animator.Hide(); |
| 0 | 239 | | } |
| | 240 | | else |
| | 241 | | { |
| 0 | 242 | | DestroyChatHead(chatHead.gameObject, forceLayoutUpdate); |
| | 243 | | } |
| | 244 | |
|
| 0 | 245 | | chatHeads.Remove(chatHead); |
| | 246 | |
|
| 0 | 247 | | if (saveStatusInStorage) |
| | 248 | | { |
| 0 | 249 | | LatestOpenChatsList.Model chatHeadToRemove = CommonScriptableObjects.latestOpenChats.GetList().FirstOrDefaul |
| 0 | 250 | | CommonScriptableObjects.latestOpenChats.Remove(chatHeadToRemove); |
| 0 | 251 | | SaveLatestOpenChats(); |
| | 252 | | } |
| 0 | 253 | | } |
| | 254 | |
|
| | 255 | | private void Animator_OnWillFinishHide(ShowHideAnimator animator) |
| | 256 | | { |
| 0 | 257 | | animator.OnWillFinishHide -= Animator_OnWillFinishHide; |
| 0 | 258 | | DestroyChatHead(animator.gameObject, true); |
| 0 | 259 | | } |
| | 260 | |
|
| | 261 | | private void SaveLatestOpenChats() |
| | 262 | | { |
| 1 | 263 | | PlayerPrefsUtils.SetString(PLAYER_PREFS_LATEST_OPEN_CHATS, JsonConvert.SerializeObject(CommonScriptableObjects.l |
| 1 | 264 | | PlayerPrefsUtils.Save(); |
| 1 | 265 | | } |
| | 266 | |
|
| | 267 | | private void LoadLatestOpenChats() |
| | 268 | | { |
| 0 | 269 | | CommonScriptableObjects.latestOpenChats.Clear(); |
| 0 | 270 | | List<LatestOpenChatsList.Model> latestOpenChatsFromStorage = JsonConvert.DeserializeObject<List<LatestOpenChatsL |
| 0 | 271 | | if (latestOpenChatsFromStorage != null) |
| | 272 | | { |
| 0 | 273 | | foreach (LatestOpenChatsList.Model item in latestOpenChatsFromStorage) |
| | 274 | | { |
| 0 | 275 | | if (UserProfileController.userProfilesCatalog.Get(item.userId) == null) |
| | 276 | | continue; |
| | 277 | |
|
| 0 | 278 | | CommonScriptableObjects.latestOpenChats.Add(item); |
| 0 | 279 | | AddChatHead(item.userId, item.lastTimestamp, false, forceLayoutUpdate: false); |
| | 280 | | } |
| | 281 | |
|
| 0 | 282 | | SetParentContainerAsDirty(); |
| | 283 | | } |
| 0 | 284 | | } |
| | 285 | |
|
| | 286 | | private void DestroyChatHead(GameObject chatHeadGO, bool forceLayoutUpdate) |
| | 287 | | { |
| 0 | 288 | | Destroy(chatHeadGO); |
| | 289 | |
|
| 0 | 290 | | if (container.childCount <= 1) |
| | 291 | | { |
| 0 | 292 | | gameObject.SetActive(false); |
| 0 | 293 | | } |
| 0 | 294 | | else if (forceLayoutUpdate) |
| | 295 | | { |
| 0 | 296 | | SetParentContainerAsDirty(); |
| | 297 | | } |
| 0 | 298 | | } |
| | 299 | |
|
| 2 | 300 | | private void SetParentContainerAsDirty() { this.enabled = true; } |
| | 301 | | } |