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