< Summary

Class:DCL.Chat.HUD.SearchChannelsWindowComponentView
Assembly:WorldChatWindowHUD
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/WorldChatWindowHUD/SearchChannelsWindowComponentView.cs
Covered lines:81
Uncovered lines:9
Coverable lines:90
Total lines:205
Line coverage:90% (81 of 90)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
Awake()0%330100%
Update()0%3.023087.5%
Dispose()0%3.333066.67%
Create()0%110100%
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%2100%
Sort()0%2100%
UpdateHeaders()0%220100%
HandleJoinRequest(...)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.Chat.HUD
 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
 043        public RectTransform Transform => (RectTransform) transform;
 044        public int EntryCount => channelList.Count();
 045        public bool IsActive => gameObject.activeInHierarchy;
 46
 47        public override void Awake()
 48        {
 2149            base.Awake();
 2150            backButton.onClick.AddListener(() => OnBack?.Invoke());
 2151            closeButton.onClick.AddListener(() => OnClose?.Invoke());
 2652            searchBar.OnSearchText += s => OnSearchUpdated?.Invoke(s);
 2153            channelList.SortingMethod = (a, b) => b.Model.memberCount.CompareTo(a.Model.memberCount);
 2154            scroll.onValueChanged.AddListener(LoadMoreEntries);
 55
 16856            foreach (var button in createChannelButtons)
 6357                button.onClick.AddListener(() => OnCreateChannel?.Invoke());
 2158        }
 59
 60        public override void Update()
 61        {
 36362            base.Update();
 63
 36364            if (isLayoutDirty)
 165                ((RectTransform) scroll.transform).ForceUpdateLayout();
 36366            isLayoutDirty = false;
 67
 36368            if (isSortDirty)
 069                channelList.Sort();
 36370            isSortDirty = false;
 36371        }
 72
 73        public override void Dispose()
 74        {
 4275            if (!this) return;
 4276            if (!gameObject) return;
 4277            base.Dispose();
 4278        }
 79
 80        public static SearchChannelsWindowComponentView Create()
 81        {
 2182            return Instantiate(Resources.Load<SearchChannelsWindowComponentView>("SocialBarV1/ChannelSearchHUD"));
 83        }
 84
 85        public void ClearAllEntries()
 86        {
 387            channelList.Clear();
 388            UpdateLayout();
 389            UpdateHeaders();
 390        }
 91
 92        public void ShowLoading()
 93        {
 294            isLoading = true;
 295            loadingContainer.SetActive(true);
 296            channelList.gameObject.SetActive(false);
 297            resultsHeaderLabel.gameObject.SetActive(false);
 298            createChannelOnSearchContent.SetActive(false);
 299            loadMoreContent.SetActive(false);
 2100        }
 101
 102        public void Set(Channel channel)
 103        {
 13104            channelList.Set(channel.ChannelId,
 105                new PublicChatEntryModel(channel.ChannelId, channel.Name, channel.Joined, channel.MemberCount,
 106                    showOnlyOnlineMembers: false, channel.Muted));
 107
 13108            var entry = channelList.Get(channel.ChannelId);
 13109            entry.OnOpenChat -= HandleJoinRequest;
 13110            entry.OnOpenChat += HandleJoinRequest;
 13111            entry.OnLeave -= HandleLeaveRequest;
 13112            entry.OnLeave += HandleLeaveRequest;
 113
 13114            UpdateLayout();
 13115            Sort();
 13116            UpdateHeaders();
 13117        }
 118
 119        public void Show()
 120        {
 8121            gameObject.SetActive(true);
 8122            searchBar.SetFocus();
 8123        }
 124
 2125        public void Hide() => gameObject.SetActive(false);
 126
 2127        public void ClearSearchInput() => searchBar.ClearSearch();
 128
 129        public void HideLoading()
 130        {
 1131            isLoading = false;
 1132            loadingContainer.SetActive(false);
 1133            channelList.gameObject.SetActive(true);
 1134            resultsHeaderLabel.gameObject.SetActive(true);
 1135            createChannelOnSearchContent.SetActive(shouldCreationBeActive);
 1136            loadMoreContent.SetActive(true);
 1137        }
 138
 1139        public void ShowLoadingMore() => loadMoreContainer.SetActive(true);
 140
 1141        public void HideLoadingMore() => loadMoreContainer.SetActive(false);
 142
 1143        public void ShowResultsHeader() => resultsHeaderLabelContainer.SetActive(true);
 144
 1145        public void HideResultsHeader() => resultsHeaderLabelContainer.SetActive(false);
 146
 1147        public void ShowCreateChannelOnSearch() => createChannelOnSearchContainer.SetActive(true);
 148
 1149        public void HideCreateChannelOnSearch() => createChannelOnSearchContainer.SetActive(false);
 150
 151        public void SetCreateChannelButtonsActive(bool isActive)
 152        {
 1153            shouldCreationBeActive = isActive;
 154
 8155            foreach (var button in createChannelButtons)
 3156                button.gameObject.SetActive(isActive);
 157
 1158            createChannelOnSearchContent.SetActive(isActive && !isLoading);
 1159        }
 160
 161        public override void RefreshControl()
 162        {
 0163            throw new NotImplementedException();
 164        }
 165
 0166        private void UpdateLayout() => isLayoutDirty = true;
 167
 0168        private void Sort() => isSortDirty = true;
 169
 170        private void UpdateHeaders()
 171        {
 16172            var text = $"Results ({channelList.Count()})";
 173
 16174            if (!string.IsNullOrEmpty(searchBar.Text))
 4175                text = "Did you mean?";
 176
 16177            resultsHeaderLabel.text = text;
 16178        }
 179
 0180        private void HandleJoinRequest(PublicChatEntry entry) => OnJoinChannel?.Invoke(entry.Model.channelId);
 181
 1182        private void HandleLeaveRequest(PublicChatEntry entry) => OnLeaveChannel?.Invoke(entry.Model.channelId);
 183
 184        private void LoadMoreEntries(Vector2 scrollPosition)
 185        {
 2186            if (scrollPosition.y < 0.005f && lastScrollPosition.y >= 0.005f)
 187            {
 1188                if (requireMoreEntriesRoutine != null)
 0189                    StopCoroutine(requireMoreEntriesRoutine);
 190
 1191                loadMoreSpinner.SetActive(true);
 1192                requireMoreEntriesRoutine = StartCoroutine(WaitThenRequireMoreEntries());
 193            }
 194
 2195            lastScrollPosition = scrollPosition;
 2196        }
 197
 198        private IEnumerator WaitThenRequireMoreEntries()
 199        {
 1200            yield return new WaitForSeconds(1f);
 1201            loadMoreSpinner.SetActive(false);
 1202            OnRequestMoreChannels?.Invoke();
 1203        }
 204    }
 205}