< Summary

Class:DCL.Chat.Notifications.ChatNotificationMessageComponentView
Assembly:NotificationMessagesHUD
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/NotificationMessagesHUD/NotificationComponent/ChatNotificationMessageComponentView.cs
Covered lines:108
Uncovered lines:18
Coverable lines:126
Total lines:268
Line coverage:85.7% (108 of 126)
Covered branches:0
Total branches:0
Covered methods:22
Total methods:26
Method coverage:84.6% (22 of 26)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
ChatNotificationMessageComponentView()0%110100%
Configure(...)0%110100%
Awake()0%220100%
Show(...)0%2100%
OnFocus()0%6200%
OnLoseFocus()0%220100%
Hide(...)0%2100%
RefreshControl()0%4.024089.47%
SetMessage(...)0%110100%
SetTimestamp(...)0%110100%
SetNotificationHeader(...)0%440100%
SetRandomColorForChannel(...)0%220100%
SetNotificationSender(...)0%330100%
SetIsMultipleNotifications()0%2100%
SetIsPrivate(...)0%3.013090%
SetImage(...)0%220100%
SetImageVisibility(...)0%110100%
SetMaxContentCharacters(...)0%110100%
SetMaxHeaderCharacters(...)0%110100%
SetMaxSenderCharacters(...)0%110100%
SetNotificationTargetId(...)0%110100%
SetOwnPlayerMention(...)0%220100%
DockRight()0%110100%
DockLeft()0%110100%
DockHorizontally(...)0%110100%
ForceUIRefresh()0%110100%

File(s)

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

