< Summary

Class:CollapsableDirectChatListComponentView
Assembly:WorldChatWindowHUD
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/WorldChatWindowHUD/CollapsableDirectChatListComponentView.cs
Covered lines:27
Uncovered lines:16
Coverable lines:43
Total lines:108
Line coverage:62.7% (27 of 43)
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%2100%
Filter(...)0%6200%
Clear(...)0%2100%
Remove(...)0%330100%
Set(...)0%220100%
RefreshBlockedEntries(...)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    {
 030        this.chatController = chatController;
 031    }
 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    private void CreateEntry(string userId)
 81    {
 3482        entryPool = GetEntryPool();
 3483        var newFriendEntry = entryPool.Get();
 3484        pooleableEntries.Add(userId, newFriendEntry);
 3485        var entry = newFriendEntry.gameObject.GetComponent<PrivateChatEntry>();
 3486        Add(userId, entry);
 3487        entry.Initialize(chatController, userContextMenu);
 3488        entry.OnOpenChat -= OnEntryOpenChat;
 3489        entry.OnOpenChat += OnEntryOpenChat;
 3490    }
 91
 292    private void OnEntryOpenChat(PrivateChatEntry entry) { OnOpenChat?.Invoke(entry); }
 93
 94    private Pool GetEntryPool()
 95    {
 3496        var entryPool = PoolManager.i.GetPool(POOL_NAME_PREFIX + name + GetInstanceID());
 5297        if (entryPool != null) return entryPool;
 98
 1699        entryPool = PoolManager.i.AddPool(
 100            POOL_NAME_PREFIX + name + GetInstanceID(),
 101            Instantiate(entryPrefab).gameObject,
 102            maxPrewarmCount: 20,
 103            isPersistent: true);
 16104        entryPool.ForcePrewarm();
 105
 16106        return entryPool;
 107    }
 108}