< Summary

Class:DCL.Chat.HUD.ChannelMembersHUDController
Assembly:WorldChatWindowHUD
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/WorldChatWindowHUD/ChannelMembersHUDController.cs
Covered lines:100
Uncovered lines:18
Coverable lines:118
Total lines:240
Line coverage:84.7% (100 of 118)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
ChannelMembersHUDController(...)0%110100%
SetChannelId(...)0%3.793055.56%
SetMembersCount(...)0%110100%
Dispose()0%110100%
SetVisibility(...)0%220100%
LoadMembers()0%110100%
SearchMembers(...)0%220100%
UpdateChannelMembers(...)0%5.395075%
LoadMoreMembers()0%5.055087.5%
SetLoadingMoreVisible(...)0%3.043083.33%
SetAutomaticReloadingActive(...)0%220100%
ReloadMembersPeriodically()0%7.914037.5%
IsLoading()0%110100%
ClearListeners()0%110100%
WaitTimeoutThenHideLoading()0%6.984042.86%

File(s)

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

#LineLine coverage
 1using System;
 2using System.Threading;
 3using Cysharp.Threading.Tasks;
 4using DCL.Chat.Channels;
 5
 6namespace DCL.Chat.HUD
 7{
 8    public class ChannelMembersHUDController : IDisposable
 9    {
 10        private const int LOAD_TIMEOUT = 2;
 11        private const int LOAD_PAGE_SIZE = 30;
 12        private const int MINUTES_FOR_AUTOMATIC_RELOADING = 1;
 13        private readonly IChatController chatController;
 14        private readonly IUserProfileBridge userProfileBridge;
 15        private IChannelMembersComponentView view;
 1616        internal DateTime loadStartedTimestamp = DateTime.MinValue;
 1617        private CancellationTokenSource loadingCancellationToken = new CancellationTokenSource();
 1618        private CancellationTokenSource reloadingCancellationToken = new CancellationTokenSource();
 19        private string currentChannelId;
 20        private int lastLimitRequested;
 21        private bool isSearching;
 22        private bool isVisible;
 23        private int currentMembersCount;
 24
 125        public IChannelMembersComponentView View => view;
 126        public string CurrentChannelId => currentChannelId;
 427        public bool IsVisible => isVisible;
 28
 1629        public ChannelMembersHUDController(IChannelMembersComponentView view, IChatController chatController,
 30            IUserProfileBridge userProfileBridge)
 31        {
 1632            this.view = view;
 1633            this.chatController = chatController;
 1634            this.userProfileBridge = userProfileBridge;
 1635        }
 36
 37        public void SetChannelId(string channelId)
 38        {
 1239            if (string.IsNullOrEmpty(channelId))
 040                return;
 41
 1242            currentChannelId = channelId;
 1243            lastLimitRequested = LOAD_PAGE_SIZE;
 44
 1245            if (isVisible)
 46            {
 047                LoadMembers();
 048                SetAutomaticReloadingActive(true);
 049                view.ClearSearchInput();
 50            }
 1251        }
 52
 953        public void SetMembersCount(int membersCount) => currentMembersCount = membersCount;
 54
 55        public void Dispose()
 56        {
 1657            ClearListeners();
 1658            view.Dispose();
 1659            loadingCancellationToken.Cancel();
 1660            loadingCancellationToken.Dispose();
 1661            reloadingCancellationToken.Cancel();
 1662            reloadingCancellationToken.Dispose();
 1663        }
 64
 65        public void SetVisibility(bool visible)
 66        {
 767            isVisible = visible;
 68
 769            if (visible)
 70            {
 671                LoadMembers();
 672                SetAutomaticReloadingActive(true);
 673                view.ClearSearchInput(notify: false);
 74            }
 75            else
 76            {
 177                SetAutomaticReloadingActive(false);
 178                ClearListeners();
 179                view.Hide();
 80            }
 181        }
 82
 83        private void LoadMembers()
 84        {
 685            ClearListeners();
 686            isSearching = false;
 87
 688            view.OnSearchUpdated += SearchMembers;
 689            view.OnRequestMoreMembers += LoadMoreMembers;
 690            chatController.OnUpdateChannelMembers += UpdateChannelMembers;
 91
 692            view.Show();
 693            view.ClearAllEntries();
 694            SetLoadingMoreVisible(false);
 695            view.ShowLoading();
 96
 697            loadStartedTimestamp = DateTime.Now;
 698            string[] channelsToGetInfo = {currentChannelId};
 699            chatController.GetChannelInfo(channelsToGetInfo);
 6100            chatController.GetChannelMembers(currentChannelId, lastLimitRequested, 0);
 101
 6102            loadingCancellationToken.Cancel();
 6103            loadingCancellationToken = new CancellationTokenSource();
 6104            WaitTimeoutThenHideLoading(loadingCancellationToken.Token).Forget();
 6105        }
 106
 107        private void SearchMembers(string searchText)
 108        {
 2109            loadStartedTimestamp = DateTime.Now;
 2110            view.ClearAllEntries();
 2111            SetLoadingMoreVisible(false);
 2112            view.ShowLoading();
 113
 2114            isSearching = !string.IsNullOrEmpty(searchText);
 115
 2116            if (!isSearching)
 117            {
 1118                chatController.GetChannelMembers(currentChannelId, lastLimitRequested, 0);
 1119                SetAutomaticReloadingActive(true);
 1120                view.HideResultsHeader();
 121            }
 122            else
 123            {
 1124                chatController.GetChannelMembers(currentChannelId, LOAD_PAGE_SIZE, 0, searchText);
 1125                SetAutomaticReloadingActive(false);
 1126                view.ShowResultsHeader();
 127            }
 128
 2129            loadingCancellationToken.Cancel();
 2130            loadingCancellationToken = new CancellationTokenSource();
 2131            WaitTimeoutThenHideLoading(loadingCancellationToken.Token).Forget();
 2132        }
 133
 134        private void UpdateChannelMembers(string channelId, ChannelMember[] channelMembers)
 135        {
 1136            SetLoadingMoreVisible(true);
 1137            view.HideLoading();
 138
 6139            foreach (ChannelMember member in channelMembers)
 140            {
 2141                UserProfile memberProfile = userProfileBridge.Get(member.userId);
 142
 2143                if (memberProfile != null)
 144                {
 2145                    ChannelMemberEntryModel userToAdd = new ChannelMemberEntryModel
 146                    {
 147                        isOnline = member.isOnline,
 148                        thumnailUrl = memberProfile.face256SnapshotURL,
 149                        userId = memberProfile.userId,
 150                        userName = memberProfile.userName,
 151                        isOptionsButtonHidden = memberProfile.userId == userProfileBridge.GetOwn().userId
 152                    };
 153
 2154                    view.Set(userToAdd);
 155                }
 156            }
 157
 1158            if (isSearching)
 159            {
 0160                SetLoadingMoreVisible(false);
 161
 0162                if (view.EntryCount > 0)
 0163                    view.ShowResultsHeader();
 164                else
 0165                    view.HideResultsHeader();
 166            }
 167            else
 168            {
 1169                SetLoadingMoreVisible(true);
 170            }
 1171        }
 172
 173        private void LoadMoreMembers()
 174        {
 1175            if (IsLoading() ||
 176                isSearching ||
 0177                lastLimitRequested >= currentMembersCount) return;
 178
 1179            loadStartedTimestamp = DateTime.Now;
 1180            SetLoadingMoreVisible(false);
 1181            chatController.GetChannelMembers(currentChannelId, LOAD_PAGE_SIZE, view.EntryCount);
 182
 1183            if (!isSearching)
 1184                lastLimitRequested = LOAD_PAGE_SIZE + view.EntryCount;
 1185        }
 186
 187        private void SetLoadingMoreVisible(bool isVisible)
 188        {
 11189            if (isVisible)
 190            {
 2191                if (lastLimitRequested >= currentMembersCount)
 0192                    view.HideLoadingMore();
 193                else
 2194                    view.ShowLoadingMore();
 195            }
 196            else
 9197                view.HideLoadingMore();
 9198        }
 199
 200        public void SetAutomaticReloadingActive(bool isActive)
 201        {
 10202            reloadingCancellationToken.Cancel();
 203
 10204            if (isActive)
 205            {
 7206                reloadingCancellationToken = new CancellationTokenSource();
 7207                ReloadMembersPeriodically(reloadingCancellationToken.Token).Forget();
 208            }
 10209        }
 210
 211        private async UniTask ReloadMembersPeriodically(CancellationToken cancellationToken)
 212        {
 0213            while (true)
 214            {
 21215                await UniTask.Delay(MINUTES_FOR_AUTOMATIC_RELOADING * 60 * 1000, cancellationToken: cancellationToken);
 216
 0217                if (cancellationToken.IsCancellationRequested)
 0218                    return;
 219
 0220                LoadMembers();
 221            }
 0222        }
 223
 1224        private bool IsLoading() => (DateTime.Now - loadStartedTimestamp).TotalSeconds < LOAD_TIMEOUT;
 225
 226        private void ClearListeners()
 227        {
 23228            view.OnSearchUpdated -= SearchMembers;
 23229            view.OnRequestMoreMembers -= LoadMoreMembers;
 23230            chatController.OnUpdateChannelMembers -= UpdateChannelMembers;
 23231        }
 232
 233        private async UniTask WaitTimeoutThenHideLoading(CancellationToken cancellationToken)
 234        {
 24235            await UniTask.Delay(LOAD_TIMEOUT * 1000, cancellationToken: cancellationToken);
 0236            if (cancellationToken.IsCancellationRequested) return;
 0237            view.HideLoading();
 0238        }
 239    }
 240}