< Summary

Class:CollapsableDirectChatListComponentView
Assembly:WorldChatWindowHUD
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/WorldChatWindowHUD/CollapsableDirectChatListComponentView.cs
Covered lines:32
Uncovered lines:12
Coverable lines:44
Total lines:109
Line coverage:72.7% (32 of 44)
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%110100%
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
 5015    private readonly Dictionary<string, PoolableObject> pooleableEntries = new Dictionary<string, PoolableObject>();
 16    private Pool entryPool;
 17    private IChatController chatController;
 18    private ILastReadMessagesService lastReadMessagesService;
 5019    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, ILastReadMessagesService lastReadMessagesService)
 29    {
 030        this.chatController = chatController;
 031        this.lastReadMessagesService = lastReadMessagesService;
 032    }
 33
 34    public void Filter(string search)
 35    {
 036        if (!gameObject.activeInHierarchy) return;
 037        var regex = new Regex(search, RegexOptions.IgnoreCase);
 038        Filter(entry => regex.IsMatch(entry.Model.userName));
 39        // throttling may intruduce race conditions & artifacts into the ui
 40        // StartCoroutine(FilterAsync(entry => regex.IsMatch(entry.Model.userName)));
 041    }
 42
 43    public void Clear(bool releaseEntriesFromPool)
 44    {
 45        // avoids releasing instances from pool just for this clear
 946        this.releaseEntriesFromPool = releaseEntriesFromPool;
 947        base.Clear();
 948        this.releaseEntriesFromPool = true;
 949        pooleableEntries.Clear();
 950    }
 51
 52    public override PrivateChatEntry Remove(string key)
 53    {
 3254        if (releaseEntriesFromPool)
 55        {
 956            if (pooleableEntries.ContainsKey(key))
 157                pooleableEntries[key].Release();
 958            pooleableEntries.Remove(key);
 59        }
 60
 3261        return base.Remove(key);
 62    }
 63
 64    public void Set(string userId, PrivateChatEntry.PrivateChatEntryModel entryModel)
 65    {
 2966        if (!Contains(entryModel.userId))
 1767            CreateEntry(userId);
 68
 2969        var entry = Get(userId);
 2970        entry.Configure(entryModel);
 2971    }
 72
 73    public void RefreshBlockedEntries(List<string> blockedUsers)
 74    {
 075        foreach (var pair in Entries)
 76        {
 077            pair.Value.SetBlockStatus(blockedUsers.Contains(pair.Key));
 78        }
 079    }
 80
 81    private void CreateEntry(string userId)
 82    {
 1783        entryPool = GetEntryPool();
 1784        var newFriendEntry = entryPool.Get();
 1785        pooleableEntries.Add(userId, newFriendEntry);
 1786        var entry = newFriendEntry.gameObject.GetComponent<PrivateChatEntry>();
 1787        Add(userId, entry);
 1788        entry.Initialize(chatController, userContextMenu, lastReadMessagesService);
 1789        entry.OnOpenChat -= OnEntryOpenChat;
 1790        entry.OnOpenChat += OnEntryOpenChat;
 1791    }
 92
 293    private void OnEntryOpenChat(PrivateChatEntry entry) { OnOpenChat?.Invoke(entry); }
 94
 95    private Pool GetEntryPool()
 96    {
 1797        var entryPool = PoolManager.i.GetPool(POOL_NAME_PREFIX + name + GetInstanceID());
 2698        if (entryPool != null) return entryPool;
 99
 8100        entryPool = PoolManager.i.AddPool(
 101            POOL_NAME_PREFIX + name + GetInstanceID(),
 102            Instantiate(entryPrefab).gameObject,
 103            maxPrewarmCount: 20,
 104            isPersistent: true);
 8105        entryPool.ForcePrewarm();
 106
 8107        return entryPool;
 108    }
 109}