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