< Summary

Class:CollapsableDirectChatListComponentView
Assembly:WorldChatWindowHUD
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/WorldChatWindowHUD/CollapsableDirectChatListComponentView.cs
Covered lines:29
Uncovered lines:17
Coverable lines:46
Total lines:114
Line coverage:63% (29 of 46)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
CollapsableDirectChatListComponentView()0%110100%
add_OnUnfriend(...)0%2100%
remove_OnUnfriend(...)0%2100%
Initialize(...)0%110100%
Filter(...)0%6200%
Clear(...)0%2100%
Remove(...)0%330100%
Set(...)0%220100%
RefreshBlockedEntries(...)0%6200%
RefreshPresence(...)0%6200%
CreateEntry(...)0%110100%
OnEntryOpenChat(...)0%220100%
GetEntryPool()0%220100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/WorldChatWindowHUD/CollapsableDirectChatListComponentView.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.Text.RegularExpressions;
 4using DCL;
 5using DCL.Chat.HUD;
 6using UIComponents.CollapsableSortedList;
 7using UnityEngine;
 8
 9public class CollapsableDirectChatListComponentView : CollapsableSortedListComponentView<string, PrivateChatEntry>
 10{
 11    private const string POOL_NAME_PREFIX = "DirectChatEntriesPool_";
 12
 13    [SerializeField] private PrivateChatEntry entryPrefab;
 14    [SerializeField] private UserContextMenu userContextMenu;
 15
 8416    private readonly Dictionary<string, PoolableObject> pooleableEntries = new Dictionary<string, PoolableObject>();
 17    private Pool entryPool;
 18    private IChatController chatController;
 8419    private bool releaseEntriesFromPool = true;
 20
 21    public event Action<PrivateChatEntry> OnOpenChat;
 22    public event Action<string> OnUnfriend
 23    {
 024        add => userContextMenu.OnUnfriend += value;
 025        remove => userContextMenu.OnUnfriend -= value;
 26    }
 27
 28    public void Initialize(IChatController chatController)
 29    {
 6830        this.chatController = chatController;
 6831    }
 32
 33    public void Filter(string search)
 34    {
 035        if (!gameObject.activeInHierarchy) return;
 036        var regex = new Regex(search, RegexOptions.IgnoreCase);
 037        Filter(entry => regex.IsMatch(entry.Model.userName));
 38        // throttling may intruduce race conditions & artifacts into the ui
 39        // StartCoroutine(FilterAsync(entry => regex.IsMatch(entry.Model.userName)));
 040    }
 41
 42    public void Clear(bool releaseEntriesFromPool)
 43    {
 44        // avoids releasing instances from pool just for this clear
 045        this.releaseEntriesFromPool = releaseEntriesFromPool;
 046        base.Clear();
 047        this.releaseEntriesFromPool = true;
 048        pooleableEntries.Clear();
 049    }
 50
 51    public override PrivateChatEntry Remove(string key)
 52    {
 1653        if (releaseEntriesFromPool)
 54        {
 1655            if (pooleableEntries.ContainsKey(key))
 1556                pooleableEntries[key].Release();
 1657            pooleableEntries.Remove(key);
 58        }
 59
 1660        return base.Remove(key);
 61    }
 62
 63    public void Set(string userId, PrivateChatEntryModel entryModel)
 64    {
 4265        if (!Contains(entryModel.userId))
 3466            CreateEntry(userId);
 67
 4268        var entry = Get(userId);
 4269        entry.Configure(entryModel);
 4270    }
 71
 72    public void RefreshBlockedEntries(List<string> blockedUsers)
 73    {
 074        foreach (var pair in Entries)
 75        {
 076            pair.Value.SetBlockStatus(blockedUsers.Contains(pair.Key));
 77        }
 078    }
 79
 80    public void RefreshPresence(string userId, bool isOnline)
 81    {
 082        if (Entries.TryGetValue(userId, out PrivateChatEntry directMessage))
 083            directMessage.SetPresence(isOnline);
 084    }
 85
 86    private void CreateEntry(string userId)
 87    {
 3488        entryPool = GetEntryPool();
 3489        var newFriendEntry = entryPool.Get();
 3490        pooleableEntries.Add(userId, newFriendEntry);
 3491        var entry = newFriendEntry.gameObject.GetComponent<PrivateChatEntry>();
 3492        Add(userId, entry);
 3493        entry.Initialize(chatController, userContextMenu);
 3494        entry.OnOpenChat -= OnEntryOpenChat;
 3495        entry.OnOpenChat += OnEntryOpenChat;
 3496    }
 97
 298    private void OnEntryOpenChat(PrivateChatEntry entry) { OnOpenChat?.Invoke(entry); }
 99
 100    private Pool GetEntryPool()
 101    {
 34102        var entryPool = PoolManager.i.GetPool(POOL_NAME_PREFIX + name + GetInstanceID());
 52103        if (entryPool != null) return entryPool;
 104
 16105        entryPool = PoolManager.i.AddPool(
 106            POOL_NAME_PREFIX + name + GetInstanceID(),
 107            Instantiate(entryPrefab).gameObject,
 108            maxPrewarmCount: 20,
 109            isPersistent: true);
 16110        entryPool.ForcePrewarm();
 111
 16112        return entryPool;
 113    }
 114}