< Summary

Class:DCL.Social.Chat.SearchChannelsWindowComponentView
Assembly:WorldChatWindowHUD
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/WorldChatWindowHUD/SearchChannelsWindowComponentView.cs
Covered lines:83
Uncovered lines:8
Coverable lines:91
Total lines:203
Line coverage:91.2% (83 of 91)
Covered branches:0
Total branches:0
Covered methods:23
Total methods:29
Method coverage:79.3% (23 of 29)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
Awake()0%330100%
Update()0%3.033085.71%
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%
ShowCreateChannelOnSearch()0%110100%
HideCreateChannelOnSearch()0%110100%
SetCreateChannelButtonsActive(...)0%330100%
RefreshControl()0%2100%
UpdateLayout()0%110100%
Sort()0%110100%
UpdateHeaders()0%220100%
HandleJoinRequest(...)0%6200%
HandleOpenChat(...)0%6200%
HandleLeaveRequest(...)0%220100%
LoadMoreEntries(...)0%4.054085.71%
WaitThenRequireMoreEntries()0%440100%

File(s)

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

#LineLine coverage
 1using System;
 2using System.Collections;
 3using DCL.Chat.Channels;
 4using DCL.Helpers;
 5using TMPro;
 6using UnityEngine;
 7using UnityEngine.UI;
 8
 9namespace DCL.Social.Chat
 10{
 11    public class SearchChannelsWindowComponentView : BaseComponentView, ISearchChannelsWindowView
 12    {
 13        [SerializeField] internal CollapsablePublicChannelListComponentView channelList;
 14        [SerializeField] internal GameObject resultsHeaderLabelContainer;
 15        [SerializeField] internal TMP_Text resultsHeaderLabel;
 16        [SerializeField] internal GameObject loadingContainer;
 17        [SerializeField] internal ScrollRect scroll;
 18        [SerializeField] internal SearchBarComponentView searchBar;
 19        [SerializeField] internal Button backButton;
 20        [SerializeField] internal Button closeButton;
 21        [SerializeField] internal GameObject loadMoreContainer;
 22        [SerializeField] internal GameObject loadMoreContent;
 23        [SerializeField] internal GameObject loadMoreSpinner;
 24        [SerializeField] internal GameObject createChannelOnSearchContainer;
 25        [SerializeField] internal GameObject createChannelOnSearchContent;
 26        [SerializeField] internal Button[] createChannelButtons;
 27
 28        private bool isLayoutDirty;
 29        private bool isSortDirty;
 30        private Vector2 lastScrollPosition;
 31        private Coroutine requireMoreEntriesRoutine;
 32        private bool shouldCreationBeActive;
 33        private bool isLoading;
 34
 35        public event Action OnBack;
 36        public event Action OnClose;
 37        public event Action<string> OnSearchUpdated;
 38        public event Action OnRequestMoreChannels;
 39        public event Action<string> OnJoinChannel;
 40        public event Action<string> OnLeaveChannel;
 41        public event Action OnCreateChannel;
 42        public event Action<string> OnOpenChannel;
 43
 044        public RectTransform Transform => (RectTransform) transform;
 045        public int EntryCount => channelList.Count();
 046        public bool IsActive => gameObject.activeInHierarchy;
 47
 48        public override void Awake()
 49        {
 2150            base.Awake();
 2151            backButton.onClick.AddListener(() => OnBack?.Invoke());
 2152            closeButton.onClick.AddListener(() => OnClose?.Invoke());
 2653            searchBar.OnSearchText += s => OnSearchUpdated?.Invoke(s);
 2154            channelList.SortingMethod = (a, b) => b.Model.memberCount.CompareTo(a.Model.memberCount);
 2155            scroll.onValueChanged.AddListener(LoadMoreEntries);
 56
 16857            foreach (var button in createChannelButtons)
 6358                button.onClick.AddListener(() => OnCreateChannel?.Invoke());
 2159        }
 60
 61        public void Update()
 62        {
 12163            if (isLayoutDirty)
 164                ((RectTransform) scroll.transform).ForceUpdateLayout();
 12165            isLayoutDirty = false;
 66
 12167            if (isSortDirty)
 068                channelList.Sort();
 12169            isSortDirty = false;
 12170        }
 71
 72        public override void Dispose()
 73        {
 4274            if (!this) return;
 4275            if (!gameObject) return;
 4276            base.Dispose();
 4277        }
 78
 79        public void ClearAllEntries()
 80        {
 381            channelList.Clear();
 382            UpdateLayout();
 383            UpdateHeaders();
 384        }
 85
 86        public void ShowLoading()
 87        {
 288            isLoading = true;
 289            loadingContainer.SetActive(true);
 290            channelList.gameObject.SetActive(false);
 291            resultsHeaderLabel.gameObject.SetActive(false);
 292            createChannelOnSearchContent.SetActive(false);
 293            loadMoreContent.SetActive(false);
 294        }
 95
 96        public void Set(Channel channel)
 97        {
 1398            channelList.Set(channel.ChannelId,
 99                new PublicChatEntryModel(channel.ChannelId, channel.Name, channel.Joined, channel.MemberCount,
 100                    showOnlyOnlineMembers: false, channel.Muted));
 101
 13102            var entry = channelList.Get(channel.ChannelId);
 13103            entry.OnJoin -= HandleJoinRequest;
 13104            entry.OnJoin += HandleJoinRequest;
 13105            entry.OnOpenChat -= HandleOpenChat;
 13106            entry.OnOpenChat += HandleOpenChat;
 13107            entry.OnLeave -= HandleLeaveRequest;
 13108            entry.OnLeave += HandleLeaveRequest;
 109
 13110            UpdateLayout();
 13111            Sort();
 13112            UpdateHeaders();
 13113        }
 114
 115        public void Show()
 116        {
 8117            gameObject.SetActive(true);
 8118            searchBar.SetFocus();
 8119        }
 120
 2121        public void Hide() => gameObject.SetActive(false);
 122
 2123        public void ClearSearchInput() => searchBar.ClearSearch();
 124
 125        public void HideLoading()
 126        {
 1127            isLoading = false;
 1128            loadingContainer.SetActive(false);
 1129            channelList.gameObject.SetActive(true);
 1130            resultsHeaderLabel.gameObject.SetActive(true);
 1131            createChannelOnSearchContent.SetActive(shouldCreationBeActive);
 1132            loadMoreContent.SetActive(true);
 1133        }
 134
 1135        public void ShowLoadingMore() => loadMoreContainer.SetActive(true);
 136
 1137        public void HideLoadingMore() => loadMoreContainer.SetActive(false);
 138
 1139        public void ShowResultsHeader() => resultsHeaderLabelContainer.SetActive(true);
 140
 1141        public void HideResultsHeader() => resultsHeaderLabelContainer.SetActive(false);
 142
 1143        public void ShowCreateChannelOnSearch() => createChannelOnSearchContainer.SetActive(true);
 144
 1145        public void HideCreateChannelOnSearch() => createChannelOnSearchContainer.SetActive(false);
 146
 147        public void SetCreateChannelButtonsActive(bool isActive)
 148        {
 1149            shouldCreationBeActive = isActive;
 150
 8151            foreach (var button in createChannelButtons)
 3152                button.gameObject.SetActive(isActive);
 153
 1154            createChannelOnSearchContent.SetActive(isActive && !isLoading);
 1155        }
 156
 157        public override void RefreshControl()
 158        {
 0159            throw new NotImplementedException();
 160        }
 161
 16162        private void UpdateLayout() => isLayoutDirty = true;
 163
 13164        private void Sort() => isSortDirty = true;
 165
 166        private void UpdateHeaders()
 167        {
 16168            var text = $"Results ({channelList.Count()})";
 169
 16170            if (!string.IsNullOrEmpty(searchBar.Text))
 4171                text = "Did you mean?";
 172
 16173            resultsHeaderLabel.text = text;
 16174        }
 175
 0176        private void HandleJoinRequest(PublicChatEntry entry) => OnJoinChannel?.Invoke(entry.Model.channelId);
 177
 0178        private void HandleOpenChat(PublicChatEntry entry) => OnOpenChannel?.Invoke(entry.Model.channelId);
 179
 1180        private void HandleLeaveRequest(PublicChatEntry entry) => OnLeaveChannel?.Invoke(entry.Model.channelId);
 181
 182        private void LoadMoreEntries(Vector2 scrollPosition)
 183        {
 2184            if (scrollPosition.y < 0.005f && lastScrollPosition.y >= 0.005f)
 185            {
 1186                if (requireMoreEntriesRoutine != null)
 0187                    StopCoroutine(requireMoreEntriesRoutine);
 188
 1189                loadMoreSpinner.SetActive(true);
 1190                requireMoreEntriesRoutine = StartCoroutine(WaitThenRequireMoreEntries());
 191            }
 192
 2193            lastScrollPosition = scrollPosition;
 2194        }
 195
 196        private IEnumerator WaitThenRequireMoreEntries()
 197        {
 1198            yield return new WaitForSeconds(1f);
 1199            loadMoreSpinner.SetActive(false);
 1200            OnRequestMoreChannels?.Invoke();
 1201        }
 202    }
 203}