< Summary

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

Metrics

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

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/NotificationBadge/UnreadNotificationBadge.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 friend.
 8/// </summary>
 9public class UnreadNotificationBadge : MonoBehaviour
 10{
 11    public TextMeshProUGUI notificationText;
 12    public GameObject notificationContainer;
 5413    public int maxNumberToShow = 9;
 14
 15    private IChatController currentChatController;
 16    private string currentUserId;
 17    private long currentTimestampReading;
 18    private int currentUnreadMessagesValue;
 19
 20    public int currentUnreadMessages
 21    {
 022        get => currentUnreadMessagesValue;
 23        set
 24        {
 2125            currentUnreadMessagesValue = value;
 2126            RefreshNotificationBadge();
 2127        }
 28    }
 29
 30    /// <summary>
 31    /// Prepares the notification badge for listening to a specific user
 32    /// </summary>
 33    /// <param name="chatController">Chat Controlled to be listened</param>
 34    /// <param name="userId">User ID to listen to</param>
 35    public void Initialize(IChatController chatController, string userId)
 36    {
 2237        if (chatController == null)
 1638            return;
 39
 640        currentChatController = chatController;
 641        currentUserId = userId;
 42
 643        CommonScriptableObjects.lastReadChatMessages.TryGetValue(currentUserId, out currentTimestampReading);
 644        UpdateUnreadMessages();
 45
 646        currentChatController.OnAddMessage -= ChatController_OnAddMessage;
 647        currentChatController.OnAddMessage += ChatController_OnAddMessage;
 48
 649        CommonScriptableObjects.lastReadChatMessages.OnAdded -= LastReadChatMessages_OnAdded;
 650        CommonScriptableObjects.lastReadChatMessages.OnAdded += LastReadChatMessages_OnAdded;
 651    }
 52
 53    private void OnDestroy()
 54    {
 5155        if (currentChatController == null)
 4556            return;
 57
 658        currentChatController.OnAddMessage -= ChatController_OnAddMessage;
 659        CommonScriptableObjects.lastReadChatMessages.OnAdded -= LastReadChatMessages_OnAdded;
 660    }
 61
 62    private void ChatController_OnAddMessage(ChatMessage newMessage)
 63    {
 1564        if (newMessage.messageType == ChatMessage.Type.PRIVATE &&
 65            newMessage.sender == currentUserId)
 66        {
 67            // A new message from [userId] is received
 1368            UpdateUnreadMessages();
 69        }
 1570    }
 71
 72    private void LastReadChatMessages_OnAdded(string addedKey, long addedValue)
 73    {
 374        if (addedKey == currentUserId)
 75        {
 76            // The player reads the latest messages of [userId]
 177            currentTimestampReading = addedValue;
 178            currentUnreadMessages = 0;
 179            UpdateUnreadMessages();
 80        }
 381    }
 82
 83    private void UpdateUnreadMessages()
 84    {
 2085        currentUnreadMessages = currentChatController.GetEntries()
 86                                                     .Count(
 5987                                                         msg => msg.messageType == ChatMessage.Type.PRIVATE &&
 88                                                                msg.sender == currentUserId &&
 89                                                                msg.timestamp > (ulong)currentTimestampReading);
 2090    }
 91
 92    private void RefreshNotificationBadge()
 93    {
 2194        if (currentUnreadMessagesValue > 0)
 95        {
 1396            notificationContainer.SetActive(true);
 1397            notificationText.text = currentUnreadMessagesValue <= maxNumberToShow ? currentUnreadMessagesValue.ToString(
 1398        }
 99        else
 100        {
 8101            notificationContainer.SetActive(false);
 102        }
 8103    }
 104}