< Summary

Class:DCL.Chat.Notifications.TopNotificationComponentView
Assembly:NotificationMessagesHUD
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/NotificationMessagesHUD/TopNotificationComponentView.cs
Covered lines:0
Uncovered lines:175
Coverable lines:175
Total lines:307
Line coverage:0% (0 of 175)
Covered branches:0
Total branches:0
Covered methods:0
Total methods:23
Method coverage:0% (0 of 23)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
TopNotificationComponentView()0%2100%
Start()0%2100%
GetPanelTransform()0%2100%
AddNewChatNotification(...)0%42600%
AddNewChatNotification(...)0%42600%
AddNewFriendRequestNotification(...)0%6200%
ShowNotificationCooldown()0%12300%
WaitBeforeShowingNewNotification()0%20400%
WaitBeforeShowingNewNotification()0%20400%
WaitBeforeShowingNewNotification()0%20400%
AnimateNewEntry()0%42600%
PopulatePrivateNotification(...)0%6200%
PopulatePublicNotification(...)0%12300%
PopulateFriendRequestNotification(...)0%2100%
PopulateMultipleNotification()0%2100%
Show(...)0%6200%
Hide(...)0%6200%
ShowNotification()0%6200%
HideNotification()0%2100%
ClickedOnNotification(...)0%6200%
ClickedOnFriendRequestNotification(...)0%6200%
Dispose()0%2100%
RefreshControl()0%2100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/NotificationMessagesHUD/TopNotificationComponentView.cs

