< 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:2
Uncovered lines:170
Coverable lines:172
Total lines:309
Line coverage:1.1% (2 of 172)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
TopNotificationComponentView()0%110100%
Create()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%2100%
PopulatePublicNotification(...)0%6200%
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 DCL.Helpers;
 3using DG.Tweening;
 4using System;
 5using System.Threading;
 6using UnityEngine;
 7
 8namespace 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;
 129        private CancellationTokenSource animationCancellationToken = new CancellationTokenSource();
 130        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        {
 039            return Instantiate(Resources.Load<TopNotificationComponentView>("SocialBarV1/TopNotificationHUD"));
 40        }
 41
 42        public override void Start()
 43        {
 044            offsetContentXPos = NORMAL_CONTENT_X_POS - X_OFFSET;
 045            offsetHeaderXPos = NORMAL_HEADER_X_POS - X_OFFSET;
 46
 047            chatNotificationComponentView.OnClickedNotification += ClickedOnNotification;
 048            chatNotificationComponentView.SetPositionOffset(NORMAL_HEADER_X_POS, NORMAL_CONTENT_X_POS);
 049            notificationRect = chatNotificationComponentView.gameObject.GetComponent<RectTransform>();
 050            chatNotificationComponentView.shouldAnimateFocus = false;
 051            chatNotificationComponentView.SetIsPrivate(true);
 52
 053            friendRequestNotificationComponentView.OnClickedNotification += ClickedOnFriendRequestNotification;
 054            friendRequestRect = friendRequestNotificationComponentView.gameObject.GetComponent<RectTransform>();
 055            friendRequestNotificationComponentView.shouldAnimateFocus = false;
 056        }
 57
 58        public Transform GetPanelTransform()
 59        {
 060            return gameObject.transform;
 61        }
 62
 63        public void AddNewChatNotification(PrivateChatMessageNotificationModel model)
 64        {
 065            stackedNotifications++;
 066            if (isShowingNotification && stackedNotifications <= 2)
 67            {
 068                waitCancellationToken.Cancel();
 069                waitCancellationToken = new CancellationTokenSource();
 070                WaitBeforeShowingNewNotification(model, waitCancellationToken.Token).Forget();
 071                return;
 72            }
 73
 074            isShowingNotification = true;
 075            animationCancellationToken.Cancel();
 076            animationCancellationToken = new CancellationTokenSource();
 077            friendRequestNotificationComponentView.gameObject.SetActive(false);
 078            chatNotificationComponentView.gameObject.SetActive(true);
 079            showableNotification = chatNotificationComponentView;
 080            if (stackedNotifications > 2)
 81            {
 082                OnResetFade?.Invoke(true);
 083                PopulateMultipleNotification();
 084                chatNotificationComponentView.SetPositionOffset(NORMAL_HEADER_X_POS, NORMAL_CONTENT_X_POS);
 085                AnimateNewEntry(notificationRect, animationCancellationToken.Token).Forget();
 086                return;
 87            }
 88
 089            stackedNotifications--;
 090            OnResetFade?.Invoke(true);
 091            PopulatePrivateNotification(model);
 092            chatNotificationComponentView.SetPositionOffset(NORMAL_HEADER_X_POS, NORMAL_CONTENT_X_POS);
 093            AnimateNewEntry(notificationRect, animationCancellationToken.Token).Forget();
 094            ShowNotificationCooldown().Forget();
 095        }
 96
 97        public void AddNewChatNotification(PublicChannelMessageNotificationModel model)
 98        {
 099            stackedNotifications++;
 0100            if (isShowingNotification && stackedNotifications <= 2)
 101            {
 0102                waitCancellationToken.Cancel();
 0103                waitCancellationToken = new CancellationTokenSource();
 0104                WaitBeforeShowingNewNotification(model, waitCancellationToken.Token).Forget();
 0105                return;
 106            }
 107
 0108            isShowingNotification = true;
 0109            animationCancellationToken.Cancel();
 0110            animationCancellationToken = new CancellationTokenSource();
 0111            friendRequestNotificationComponentView.gameObject.SetActive(false);
 0112            chatNotificationComponentView.gameObject.SetActive(true);
 0113            showableNotification = chatNotificationComponentView;
 0114            if (stackedNotifications > 2)
 115            {
 0116                OnResetFade?.Invoke(true);
 0117                PopulateMultipleNotification();
 0118                chatNotificationComponentView.SetPositionOffset(NORMAL_HEADER_X_POS, NORMAL_CONTENT_X_POS);
 0119                AnimateNewEntry(notificationRect, animationCancellationToken.Token).Forget();
 0120                return;
 121            }
 122
 0123            stackedNotifications--;
 0124            OnResetFade?.Invoke(true);
 0125            PopulatePublicNotification(model);
 0126            chatNotificationComponentView.SetPositionOffset(offsetHeaderXPos, offsetContentXPos);
 0127            AnimateNewEntry(notificationRect, animationCancellationToken.Token).Forget();
 0128            ShowNotificationCooldown().Forget();
 0129        }
 130
 131        public void AddNewFriendRequestNotification(FriendRequestNotificationModel model)
 132        {
 0133            isShowingNotification = true;
 0134            animationCancellationToken.Cancel();
 0135            animationCancellationToken = new CancellationTokenSource();
 0136            chatNotificationComponentView.gameObject.SetActive(false);
 0137            friendRequestNotificationComponentView.gameObject.SetActive(true);
 0138            showableNotification = friendRequestNotificationComponentView;
 139
 0140            OnResetFade?.Invoke(true);
 0141            PopulateFriendRequestNotification(model);
 0142            AnimateNewEntry(friendRequestRect, animationCancellationToken.Token).Forget();
 0143            ShowNotificationCooldown().Forget();
 0144        }
 145
 146        private async UniTaskVoid ShowNotificationCooldown()
 147        {
 0148            await UniTask.Delay(NEW_NOTIFICATION_DELAY);
 0149            stackedNotifications--;
 0150            isShowingNotification = false;
 0151        }
 152
 153        private async UniTaskVoid WaitBeforeShowingNewNotification(PublicChannelMessageNotificationModel model, Cancella
 154        {
 0155            while (isShowingNotification)
 0156                await UniTask.NextFrame(cancellationToken).AttachExternalCancellation(cancellationToken);
 157
 0158            AddNewChatNotification(model);
 0159        }
 160
 161        private async UniTaskVoid WaitBeforeShowingNewNotification(PrivateChatMessageNotificationModel model, Cancellati
 162        {
 0163            while (isShowingNotification)
 0164                await UniTask.NextFrame(cancellationToken).AttachExternalCancellation(cancellationToken);
 165
 0166            AddNewChatNotification(model);
 0167        }
 168
 169        private async UniTaskVoid WaitBeforeShowingNewNotification(FriendRequestNotificationModel model, CancellationTok
 170        {
 0171            while (isShowingNotification)
 0172                await UniTask.NextFrame(cancellationToken).AttachExternalCancellation(cancellationToken);
 173
 0174            AddNewFriendRequestNotification(model);
 0175        }
 176
 177        private async UniTaskVoid AnimateNewEntry(RectTransform notification, CancellationToken cancellationToken)
 178        {
 0179            cancellationToken.ThrowIfCancellationRequested();
 0180            var mySequence = DOTween.Sequence().AppendInterval(0.2f)
 181                .Append(notification.DOScale(1, 0.3f).SetEase(Ease.OutBack));
 182            try
 183            {
 0184                Vector2 endPosition = new Vector2(0, 0);
 0185                Vector2 currentPosition = notification.anchoredPosition;
 0186                notification.localScale = Vector3.zero;
 0187                DOTween.To(() => currentPosition, x => currentPosition = x, endPosition, 0.8f).SetEase(Ease.OutCubic);
 0188                while (notification.anchoredPosition.y < 0)
 189                {
 0190                    notification.anchoredPosition = currentPosition;
 0191                    await UniTask.NextFrame(cancellationToken).AttachExternalCancellation(cancellationToken);
 192                }
 193
 0194                mySequence.Play();
 0195            }
 0196            catch (OperationCanceledException)
 197            {
 0198                if (!DOTween.IsTweening(notification))
 0199                    notification.DOScale(1, 0.3f).SetEase(Ease.OutBack);
 0200            }
 0201        }
 202
 203        private void PopulatePrivateNotification(PrivateChatMessageNotificationModel model)
 204        {
 0205            chatNotificationComponentView.SetIsPrivate(true);
 0206            chatNotificationComponentView.SetMessage(model.Body);
 0207            chatNotificationComponentView.SetNotificationHeader("Private message");
 0208            chatNotificationComponentView.SetNotificationSender($"{model.Username}:");
 0209            chatNotificationComponentView.SetNotificationTargetId(model.SenderId);
 0210        }
 211
 212        private void PopulatePublicNotification(PublicChannelMessageNotificationModel model)
 213        {
 0214            chatNotificationComponentView.SetIsPrivate(false);
 0215            chatNotificationComponentView.SetMessage(model.Body);
 216
 0217            var channelName = model.ChannelName == "nearby" ? "~nearby" : $"#{model.ChannelName}";
 218
 0219            chatNotificationComponentView.SetNotificationTargetId(model.ChannelId);
 0220            chatNotificationComponentView.SetNotificationHeader(channelName);
 0221            chatNotificationComponentView.SetNotificationSender($"{model.Username}:");
 0222        }
 223
 224        private void PopulateFriendRequestNotification(FriendRequestNotificationModel model)
 225        {
 0226            friendRequestNotificationComponentView.SetFriendRequestId(model.FriendRequestId);
 0227            friendRequestNotificationComponentView.SetUser(model.UserId, model.UserName);
 0228            friendRequestNotificationComponentView.SetHeader(model.Header);
 0229            friendRequestNotificationComponentView.SetMessage(model.Message);
 0230            friendRequestNotificationComponentView.SetTimestamp(Utils.UnixTimeStampToLocalTime(model.Timestamp));
 0231            friendRequestNotificationComponentView.SetIsAccepted(model.IsAccepted);
 0232        }
 233
 234        private void PopulateMultipleNotification()
 235        {
 0236            chatNotificationComponentView.SetMessage("");
 0237            chatNotificationComponentView.SetNotificationTargetId("conversationList");
 0238            chatNotificationComponentView.SetNotificationHeader("CHAT NOTIFICATIONS");
 0239            chatNotificationComponentView.SetNotificationSender($"{stackedNotifications} messages");
 0240            chatNotificationComponentView.SetIsMultipleNotifications();
 0241        }
 242
 243        public override void Show(bool instant = false)
 244        {
 0245            if (gameObject.activeInHierarchy)
 0246                return;
 247
 0248            friendRequestNotificationComponentView.gameObject.SetActive(false);
 0249            chatNotificationComponentView.gameObject.SetActive(false);
 0250            gameObject.SetActive(true);
 0251        }
 252
 253        public override void Hide(bool instant = false)
 254        {
 0255            if (!gameObject.activeInHierarchy)
 0256                return;
 257
 0258            isShowingNotification = false;
 0259            stackedNotifications = 0;
 0260            friendRequestNotificationComponentView.gameObject.SetActive(false);
 0261            chatNotificationComponentView.gameObject.SetActive(false);
 0262            gameObject.SetActive(false);
 0263        }
 264
 265        public void ShowNotification()
 266        {
 0267            if (showableNotification == null)
 0268                return;
 269
 0270            showableNotification.Show();
 0271        }
 272
 273        public void HideNotification()
 274        {
 0275            isShowingNotification = false;
 0276            stackedNotifications = 0;
 0277            friendRequestNotificationComponentView.Hide();
 0278            chatNotificationComponentView.Hide();
 0279        }
 280
 281        private void ClickedOnNotification(string targetId)
 282        {
 0283            HideNotification();
 0284            isShowingNotification = false;
 0285            stackedNotifications = 0;
 0286            OnClickedChatMessage?.Invoke(targetId);
 0287        }
 288
 289        private void ClickedOnFriendRequestNotification(string friendRequestId, string userId, bool isAcceptedFromPeer)
 290        {
 0291            HideNotification();
 0292            isShowingNotification = false;
 0293            stackedNotifications = 0;
 0294            OnClickedFriendRequest?.Invoke(friendRequestId, userId, isAcceptedFromPeer);
 0295        }
 296
 297        public override void Dispose()
 298        {
 0299            base.Dispose();
 300
 0301            chatNotificationComponentView.OnClickedNotification -= ClickedOnNotification;
 0302            friendRequestNotificationComponentView.OnClickedNotification -= ClickedOnFriendRequestNotification;
 0303        }
 304
 305        public override void RefreshControl()
 306        {
 0307        }
 308    }
 309}