< Summary

Class:UnreadWorldNotificationBadge
Assembly:NotificationBadge
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/NotificationBadge/UnreadWorldNotificationBadge.cs
Covered lines:36
Uncovered lines:1
Coverable lines:37
Total lines:98
Line coverage:97.2% (36 of 37)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
UnreadWorldNotificationBadge()0%110100%
Start()0%110100%
Initialize(...)0%220100%
OnDestroy()0%220100%
ChatController_OnAddMessage(...)0%330100%
LastReadWorldChatMessages_OnChange(...)0%110100%
UpdateUnreadMessages()0%110100%
RefreshNotificationBadge()0%330100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/NotificationBadge/UnreadWorldNotificationBadge.cs

#LineLine coverage
 1using DCL.Interface;
 2using System.Linq;
 3using TMPro;
 4using UnityEngine;
 5
 6/// <summary>
 7/// Shows the number of unread messages from a the world chat.
 8/// </summary>
 9public class UnreadWorldNotificationBadge : MonoBehaviour
 10{
 11    public TextMeshProUGUI notificationText;
 12    public GameObject notificationContainer;
 1113    public int maxNumberToShow = 9;
 14
 15    private IChatController currentChatController;
 16    private long currentTimestampReading;
 17    private int currentUnreadMessagesValue;
 18
 19    public int currentUnreadMessages
 20    {
 021        get => currentUnreadMessagesValue;
 22        set
 23        {
 1924            currentUnreadMessagesValue = value;
 1925            RefreshNotificationBadge();
 1926        }
 27    }
 28
 429    private void Start() { Initialize(ChatController.i); }
 30
 31    /// <summary>
 32    /// Prepares the notification badge for listening to the world chat
 33    /// </summary>
 34    /// <param name="chatController">Chat Controlled to be listened</param>
 35    public void Initialize(IChatController chatController)
 36    {
 737        if (chatController == null)
 238            return;
 39
 540        currentChatController = chatController;
 541        currentTimestampReading = CommonScriptableObjects.lastReadWorldChatMessages.Get();
 542        UpdateUnreadMessages();
 43
 544        currentChatController.OnAddMessage -= ChatController_OnAddMessage;
 545        currentChatController.OnAddMessage += ChatController_OnAddMessage;
 46
 547        CommonScriptableObjects.lastReadWorldChatMessages.OnChange -= LastReadWorldChatMessages_OnChange;
 548        CommonScriptableObjects.lastReadWorldChatMessages.OnChange += LastReadWorldChatMessages_OnChange;
 549    }
 50
 51    private void OnDestroy()
 52    {
 953        if (currentChatController == null)
 454            return;
 55
 556        currentChatController.OnAddMessage -= ChatController_OnAddMessage;
 557        CommonScriptableObjects.lastReadWorldChatMessages.OnChange -= LastReadWorldChatMessages_OnChange;
 558    }
 59
 60    private void ChatController_OnAddMessage(ChatMessage newMessage)
 61    {
 1462        if (newMessage.messageType == ChatMessage.Type.PUBLIC &&
 63            newMessage.sender != UserProfile.GetOwnUserProfile().userId)
 64        {
 65            // A new message from world is received
 1266            UpdateUnreadMessages();
 67        }
 1468    }
 69
 70    private void LastReadWorldChatMessages_OnChange(long current, long previous)
 71    {
 72        // The player reads the latest messages of [userId]
 173        currentTimestampReading = current;
 174        currentUnreadMessages = 0;
 175        UpdateUnreadMessages();
 176    }
 77
 78    private void UpdateUnreadMessages()
 79    {
 1880        currentUnreadMessages = currentChatController.GetEntries()
 81                                                     .Count(
 5882                                                         msg => msg.messageType == ChatMessage.Type.PUBLIC &&
 83                                                                msg.timestamp > (ulong)currentTimestampReading);
 1884    }
 85
 86    private void RefreshNotificationBadge()
 87    {
 1988        if (currentUnreadMessagesValue > 0)
 89        {
 1290            notificationContainer.SetActive(true);
 1291            notificationText.text = currentUnreadMessagesValue <= maxNumberToShow ? currentUnreadMessagesValue.ToString(
 1292        }
 93        else
 94        {
 795            notificationContainer.SetActive(false);
 96        }
 797    }
 98}