< 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:132
Coverable lines:132
Total lines:247
Line coverage:0% (0 of 132)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
TopNotificationComponentView()0%2100%
Create()0%2100%
Start()0%2100%
GetPanelTransform()0%2100%
AddNewChatNotification(...)0%42600%
AddNewChatNotification(...)0%42600%
ShowNotificationCooldown()0%12300%
WaitBeforeShowingNewNotification()0%20400%
WaitBeforeShowingNewNotification()0%20400%
AnimateNewEntry()0%42600%
PopulatePrivateNotification(...)0%2100%
PopulatePublicNotification(...)0%6200%
PopulateMultipleNotification()0%2100%
Show(...)0%6200%
Hide(...)0%6200%
ShowNotification()0%2100%
HideNotification()0%2100%
ClickedOnNotification(...)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 System;
 2using System.Threading;
 3using Cysharp.Threading.Tasks;
 4using DG.Tweening;
 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> 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;
 026        private CancellationTokenSource animationCancellationToken = new CancellationTokenSource();
 027        private CancellationTokenSource waitCancellationToken = new CancellationTokenSource();
 28        private RectTransform notificationRect;
 29
 30        public bool isShowingNotification;
 31
 32        public static TopNotificationComponentView Create()
 33        {
 034            return Instantiate(Resources.Load<TopNotificationComponentView>("SocialBarV1/TopNotificationHUD"));
 35        }
 36
 37        public override void Start()
 38        {
 039            chatNotificationComponentView.OnClickedNotification += ClickedOnNotification;
 040            offsetContentXPos = NORMAL_CONTENT_X_POS - X_OFFSET;
 041            offsetHeaderXPos = NORMAL_HEADER_X_POS - X_OFFSET;
 042            chatNotificationComponentView.SetPositionOffset(NORMAL_HEADER_X_POS, NORMAL_CONTENT_X_POS);
 043            notificationRect = chatNotificationComponentView.gameObject.GetComponent<RectTransform>();
 044            chatNotificationComponentView.shouldAnimateFocus = false;
 045            chatNotificationComponentView.SetIsPrivate(true);
 046        }
 47
 48        public Transform GetPanelTransform()
 49        {
 050            return gameObject.transform;
 51        }
 52
 53        public void AddNewChatNotification(PrivateChatMessageNotificationModel model)
 54        {
 055            stackedNotifications++;
 056            if (isShowingNotification && stackedNotifications <= 2)
 57            {
 058                waitCancellationToken.Cancel();
 059                waitCancellationToken = new CancellationTokenSource();
 060                WaitBeforeShowingNewNotification(model, waitCancellationToken.Token).Forget();
 061                return;
 62            }
 63
 064            isShowingNotification = true;
 065            animationCancellationToken.Cancel();
 066            animationCancellationToken = new CancellationTokenSource();
 067            chatNotificationComponentView.gameObject.SetActive(true);
 068            if (stackedNotifications > 2)
 69            {
 070                OnResetFade?.Invoke(true);
 071                PopulateMultipleNotification();
 072                chatNotificationComponentView.SetPositionOffset(NORMAL_HEADER_X_POS, NORMAL_CONTENT_X_POS);
 073                AnimateNewEntry(notificationRect, animationCancellationToken.Token).Forget();
 074                return;
 75            }
 76
 077            stackedNotifications--;
 078            OnResetFade?.Invoke(true);
 079            PopulatePrivateNotification(model);
 080            chatNotificationComponentView.SetPositionOffset(NORMAL_HEADER_X_POS, NORMAL_CONTENT_X_POS);
 081            AnimateNewEntry(notificationRect, animationCancellationToken.Token).Forget();
 082            ShowNotificationCooldown().Forget();
 083        }
 84
 85        public void AddNewChatNotification(PublicChannelMessageNotificationModel model)
 86        {
 087            stackedNotifications++;
 088            if (isShowingNotification && stackedNotifications <= 2)
 89            {
 090                waitCancellationToken.Cancel();
 091                waitCancellationToken = new CancellationTokenSource();
 092                WaitBeforeShowingNewNotification(model, waitCancellationToken.Token).Forget();
 093                return;
 94            }
 95
 096            isShowingNotification = true;
 097            animationCancellationToken.Cancel();
 098            animationCancellationToken = new CancellationTokenSource();
 099            chatNotificationComponentView.gameObject.SetActive(true);
 0100            if (stackedNotifications > 2)
 101            {
 0102                OnResetFade?.Invoke(true);
 0103                PopulateMultipleNotification();
 0104                chatNotificationComponentView.SetPositionOffset(NORMAL_HEADER_X_POS, NORMAL_CONTENT_X_POS);
 0105                AnimateNewEntry(notificationRect, animationCancellationToken.Token).Forget();
 0106                return;
 107            }
 108
 0109            stackedNotifications--;
 0110            OnResetFade?.Invoke(true);
 0111            PopulatePublicNotification(model);
 0112            chatNotificationComponentView.SetPositionOffset(offsetHeaderXPos, offsetContentXPos);
 0113            AnimateNewEntry(notificationRect, animationCancellationToken.Token).Forget();
 0114            ShowNotificationCooldown().Forget();
 0115        }
 116
 117        private async UniTaskVoid ShowNotificationCooldown()
 118        {
 0119            await UniTask.Delay(NEW_NOTIFICATION_DELAY);
 0120            stackedNotifications--;
 0121            isShowingNotification = false;
 0122        }
 123
 124        private async UniTaskVoid WaitBeforeShowingNewNotification(PublicChannelMessageNotificationModel model, Cancella
 125        {
 0126            while (isShowingNotification)
 0127                await UniTask.NextFrame(cancellationToken).AttachExternalCancellation(cancellationToken);
 128
 0129            AddNewChatNotification(model);
 0130        }
 131
 132        private async UniTaskVoid WaitBeforeShowingNewNotification(PrivateChatMessageNotificationModel model, Cancellati
 133        {
 0134            while (isShowingNotification)
 0135                await UniTask.NextFrame(cancellationToken).AttachExternalCancellation(cancellationToken);
 136
 0137            AddNewChatNotification(model);
 0138        }
 139
 140        private async UniTaskVoid AnimateNewEntry(RectTransform notification, CancellationToken cancellationToken)
 141        {
 0142            cancellationToken.ThrowIfCancellationRequested();
 0143            var mySequence = DOTween.Sequence().AppendInterval(0.2f)
 144                .Append(notification.DOScale(1, 0.3f).SetEase(Ease.OutBack));
 145            try
 146            {
 0147                Vector2 endPosition = new Vector2(0, 0);
 0148                Vector2 currentPosition = notification.anchoredPosition;
 0149                notification.localScale = Vector3.zero;
 0150                DOTween.To(() => currentPosition, x => currentPosition = x, endPosition, 0.8f).SetEase(Ease.OutCubic);
 0151                while (notification.anchoredPosition.y < 0)
 152                {
 0153                    notification.anchoredPosition = currentPosition;
 0154                    await UniTask.NextFrame(cancellationToken).AttachExternalCancellation(cancellationToken);
 155                }
 156
 0157                mySequence.Play();
 0158            }
 0159            catch (OperationCanceledException)
 160            {
 0161                if (!DOTween.IsTweening(notification))
 0162                    notification.DOScale(1, 0.3f).SetEase(Ease.OutBack);
 0163            }
 0164        }
 165
 166        private void PopulatePrivateNotification(PrivateChatMessageNotificationModel model)
 167        {
 0168            chatNotificationComponentView.SetIsPrivate(true);
 0169            chatNotificationComponentView.SetMessage(model.Body);
 0170            chatNotificationComponentView.SetNotificationHeader("Private message");
 0171            chatNotificationComponentView.SetNotificationSender($"{model.Username}:");
 0172            chatNotificationComponentView.SetNotificationTargetId(model.SenderId);
 0173        }
 174
 175        private void PopulatePublicNotification(PublicChannelMessageNotificationModel model)
 176        {
 0177            chatNotificationComponentView.SetIsPrivate(false);
 0178            chatNotificationComponentView.SetMessage(model.Body);
 179
 0180            var channelName = model.ChannelName == "nearby" ? "~nearby" : $"#{model.ChannelName}";
 181
 0182            chatNotificationComponentView.SetNotificationTargetId(model.ChannelId);
 0183            chatNotificationComponentView.SetNotificationHeader(channelName);
 0184            chatNotificationComponentView.SetNotificationSender($"{model.Username}:");
 0185        }
 186
 187        private void PopulateMultipleNotification()
 188        {
 0189            chatNotificationComponentView.SetMessage("");
 0190            chatNotificationComponentView.SetNotificationTargetId("conversationList");
 0191            chatNotificationComponentView.SetNotificationHeader("CHAT NOTIFICATIONS");
 0192            chatNotificationComponentView.SetNotificationSender($"{stackedNotifications} messages");
 0193            chatNotificationComponentView.SetIsMultipleNotifications();
 0194        }
 195
 196        public override void Show(bool instant = false)
 197        {
 0198            if (gameObject.activeInHierarchy)
 0199                return;
 200
 0201            chatNotificationComponentView.gameObject.SetActive(false);
 0202            gameObject.SetActive(true);
 0203        }
 204
 205        public override void Hide(bool instant = false)
 206        {
 0207            if (!gameObject.activeInHierarchy)
 0208                return;
 209
 0210            isShowingNotification = false;
 0211            stackedNotifications = 0;
 0212            chatNotificationComponentView.gameObject.SetActive(false);
 0213            gameObject.SetActive(false);
 0214        }
 215
 216        public void ShowNotification()
 217        {
 0218            chatNotificationComponentView.Show();
 0219        }
 220
 221        public void HideNotification()
 222        {
 0223            isShowingNotification = false;
 0224            stackedNotifications = 0;
 0225            chatNotificationComponentView.Hide();
 0226        }
 227
 228        private void ClickedOnNotification(string targetId)
 229        {
 0230            HideNotification();
 0231            isShowingNotification = false;
 0232            stackedNotifications = 0;
 0233            OnClickedNotification?.Invoke(targetId);
 0234        }
 235
 236        public override void Dispose()
 237        {
 0238            base.Dispose();
 239
 0240            chatNotificationComponentView.OnClickedNotification -= ClickedOnNotification;
 0241        }
 242
 243        public override void RefreshControl()
 244        {
 0245        }
 246    }
 247}