< 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:107
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 UIComponents.CollapsableSortedList;
 6using UnityEngine;
 7
 8public class CollapsableDirectChatListComponentView : CollapsableSortedListComponentView<string, PrivateChatEntry>
 9{
 10    private const string POOL_NAME_PREFIX = "DirectChatEntriesPool_";
 11
 12    [SerializeField] private PrivateChatEntry entryPrefab;
 13    [SerializeField] private UserContextMenu userContextMenu;
 14
 5815    private readonly Dictionary<string, PoolableObject> pooleableEntries = new Dictionary<string, PoolableObject>();
 16    private Pool entryPool;
 17    private IChatController chatController;
 5818    private bool releaseEntriesFromPool = true;
 19
 20    public event Action<PrivateChatEntry> OnOpenChat;
 21    public event Action<string> OnUnfriend
 22    {
 023        add => userContextMenu.OnUnfriend += value;
 024        remove => userContextMenu.OnUnfriend -= value;
 25    }
 26
 27    public void Initialize(IChatController chatController)
 28    {
 029        this.chatController = chatController;
 030    }
 31
 32    public void Filter(string search)
 33    {
 034        if (!gameObject.activeInHierarchy) return;
 035        var regex = new Regex(search, RegexOptions.IgnoreCase);
 036        Filter(entry => regex.IsMatch(entry.Model.userName));
 37        // throttling may intruduce race conditions & artifacts into the ui
 38        // StartCoroutine(FilterAsync(entry => regex.IsMatch(entry.Model.userName)));
 039    }
 40
 41    public void Clear(bool releaseEntriesFromPool)
 42    {
 43        // avoids releasing instances from pool just for this clear
 044        this.releaseEntriesFromPool = releaseEntriesFromPool;
 045        base.Clear();
 046        this.releaseEntriesFromPool = true;
 047        pooleableEntries.Clear();
 048    }
 49
 50    public override PrivateChatEntry Remove(string key)
 51    {
 1452        if (releaseEntriesFromPool)
 53        {
 1454            if (pooleableEntries.ContainsKey(key))
 1355                pooleableEntries[key].Release();
 1456            pooleableEntries.Remove(key);
 57        }
 58
 1459        return base.Remove(key);
 60    }
 61
 62    public void Set(string userId, PrivateChatEntry.PrivateChatEntryModel entryModel)
 63    {
 3764        if (!Contains(entryModel.userId))
 2965            CreateEntry(userId);
 66
 3767        var entry = Get(userId);
 3768        entry.Configure(entryModel);
 3769    }
 70
 71    public void RefreshBlockedEntries(List<string> blockedUsers)
 72    {
 073        foreach (var pair in Entries)
 74        {
 075            pair.Value.SetBlockStatus(blockedUsers.Contains(pair.Key));
 76        }
 077    }
 78
 79    private void CreateEntry(string userId)
 80    {
 2981        entryPool = GetEntryPool();
 2982        var newFriendEntry = entryPool.Get();
 2983        pooleableEntries.Add(userId, newFriendEntry);
 2984        var entry = newFriendEntry.gameObject.GetComponent<PrivateChatEntry>();
 2985        Add(userId, entry);
 2986        entry.Initialize(chatController, userContextMenu);
 2987        entry.OnOpenChat -= OnEntryOpenChat;
 2988        entry.OnOpenChat += OnEntryOpenChat;
 2989    }
 90
 291    private void OnEntryOpenChat(PrivateChatEntry entry) { OnOpenChat?.Invoke(entry); }
 92
 93    private Pool GetEntryPool()
 94    {
 2995        var entryPool = PoolManager.i.GetPool(POOL_NAME_PREFIX + name + GetInstanceID());
 4796        if (entryPool != null) return entryPool;
 97
 1198        entryPool = PoolManager.i.AddPool(
 99            POOL_NAME_PREFIX + name + GetInstanceID(),
 100            Instantiate(entryPrefab).gameObject,
 101            maxPrewarmCount: 20,
 102            isPersistent: true);
 11103        entryPool.ForcePrewarm();
 104
 11105        return entryPool;
 106    }
 107}