#LineLine coverage
 1using DCL.Helpers;
 2using System;
 3using System.Text;
 4using TMPro;
 5using UnityEngine;
 6using UnityEngine.UI;
 7using Random = System.Random;
 8
 9namespace DCL.Chat.Notifications
 10{
 11    public class ChatNotificationMessageComponentView :
 12        BaseComponentView,
 13        IChatNotificationMessageComponentView,
 14        IComponentModelConfig<ChatNotificationMessageComponentModel>,
 15        IShowableNotificationView
 16    {
 17        private const string NEAR_BY_CHANNEL = "~nearby";
 18
 19        [Header("Prefab References")]
 20        [SerializeField] internal Button button;
 21        [SerializeField] public TMP_Text notificationMessage;
 22        [SerializeField] internal TMP_Text notificationHeader;
 23        [SerializeField] internal TMP_Text notificationSender;
 24        [SerializeField] internal TMP_Text notificationTimestamp;
 25        [SerializeField] internal ImageComponentView image;
 26        [SerializeField] internal GameObject imageContainer;
 27        [SerializeField] internal GameObject imageBackground;
 28        [SerializeField] internal GameObject multiNotificationBackground;
 29        [SerializeField] internal GameObject mentionMark;
 30        [SerializeField] internal bool isPrivate;
 31        [SerializeField] internal RectTransform backgroundTransform;
 32        [SerializeField] internal RectTransform messageContainerTransform;
 33        [SerializeField] internal bool isTopNorification;
 34
 35        [Header("Configuration")]
 36        [SerializeField] internal ChatNotificationMessageComponentModel model;
 37        [SerializeField] private Color privateColor;
 38        [SerializeField] private Color publicColor;
 39        [SerializeField] private Color standardColor;
 40        [SerializeField] private Color[] channelColors;
 41
 42        public event Action<string> OnClickedNotification;
 2043        public bool shouldAnimateFocus = true;
 44        public string notificationTargetId;
 45        private int maxHeaderCharacters, maxSenderCharacters;
 46        private float startingXPosition;
 47
 48        public void Configure(ChatNotificationMessageComponentModel newModel)
 49        {
 250            model = newModel;
 251            RefreshControl();
 252        }
 53
 54        public override void Awake()
 55        {
 1956            base.Awake();
 2057            button?.onClick.AddListener(() => OnClickedNotification?.Invoke(notificationTargetId));
 1958            startingXPosition = messageContainerTransform.anchoredPosition.x;
 1959            RefreshControl();
 1960        }
 61
 62        public override void Show(bool instant = false)
 63        {
 064            showHideAnimator.animSpeedFactor = 0.7f;
 065            base.Show(instant);
 066            ForceUIRefresh();
 067        }
 68
 69        public override void OnFocus()
 70        {
 071            base.OnFocus();
 072            if (shouldAnimateFocus)
 073                messageContainerTransform.anchoredPosition = new Vector2(startingXPosition + 5, messageContainerTransfor
 074        }
 75
 76        public override void OnLoseFocus()
 77        {
 2478            base.OnLoseFocus();
 2479            if (shouldAnimateFocus)
 2480                messageContainerTransform.anchoredPosition = new Vector2(startingXPosition, messageContainerTransform.an
 2481        }
 82
 83        public override void Hide(bool instant = false)
 84        {
 085            showHideAnimator.animSpeedFactor = 15f;
 086            base.Hide(instant);
 087        }
 88
 89        public override void RefreshControl()
 90        {
 2691            if (model == null)
 092                return;
 93
 2694            SetMaxContentCharacters(model.maxContentCharacters);
 2695            SetMaxHeaderCharacters(model.maxHeaderCharacters);
 2696            SetMaxSenderCharacters(model.maxSenderCharacters);
 2697            SetNotificationSender(model.messageSender);
 2698            SetMessage(model.message);
 2699            SetTimestamp(model.time);
 26100            SetIsPrivate(model.isPrivate);
 26101            SetNotificationHeader(model.messageHeader);
 26102            SetImage(model.imageUri);
 26103            SetImageVisibility(model.isImageVisible);
 26104            SetOwnPlayerMention(model.isOwnPlayerMentioned);
 105
 26106            if (isTopNorification) return;
 107
 26108            if (model.isDockedLeft)
 24109                DockLeft();
 110            else
 2111                DockRight();
 2112        }
 113
 114        public void SetMessage(string message)
 115        {
 32116            model.message = notificationMessage.ReplaceUnsupportedCharacters(message, '?');
 32117            notificationMessage.text = message;
 118
 32119            ForceUIRefresh();
 32120        }
 121
 122        public void SetTimestamp(string timestamp)
 123        {
 32124            model.time = timestamp;
 32125            notificationTimestamp.text = timestamp;
 126
 32127            ForceUIRefresh();
 32128        }
 129
 130        public void SetNotificationHeader(string header)
 131        {
 33132            if (!isPrivate)
 133            {
 7134                if (header == NEAR_BY_CHANNEL)
 4135                    notificationHeader.color = publicColor;
 136                else
 3137                    SetRandomColorForChannel(header);
 138            }
 139
 33140            model.messageHeader = header;
 33141            if (header.Length <= maxHeaderCharacters)
 30142                notificationHeader.text = header;
 143            else
 3144                notificationHeader.text = $"{header.Substring(0, maxHeaderCharacters)}...";
 145
 33146            ForceUIRefresh();
 33147        }
 148
 149
 150        private void SetRandomColorForChannel(string header)
 151        {
 3152            int seed = 0;
 3153            byte[] ASCIIvalues = Encoding.ASCII.GetBytes(header);
 66154            foreach (var value in ASCIIvalues)
 155            {
 30156                seed += (int)value;
 157            }
 3158            Random rand1 = new Random(seed);
 3159            notificationHeader.color = channelColors[rand1.Next(0, channelColors.Length)];
 3160        }
 161
 162        public void SetNotificationSender(string sender)
 163        {
 31164            model.messageSender = sender;
 31165            if (sender.Length <= maxSenderCharacters)
 29166                notificationSender.text = $"{sender}";
 167            else
 2168                notificationSender.text = $"{sender.Substring(0, maxSenderCharacters)}:";
 169
 31170            ForceUIRefresh();
 31171        }
 172
 173        public void SetIsMultipleNotifications()
 174        {
 0175            imageBackground.SetActive(false);
 0176            multiNotificationBackground.SetActive(true);
 0177            notificationHeader.color = standardColor;
 0178            ForceUIRefresh();
 0179        }
 180
 181        public void SetIsPrivate(bool isPrivate)
 182        {
 33183            model.isPrivate = isPrivate;
 33184            this.isPrivate = isPrivate;
 33185            imageBackground.SetActive(isPrivate);
 33186            if (multiNotificationBackground != null)
 0187                multiNotificationBackground.SetActive(false);
 188
 33189            if (isPrivate)
 25190                notificationHeader.color = privateColor;
 191            else
 8192                notificationHeader.color = publicColor;
 33193            ForceUIRefresh();
 33194        }
 195
 196        public void SetImage(string uri)
 197        {
 26198            if (!isPrivate)
 4199                return;
 200
 22201            image.SetImage((Sprite)null);
 22202            model.imageUri = uri;
 22203            image.SetImage(uri);
 22204            ForceUIRefresh();
 22205        }
 206
 207        public void SetImageVisibility(bool visible)
 208        {
 31209            model.isImageVisible = visible;
 31210            imageContainer.SetActive(visible);
 31211        }
 212
 213        public void SetMaxContentCharacters(int maxContentCharacters)
 214        {
 32215            maxContentCharacters = Mathf.Max(0, maxContentCharacters);
 32216            model.maxContentCharacters = maxContentCharacters;
 32217        }
 218
 219        public void SetMaxHeaderCharacters(int maxHeaderCharacters)
 220        {
 28221            model.maxHeaderCharacters = maxHeaderCharacters;
 28222            this.maxHeaderCharacters = maxHeaderCharacters;
 28223        }
 224
 225        public void SetMaxSenderCharacters(int maxSenderCharacters)
 226        {
 26227            model.maxSenderCharacters = maxSenderCharacters;
 26228            this.maxSenderCharacters = maxSenderCharacters;
 26229        }
 230
 231        public void SetNotificationTargetId(string notificationTargetId)
 232        {
 5233            model.notificationTargetId = notificationTargetId;
 5234            this.notificationTargetId = notificationTargetId;
 5235        }
 236
 237        public void SetOwnPlayerMention(bool isMentioned)
 238        {
 31239            model.isOwnPlayerMentioned = isMentioned;
 240
 31241            if (mentionMark != null)
 31242                mentionMark.SetActive(isMentioned);
 31243        }
 244
 245        public void DockRight()
 246        {
 4247            model.isDockedLeft = false;
 4248            DockHorizontally(1f);
 4249        }
 250
 251        public void DockLeft()
 252        {
 27253            model.isDockedLeft = true;
 27254            DockHorizontally(0);
 27255        }
 256
 257        private void DockHorizontally(float anchor)
 258        {
 31259            messageContainerTransform.pivot = new Vector2(anchor, 0.5f);
 31260            messageContainerTransform.anchorMin = new Vector2(anchor, 0.5f);
 31261            messageContainerTransform.anchorMax = new Vector2(anchor, 0.5f);
 31262            backgroundTransform.pivot = new Vector2(anchor, 0.5f);
 31263        }
 264
 265        private void ForceUIRefresh() =>
 183266            backgroundTransform.ForceUpdateLayout();
 267    }
 268}