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