| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using DCL.Helpers; |
| | 3 | | using DG.Tweening; |
| | 4 | | using System; |
| | 5 | | using System.Collections.Generic; |
| | 6 | | using System.Threading; |
| | 7 | | using TMPro; |
| | 8 | | using UnityEngine; |
| | 9 | | using UnityEngine.UI; |
| | 10 | |
|
| | 11 | | namespace DCL.Chat.Notifications |
| | 12 | | { |
| | 13 | | public class MainChatNotificationsComponentView : BaseComponentView, IMainChatNotificationsComponentView |
| | 14 | | { |
| | 15 | | [SerializeField] private RectTransform chatEntriesContainer; |
| | 16 | | [SerializeField] private GameObject chatNotification; |
| | 17 | | [SerializeField] private GameObject friendRequestNotification; |
| | 18 | | [SerializeField] private ScrollRect scrollRectangle; |
| | 19 | | [SerializeField] private Button notificationButton; |
| | 20 | | [SerializeField] private ShowHideAnimator panelAnimator; |
| | 21 | | [SerializeField] private ShowHideAnimator scrollbarAnimator; |
| | 22 | |
|
| | 23 | | private const string CHAT_NOTIFICATION_POOL_NAME_PREFIX = "ChatNotificationEntriesPool_"; |
| | 24 | | private const string FRIEND_REQUEST_NOTIFICATION_POOL_NAME_PREFIX = "FriendRequestNotificationEntriesPool_"; |
| | 25 | | private const int MAX_NOTIFICATION_ENTRIES = 30; |
| | 26 | |
|
| | 27 | | public event Action<string> OnClickedChatMessage; |
| | 28 | | public event IMainChatNotificationsComponentView.ClickedNotificationDelegate OnClickedFriendRequest; |
| | 29 | | public event Action<bool> OnResetFade; |
| | 30 | | public event Action<bool> OnPanelFocus; |
| | 31 | |
|
| 9 | 32 | | internal readonly Queue<PoolableObject> poolableQueue = new (); |
| 9 | 33 | | internal readonly Queue<BaseComponentView> notificationQueue = new (); |
| | 34 | |
|
| 9 | 35 | | private readonly Vector2 notificationOffset = new (0, -56); |
| | 36 | |
|
| | 37 | | private bool isOverMessage; |
| | 38 | | private bool isOverPanel; |
| 9 | 39 | | private int notificationCount = 1; |
| | 40 | | private TMP_Text notificationMessage; |
| 9 | 41 | | private CancellationTokenSource animationCancellationToken = new (); |
| | 42 | |
|
| | 43 | | public override void Awake() |
| | 44 | | { |
| 8 | 45 | | onFocused += FocusedOnPanel; |
| 8 | 46 | | notificationMessage = notificationButton.GetComponentInChildren<TMP_Text>(); |
| 8 | 47 | | notificationButton?.onClick.RemoveAllListeners(); |
| 8 | 48 | | notificationButton?.onClick.AddListener(() => SetScrollToEnd()); |
| 8 | 49 | | scrollRectangle.onValueChanged.AddListener(ResetNotificationButtonFromScroll); |
| 8 | 50 | | } |
| | 51 | |
|
| | 52 | | public override void Show(bool instant = false) => |
| 1 | 53 | | gameObject.SetActive(true); |
| | 54 | |
|
| | 55 | | public override void Hide(bool instant = false) |
| | 56 | | { |
| 1 | 57 | | SetScrollToEnd(); |
| 1 | 58 | | gameObject.SetActive(false); |
| 1 | 59 | | } |
| | 60 | |
|
| | 61 | | public Transform GetPanelTransform() => |
| 0 | 62 | | gameObject.transform; |
| | 63 | |
|
| | 64 | | private void ResetNotificationButtonFromScroll(Vector2 newValue) |
| | 65 | | { |
| 0 | 66 | | if (newValue.y <= 0.0) { ResetNotificationButton(); } |
| 0 | 67 | | } |
| | 68 | |
|
| | 69 | | public void ShowPanel() |
| | 70 | | { |
| 0 | 71 | | panelAnimator?.Show(); |
| 0 | 72 | | scrollbarAnimator?.Show(); |
| 0 | 73 | | } |
| | 74 | |
|
| | 75 | | public void HidePanel() |
| | 76 | | { |
| 0 | 77 | | panelAnimator?.Hide(); |
| 0 | 78 | | scrollbarAnimator?.Hide(); |
| 0 | 79 | | } |
| | 80 | |
|
| | 81 | | public int GetNotificationsCount() => |
| 0 | 82 | | notificationQueue.Count; |
| | 83 | |
|
| | 84 | | public void ShowNotifications() |
| | 85 | | { |
| 0 | 86 | | foreach (BaseComponentView notification in notificationQueue) |
| 0 | 87 | | notification.Show(); |
| 0 | 88 | | } |
| | 89 | |
|
| | 90 | | public void HideNotifications() |
| | 91 | | { |
| 0 | 92 | | foreach (BaseComponentView notification in notificationQueue) |
| 0 | 93 | | notification.Hide(); |
| 0 | 94 | | } |
| | 95 | |
|
| | 96 | | public void AddNewChatNotification(PrivateChatMessageNotificationModel model) |
| | 97 | | { |
| 2 | 98 | | Pool entryPool = GetChatNotificationEntryPool(); |
| 2 | 99 | | PoolableObject newNotification = entryPool.Get(); |
| | 100 | |
|
| 2 | 101 | | var entry = newNotification.gameObject.GetComponent<ChatNotificationMessageComponentView>(); |
| 2 | 102 | | poolableQueue.Enqueue(newNotification); |
| 2 | 103 | | notificationQueue.Enqueue(entry); |
| | 104 | |
|
| 2 | 105 | | entry.OnClickedNotification -= ClickedOnNotification; |
| 2 | 106 | | entry.onFocused -= FocusedOnNotification; |
| 2 | 107 | | entry.showHideAnimator.OnWillFinishHide -= SetScrollToEnd; |
| | 108 | |
|
| 2 | 109 | | PopulatePrivateNotification(entry, model); |
| | 110 | |
|
| 2 | 111 | | entry.transform.SetParent(chatEntriesContainer, false); |
| 2 | 112 | | entry.RefreshControl(); |
| 2 | 113 | | entry.SetTimestamp(Utils.UnixTimeStampToLocalTime(model.Timestamp)); |
| 2 | 114 | | entry.OnClickedNotification += ClickedOnNotification; |
| 2 | 115 | | entry.onFocused += FocusedOnNotification; |
| 2 | 116 | | entry.showHideAnimator.OnWillFinishHide += SetScrollToEnd; |
| | 117 | |
|
| 2 | 118 | | chatEntriesContainer.anchoredPosition += notificationOffset; |
| | 119 | |
|
| 2 | 120 | | if (isOverPanel) |
| | 121 | | { |
| 0 | 122 | | notificationButton.gameObject.SetActive(true); |
| 0 | 123 | | IncreaseNotificationCount(); |
| | 124 | | } |
| | 125 | | else |
| | 126 | | { |
| 2 | 127 | | ResetNotificationButton(); |
| 2 | 128 | | animationCancellationToken.Cancel(); |
| 2 | 129 | | animationCancellationToken = new CancellationTokenSource(); |
| 2 | 130 | | AnimateNewEntry(entry.gameObject.transform, animationCancellationToken.Token).Forget(); |
| | 131 | | } |
| | 132 | |
|
| 2 | 133 | | OnResetFade?.Invoke(!isOverMessage && !isOverPanel); |
| 2 | 134 | | CheckNotificationCountAndRelease(); |
| 2 | 135 | | } |
| | 136 | |
|
| | 137 | | public void AddNewChatNotification(PublicChannelMessageNotificationModel model) |
| | 138 | | { |
| 3 | 139 | | Pool entryPool = GetChatNotificationEntryPool(); |
| 3 | 140 | | PoolableObject newNotification = entryPool.Get(); |
| | 141 | |
|
| 3 | 142 | | var entry = newNotification.gameObject.GetComponent<ChatNotificationMessageComponentView>(); |
| 3 | 143 | | poolableQueue.Enqueue(newNotification); |
| 3 | 144 | | notificationQueue.Enqueue(entry); |
| | 145 | |
|
| 3 | 146 | | entry.OnClickedNotification -= ClickedOnNotification; |
| 3 | 147 | | entry.onFocused -= FocusedOnNotification; |
| 3 | 148 | | entry.showHideAnimator.OnWillFinishHide -= SetScrollToEnd; |
| | 149 | |
|
| 3 | 150 | | PopulatePublicNotification(entry, model); |
| | 151 | |
|
| 3 | 152 | | entry.transform.SetParent(chatEntriesContainer, false); |
| 3 | 153 | | entry.RefreshControl(); |
| 3 | 154 | | entry.SetTimestamp(Utils.UnixTimeStampToLocalTime(model.Timestamp)); |
| 3 | 155 | | entry.OnClickedNotification += ClickedOnNotification; |
| 3 | 156 | | entry.onFocused += FocusedOnNotification; |
| 3 | 157 | | entry.showHideAnimator.OnWillFinishHide += SetScrollToEnd; |
| | 158 | |
|
| 3 | 159 | | chatEntriesContainer.anchoredPosition += notificationOffset; |
| | 160 | |
|
| 3 | 161 | | if (isOverPanel) |
| | 162 | | { |
| 0 | 163 | | notificationButton.gameObject.SetActive(true); |
| 0 | 164 | | IncreaseNotificationCount(); |
| | 165 | | } |
| | 166 | | else |
| | 167 | | { |
| 3 | 168 | | ResetNotificationButton(); |
| 3 | 169 | | animationCancellationToken.Cancel(); |
| 3 | 170 | | animationCancellationToken = new CancellationTokenSource(); |
| 3 | 171 | | AnimateNewEntry(entry.gameObject.transform, animationCancellationToken.Token).Forget(); |
| | 172 | | } |
| | 173 | |
|
| 3 | 174 | | if (model.ShouldPlayMentionSfx) |
| 0 | 175 | | AudioScriptableObjects.ChatReceiveMentionEvent.Play(true); |
| | 176 | |
|
| 3 | 177 | | OnResetFade?.Invoke(!isOverMessage && !isOverPanel); |
| 3 | 178 | | CheckNotificationCountAndRelease(); |
| 3 | 179 | | } |
| | 180 | |
|
| | 181 | | public void AddNewFriendRequestNotification(FriendRequestNotificationModel model) |
| | 182 | | { |
| 0 | 183 | | Pool entryPool = GetFriendRequestNotificationEntryPool(); |
| 0 | 184 | | PoolableObject newNotification = entryPool.Get(); |
| | 185 | |
|
| 0 | 186 | | var entry = newNotification.gameObject.GetComponent<FriendRequestNotificationComponentView>(); |
| 0 | 187 | | poolableQueue.Enqueue(newNotification); |
| 0 | 188 | | notificationQueue.Enqueue(entry); |
| | 189 | |
|
| 0 | 190 | | entry.OnClickedNotification -= ClickedOnFriendRequest; |
| 0 | 191 | | entry.onFocused -= FocusedOnNotification; |
| 0 | 192 | | entry.showHideAnimator.OnWillFinishHide -= SetScrollToEnd; |
| | 193 | |
|
| 0 | 194 | | PopulateFriendRequestNotification(entry, model); |
| | 195 | |
|
| 0 | 196 | | entry.transform.SetParent(chatEntriesContainer, false); |
| 0 | 197 | | entry.RefreshControl(); |
| 0 | 198 | | entry.OnClickedNotification += ClickedOnFriendRequest; |
| 0 | 199 | | entry.onFocused += FocusedOnNotification; |
| 0 | 200 | | entry.showHideAnimator.OnWillFinishHide += SetScrollToEnd; |
| | 201 | |
|
| 0 | 202 | | chatEntriesContainer.anchoredPosition += notificationOffset; |
| | 203 | |
|
| 0 | 204 | | if (isOverPanel) |
| | 205 | | { |
| 0 | 206 | | notificationButton.gameObject.SetActive(true); |
| 0 | 207 | | IncreaseNotificationCount(); |
| | 208 | | } |
| | 209 | | else |
| | 210 | | { |
| 0 | 211 | | ResetNotificationButton(); |
| 0 | 212 | | animationCancellationToken.Cancel(); |
| 0 | 213 | | animationCancellationToken = new CancellationTokenSource(); |
| 0 | 214 | | AnimateNewEntry(entry.gameObject.transform, animationCancellationToken.Token).Forget(); |
| | 215 | | } |
| | 216 | |
|
| 0 | 217 | | OnResetFade?.Invoke(!isOverMessage && !isOverPanel); |
| 0 | 218 | | CheckNotificationCountAndRelease(); |
| | 219 | |
|
| | 220 | | // TODO: refactor sfx usage into non-static |
| 0 | 221 | | AudioScriptableObjects.FriendRequestEvent.Play(); |
| 0 | 222 | | } |
| | 223 | |
|
| | 224 | | private async UniTaskVoid AnimateNewEntry(Transform notification, CancellationToken cancellationToken) |
| | 225 | | { |
| 5 | 226 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 227 | |
|
| 5 | 228 | | Sequence mySequence = DOTween.Sequence() |
| | 229 | | .AppendInterval(0.2f) |
| | 230 | | .Append(notification.DOScale(1, 0.3f).SetEase(Ease.OutBack)); |
| | 231 | |
|
| | 232 | | try |
| | 233 | | { |
| 5 | 234 | | Vector2 endPosition = new Vector2(0, 0); |
| 5 | 235 | | Vector2 currentPosition = chatEntriesContainer.anchoredPosition; |
| 5 | 236 | | notification.localScale = Vector3.zero; |
| 200 | 237 | | DOTween.To(() => currentPosition, x => currentPosition = x, endPosition, 0.8f).SetEase(Ease.OutCubic); |
| | 238 | |
|
| 10 | 239 | | while (chatEntriesContainer.anchoredPosition.y < 0) |
| | 240 | | { |
| 5 | 241 | | chatEntriesContainer.anchoredPosition = currentPosition; |
| 15 | 242 | | await UniTask.NextFrame(cancellationToken); |
| | 243 | | } |
| | 244 | |
|
| 0 | 245 | | mySequence.Play(); |
| 0 | 246 | | } |
| 0 | 247 | | catch (OperationCanceledException) |
| | 248 | | { |
| 0 | 249 | | if (!DOTween.IsTweening(notification)) |
| 0 | 250 | | notification.DOScale(1, 0.3f).SetEase(Ease.OutBack); |
| 0 | 251 | | } |
| 0 | 252 | | } |
| | 253 | |
|
| | 254 | | private void ResetNotificationButton() |
| | 255 | | { |
| 6 | 256 | | notificationButton.gameObject.SetActive(false); |
| 6 | 257 | | notificationCount = 0; |
| | 258 | |
|
| 6 | 259 | | if (notificationMessage != null) |
| 6 | 260 | | notificationMessage.text = notificationCount.ToString(); |
| 6 | 261 | | } |
| | 262 | |
|
| | 263 | | private void IncreaseNotificationCount() |
| | 264 | | { |
| 0 | 265 | | notificationCount++; |
| | 266 | |
|
| 0 | 267 | | if (notificationMessage != null) |
| 0 | 268 | | notificationMessage.text = notificationCount <= 9 ? notificationCount.ToString() : "9+"; |
| 0 | 269 | | } |
| | 270 | |
|
| | 271 | | private void SetScrollToEnd(ShowHideAnimator animator = null) |
| | 272 | | { |
| 1 | 273 | | scrollRectangle.normalizedPosition = new Vector2(0, 0); |
| 1 | 274 | | ResetNotificationButton(); |
| 1 | 275 | | } |
| | 276 | |
|
| | 277 | | private void PopulatePrivateNotification(ChatNotificationMessageComponentView view, |
| | 278 | | PrivateChatMessageNotificationModel model) |
| | 279 | | { |
| 2 | 280 | | string senderName = model.ImTheSender ? "You" : model.SenderUsername; |
| | 281 | |
|
| 2 | 282 | | view.SetIsPrivate(true); |
| 2 | 283 | | view.SetMaxContentCharacters(40 - senderName.Length); |
| 2 | 284 | | view.SetMessage(model.Body); |
| 2 | 285 | | view.SetNotificationHeader($"DM - {model.PeerUsername}"); |
| 2 | 286 | | view.SetNotificationSender($"{senderName}:"); |
| 2 | 287 | | view.SetNotificationTargetId(model.TargetId); |
| 2 | 288 | | view.SetImageVisibility(!model.ImTheSender); |
| 2 | 289 | | view.SetOwnPlayerMention(model.IsOwnPlayerMentioned); |
| | 290 | |
|
| 2 | 291 | | if (!string.IsNullOrEmpty(model.ProfilePicture)) |
| 0 | 292 | | view.SetImage(model.ProfilePicture); |
| | 293 | |
|
| 2 | 294 | | if (model.ImTheSender) |
| 1 | 295 | | view.DockRight(); |
| | 296 | | else |
| 1 | 297 | | view.DockLeft(); |
| 1 | 298 | | } |
| | 299 | |
|
| | 300 | | private void PopulatePublicNotification(ChatNotificationMessageComponentView view, |
| | 301 | | PublicChannelMessageNotificationModel model) |
| | 302 | | { |
| 3 | 303 | | string channelId = model.ChannelId; |
| 3 | 304 | | string channelName = model.ChannelName == "nearby" ? "~nearby" : $"#{model.ChannelName}"; |
| 3 | 305 | | string senderName = model.ImTheSender ? "You" : model.Username; |
| | 306 | |
|
| 3 | 307 | | view.SetIsPrivate(false); |
| 3 | 308 | | view.SetMaxContentCharacters(40 - senderName.Length); |
| 3 | 309 | | view.SetMessage(model.Body); |
| 3 | 310 | | view.SetNotificationTargetId(channelId); |
| 3 | 311 | | view.SetNotificationHeader(channelName); |
| 3 | 312 | | view.SetNotificationSender($"{senderName}:"); |
| 3 | 313 | | view.SetImageVisibility(false); |
| 3 | 314 | | view.SetOwnPlayerMention(model.IsOwnPlayerMentioned); |
| | 315 | |
|
| 3 | 316 | | if (model.ImTheSender) |
| 1 | 317 | | view.DockRight(); |
| | 318 | | else |
| 2 | 319 | | view.DockLeft(); |
| 2 | 320 | | } |
| | 321 | |
|
| | 322 | | private void ClickedOnNotification(string targetId) |
| | 323 | | { |
| 0 | 324 | | OnClickedChatMessage?.Invoke(targetId); |
| 0 | 325 | | } |
| | 326 | |
|
| | 327 | | private void PopulateFriendRequestNotification(FriendRequestNotificationComponentView friendRequestNotificationC |
| | 328 | | FriendRequestNotificationModel model) |
| | 329 | | { |
| 0 | 330 | | friendRequestNotificationComponentView.SetFriendRequestId(model.FriendRequestId); |
| 0 | 331 | | friendRequestNotificationComponentView.SetUser(model.UserId, model.UserName); |
| 0 | 332 | | friendRequestNotificationComponentView.SetHeader(model.Header); |
| 0 | 333 | | friendRequestNotificationComponentView.SetMessage(model.Message); |
| 0 | 334 | | DateTime localTime = model.Timestamp.ToLocalTime(); |
| 0 | 335 | | friendRequestNotificationComponentView.SetTimestamp($"{localTime.Hour}:{localTime.Minute:D2}"); |
| 0 | 336 | | friendRequestNotificationComponentView.SetIsAccepted(model.IsAccepted); |
| 0 | 337 | | } |
| | 338 | |
|
| | 339 | | private void ClickedOnFriendRequest(string friendRequestId, string userId, bool isAcceptedFromPeer) => |
| 0 | 340 | | OnClickedFriendRequest?.Invoke(friendRequestId, userId, isAcceptedFromPeer); |
| | 341 | |
|
| | 342 | | private void FocusedOnNotification(bool isInFocus) |
| | 343 | | { |
| 5 | 344 | | isOverMessage = isInFocus; |
| 5 | 345 | | OnResetFade?.Invoke(!isOverMessage && !isOverPanel); |
| 0 | 346 | | } |
| | 347 | |
|
| | 348 | | private void FocusedOnPanel(bool isInFocus) |
| | 349 | | { |
| 8 | 350 | | isOverPanel = isInFocus; |
| 8 | 351 | | OnPanelFocus?.Invoke(isOverPanel); |
| 8 | 352 | | OnResetFade?.Invoke(!isOverMessage && !isOverPanel); |
| 0 | 353 | | } |
| | 354 | |
|
| | 355 | | private void CheckNotificationCountAndRelease() |
| | 356 | | { |
| 10 | 357 | | if (poolableQueue.Count < MAX_NOTIFICATION_ENTRIES) return; |
| | 358 | |
|
| 0 | 359 | | BaseComponentView notificationToDequeue = notificationQueue.Dequeue(); |
| 0 | 360 | | notificationToDequeue.onFocused -= FocusedOnNotification; |
| | 361 | |
|
| 0 | 362 | | PoolableObject pooledObj = poolableQueue.Dequeue(); |
| | 363 | |
|
| | 364 | | switch (notificationToDequeue) |
| | 365 | | { |
| | 366 | | case FriendRequestNotificationComponentView: |
| 0 | 367 | | GetFriendRequestNotificationEntryPool().Release(pooledObj); |
| 0 | 368 | | break; |
| | 369 | | case ChatNotificationMessageComponentView: |
| 0 | 370 | | GetChatNotificationEntryPool().Release(pooledObj); |
| 0 | 371 | | break; |
| | 372 | | default: |
| 0 | 373 | | Debug.LogError($"Failed to release notification of type {notificationToDequeue.GetType()}. No object |
| | 374 | | break; |
| | 375 | | } |
| 0 | 376 | | } |
| | 377 | |
|
| | 378 | | private Pool GetChatNotificationEntryPool() |
| | 379 | | { |
| 5 | 380 | | var entryPool = PoolManager.i.GetPool(CHAT_NOTIFICATION_POOL_NAME_PREFIX + name + GetInstanceID()); |
| 5 | 381 | | if (entryPool != null) return entryPool; |
| | 382 | |
|
| 5 | 383 | | entryPool = PoolManager.i.AddPool( |
| | 384 | | CHAT_NOTIFICATION_POOL_NAME_PREFIX + name + GetInstanceID(), |
| | 385 | | Instantiate(chatNotification), |
| | 386 | | maxPrewarmCount: MAX_NOTIFICATION_ENTRIES, |
| | 387 | | isPersistent: true); |
| | 388 | |
|
| 5 | 389 | | entryPool.ForcePrewarm(); |
| | 390 | |
|
| 5 | 391 | | return entryPool; |
| | 392 | | } |
| | 393 | |
|
| | 394 | | private Pool GetFriendRequestNotificationEntryPool() |
| | 395 | | { |
| 0 | 396 | | var entryPool = PoolManager.i.GetPool(FRIEND_REQUEST_NOTIFICATION_POOL_NAME_PREFIX + name + GetInstanceID()) |
| | 397 | |
|
| 0 | 398 | | if (entryPool != null) |
| 0 | 399 | | return entryPool; |
| | 400 | |
|
| 0 | 401 | | entryPool = PoolManager.i.AddPool( |
| | 402 | | FRIEND_REQUEST_NOTIFICATION_POOL_NAME_PREFIX + name + GetInstanceID(), |
| | 403 | | Instantiate(friendRequestNotification), |
| | 404 | | maxPrewarmCount: MAX_NOTIFICATION_ENTRIES, |
| | 405 | | isPersistent: true); |
| | 406 | |
|
| 0 | 407 | | entryPool.ForcePrewarm(); |
| | 408 | |
|
| 0 | 409 | | return entryPool; |
| | 410 | | } |
| | 411 | |
|
| 0 | 412 | | public override void RefreshControl() { } |
| | 413 | | } |
| | 414 | | } |