< Summary

Class:UnreadNotificationBadge
Assembly:NotificationBadge
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/NotificationBadge/UnreadNotificationBadge.cs
Covered lines:31
Uncovered lines:2
Coverable lines:33
Total lines:86
Line coverage:93.9% (31 of 33)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
UnreadNotificationBadge()0%110100%
Initialize(...)0%22090.91%
OnEnable()0%220100%
OnDestroy()0%330100%
HandleMessageAdded(...)0%110100%
HandleUnreadMessagesUpdated(...)0%220100%
UpdateUnreadMessages()0%110100%

File(s)

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

#LineLine coverage
 1using DCL.Interface;
 2using TMPro;
 3using UnityEngine;
 4
 5/// <summary>
 6/// Shows the number of unread messages from a friend.
 7/// </summary>
 8public class UnreadNotificationBadge : MonoBehaviour
 9{
 10    public TextMeshProUGUI notificationText;
 11    public GameObject notificationContainer;
 6812    public int maxNumberToShow = 9;
 13
 14    private IChatController currentChatController;
 15    private string currentUserId;
 16    private int currentUnreadMessagesValue;
 17    private ILastReadMessagesService lastReadMessagesService;
 18    private bool isInitialized;
 19
 20    public int CurrentUnreadMessages
 21    {
 022        get => currentUnreadMessagesValue;
 23        set
 24        {
 10725            currentUnreadMessagesValue = value;
 26
 10727            if (currentUnreadMessagesValue > 0)
 28            {
 1329                notificationContainer.SetActive(true);
 1330                notificationText.text = currentUnreadMessagesValue <= maxNumberToShow ? currentUnreadMessagesValue.ToStr
 1331            }
 32            else
 33            {
 9434                notificationContainer.SetActive(false);
 35            }
 9436        }
 37    }
 38
 39    /// <summary>
 40    /// Prepares the notification badge for listening to a specific user
 41    /// </summary>
 42    /// <param name="chatController">Chat Controlled to be listened</param>
 43    /// <param name="userId">User ID to listen to</param>
 44    /// <param name="lastReadMessagesService">Service that handles unread messages</param>
 45    public void Initialize(IChatController chatController, string userId, ILastReadMessagesService lastReadMessagesServi
 46    {
 5647        if (chatController == null)
 048            return;
 49
 5650        this.lastReadMessagesService = lastReadMessagesService;
 5651        currentChatController = chatController;
 5652        currentUserId = userId;
 53
 5654        UpdateUnreadMessages();
 55
 5656        currentChatController.OnAddMessage -= HandleMessageAdded;
 5657        currentChatController.OnAddMessage += HandleMessageAdded;
 5658        lastReadMessagesService.OnUpdated += HandleUnreadMessagesUpdated;
 59
 5660        isInitialized = true;
 5661    }
 62
 63    private void OnEnable()
 64    {
 21565        if (!isInitialized) return;
 3566        UpdateUnreadMessages();
 3567    }
 68
 69    private void OnDestroy()
 70    {
 4671        if (currentChatController != null)
 4072            currentChatController.OnAddMessage -= HandleMessageAdded;
 4673        if (lastReadMessagesService != null)
 4074            lastReadMessagesService.OnUpdated -= HandleUnreadMessagesUpdated;
 4675    }
 76
 1577    private void HandleMessageAdded(ChatMessage newMessage) => UpdateUnreadMessages();
 78
 79    private void HandleUnreadMessagesUpdated(string userId)
 80    {
 381        if (userId != currentUserId) return;
 182        UpdateUnreadMessages();
 183    }
 84
 10785    private void UpdateUnreadMessages() => CurrentUnreadMessages = lastReadMessagesService.GetUnreadCount(currentUserId)
 86}