< Summary

Class:UnreadNotificationBadge
Assembly:NotificationBadge
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/NotificationBadge/UnreadNotificationBadge.cs
Covered lines:42
Uncovered lines:5
Coverable lines:47
Total lines:118
Line coverage:89.3% (42 of 47)
Covered branches:0
Total branches:0
Covered methods:9
Total methods:10
Method coverage:90% (9 of 10)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
UnreadNotificationBadge()0%110100%
Initialize(...)0%33094.12%
HandleUnseenMessagesUpdated(...)0%220100%
HandleChannelUnseenMessagesUpdated(...)0%220100%
OnEnable()0%220100%
OnDestroy()0%330100%
UpdateUnreadMessages()0%110100%
HandleOwnPlayerMentioned(...)0%12300%

File(s)

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

#LineLine coverage
 1using DCL;
 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;
 9112    public int maxNumberToShow = 9;
 13    public GameObject ownPlayerMentionMark;
 14
 15    private IChatController chatController;
 16    private DataStore_Mentions mentionsDataStore;
 17    private string currentUserId;
 18    private int currentUnreadMessagesValue;
 19    private bool isInitialized;
 20
 21    public int CurrentUnreadMessages
 22    {
 2223        get => currentUnreadMessagesValue;
 24        set
 25        {
 15926            currentUnreadMessagesValue = value;
 27
 15928            if (currentUnreadMessagesValue > 0)
 29            {
 2730                notificationContainer.SetActive(true);
 2731                notificationText.text = currentUnreadMessagesValue <= maxNumberToShow ? currentUnreadMessagesValue.ToStr
 32            }
 33            else
 34            {
 13235                notificationContainer.SetActive(false);
 36
 13237                if (ownPlayerMentionMark != null)
 11538                    ownPlayerMentionMark.SetActive(false);
 39            }
 13240        }
 41    }
 42
 43    /// <summary>
 44    /// Prepares the notification badge for listening to a specific user
 45    /// </summary>
 46    /// <param name="chatController">Chat Controlled to be listened</param>
 47    /// <param name="userId">User ID to listen to</param>
 48    public void Initialize(
 49        IChatController chatController,
 50        string userId,
 51        DataStore_Mentions mentionsDataStore)
 52    {
 11153        if (chatController == null)
 054            return;
 55
 11156        this.chatController = chatController;
 11157        currentUserId = userId;
 58
 11159        UpdateUnreadMessages();
 60
 11161        chatController.OnUserUnseenMessagesUpdated -= HandleUnseenMessagesUpdated;
 11162        chatController.OnUserUnseenMessagesUpdated += HandleUnseenMessagesUpdated;
 63
 11164        chatController.OnChannelUnseenMessagesUpdated -= HandleChannelUnseenMessagesUpdated;
 11165        chatController.OnChannelUnseenMessagesUpdated += HandleChannelUnseenMessagesUpdated;
 66
 11167        if (mentionsDataStore != null)
 68        {
 11169            this.mentionsDataStore = mentionsDataStore;
 11170            mentionsDataStore.ownPlayerMentionedInChannel.OnChange -= HandleOwnPlayerMentioned;
 11171            mentionsDataStore.ownPlayerMentionedInChannel.OnChange += HandleOwnPlayerMentioned;
 11172            mentionsDataStore.ownPlayerMentionedInDM.OnChange -= HandleOwnPlayerMentioned;
 11173            mentionsDataStore.ownPlayerMentionedInDM.OnChange += HandleOwnPlayerMentioned;
 74        }
 75
 11176        isInitialized = true;
 11177    }
 78
 79    private void HandleUnseenMessagesUpdated(string userId, int unseenMessages)
 80    {
 1881        if (userId != currentUserId) return;
 1482        CurrentUnreadMessages = unseenMessages;
 1483    }
 84
 85    private void HandleChannelUnseenMessagesUpdated(string channelId, int unseenMessages)
 86    {
 1987        if (channelId != currentUserId) return;
 1588        CurrentUnreadMessages = unseenMessages;
 1589    }
 90
 91    private void OnEnable()
 92    {
 24793        if (!isInitialized) return;
 1994        UpdateUnreadMessages();
 1995    }
 96
 97    private void OnDestroy()
 98    {
 6399        if (chatController != null)
 100        {
 59101            chatController.OnUserUnseenMessagesUpdated -= HandleUnseenMessagesUpdated;
 59102            chatController.OnChannelUnseenMessagesUpdated -= HandleChannelUnseenMessagesUpdated;
 103        }
 104
 63105        if (mentionsDataStore != null)
 59106            mentionsDataStore.ownPlayerMentionedInChannel.OnChange -= HandleOwnPlayerMentioned;
 63107    }
 108
 109    private void UpdateUnreadMessages() =>
 130110        CurrentUnreadMessages = chatController.GetAllocatedUnseenMessages(currentUserId) + chatController.GetAllocatedUn
 111
 112    private void HandleOwnPlayerMentioned(string channelId, string previous)
 113    {
 0114        if (ownPlayerMentionMark == null) return;
 0115        if (channelId != currentUserId) return;
 0116        ownPlayerMentionMark.SetActive(true);
 0117    }
 118}