< Summary

Class:UnreadWorldNotificationBadge
Assembly:NotificationBadge
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/NotificationBadge/UnreadWorldNotificationBadge.cs
Covered lines:35
Uncovered lines:2
Coverable lines:37
Total lines:98
Line coverage:94.5% (35 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%22090%
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;
 2813    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        {
 2124            currentUnreadMessagesValue = value;
 2125            RefreshNotificationBadge();
 2126        }
 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)
 038            return;
 39
 740        currentChatController = chatController;
 741        currentTimestampReading = CommonScriptableObjects.lastReadWorldChatMessages.Get();
 742        UpdateUnreadMessages();
 43
 744        currentChatController.OnAddMessage -= ChatController_OnAddMessage;
 745        currentChatController.OnAddMessage += ChatController_OnAddMessage;
 46
 747        CommonScriptableObjects.lastReadWorldChatMessages.OnChange -= LastReadWorldChatMessages_OnChange;
 748        CommonScriptableObjects.lastReadWorldChatMessages.OnChange += LastReadWorldChatMessages_OnChange;
 749    }
 50
 51    private void OnDestroy()
 52    {
 1653        if (currentChatController == null)
 954            return;
 55
 756        currentChatController.OnAddMessage -= ChatController_OnAddMessage;
 757        CommonScriptableObjects.lastReadWorldChatMessages.OnChange -= LastReadWorldChatMessages_OnChange;
 758    }
 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    {
 2080        currentUnreadMessages = currentChatController.GetEntries()
 81                                                     .Count(
 5882                                                         msg => msg.messageType == ChatMessage.Type.PUBLIC &&
 83                                                                msg.timestamp > (ulong)currentTimestampReading);
 2084    }
 85
 86    private void RefreshNotificationBadge()
 87    {
 2188        if (currentUnreadMessagesValue > 0)
 89        {
 1290            notificationContainer.SetActive(true);
 1291            notificationText.text = currentUnreadMessagesValue <= maxNumberToShow ? currentUnreadMessagesValue.ToString(
 1292        }
 93        else
 94        {
 995            notificationContainer.SetActive(false);
 96        }
 997    }
 98}