#LineLine coverage
 1using Cysharp.Threading.Tasks;
 2using DG.Tweening;
 3using System;
 4using System.Threading;
 5using UnityEngine;
 6
 7namespace 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;
 028        private CancellationTokenSource animationCancellationToken = new CancellationTokenSource();
 029        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        {
 038            offsetContentXPos = NORMAL_CONTENT_X_POS - X_OFFSET;
 039            offsetHeaderXPos = NORMAL_HEADER_X_POS - X_OFFSET;
 40
 041            chatNotificationComponentView.OnClickedNotification += ClickedOnNotification;
 042            notificationRect = chatNotificationComponentView.gameObject.GetComponent<RectTransform>();
 043            chatNotificationComponentView.shouldAnimateFocus = false;
 044            chatNotificationComponentView.SetIsPrivate(true);
 45
 046            friendRequestNotificationComponentView.OnClickedNotification += ClickedOnFriendRequestNotification;
 047            friendRequestRect = friendRequestNotificationComponentView.gameObject.GetComponent<RectTransform>();
 048            friendRequestNotificationComponentView.shouldAnimateFocus = false;
 049        }
 50
 51        public Transform GetPanelTransform()
 52        {
 053            return gameObject.transform;
 54        }
 55
 56        public void AddNewChatNotification(PrivateChatMessageNotificationModel model)
 57        {
 058            stackedNotifications++;
 059            if (isShowingNotification && stackedNotifications <= 2)
 60            {
 061                waitCancellationToken.Cancel();
 062                waitCancellationToken = new CancellationTokenSource();
 063                WaitBeforeShowingNewNotification(model, waitCancellationToken.Token).Forget();
 064                return;
 65            }
 66
 067            isShowingNotification = true;
 068            animationCancellationToken.Cancel();
 069            animationCancellationToken = new CancellationTokenSource();
 070            friendRequestNotificationComponentView.gameObject.SetActive(false);
 071            chatNotificationComponentView.gameObject.SetActive(true);
 072            showableNotification = chatNotificationComponentView;
 073            if (stackedNotifications > 2)
 74            {
 075                OnResetFade?.Invoke(true);
 076                PopulateMultipleNotification();
 077                AnimateNewEntry(notificationRect, animationCancellationToken.Token).Forget();
 078                return;
 79            }
 80
 081            stackedNotifications--;
 082            OnResetFade?.Invoke(true);
 083            PopulatePrivateNotification(model);
 084            AnimateNewEntry(notificationRect, animationCancellationToken.Token).Forget();
 085            ShowNotificationCooldown().Forget();
 086        }
 87
 88        public void AddNewChatNotification(PublicChannelMessageNotificationModel model)
 89        {
 090            stackedNotifications++;
 091            if (isShowingNotification && stackedNotifications <= 2)
 92            {
 093                waitCancellationToken.Cancel();
 094                waitCancellationToken = new CancellationTokenSource();
 095                WaitBeforeShowingNewNotification(model, waitCancellationToken.Token).Forget();
 096                return;
 97            }
 98
 099            isShowingNotification = true;
 0100            animationCancellationToken.Cancel();
 0101            animationCancellationToken = new CancellationTokenSource();
 0102            friendRequestNotificationComponentView.gameObject.SetActive(false);
 0103            chatNotificationComponentView.gameObject.SetActive(true);
 0104            showableNotification = chatNotificationComponentView;
 0105            if (stackedNotifications > 2)
 106            {
 0107                OnResetFade?.Invoke(true);
 0108                PopulateMultipleNotification();
 0109                AnimateNewEntry(notificationRect, animationCancellationToken.Token).Forget();
 0110                return;
 111            }
 112
 0113            stackedNotifications--;
 0114            OnResetFade?.Invoke(true);
 0115            PopulatePublicNotification(model);
 0116            AnimateNewEntry(notificationRect, animationCancellationToken.Token).Forget();
 0117            ShowNotificationCooldown().Forget();
 0118        }
 119
 120        public void AddNewFriendRequestNotification(FriendRequestNotificationModel model)
 121        {
 0122            isShowingNotification = true;
 0123            animationCancellationToken.Cancel();
 0124            animationCancellationToken = new CancellationTokenSource();
 0125            chatNotificationComponentView.gameObject.SetActive(false);
 0126            friendRequestNotificationComponentView.gameObject.SetActive(true);
 0127            showableNotification = friendRequestNotificationComponentView;
 128
 0129            OnResetFade?.Invoke(true);
 0130            PopulateFriendRequestNotification(model);
 0131            AnimateNewEntry(friendRequestRect, animationCancellationToken.Token).Forget();
 0132            ShowNotificationCooldown().Forget();
 0133        }
 134
 135        private async UniTaskVoid ShowNotificationCooldown()
 136        {
 0137            await UniTask.Delay(NEW_NOTIFICATION_DELAY);
 0138            stackedNotifications--;
 0139            isShowingNotification = false;
 0140        }
 141
 142        private async UniTaskVoid WaitBeforeShowingNewNotification(PublicChannelMessageNotificationModel model, Cancella
 143        {
 0144            while (isShowingNotification)
 0145                await UniTask.NextFrame(cancellationToken).AttachExternalCancellation(cancellationToken);
 146
 0147            AddNewChatNotification(model);
 0148        }
 149
 150        private async UniTaskVoid WaitBeforeShowingNewNotification(PrivateChatMessageNotificationModel model, Cancellati
 151        {
 0152            while (isShowingNotification)
 0153                await UniTask.NextFrame(cancellationToken).AttachExternalCancellation(cancellationToken);
 154
 0155            AddNewChatNotification(model);
 0156        }
 157
 158        private async UniTaskVoid WaitBeforeShowingNewNotification(FriendRequestNotificationModel model, CancellationTok
 159        {
 0160            while (isShowingNotification)
 0161                await UniTask.NextFrame(cancellationToken).AttachExternalCancellation(cancellationToken);
 162
 0163            AddNewFriendRequestNotification(model);
 0164        }
 165
 166        private async UniTaskVoid AnimateNewEntry(RectTransform notification, CancellationToken cancellationToken)
 167        {
 0168            cancellationToken.ThrowIfCancellationRequested();
 0169            var mySequence = DOTween.Sequence().AppendInterval(0.2f)
 170                .Append(notification.DOScale(1, 0.3f).SetEase(Ease.OutBack));
 171            try
 172            {
 0173                Vector2 endPosition = new Vector2(0, 0);
 0174                Vector2 currentPosition = notification.anchoredPosition;
 0175                notification.localScale = Vector3.zero;
 0176                DOTween.To(() => currentPosition, x => currentPosition = x, endPosition, 0.8f).SetEase(Ease.OutCubic);
 0177                while (notification.anchoredPosition.y < 0)
 178                {
 0179                    notification.anchoredPosition = currentPosition;
 0180                    await UniTask.NextFrame(cancellationToken).AttachExternalCancellation(cancellationToken);
 181                }
 182
 0183                mySequence.Play();
 0184            }
 0185            catch (OperationCanceledException)
 186            {
 0187                if (!DOTween.IsTweening(notification))
 0188                    notification.DOScale(1, 0.3f).SetEase(Ease.OutBack);
 0189            }
 0190        }
 191
 192        private void PopulatePrivateNotification(PrivateChatMessageNotificationModel model)
 193        {
 0194            string senderName = model.ImTheSender ? "You" : model.SenderUsername;
 195
 0196            chatNotificationComponentView.SetIsPrivate(true);
 0197            chatNotificationComponentView.SetMaxContentCharacters(40 - senderName.Length);
 0198            chatNotificationComponentView.SetMessage(model.Body);
 0199            chatNotificationComponentView.SetNotificationHeader($"DM - {model.PeerUsername}");
 0200            chatNotificationComponentView.SetNotificationSender($"{senderName}:");
 0201            chatNotificationComponentView.SetNotificationTargetId(model.TargetId);
 0202            chatNotificationComponentView.SetImageVisibility(true);
 0203            chatNotificationComponentView.SetOwnPlayerMention(model.IsOwnPlayerMentioned);
 0204            chatNotificationComponentView.SetImage(model.ProfilePicture);
 0205        }
 206
 207        private void PopulatePublicNotification(PublicChannelMessageNotificationModel model)
 208        {
 0209            string channelName = model.ChannelName == "nearby" ? "~nearby" : $"#{model.ChannelName}";
 0210            string senderName = model.ImTheSender ? "You" : model.Username;
 211
 0212            chatNotificationComponentView.SetIsPrivate(false);
 0213            chatNotificationComponentView.SetMaxContentCharacters(40 - senderName.Length);
 0214            chatNotificationComponentView.SetMessage(model.Body);
 0215            chatNotificationComponentView.SetNotificationTargetId(model.ChannelId);
 0216            chatNotificationComponentView.SetNotificationHeader(channelName);
 0217            chatNotificationComponentView.SetNotificationSender($"{senderName}:");
 0218            chatNotificationComponentView.SetImageVisibility(false);
 0219        }
 220
 221        private void PopulateFriendRequestNotification(FriendRequestNotificationModel model)
 222        {
 0223            friendRequestNotificationComponentView.SetFriendRequestId(model.FriendRequestId);
 0224            friendRequestNotificationComponentView.SetUser(model.UserId, model.UserName);
 0225            friendRequestNotificationComponentView.SetHeader(model.Header);
 0226            friendRequestNotificationComponentView.SetMessage(model.Message);
 0227            DateTime localTime = model.Timestamp.ToLocalTime();
 0228            friendRequestNotificationComponentView.SetTimestamp($"{localTime.Hour}:{localTime.Minute:D2}");
 0229            friendRequestNotificationComponentView.SetIsAccepted(model.IsAccepted);
 0230        }
 231
 232        private void PopulateMultipleNotification()
 233        {
 0234            chatNotificationComponentView.SetMessage("");
 0235            chatNotificationComponentView.SetNotificationTargetId("conversationList");
 0236            chatNotificationComponentView.SetNotificationHeader("CHAT NOTIFICATIONS");
 0237            chatNotificationComponentView.SetNotificationSender($"{stackedNotifications} messages");
 0238            chatNotificationComponentView.SetIsMultipleNotifications();
 0239        }
 240
 241        public override void Show(bool instant = false)
 242        {
 0243            if (gameObject.activeInHierarchy)
 0244                return;
 245
 0246            friendRequestNotificationComponentView.gameObject.SetActive(false);
 0247            chatNotificationComponentView.gameObject.SetActive(false);
 0248            gameObject.SetActive(true);
 0249        }
 250
 251        public override void Hide(bool instant = false)
 252        {
 0253            if (!gameObject.activeInHierarchy)
 0254                return;
 255
 0256            isShowingNotification = false;
 0257            stackedNotifications = 0;
 0258            friendRequestNotificationComponentView.gameObject.SetActive(false);
 0259            chatNotificationComponentView.gameObject.SetActive(false);
 0260            gameObject.SetActive(false);
 0261        }
 262
 263        public void ShowNotification()
 264        {
 0265            if (showableNotification == null)
 0266                return;
 267
 0268            showableNotification.Show();
 0269        }
 270
 271        public void HideNotification()
 272        {
 0273            isShowingNotification = false;
 0274            stackedNotifications = 0;
 0275            friendRequestNotificationComponentView.Hide();
 0276            chatNotificationComponentView.Hide();
 0277        }
 278
 279        private void ClickedOnNotification(string targetId)
 280        {
 0281            HideNotification();
 0282            isShowingNotification = false;
 0283            stackedNotifications = 0;
 0284            OnClickedChatMessage?.Invoke(targetId);
 0285        }
 286
 287        private void ClickedOnFriendRequestNotification(string friendRequestId, string userId, bool isAcceptedFromPeer)
 288        {
 0289            HideNotification();
 0290            isShowingNotification = false;
 0291            stackedNotifications = 0;
 0292            OnClickedFriendRequest?.Invoke(friendRequestId, userId, isAcceptedFromPeer);
 0293        }
 294
 295        public override void Dispose()
 296        {
 0297            base.Dispose();
 298
 0299            chatNotificationComponentView.OnClickedNotification -= ClickedOnNotification;
 0300            friendRequestNotificationComponentView.OnClickedNotification -= ClickedOnFriendRequestNotification;
 0301        }
 302
 303        public override void RefreshControl()
 304        {
 0305        }
 306    }
 307}