| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Threading; |
| | 4 | | using Cysharp.Threading.Tasks; |
| | 5 | | using DCL.Helpers; |
| | 6 | | using DCL.Interface; |
| | 7 | | using DG.Tweening; |
| | 8 | | using TMPro; |
| | 9 | | using UnityEngine; |
| | 10 | | using UnityEngine.UI; |
| | 11 | |
|
| | 12 | | namespace DCL.Chat.Notifications |
| | 13 | | { |
| | 14 | | public class MainChatNotificationsComponentView : BaseComponentView, IMainChatNotificationsComponentView |
| | 15 | | { |
| | 16 | | [SerializeField] private RectTransform chatEntriesContainer; |
| | 17 | | [SerializeField] private GameObject chatNotification; |
| | 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 int MAX_NOTIFICATION_ENTRIES = 30; |
| | 25 | |
|
| | 26 | | public event Action<string> OnClickedNotification; |
| | 27 | | public event Action<bool> OnResetFade; |
| | 28 | | public event Action<bool> OnPanelFocus; |
| | 29 | |
|
| 7 | 30 | | internal readonly Queue<PoolableObject> poolableQueue = new Queue<PoolableObject>(); |
| | 31 | |
|
| 7 | 32 | | internal readonly Queue<ChatNotificationMessageComponentView> notificationQueue = |
| | 33 | | new Queue<ChatNotificationMessageComponentView>(); |
| | 34 | |
|
| 7 | 35 | | private readonly Vector2 notificationOffset = new Vector2(0, -56); |
| | 36 | |
|
| | 37 | | private Pool entryPool; |
| | 38 | | private bool isOverMessage; |
| | 39 | | private bool isOverPanel; |
| 7 | 40 | | private int notificationCount = 1; |
| | 41 | | private TMP_Text notificationMessage; |
| 7 | 42 | | private CancellationTokenSource animationCancellationToken = new CancellationTokenSource(); |
| 1 | 43 | | private BaseVariable<string> openedChat => DataStore.i.HUDs.openedChat; |
| | 44 | |
|
| | 45 | | public static MainChatNotificationsComponentView Create() |
| | 46 | | { |
| 6 | 47 | | return Instantiate(Resources.Load<MainChatNotificationsComponentView>("SocialBarV1/ChatNotificationHUD")); |
| | 48 | | } |
| | 49 | |
|
| | 50 | | public void Awake() |
| | 51 | | { |
| 6 | 52 | | onFocused += FocusedOnPanel; |
| 6 | 53 | | notificationMessage = notificationButton.GetComponentInChildren<TMP_Text>(); |
| 6 | 54 | | notificationButton?.onClick.RemoveAllListeners(); |
| 6 | 55 | | notificationButton?.onClick.AddListener(() => SetScrollToEnd()); |
| 6 | 56 | | scrollRectangle.onValueChanged.AddListener(ResetNotificationButtonFromScroll); |
| 6 | 57 | | } |
| | 58 | |
|
| | 59 | | public override void Show(bool instant = false) |
| | 60 | | { |
| 1 | 61 | | openedChat.Set(""); |
| 1 | 62 | | gameObject.SetActive(true); |
| 1 | 63 | | } |
| | 64 | |
|
| | 65 | | public override void Hide(bool instant = false) |
| | 66 | | { |
| 1 | 67 | | SetScrollToEnd(); |
| 1 | 68 | | gameObject.SetActive(false); |
| 1 | 69 | | } |
| | 70 | |
|
| | 71 | | public Transform GetPanelTransform() |
| | 72 | | { |
| 0 | 73 | | return gameObject.transform; |
| | 74 | | } |
| | 75 | |
|
| | 76 | | private void ResetNotificationButtonFromScroll(Vector2 newValue) |
| | 77 | | { |
| 0 | 78 | | if (newValue.y <= 0.0) |
| | 79 | | { |
| 0 | 80 | | ResetNotificationButton(); |
| | 81 | | } |
| 0 | 82 | | } |
| | 83 | |
|
| | 84 | | public void ShowPanel() |
| | 85 | | { |
| 0 | 86 | | panelAnimator?.Show(); |
| 0 | 87 | | scrollbarAnimator?.Show(); |
| 0 | 88 | | } |
| | 89 | |
|
| | 90 | | public void HidePanel() |
| | 91 | | { |
| 0 | 92 | | panelAnimator?.Hide(); |
| 0 | 93 | | scrollbarAnimator?.Hide(); |
| 0 | 94 | | } |
| | 95 | |
|
| | 96 | | public void ShowNotifications() |
| | 97 | | { |
| 0 | 98 | | foreach (ChatNotificationMessageComponentView notification in notificationQueue) |
| | 99 | | { |
| 0 | 100 | | notification.Show(); |
| | 101 | | } |
| 0 | 102 | | } |
| | 103 | |
|
| | 104 | | public void HideNotifications() |
| | 105 | | { |
| 0 | 106 | | foreach (ChatNotificationMessageComponentView notification in notificationQueue) |
| | 107 | | { |
| 0 | 108 | | notification.Hide(); |
| | 109 | | } |
| 0 | 110 | | } |
| | 111 | |
|
| | 112 | | public void AddNewChatNotification(PrivateChatMessageNotificationModel model) |
| | 113 | | { |
| 1 | 114 | | entryPool = GetNotificationEntryPool(); |
| 1 | 115 | | var newNotification = entryPool.Get(); |
| | 116 | |
|
| 1 | 117 | | var entry = newNotification.gameObject.GetComponent<ChatNotificationMessageComponentView>(); |
| 1 | 118 | | poolableQueue.Enqueue(newNotification); |
| 1 | 119 | | notificationQueue.Enqueue(entry); |
| | 120 | |
|
| 1 | 121 | | entry.OnClickedNotification -= ClickedOnNotification; |
| 1 | 122 | | entry.onFocused -= FocusedOnNotification; |
| 1 | 123 | | entry.showHideAnimator.OnWillFinishHide -= SetScrollToEnd; |
| | 124 | |
|
| 1 | 125 | | PopulatePrivateNotification(entry, model); |
| | 126 | |
|
| 1 | 127 | | entry.transform.SetParent(chatEntriesContainer, false); |
| 1 | 128 | | entry.RefreshControl(); |
| 1 | 129 | | entry.SetTimestamp(Utils.UnixTimeStampToLocalTime(model.Timestamp)); |
| 1 | 130 | | entry.OnClickedNotification += ClickedOnNotification; |
| 1 | 131 | | entry.onFocused += FocusedOnNotification; |
| 1 | 132 | | entry.showHideAnimator.OnWillFinishHide += SetScrollToEnd; |
| | 133 | |
|
| 1 | 134 | | chatEntriesContainer.anchoredPosition += notificationOffset; |
| 1 | 135 | | if (isOverPanel) |
| | 136 | | { |
| 0 | 137 | | notificationButton.gameObject.SetActive(true); |
| 0 | 138 | | IncreaseNotificationCount(); |
| 0 | 139 | | } |
| | 140 | | else |
| | 141 | | { |
| 1 | 142 | | ResetNotificationButton(); |
| 1 | 143 | | animationCancellationToken.Cancel(); |
| 1 | 144 | | animationCancellationToken = new CancellationTokenSource(); |
| 1 | 145 | | AnimateNewEntry(entry.gameObject.transform, animationCancellationToken.Token).Forget(); |
| | 146 | | } |
| | 147 | |
|
| 1 | 148 | | OnResetFade?.Invoke(!isOverMessage && !isOverPanel); |
| 1 | 149 | | CheckNotificationCountAndRelease(); |
| 1 | 150 | | } |
| | 151 | |
|
| | 152 | | public void AddNewChatNotification(PublicChannelMessageNotificationModel model) |
| | 153 | | { |
| 2 | 154 | | entryPool = GetNotificationEntryPool(); |
| 2 | 155 | | var newNotification = entryPool.Get(); |
| | 156 | |
|
| 2 | 157 | | var entry = newNotification.gameObject.GetComponent<ChatNotificationMessageComponentView>(); |
| 2 | 158 | | poolableQueue.Enqueue(newNotification); |
| 2 | 159 | | notificationQueue.Enqueue(entry); |
| | 160 | |
|
| 2 | 161 | | entry.OnClickedNotification -= ClickedOnNotification; |
| 2 | 162 | | entry.onFocused -= FocusedOnNotification; |
| 2 | 163 | | entry.showHideAnimator.OnWillFinishHide -= SetScrollToEnd; |
| | 164 | |
|
| 2 | 165 | | PopulatePublicNotification(entry, model); |
| | 166 | |
|
| 2 | 167 | | entry.transform.SetParent(chatEntriesContainer, false); |
| 2 | 168 | | entry.RefreshControl(); |
| 2 | 169 | | entry.SetTimestamp(Utils.UnixTimeStampToLocalTime(model.Timestamp)); |
| 2 | 170 | | entry.OnClickedNotification += ClickedOnNotification; |
| 2 | 171 | | entry.onFocused += FocusedOnNotification; |
| 2 | 172 | | entry.showHideAnimator.OnWillFinishHide += SetScrollToEnd; |
| | 173 | |
|
| 2 | 174 | | chatEntriesContainer.anchoredPosition += notificationOffset; |
| 2 | 175 | | if (isOverPanel) |
| | 176 | | { |
| 0 | 177 | | notificationButton.gameObject.SetActive(true); |
| 0 | 178 | | IncreaseNotificationCount(); |
| 0 | 179 | | } |
| | 180 | | else |
| | 181 | | { |
| 2 | 182 | | ResetNotificationButton(); |
| 2 | 183 | | animationCancellationToken.Cancel(); |
| 2 | 184 | | animationCancellationToken = new CancellationTokenSource(); |
| 2 | 185 | | AnimateNewEntry(entry.gameObject.transform, animationCancellationToken.Token).Forget(); |
| | 186 | | } |
| | 187 | |
|
| 2 | 188 | | OnResetFade?.Invoke(!isOverMessage && !isOverPanel); |
| 2 | 189 | | CheckNotificationCountAndRelease(); |
| 2 | 190 | | } |
| | 191 | |
|
| | 192 | | private async UniTaskVoid AnimateNewEntry(Transform notification, CancellationToken cancellationToken) |
| | 193 | | { |
| 3 | 194 | | cancellationToken.ThrowIfCancellationRequested(); |
| 3 | 195 | | Sequence mySequence = DOTween.Sequence().AppendInterval(0.2f) |
| | 196 | | .Append(notification.DOScale(1, 0.3f).SetEase(Ease.OutBack)); |
| | 197 | | try |
| | 198 | | { |
| 3 | 199 | | Vector2 endPosition = new Vector2(0, 0); |
| 3 | 200 | | Vector2 currentPosition = chatEntriesContainer.anchoredPosition; |
| 3 | 201 | | notification.localScale = Vector3.zero; |
| 105 | 202 | | DOTween.To(() => currentPosition, x => currentPosition = x, endPosition, 0.8f).SetEase(Ease.OutCubic); |
| 6 | 203 | | while (chatEntriesContainer.anchoredPosition.y < 0) |
| | 204 | | { |
| 3 | 205 | | chatEntriesContainer.anchoredPosition = currentPosition; |
| 9 | 206 | | await UniTask.NextFrame(cancellationToken); |
| | 207 | | } |
| | 208 | |
|
| 0 | 209 | | mySequence.Play(); |
| 0 | 210 | | } |
| 0 | 211 | | catch (OperationCanceledException) |
| | 212 | | { |
| 0 | 213 | | if (!DOTween.IsTweening(notification)) |
| 0 | 214 | | notification.DOScale(1, 0.3f).SetEase(Ease.OutBack); |
| 0 | 215 | | } |
| 0 | 216 | | } |
| | 217 | |
|
| | 218 | | private void ResetNotificationButton() |
| | 219 | | { |
| 4 | 220 | | notificationButton.gameObject.SetActive(false); |
| 4 | 221 | | notificationCount = 0; |
| | 222 | |
|
| 4 | 223 | | if (notificationMessage != null) |
| 4 | 224 | | notificationMessage.text = notificationCount.ToString(); |
| 4 | 225 | | } |
| | 226 | |
|
| | 227 | | private void IncreaseNotificationCount() |
| | 228 | | { |
| 0 | 229 | | notificationCount++; |
| 0 | 230 | | if (notificationMessage != null) |
| 0 | 231 | | notificationMessage.text = notificationCount <= 9 ? notificationCount.ToString() : "9+"; |
| 0 | 232 | | } |
| | 233 | |
|
| | 234 | | private void SetScrollToEnd(ShowHideAnimator animator = null) |
| | 235 | | { |
| 1 | 236 | | scrollRectangle.normalizedPosition = new Vector2(0, 0); |
| 1 | 237 | | ResetNotificationButton(); |
| 1 | 238 | | } |
| | 239 | |
|
| | 240 | | private void PopulatePrivateNotification(ChatNotificationMessageComponentView chatNotificationComponentView, |
| | 241 | | PrivateChatMessageNotificationModel model) |
| | 242 | | { |
| 1 | 243 | | chatNotificationComponentView.SetIsPrivate(true); |
| 1 | 244 | | chatNotificationComponentView.SetMessage(model.Body); |
| 1 | 245 | | chatNotificationComponentView.SetNotificationHeader("Private message"); |
| 1 | 246 | | chatNotificationComponentView.SetNotificationSender($"{model.Username}:"); |
| 1 | 247 | | chatNotificationComponentView.SetNotificationTargetId(model.SenderId); |
| 1 | 248 | | if (!string.IsNullOrEmpty(model.ProfilePicture)) |
| 0 | 249 | | chatNotificationComponentView.SetImage(model.ProfilePicture); |
| 1 | 250 | | } |
| | 251 | |
|
| | 252 | | private void PopulatePublicNotification(ChatNotificationMessageComponentView chatNotificationComponentView, |
| | 253 | | PublicChannelMessageNotificationModel model) |
| | 254 | | { |
| 2 | 255 | | chatNotificationComponentView.SetIsPrivate(false); |
| 2 | 256 | | chatNotificationComponentView.SetMessage(model.Body); |
| | 257 | |
|
| 2 | 258 | | var channelId = model.ChannelId; |
| 2 | 259 | | var channelName = model.ChannelName == "nearby" ? "~nearby" : $"#{model.ChannelName}"; |
| | 260 | |
|
| 2 | 261 | | chatNotificationComponentView.SetNotificationTargetId(channelId); |
| 2 | 262 | | chatNotificationComponentView.SetNotificationHeader(channelName); |
| 2 | 263 | | chatNotificationComponentView.SetNotificationSender($"{model.Username}:"); |
| 2 | 264 | | } |
| | 265 | |
|
| | 266 | | private void ClickedOnNotification(string targetId) |
| | 267 | | { |
| 0 | 268 | | OnClickedNotification?.Invoke(targetId); |
| 0 | 269 | | } |
| | 270 | |
|
| | 271 | | private void FocusedOnNotification(bool isInFocus) |
| | 272 | | { |
| 3 | 273 | | isOverMessage = isInFocus; |
| 3 | 274 | | OnResetFade?.Invoke(!isOverMessage && !isOverPanel); |
| 0 | 275 | | } |
| | 276 | |
|
| | 277 | | private void FocusedOnPanel(bool isInFocus) |
| | 278 | | { |
| 6 | 279 | | isOverPanel = isInFocus; |
| 6 | 280 | | OnPanelFocus?.Invoke(isOverPanel); |
| 6 | 281 | | OnResetFade?.Invoke(!isOverMessage && !isOverPanel); |
| 0 | 282 | | } |
| | 283 | |
|
| | 284 | | private void CheckNotificationCountAndRelease() |
| | 285 | | { |
| 3 | 286 | | if (poolableQueue.Count >= MAX_NOTIFICATION_ENTRIES) |
| | 287 | | { |
| 0 | 288 | | ChatNotificationMessageComponentView notificationToDequeue = notificationQueue.Dequeue(); |
| 0 | 289 | | notificationToDequeue.onFocused -= FocusedOnNotification; |
| 0 | 290 | | entryPool.Release(poolableQueue.Dequeue()); |
| | 291 | | } |
| 3 | 292 | | } |
| | 293 | |
|
| | 294 | | private Pool GetNotificationEntryPool() |
| | 295 | | { |
| 3 | 296 | | var entryPool = PoolManager.i.GetPool(NOTIFICATION_POOL_NAME_PREFIX + name + GetInstanceID()); |
| 3 | 297 | | if (entryPool != null) return entryPool; |
| | 298 | |
|
| 3 | 299 | | entryPool = PoolManager.i.AddPool( |
| | 300 | | NOTIFICATION_POOL_NAME_PREFIX + name + GetInstanceID(), |
| | 301 | | Instantiate(chatNotification).gameObject, |
| | 302 | | maxPrewarmCount: MAX_NOTIFICATION_ENTRIES, |
| | 303 | | isPersistent: true); |
| 3 | 304 | | entryPool.ForcePrewarm(); |
| | 305 | |
|
| 3 | 306 | | return entryPool; |
| | 307 | | } |
| | 308 | |
|
| | 309 | | public override void RefreshControl() |
| | 310 | | { |
| 0 | 311 | | } |
| | 312 | | } |
| | 313 | | } |