| | 1 | | using System; |
| | 2 | | using System.Threading; |
| | 3 | | using Cysharp.Threading.Tasks; |
| | 4 | | using DG.Tweening; |
| | 5 | | using UnityEngine; |
| | 6 | |
|
| | 7 | | namespace DCL.Chat.Notifications |
| | 8 | | { |
| | 9 | | public class TopNotificationComponentView : BaseComponentView, ITopNotificationsComponentView |
| | 10 | | { |
| | 11 | | private const float X_OFFSET = 32f; |
| | 12 | | private const float NORMAL_CONTENT_X_POS = 111; |
| | 13 | | private const float NORMAL_HEADER_X_POS = 70; |
| | 14 | | private const int NEW_NOTIFICATION_DELAY = 5000; |
| | 15 | |
|
| | 16 | | public event Action<string> OnClickedNotification; |
| | 17 | |
|
| | 18 | | [SerializeField] private ChatNotificationMessageComponentView chatNotificationComponentView; |
| | 19 | |
|
| | 20 | | public event Action<bool> OnResetFade; |
| | 21 | |
|
| | 22 | | //This structure is temporary for the first integration of the top notification, it will change when further def |
| | 23 | | private float offsetContentXPos; |
| | 24 | | private float offsetHeaderXPos; |
| | 25 | | private int stackedNotifications; |
| 0 | 26 | | private CancellationTokenSource animationCancellationToken = new CancellationTokenSource(); |
| 0 | 27 | | private CancellationTokenSource waitCancellationToken = new CancellationTokenSource(); |
| | 28 | | private RectTransform notificationRect; |
| | 29 | |
|
| | 30 | | public bool isShowingNotification; |
| | 31 | |
|
| | 32 | | public static TopNotificationComponentView Create() |
| | 33 | | { |
| 0 | 34 | | return Instantiate(Resources.Load<TopNotificationComponentView>("SocialBarV1/TopNotificationHUD")); |
| | 35 | | } |
| | 36 | |
|
| | 37 | | public override void Start() |
| | 38 | | { |
| 0 | 39 | | chatNotificationComponentView.OnClickedNotification += ClickedOnNotification; |
| 0 | 40 | | offsetContentXPos = NORMAL_CONTENT_X_POS - X_OFFSET; |
| 0 | 41 | | offsetHeaderXPos = NORMAL_HEADER_X_POS - X_OFFSET; |
| 0 | 42 | | chatNotificationComponentView.SetPositionOffset(NORMAL_HEADER_X_POS, NORMAL_CONTENT_X_POS); |
| 0 | 43 | | notificationRect = chatNotificationComponentView.gameObject.GetComponent<RectTransform>(); |
| 0 | 44 | | chatNotificationComponentView.shouldAnimateFocus = false; |
| 0 | 45 | | chatNotificationComponentView.SetIsPrivate(true); |
| 0 | 46 | | } |
| | 47 | |
|
| | 48 | | public Transform GetPanelTransform() |
| | 49 | | { |
| 0 | 50 | | return gameObject.transform; |
| | 51 | | } |
| | 52 | |
|
| | 53 | | public void AddNewChatNotification(PrivateChatMessageNotificationModel model) |
| | 54 | | { |
| 0 | 55 | | stackedNotifications++; |
| 0 | 56 | | if (isShowingNotification && stackedNotifications <= 2) |
| | 57 | | { |
| 0 | 58 | | waitCancellationToken.Cancel(); |
| 0 | 59 | | waitCancellationToken = new CancellationTokenSource(); |
| 0 | 60 | | WaitBeforeShowingNewNotification(model, waitCancellationToken.Token).Forget(); |
| 0 | 61 | | return; |
| | 62 | | } |
| | 63 | |
|
| 0 | 64 | | isShowingNotification = true; |
| 0 | 65 | | animationCancellationToken.Cancel(); |
| 0 | 66 | | animationCancellationToken = new CancellationTokenSource(); |
| 0 | 67 | | chatNotificationComponentView.gameObject.SetActive(true); |
| 0 | 68 | | if (stackedNotifications > 2) |
| | 69 | | { |
| 0 | 70 | | OnResetFade?.Invoke(true); |
| 0 | 71 | | PopulateMultipleNotification(); |
| 0 | 72 | | chatNotificationComponentView.SetPositionOffset(NORMAL_HEADER_X_POS, NORMAL_CONTENT_X_POS); |
| 0 | 73 | | AnimateNewEntry(notificationRect, animationCancellationToken.Token).Forget(); |
| 0 | 74 | | return; |
| | 75 | | } |
| | 76 | |
|
| 0 | 77 | | stackedNotifications--; |
| 0 | 78 | | OnResetFade?.Invoke(true); |
| 0 | 79 | | PopulatePrivateNotification(model); |
| 0 | 80 | | chatNotificationComponentView.SetPositionOffset(NORMAL_HEADER_X_POS, NORMAL_CONTENT_X_POS); |
| 0 | 81 | | AnimateNewEntry(notificationRect, animationCancellationToken.Token).Forget(); |
| 0 | 82 | | ShowNotificationCooldown().Forget(); |
| 0 | 83 | | } |
| | 84 | |
|
| | 85 | | public void AddNewChatNotification(PublicChannelMessageNotificationModel model) |
| | 86 | | { |
| 0 | 87 | | stackedNotifications++; |
| 0 | 88 | | if (isShowingNotification && stackedNotifications <= 2) |
| | 89 | | { |
| 0 | 90 | | waitCancellationToken.Cancel(); |
| 0 | 91 | | waitCancellationToken = new CancellationTokenSource(); |
| 0 | 92 | | WaitBeforeShowingNewNotification(model, waitCancellationToken.Token).Forget(); |
| 0 | 93 | | return; |
| | 94 | | } |
| | 95 | |
|
| 0 | 96 | | isShowingNotification = true; |
| 0 | 97 | | animationCancellationToken.Cancel(); |
| 0 | 98 | | animationCancellationToken = new CancellationTokenSource(); |
| 0 | 99 | | chatNotificationComponentView.gameObject.SetActive(true); |
| 0 | 100 | | if (stackedNotifications > 2) |
| | 101 | | { |
| 0 | 102 | | OnResetFade?.Invoke(true); |
| 0 | 103 | | PopulateMultipleNotification(); |
| 0 | 104 | | chatNotificationComponentView.SetPositionOffset(NORMAL_HEADER_X_POS, NORMAL_CONTENT_X_POS); |
| 0 | 105 | | AnimateNewEntry(notificationRect, animationCancellationToken.Token).Forget(); |
| 0 | 106 | | return; |
| | 107 | | } |
| | 108 | |
|
| 0 | 109 | | stackedNotifications--; |
| 0 | 110 | | OnResetFade?.Invoke(true); |
| 0 | 111 | | PopulatePublicNotification(model); |
| 0 | 112 | | chatNotificationComponentView.SetPositionOffset(offsetHeaderXPos, offsetContentXPos); |
| 0 | 113 | | AnimateNewEntry(notificationRect, animationCancellationToken.Token).Forget(); |
| 0 | 114 | | ShowNotificationCooldown().Forget(); |
| 0 | 115 | | } |
| | 116 | |
|
| | 117 | | private async UniTaskVoid ShowNotificationCooldown() |
| | 118 | | { |
| 0 | 119 | | await UniTask.Delay(NEW_NOTIFICATION_DELAY); |
| 0 | 120 | | stackedNotifications--; |
| 0 | 121 | | isShowingNotification = false; |
| 0 | 122 | | } |
| | 123 | |
|
| | 124 | | private async UniTaskVoid WaitBeforeShowingNewNotification(PublicChannelMessageNotificationModel model, Cancella |
| | 125 | | { |
| 0 | 126 | | while (isShowingNotification) |
| 0 | 127 | | await UniTask.NextFrame(cancellationToken).AttachExternalCancellation(cancellationToken); |
| | 128 | |
|
| 0 | 129 | | AddNewChatNotification(model); |
| 0 | 130 | | } |
| | 131 | |
|
| | 132 | | private async UniTaskVoid WaitBeforeShowingNewNotification(PrivateChatMessageNotificationModel model, Cancellati |
| | 133 | | { |
| 0 | 134 | | while (isShowingNotification) |
| 0 | 135 | | await UniTask.NextFrame(cancellationToken).AttachExternalCancellation(cancellationToken); |
| | 136 | |
|
| 0 | 137 | | AddNewChatNotification(model); |
| 0 | 138 | | } |
| | 139 | |
|
| | 140 | | private async UniTaskVoid AnimateNewEntry(RectTransform notification, CancellationToken cancellationToken) |
| | 141 | | { |
| 0 | 142 | | cancellationToken.ThrowIfCancellationRequested(); |
| 0 | 143 | | var mySequence = DOTween.Sequence().AppendInterval(0.2f) |
| | 144 | | .Append(notification.DOScale(1, 0.3f).SetEase(Ease.OutBack)); |
| | 145 | | try |
| | 146 | | { |
| 0 | 147 | | Vector2 endPosition = new Vector2(0, 0); |
| 0 | 148 | | Vector2 currentPosition = notification.anchoredPosition; |
| 0 | 149 | | notification.localScale = Vector3.zero; |
| 0 | 150 | | DOTween.To(() => currentPosition, x => currentPosition = x, endPosition, 0.8f).SetEase(Ease.OutCubic); |
| 0 | 151 | | while (notification.anchoredPosition.y < 0) |
| | 152 | | { |
| 0 | 153 | | notification.anchoredPosition = currentPosition; |
| 0 | 154 | | await UniTask.NextFrame(cancellationToken).AttachExternalCancellation(cancellationToken); |
| | 155 | | } |
| | 156 | |
|
| 0 | 157 | | mySequence.Play(); |
| 0 | 158 | | } |
| 0 | 159 | | catch (OperationCanceledException) |
| | 160 | | { |
| 0 | 161 | | if (!DOTween.IsTweening(notification)) |
| 0 | 162 | | notification.DOScale(1, 0.3f).SetEase(Ease.OutBack); |
| 0 | 163 | | } |
| 0 | 164 | | } |
| | 165 | |
|
| | 166 | | private void PopulatePrivateNotification(PrivateChatMessageNotificationModel model) |
| | 167 | | { |
| 0 | 168 | | chatNotificationComponentView.SetIsPrivate(true); |
| 0 | 169 | | chatNotificationComponentView.SetMessage(model.Body); |
| 0 | 170 | | chatNotificationComponentView.SetNotificationHeader("Private message"); |
| 0 | 171 | | chatNotificationComponentView.SetNotificationSender($"{model.Username}:"); |
| 0 | 172 | | chatNotificationComponentView.SetNotificationTargetId(model.SenderId); |
| 0 | 173 | | } |
| | 174 | |
|
| | 175 | | private void PopulatePublicNotification(PublicChannelMessageNotificationModel model) |
| | 176 | | { |
| 0 | 177 | | chatNotificationComponentView.SetIsPrivate(false); |
| 0 | 178 | | chatNotificationComponentView.SetMessage(model.Body); |
| | 179 | |
|
| 0 | 180 | | var channelName = model.ChannelName == "nearby" ? "~nearby" : $"#{model.ChannelName}"; |
| | 181 | |
|
| 0 | 182 | | chatNotificationComponentView.SetNotificationTargetId(model.ChannelId); |
| 0 | 183 | | chatNotificationComponentView.SetNotificationHeader(channelName); |
| 0 | 184 | | chatNotificationComponentView.SetNotificationSender($"{model.Username}:"); |
| 0 | 185 | | } |
| | 186 | |
|
| | 187 | | private void PopulateMultipleNotification() |
| | 188 | | { |
| 0 | 189 | | chatNotificationComponentView.SetMessage(""); |
| 0 | 190 | | chatNotificationComponentView.SetNotificationTargetId("conversationList"); |
| 0 | 191 | | chatNotificationComponentView.SetNotificationHeader("CHAT NOTIFICATIONS"); |
| 0 | 192 | | chatNotificationComponentView.SetNotificationSender($"{stackedNotifications} messages"); |
| 0 | 193 | | chatNotificationComponentView.SetIsMultipleNotifications(); |
| 0 | 194 | | } |
| | 195 | |
|
| | 196 | | public override void Show(bool instant = false) |
| | 197 | | { |
| 0 | 198 | | if (gameObject.activeInHierarchy) |
| 0 | 199 | | return; |
| | 200 | |
|
| 0 | 201 | | chatNotificationComponentView.gameObject.SetActive(false); |
| 0 | 202 | | gameObject.SetActive(true); |
| 0 | 203 | | } |
| | 204 | |
|
| | 205 | | public override void Hide(bool instant = false) |
| | 206 | | { |
| 0 | 207 | | if (!gameObject.activeInHierarchy) |
| 0 | 208 | | return; |
| | 209 | |
|
| 0 | 210 | | isShowingNotification = false; |
| 0 | 211 | | stackedNotifications = 0; |
| 0 | 212 | | chatNotificationComponentView.gameObject.SetActive(false); |
| 0 | 213 | | gameObject.SetActive(false); |
| 0 | 214 | | } |
| | 215 | |
|
| | 216 | | public void ShowNotification() |
| | 217 | | { |
| 0 | 218 | | chatNotificationComponentView.Show(); |
| 0 | 219 | | } |
| | 220 | |
|
| | 221 | | public void HideNotification() |
| | 222 | | { |
| 0 | 223 | | isShowingNotification = false; |
| 0 | 224 | | stackedNotifications = 0; |
| 0 | 225 | | chatNotificationComponentView.Hide(); |
| 0 | 226 | | } |
| | 227 | |
|
| | 228 | | private void ClickedOnNotification(string targetId) |
| | 229 | | { |
| 0 | 230 | | HideNotification(); |
| 0 | 231 | | isShowingNotification = false; |
| 0 | 232 | | stackedNotifications = 0; |
| 0 | 233 | | OnClickedNotification?.Invoke(targetId); |
| 0 | 234 | | } |
| | 235 | |
|
| | 236 | | public override void Dispose() |
| | 237 | | { |
| 0 | 238 | | base.Dispose(); |
| | 239 | |
|
| 0 | 240 | | chatNotificationComponentView.OnClickedNotification -= ClickedOnNotification; |
| 0 | 241 | | } |
| | 242 | |
|
| | 243 | | public override void RefreshControl() |
| | 244 | | { |
| 0 | 245 | | } |
| | 246 | | } |
| | 247 | | } |