< Summary

Class: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:242
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 TMPro;
 2using UnityEngine;
 3using UnityEngine.UI;
 4using System;
 5using System.Collections;
 6using System.Collections.Generic;
 7using System.Text;
 8using DCL.Helpers;
 9using DG.Tweening;
 10using System.Threading;
 11using Cysharp.Threading.Tasks;
 12
 13public class ChatNotificationMessageComponentView : BaseComponentView, IChatNotificationMessageComponentView, IComponent
 14{
 15    private const string NEAR_BY_CHANNEL = "~nearby";
 16
 17    [Header("Prefab References")]
 18    [SerializeField] internal Button button;
 19    [SerializeField] public TMP_Text notificationMessage;
 20    [SerializeField] internal TMP_Text notificationHeader;
 21    [SerializeField] internal TMP_Text notificationSender;
 22    [SerializeField] internal TMP_Text notificationTimestamp;
 23    [SerializeField] internal ImageComponentView image;
 24    [SerializeField] internal GameObject imageBackground;
 25    [SerializeField] internal GameObject multiNotificationBackground;
 26    [SerializeField] internal GameObject firstSeparator;
 27    [SerializeField] internal GameObject secondSeparator;
 28    [SerializeField] internal bool isPrivate;
 29    [SerializeField] internal RectTransform backgroundTransform;
 30    [SerializeField] internal RectTransform messageContainerTransform;
 31    [SerializeField] internal RectTransform header;
 32    [SerializeField] internal RectTransform content;
 33
 34    [Header("Configuration")]
 35    [SerializeField] internal ChatNotificationMessageComponentModel model;
 36    [SerializeField] private Color privateColor;
 37    [SerializeField] private Color publicColor;
 38    [SerializeField] private Color standardColor;
 39    [SerializeField] private Color[] channelColors;
 40
 41    public event Action<string> OnClickedNotification;
 1742    public bool shouldAnimateFocus = true;
 43    public string notificationTargetId;
 44    private int maxContentCharacters, maxHeaderCharacters, maxSenderCharacters;
 45    private float startingXPosition;
 46    private Image backgroundImage;
 47
 48    public void Configure(ChatNotificationMessageComponentModel newModel)
 49    {
 250        model = newModel;
 251        RefreshControl();
 252    }
 53
 54    public override void Awake()
 55    {
 1656        base.Awake();
 1757        button?.onClick.AddListener(()=>OnClickedNotification?.Invoke(notificationTargetId));
 1658        startingXPosition = messageContainerTransform.anchoredPosition.x;
 1659        backgroundImage = imageBackground.GetComponent<Image>();
 1660        RefreshControl();
 1661    }
 62
 63    public override void Show(bool instant = false)
 64    {
 065        showHideAnimator.animSpeedFactor = 0.7f;
 066        base.Show(instant);
 067        ForceUIRefresh();
 068    }
 69
 70    public override void OnFocus()
 71    {
 072        base.OnFocus();
 073        if(shouldAnimateFocus)
 074            messageContainerTransform.anchoredPosition = new Vector2(startingXPosition + 5, messageContainerTransform.an
 075    }
 76
 77    public override void OnLoseFocus()
 78    {
 1979        base.OnLoseFocus();
 1980        if(shouldAnimateFocus)
 1981            messageContainerTransform.anchoredPosition = new Vector2(startingXPosition, messageContainerTransform.anchor
 1982    }
 83
 84    public override void Hide(bool instant = false)
 85    {
 086        showHideAnimator.animSpeedFactor = 0.05f;
 087        base.Hide(instant);
 088    }
 89
 90    public override void RefreshControl()
 91    {
 2192        if (model == null)
 093            return;
 94
 2195        SetMaxContentCharacters(model.maxContentCharacters);
 2196        SetMaxHeaderCharacters(model.maxHeaderCharacters);
 2197        SetMaxSenderCharacters(model.maxSenderCharacters);
 2198        SetNotificationSender(model.messageSender);
 2199        SetMessage(model.message);
 21100        SetTimestamp(model.time);
 21101        SetIsPrivate(model.isPrivate);
 21102        SetNotificationHeader(model.messageHeader);
 21103        SetImage(model.imageUri);
 21104    }
 105
 106    public void SetMessage(string message)
 107    {
 26108        model.message = message;
 26109        if (message.Length <= maxContentCharacters)
 23110            notificationMessage.text = message;
 111        else
 3112            notificationMessage.text = $"{message.Substring(0, maxContentCharacters)}...";
 113
 26114        ForceUIRefresh();
 26115    }
 116
 117    public void SetTimestamp(string timestamp)
 118    {
 25119        model.time = timestamp;
 25120        notificationTimestamp.text = timestamp;
 121
 25122        ForceUIRefresh();
 25123    }
 124
 125    public void SetNotificationHeader(string header)
 126    {
 26127        if (!isPrivate)
 128        {
 5129            if(header == NEAR_BY_CHANNEL)
 2130                notificationHeader.color = publicColor;
 131            else
 3132                SetRandomColorForChannel(header);
 133        }
 134
 26135        model.messageHeader = header;
 26136        if(header.Length <= maxHeaderCharacters)
 23137            notificationHeader.text = header;
 138        else
 3139            notificationHeader.text = $"{header.Substring(0, maxHeaderCharacters)}...";
 140
 26141        ForceUIRefresh();
 26142    }
 143
 144
 145    private void SetRandomColorForChannel(string header)
 146    {
 3147        int seed = 0;
 3148        byte[] ASCIIvalues = Encoding.ASCII.GetBytes(header);
 66149        foreach (var value in ASCIIvalues)
 150        {
 30151            seed += (int)value;
 152        }
 3153        System.Random rand1 = new System.Random(seed);
 3154        notificationHeader.color = channelColors[rand1.Next(0, channelColors.Length)];
 3155    }
 156
 157    public void SetNotificationSender(string sender)
 158    {
 24159        model.messageSender = sender;
 24160        if (sender.Length <= maxSenderCharacters)
 16161            notificationSender.text = $"{sender}";
 162        else
 8163            notificationSender.text = $"{sender.Substring(0, maxSenderCharacters)}:";
 164
 24165        ForceUIRefresh();
 24166    }
 167
 168    public void SetIsMultipleNotifications()
 169    {
 0170        imageBackground.SetActive(false);
 0171        multiNotificationBackground.SetActive(true);
 0172        notificationHeader.color = standardColor;
 0173        ForceUIRefresh();
 0174    }
 175
 176    public void SetIsPrivate(bool isPrivate)
 177    {
 26178        model.isPrivate = isPrivate;
 26179        this.isPrivate = isPrivate;
 26180        imageBackground.SetActive(isPrivate);
 26181        firstSeparator.SetActive(isPrivate);
 26182        secondSeparator.SetActive(isPrivate);
 26183        if(multiNotificationBackground != null)
 0184            multiNotificationBackground.SetActive(false);
 185
 26186        if (isPrivate)
 20187            notificationHeader.color = privateColor;
 188        else
 6189            notificationHeader.color = publicColor;
 26190        ForceUIRefresh();
 26191    }
 192
 193    public void SetImage(string uri)
 194    {
 21195        if (!isPrivate)
 3196            return;
 197
 18198        image.SetImage((Sprite)null);
 18199        model.imageUri = uri;
 18200        image.SetImage(uri);
 18201        ForceUIRefresh();
 18202    }
 203
 204    public void SetPositionOffset(float xPosHeader, float xPosContent)
 205    {
 0206        if(header != null)
 0207            header.anchoredPosition = new Vector2(xPosHeader, header.anchoredPosition.y);
 0208        if(content != null)
 0209            content.anchoredPosition = new Vector2(xPosContent, content.anchoredPosition.y);
 210
 0211        ForceUIRefresh();
 0212    }
 213
 214    public void SetMaxContentCharacters(int maxContentCharacters)
 215    {
 23216        model.maxContentCharacters = maxContentCharacters;
 23217        this.maxContentCharacters = maxContentCharacters;
 23218    }
 219
 220    public void SetMaxHeaderCharacters(int maxHeaderCharacters)
 221    {
 23222        model.maxHeaderCharacters = maxHeaderCharacters;
 23223        this.maxHeaderCharacters = maxHeaderCharacters;
 23224    }
 225
 226    public void SetMaxSenderCharacters(int maxSenderCharacters)
 227    {
 21228        model.maxSenderCharacters = maxSenderCharacters;
 21229        this.maxSenderCharacters = maxSenderCharacters;
 21230    }
 231
 232    public void SetNotificationTargetId(string notificationTargetId)
 233    {
 3234        model.notificationTargetId = notificationTargetId;
 3235        this.notificationTargetId = notificationTargetId;
 3236    }
 237
 238    private void ForceUIRefresh()
 239    {
 145240        Utils.ForceRebuildLayoutImmediate(backgroundTransform);
 145241    }
 242}