< Summary

Class:DCL.Chat.HUD.ChannelMembersComponentView
Assembly:WorldChatWindowHUD
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/WorldChatWindowHUD/ChannelMembersComponentView.cs
Covered lines:53
Uncovered lines:16
Coverable lines:69
Total lines:165
Line coverage:76.8% (53 of 69)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
ChannelMembersComponentView()0%110100%
Awake()0%220100%
Update()0%3.013088.89%
Dispose()0%3.333066.67%
ClearAllEntries()0%110100%
ShowLoading()0%110100%
Set(...)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%
Create()0%2100%

File(s)

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

#LineLine coverage
 1using System;
 2using System.Collections;
 3using System.Collections.Generic;
 4using DCL.Helpers;
 5using TMPro;
 6using UnityEngine;
 7using UnityEngine.UI;
 8
 9namespace DCL.Chat.HUD
 10{
 11    public class ChannelMembersComponentView : BaseComponentView, IChannelMembersComponentView
 12    {
 13        private const float REQUEST_MORE_ENTRIES_SCROLL_THRESHOLD = 0.005f;
 14        private const int ENTRIES_THROTTLING = 30;
 15
 16        [SerializeField] internal CollapsableChannelMemberListComponentView memberList;
 17        [SerializeField] internal GameObject resultsHeaderLabelContainer;
 18        [SerializeField] internal TMP_Text resultsHeaderLabel;
 19        [SerializeField] internal GameObject loadingContainer;
 20        [SerializeField] internal ScrollRect scroll;
 21        [SerializeField] internal SearchBarComponentView searchBar;
 22        [SerializeField] internal GameObject loadMoreContainer;
 23        [SerializeField] internal GameObject loadMoreSpinner;
 24
 1725        private readonly Queue<ChannelMemberEntryModel> queuedEntries = new Queue<ChannelMemberEntryModel>();
 26        private bool isLayoutDirty;
 27        private bool isSortDirty;
 28        private Vector2 lastScrollPosition;
 29        private Coroutine requireMoreEntriesRoutine;
 30
 31        public event Action<string> OnSearchUpdated;
 32        public event Action OnRequestMoreMembers;
 33
 034        public int EntryCount => memberList.Count() + queuedEntries.Count;
 35
 36        public override void Awake()
 37        {
 1138            base.Awake();
 39
 1240            searchBar.OnSearchText += s => OnSearchUpdated?.Invoke(s);
 1141            memberList.SortingMethod = (a, b) => a.Model.userName.CompareTo(b.Model.userName);
 1142            scroll.onValueChanged.AddListener(LoadMoreEntries);
 1143        }
 44
 45        public override void Update()
 46        {
 347            base.Update();
 48
 349            if (isLayoutDirty)
 050                ((RectTransform)scroll.transform).ForceUpdateLayout();
 51
 352            isLayoutDirty = false;
 53
 354            SetQueuedEntries();
 55
 356            if (isSortDirty)
 157                memberList.Sort();
 58
 359            isSortDirty = false;
 360        }
 61
 62        public override void Dispose()
 63        {
 2364            if (!this) return;
 2365            if (!gameObject) return;
 2366            base.Dispose();
 2367        }
 68
 69        public void ClearAllEntries()
 70        {
 171            memberList.Clear();
 172            queuedEntries.Clear();
 173            UpdateLayout();
 174            UpdateHeaders();
 175        }
 76
 77        public void ShowLoading()
 78        {
 179            loadingContainer.SetActive(true);
 180            memberList.gameObject.SetActive(false);
 181            resultsHeaderLabel.gameObject.SetActive(false);
 182        }
 83
 184        public void Set(ChannelMemberEntryModel user) => queuedEntries.Enqueue(user);
 85
 186        public override void Show(bool instant = false) => gameObject.SetActive(true);
 87
 188        public override void Hide(bool instant = false) => gameObject.SetActive(false);
 89
 190        public void ClearSearchInput(bool notify = true) => searchBar.ClearSearch(notify);
 91
 92        public void HideLoading()
 93        {
 194            loadingContainer.SetActive(false);
 195            memberList.gameObject.SetActive(true);
 196            resultsHeaderLabel.gameObject.SetActive(true);
 197        }
 98
 199        public void ShowLoadingMore() => loadMoreContainer.SetActive(true);
 100
 1101        public void HideLoadingMore() => loadMoreContainer.SetActive(false);
 102
 1103        public void ShowResultsHeader() => resultsHeaderLabelContainer.SetActive(true);
 104
 1105        public void HideResultsHeader() => resultsHeaderLabelContainer.SetActive(false);
 106
 0107        public override void RefreshControl() { }
 108
 2109        private void UpdateLayout() => isLayoutDirty = true;
 110
 1111        private void Sort() => isSortDirty = true;
 112
 113        private void UpdateHeaders()
 114        {
 2115            var text = $"Results ({memberList.Count()})";
 116
 2117            if (!string.IsNullOrEmpty(searchBar.Text))
 0118                text = "Did you mean?";
 119
 2120            resultsHeaderLabel.text = text;
 2121        }
 122
 123        private void LoadMoreEntries(Vector2 scrollPosition)
 124        {
 0125            if (scrollPosition.y < REQUEST_MORE_ENTRIES_SCROLL_THRESHOLD &&
 126                lastScrollPosition.y >= REQUEST_MORE_ENTRIES_SCROLL_THRESHOLD)
 127            {
 0128                if (requireMoreEntriesRoutine != null)
 0129                    StopCoroutine(requireMoreEntriesRoutine);
 130
 0131                loadMoreSpinner.SetActive(true);
 0132                requireMoreEntriesRoutine = StartCoroutine(WaitThenRequireMoreEntries());
 133            }
 134
 0135            lastScrollPosition = scrollPosition;
 0136        }
 137
 138        private IEnumerator WaitThenRequireMoreEntries()
 139        {
 0140            yield return new WaitForSeconds(1f);
 0141            loadMoreSpinner.SetActive(false);
 0142            OnRequestMoreMembers?.Invoke();
 0143        }
 144
 145        private void SetQueuedEntries()
 146        {
 5147            if (queuedEntries.Count <= 0) return;
 148
 4149            for (var i = 0; i < ENTRIES_THROTTLING && queuedEntries.Count > 0; i++)
 150            {
 1151                var user = queuedEntries.Dequeue();
 1152                memberList.Set(user.userId, user);
 153            }
 154
 1155            UpdateLayout();
 1156            Sort();
 1157            UpdateHeaders();
 1158        }
 159
 160        public static ChannelMembersComponentView Create()
 161        {
 0162            return Instantiate(Resources.Load<ChannelMembersComponentView>("SocialBarV1/ChannelMembersHUD"));
 163        }
 164    }
 165}