< 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:90
Uncovered lines:24
Coverable lines:114
Total lines:244
Line coverage:78.9% (90 of 114)
Covered branches:0
Total branches:0

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%22091.67%
SetMessage(...)0%220100%
SetTimestamp(...)0%110100%
SetNotificationHeader(...)0%440100%
SetRandomColorForChannel(...)0%220100%
SetNotificationSender(...)0%330100%
SetIsMultipleNotifications()0%2100%
SetIsPrivate(...)0%3.013091.67%
SetImage(...)0%220100%
SetPositionOffset(...)0%12300%
SetMaxContentCharacters(...)0%110100%
SetMaxHeaderCharacters(...)0%110100%
SetMaxSenderCharacters(...)0%110100%
SetNotificationTargetId(...)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;
 7
 8namespace DCL.Chat.Notifications
 9{
 10    public class ChatNotificationMessageComponentView :
 11        BaseComponentView,
 12        IChatNotificationMessageComponentView,
 13        IComponentModelConfig<ChatNotificationMessageComponentModel>,
 14        IShowableNotificationView
 15    {
 16        private const string NEAR_BY_CHANNEL = "~nearby";
 17
 18        [Header("Prefab References")]
 19        [SerializeField] internal Button button;
 20        [SerializeField] public TMP_Text notificationMessage;
 21        [SerializeField] internal TMP_Text notificationHeader;
 22        [SerializeField] internal TMP_Text notificationSender;
 23        [SerializeField] internal TMP_Text notificationTimestamp;
 24        [SerializeField] internal ImageComponentView image;
 25        [SerializeField] internal GameObject imageBackground;
 26        [SerializeField] internal GameObject multiNotificationBackground;
 27        [SerializeField] internal GameObject firstSeparator;
 28        [SerializeField] internal GameObject secondSeparator;
 29        [SerializeField] internal bool isPrivate;
 30        [SerializeField] internal RectTransform backgroundTransform;
 31        [SerializeField] internal RectTransform messageContainerTransform;
 32        [SerializeField] internal RectTransform header;
 33        [SerializeField] internal RectTransform content;
 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;
 1843        public bool shouldAnimateFocus = true;
 44        public string notificationTargetId;
 45        private int maxContentCharacters, maxHeaderCharacters, maxSenderCharacters;
 46        private float startingXPosition;
 47        private Image backgroundImage;
 48
 49        public void Configure(ChatNotificationMessageComponentModel newModel)
 50        {
 251            model = newModel;
 252            RefreshControl();
 253        }
 54
 55        public override void Awake()
 56        {
 1657            base.Awake();
 1758            button?.onClick.AddListener(() => OnClickedNotification?.Invoke(notificationTargetId));
 1659            startingXPosition = messageContainerTransform.anchoredPosition.x;
 1660            backgroundImage = imageBackground.GetComponent<Image>();
 1661            RefreshControl();
 1662        }
 63
 64        public override void Show(bool instant = false)
 65        {
 066            showHideAnimator.animSpeedFactor = 0.7f;
 067            base.Show(instant);
 068            ForceUIRefresh();
 069        }
 70
 71        public override void OnFocus()
 72        {
 073            base.OnFocus();
 074            if (shouldAnimateFocus)
 075                messageContainerTransform.anchoredPosition = new Vector2(startingXPosition + 5, messageContainerTransfor
 076        }
 77
 78        public override void OnLoseFocus()
 79        {
 1980            base.OnLoseFocus();
 1981            if (shouldAnimateFocus)
 1982                messageContainerTransform.anchoredPosition = new Vector2(startingXPosition, messageContainerTransform.an
 1983        }
 84
 85        public override void Hide(bool instant = false)
 86        {
 087            showHideAnimator.animSpeedFactor = 0.05f;
 088            base.Hide(instant);
 089        }
 90
 91        public override void RefreshControl()
 92        {
 2193            if (model == null)
 094                return;
 95
 2196            SetMaxContentCharacters(model.maxContentCharacters);
 2197            SetMaxHeaderCharacters(model.maxHeaderCharacters);
 2198            SetMaxSenderCharacters(model.maxSenderCharacters);
 2199            SetNotificationSender(model.messageSender);
 21100            SetMessage(model.message);
 21101            SetTimestamp(model.time);
 21102            SetIsPrivate(model.isPrivate);
 21103            SetNotificationHeader(model.messageHeader);
 21104            SetImage(model.imageUri);
 21105        }
 106
 107        public void SetMessage(string message)
 108        {
 26109            model.message = notificationMessage.ReplaceUnsupportedCharacters(message, '?');
 26110            if (message.Length <= maxContentCharacters)
 23111                notificationMessage.text = message;
 112            else
 3113                notificationMessage.text = $"{message.Substring(0, maxContentCharacters)}...";
 114
 26115            ForceUIRefresh();
 26116        }
 117
 118        public void SetTimestamp(string timestamp)
 119        {
 25120            model.time = timestamp;
 25121            notificationTimestamp.text = timestamp;
 122
 25123            ForceUIRefresh();
 25124        }
 125
 126        public void SetNotificationHeader(string header)
 127        {
 26128            if (!isPrivate)
 129            {
 5130                if (header == NEAR_BY_CHANNEL)
 2131                    notificationHeader.color = publicColor;
 132                else
 3133                    SetRandomColorForChannel(header);
 134            }
 135
 26136            model.messageHeader = header;
 26137            if (header.Length <= maxHeaderCharacters)
 23138                notificationHeader.text = header;
 139            else
 3140                notificationHeader.text = $"{header.Substring(0, maxHeaderCharacters)}...";
 141
 26142            ForceUIRefresh();
 26143        }
 144
 145
 146        private void SetRandomColorForChannel(string header)
 147        {
 3148            int seed = 0;
 3149            byte[] ASCIIvalues = Encoding.ASCII.GetBytes(header);
 66150            foreach (var value in ASCIIvalues)
 151            {
 30152                seed += (int)value;
 153            }
 3154            System.Random rand1 = new System.Random(seed);
 3155            notificationHeader.color = channelColors[rand1.Next(0, channelColors.Length)];
 3156        }
 157
 158        public void SetNotificationSender(string sender)
 159        {
 24160            model.messageSender = sender;
 24161            if (sender.Length <= maxSenderCharacters)
 16162                notificationSender.text = $"{sender}";
 163            else
 8164                notificationSender.text = $"{sender.Substring(0, maxSenderCharacters)}:";
 165
 24166            ForceUIRefresh();
 24167        }
 168
 169        public void SetIsMultipleNotifications()
 170        {
 0171            imageBackground.SetActive(false);
 0172            multiNotificationBackground.SetActive(true);
 0173            notificationHeader.color = standardColor;
 0174            ForceUIRefresh();
 0175        }
 176
 177        public void SetIsPrivate(bool isPrivate)
 178        {
 26179            model.isPrivate = isPrivate;
 26180            this.isPrivate = isPrivate;
 26181            imageBackground.SetActive(isPrivate);
 26182            firstSeparator.SetActive(isPrivate);
 26183            secondSeparator.SetActive(isPrivate);
 26184            if (multiNotificationBackground != null)
 0185                multiNotificationBackground.SetActive(false);
 186
 26187            if (isPrivate)
 20188                notificationHeader.color = privateColor;
 189            else
 6190                notificationHeader.color = publicColor;
 26191            ForceUIRefresh();
 26192        }
 193
 194        public void SetImage(string uri)
 195        {
 21196            if (!isPrivate)
 3197                return;
 198
 18199            image.SetImage((Sprite)null);
 18200            model.imageUri = uri;
 18201            image.SetImage(uri);
 18202            ForceUIRefresh();
 18203        }
 204
 205        public void SetPositionOffset(float xPosHeader, float xPosContent)
 206        {
 0207            if (header != null)
 0208                header.anchoredPosition = new Vector2(xPosHeader, header.anchoredPosition.y);
 0209            if (content != null)
 0210                content.anchoredPosition = new Vector2(xPosContent, content.anchoredPosition.y);
 211
 0212            ForceUIRefresh();
 0213        }
 214
 215        public void SetMaxContentCharacters(int maxContentCharacters)
 216        {
 23217            model.maxContentCharacters = maxContentCharacters;
 23218            this.maxContentCharacters = maxContentCharacters;
 23219        }
 220
 221        public void SetMaxHeaderCharacters(int maxHeaderCharacters)
 222        {
 23223            model.maxHeaderCharacters = maxHeaderCharacters;
 23224            this.maxHeaderCharacters = maxHeaderCharacters;
 23225        }
 226
 227        public void SetMaxSenderCharacters(int maxSenderCharacters)
 228        {
 21229            model.maxSenderCharacters = maxSenderCharacters;
 21230            this.maxSenderCharacters = maxSenderCharacters;
 21231        }
 232
 233        public void SetNotificationTargetId(string notificationTargetId)
 234        {
 3235            model.notificationTargetId = notificationTargetId;
 3236            this.notificationTargetId = notificationTargetId;
 3237        }
 238
 239        private void ForceUIRefresh()
 240        {
 145241            Utils.ForceRebuildLayoutImmediate(backgroundTransform);
 145242        }
 243    }
 244}