< Summary

Class:DCL.Social.Chat.ChannelMembersComponentView
Assembly:WorldChatWindowHUD
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/WorldChatWindowHUD/ChannelMembersComponentView.cs
Covered lines:67
Uncovered lines:14
Coverable lines:81
Total lines:185
Line coverage:82.7% (67 of 81)
Covered branches:0
Total branches:0
Covered methods:21
Total methods:25
Method coverage:84% (21 of 25)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
ChannelMembersComponentView()0%110100%
Awake()0%220100%
Update()0%330100%
Dispose()0%3.333066.67%
ClearAllEntries()0%110100%
ShowLoading()0%110100%
Set(...)0%110100%
Remove(...)0%110100%
Show(...)0%110100%
Hide(...)0%110100%
ClearSearchInput(...)0%110100%
HideLoading()0%110100%
ShowLoadingMore()0%110100%
HideLoadingMore()0%110100%
ShowResultsHeader()0%110100%
HideResultsHeader()0%110100%
RefreshControl()0%2100%
UpdateLayout()0%110100%
Sort()0%110100%
UpdateHeaders()0%2.032080%
LoadMoreEntries(...)0%20400%
WaitThenRequireMoreEntries()0%20400%
SetQueuedEntries()0%440100%
SetSearchModeActive(...)0%330100%

File(s)

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

#LineLine coverage
 1using DCL.Social.Chat;
 2using System;
 3using System.Collections;
 4using System.Collections.Generic;
 5using DCL.Helpers;
 6using TMPro;
 7using UnityEngine;
 8using UnityEngine.UI;
 9
 10namespace DCL.Social.Chat
 11{
 12    public class ChannelMembersComponentView : BaseComponentView, IChannelMembersComponentView
 13    {
 14        private const float REQUEST_MORE_ENTRIES_SCROLL_THRESHOLD = 0.005f;
 15        private const int ENTRIES_THROTTLING = 30;
 16
 17        [SerializeField] internal CollapsableChannelMemberListComponentView memberList;
 18        [SerializeField] internal GameObject resultsHeaderLabelContainer;
 19        [SerializeField] internal TMP_Text resultsHeaderLabel;
 20        [SerializeField] internal GameObject loadingContainer;
 21        [SerializeField] internal ScrollRect scroll;
 22        [SerializeField] internal SearchBarComponentView searchBar;
 23        [SerializeField] internal GameObject loadMoreContainer;
 24        [SerializeField] internal GameObject loadMoreSpinner;
 25        [SerializeField] internal GameObject emptyStateForCommon;
 26        [SerializeField] internal GameObject emptyStateForSearch;
 27
 2828        private readonly Queue<ChannelMemberEntryModel> queuedEntries = new Queue<ChannelMemberEntryModel>();
 29        private bool isLayoutDirty;
 30        private bool isSortDirty;
 31        private Vector2 lastScrollPosition;
 32        private Coroutine requireMoreEntriesRoutine;
 33
 34        public event Action<string> OnSearchUpdated;
 35        public event Action OnRequestMoreMembers;
 36
 037        public int EntryCount => memberList.Count() + queuedEntries.Count;
 38
 39        public override void Awake()
 40        {
 1441            base.Awake();
 42
 1443            searchBar.OnSearchText += s =>
 44            {
 345                SetSearchModeActive(!string.IsNullOrEmpty(s));
 346                OnSearchUpdated?.Invoke(s);
 247            };
 1448            memberList.SortingMethod = (a, b) => a.Model.userName.CompareTo(b.Model.userName);
 1449            scroll.onValueChanged.AddListener(LoadMoreEntries);
 1450        }
 51
 52        public void Update()
 53        {
 6054            if (isLayoutDirty)
 255                ((RectTransform)scroll.transform).ForceUpdateLayout();
 56
 6057            isLayoutDirty = false;
 58
 6059            SetQueuedEntries();
 60
 6061            if (isSortDirty)
 262                memberList.Sort();
 63
 6064            isSortDirty = false;
 6065        }
 66
 67        public override void Dispose()
 68        {
 2869            if (!this) return;
 2870            if (!gameObject) return;
 2871            base.Dispose();
 2872        }
 73
 74        public void ClearAllEntries()
 75        {
 576            memberList.Clear();
 577            queuedEntries.Clear();
 578            UpdateLayout();
 579            UpdateHeaders();
 580        }
 81
 82        public void ShowLoading()
 83        {
 284            loadingContainer.SetActive(true);
 285            memberList.gameObject.SetActive(false);
 286            resultsHeaderLabel.gameObject.SetActive(false);
 287        }
 88
 289        public void Set(ChannelMemberEntryModel user) => queuedEntries.Enqueue(user);
 90
 91        public void Remove(string userId)
 92        {
 193            memberList.Remove(userId);
 194            UpdateLayout();
 195            UpdateHeaders();
 196        }
 97
 398        public override void Show(bool instant = false) => gameObject.SetActive(true);
 99
 1100        public override void Hide(bool instant = false) => gameObject.SetActive(false);
 101
 102        public void ClearSearchInput(bool notify = true)
 103        {
 5104            searchBar.ClearSearch(notify);
 5105            SetSearchModeActive(false);
 5106        }
 107
 108        public void HideLoading()
 109        {
 1110            loadingContainer.SetActive(false);
 1111            memberList.gameObject.SetActive(true);
 1112            resultsHeaderLabel.gameObject.SetActive(true);
 1113        }
 114
 1115        public void ShowLoadingMore() => loadMoreContainer.SetActive(true);
 116
 2117        public void HideLoadingMore() => loadMoreContainer.SetActive(false);
 118
 1119        public void ShowResultsHeader() => resultsHeaderLabelContainer.SetActive(true);
 120
 1121        public void HideResultsHeader() => resultsHeaderLabelContainer.SetActive(false);
 122
 0123        public override void RefreshControl() { }
 124
 8125        private void UpdateLayout() => isLayoutDirty = true;
 126
 2127        private void Sort() => isSortDirty = true;
 128
 129        private void UpdateHeaders()
 130        {
 8131            var text = $"Results ({memberList.Count()})";
 132
 8133            if (!string.IsNullOrEmpty(searchBar.Text))
 0134                text = "Did you mean?";
 135
 8136            resultsHeaderLabel.text = text;
 8137        }
 138
 139        private void LoadMoreEntries(Vector2 scrollPosition)
 140        {
 0141            if (scrollPosition.y < REQUEST_MORE_ENTRIES_SCROLL_THRESHOLD &&
 142                lastScrollPosition.y >= REQUEST_MORE_ENTRIES_SCROLL_THRESHOLD)
 143            {
 0144                if (requireMoreEntriesRoutine != null)
 0145                    StopCoroutine(requireMoreEntriesRoutine);
 146
 0147                loadMoreSpinner.SetActive(true);
 0148                requireMoreEntriesRoutine = StartCoroutine(WaitThenRequireMoreEntries());
 149            }
 150
 0151            lastScrollPosition = scrollPosition;
 0152        }
 153
 154        private IEnumerator WaitThenRequireMoreEntries()
 155        {
 0156            yield return new WaitForSeconds(1f);
 0157            loadMoreSpinner.SetActive(false);
 0158            OnRequestMoreMembers?.Invoke();
 0159        }
 160
 161        private void SetQueuedEntries()
 162        {
 118163            if (queuedEntries.Count <= 0) return;
 164
 8165            for (var i = 0; i < ENTRIES_THROTTLING && queuedEntries.Count > 0; i++)
 166            {
 2167                var user = queuedEntries.Dequeue();
 2168                memberList.Set(user.userId, user);
 169            }
 170
 2171            UpdateLayout();
 2172            Sort();
 2173            UpdateHeaders();
 2174        }
 175
 176        private void SetSearchModeActive(bool isActive)
 177        {
 8178            if (emptyStateForCommon != null)
 5179                emptyStateForCommon.SetActive(!isActive);
 180
 8181            if (emptyStateForSearch != null)
 5182                emptyStateForSearch.SetActive(isActive);
 8183        }
 184    }
 185}