< Summary

Class:DCL.Social.Chat.Channels.JoinChannelComponentController
Assembly:JoinChannelHUD
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/JoinChannelHUD/JoinChannelComponentController.cs
Covered lines:73
Uncovered lines:5
Coverable lines:78
Total lines:186
Line coverage:93.5% (73 of 78)
Covered branches:0
Total branches:0
Covered methods:9
Total methods:9
Method coverage:100% (9 of 9)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
JoinChannelComponentController(...)0%110100%
Dispose()0%110100%
OnChannelToJoinChanged(...)0%5.015092.86%
OnCancelJoin()0%220100%
OnConfirmJoin(...)0%110100%
<OnConfirmJoin()0%14.513079.31%
ShowErrorToast(...)0%110100%
OpenChannelWindow(...)0%110100%
GetChannelLinkSource()0%14.189060%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/JoinChannelHUD/JoinChannelComponentController.cs

#LineLine coverage
 1using Cysharp.Threading.Tasks;
 2using DCL.Chat;
 3using DCL.Chat.Channels;
 4using DCL.Tasks;
 5using SocialFeaturesAnalytics;
 6using System;
 7using System.Collections.Generic;
 8using System.Linq;
 9using System.Threading;
 10using Channel = DCL.Chat.Channels.Channel;
 11
 12namespace DCL.Social.Chat.Channels
 13{
 14    public class JoinChannelComponentController : IDisposable
 15    {
 16        internal readonly IJoinChannelComponentView view;
 17        internal readonly IChatController chatController;
 18        private readonly DataStore dataStore;
 19        internal readonly DataStore_Channels channelsDataStore;
 20        private readonly ISocialAnalytics socialAnalytics;
 21        private readonly BaseVariable<(string playerId, string source)> currentPlayerInfoCardId;
 22        private readonly IChannelsFeatureFlagService channelsFeatureFlagService;
 1323        private CancellationTokenSource joinChannelCancellationToken = new ();
 24        private string channelName;
 25
 1326        public JoinChannelComponentController(
 27            IJoinChannelComponentView view,
 28            IChatController chatController,
 29            DataStore dataStore,
 30            ISocialAnalytics socialAnalytics,
 31            IChannelsFeatureFlagService channelsFeatureFlagService)
 32        {
 1333            this.view = view;
 1334            this.chatController = chatController;
 1335            this.dataStore = dataStore;
 1336            channelsDataStore = dataStore.channels;
 1337            this.socialAnalytics = socialAnalytics;
 1338            this.currentPlayerInfoCardId = dataStore.HUDs.currentPlayerId;
 1339            this.channelsFeatureFlagService = channelsFeatureFlagService;
 40
 1341            channelsDataStore.currentJoinChannelModal.OnChange += OnChannelToJoinChanged;
 1342            this.view.OnCancelJoin += OnCancelJoin;
 1343            this.view.OnConfirmJoin += OnConfirmJoin;
 1344        }
 45
 46        public void Dispose()
 47        {
 1348            joinChannelCancellationToken.SafeCancelAndDispose();
 1349            channelsDataStore.currentJoinChannelModal.OnChange -= OnChannelToJoinChanged;
 1350            view.OnCancelJoin -= OnCancelJoin;
 1351            view.OnConfirmJoin -= OnConfirmJoin;
 1352        }
 53
 54        private void OnChannelToJoinChanged(string currentChannelName, string previousChannelName)
 55        {
 856            if (!channelsFeatureFlagService.IsChannelsFeatureEnabled())
 057                return;
 58
 859            if (string.IsNullOrEmpty(currentChannelName))
 460                return;
 61
 462            currentChannelName = currentChannelName.Replace("#", "")
 63                                                   .Replace("~", "")
 64                                                   .ToLower();
 65
 466            Channel alreadyJoinedChannel = chatController.GetAllocatedChannelByName(currentChannelName);
 67
 468            if (alreadyJoinedChannel is { Joined: true })
 69            {
 170                OpenChannelWindow(alreadyJoinedChannel);
 171                return;
 72            }
 73
 374            channelName = currentChannelName;
 375            view.SetChannel(currentChannelName);
 376            view.HideLoading();
 377            view.Show();
 378        }
 79
 80        private void OnCancelJoin()
 81        {
 282            if (channelsDataStore.channelJoinedSource.Get() == ChannelJoinedSource.Link)
 183                socialAnalytics.SendChannelLinkClicked(channelName, false, GetChannelLinkSource());
 84
 285            view.Hide();
 286            channelsDataStore.currentJoinChannelModal.Set(null);
 287        }
 88
 89        private void OnConfirmJoin(string channelName)
 90        {
 791            channelName = channelName
 92                         .Replace("#", "")
 93                         .Replace("~", "")
 94                         .ToLower();
 95
 96            async UniTaskVoid JoinAsync(string channelName, CancellationToken cancellationToken = default)
 97            {
 98                try
 99                {
 7100                    Channel alreadyJoinedChannel = chatController.GetAllocatedChannelByName(channelName);
 101
 7102                    if (alreadyJoinedChannel != null)
 103                    {
 2104                        if (!alreadyJoinedChannel.Joined)
 105                        {
 2106                            view.ShowLoading();
 2107                            alreadyJoinedChannel = await chatController.JoinOrCreateChannelAsync(alreadyJoinedChannel.Ch
 108                        }
 109
 2110                        OpenChannelWindow(alreadyJoinedChannel);
 111                    }
 112                    else
 113                    {
 5114                        view.ShowLoading();
 115
 5116                        (string pageToken, Channel[] channels) = await chatController.GetChannelsByNameAsync(1, channelN
 5117                        Channel channelToJoin = channels.FirstOrDefault(channel => channel.Name == channelName);
 118
 3119                        if (channelToJoin != null)
 120                        {
 1121                            channelToJoin = await chatController.JoinOrCreateChannelAsync(channelToJoin.ChannelId, cance
 1122                            OpenChannelWindow(channelToJoin);
 123                        }
 124                        else
 2125                            ShowErrorToast("The channel you are trying to access does not exist");
 126                    }
 5127                }
 1128                catch (ChannelException)
 129                {
 1130                    ShowErrorToast("There was a problem trying to process your request, try again later");
 1131                    throw;
 132                }
 133                finally
 134                {
 7135                    if (channelsDataStore.channelJoinedSource.Get() == ChannelJoinedSource.Link)
 1136                        socialAnalytics.SendChannelLinkClicked(channelName, true, GetChannelLinkSource());
 137
 7138                    view.HideLoading();
 7139                    view.Hide();
 7140                    channelsDataStore.currentJoinChannelModal.Set(null);
 141                }
 5142            }
 143
 7144            joinChannelCancellationToken = joinChannelCancellationToken.SafeRestart();
 7145            JoinAsync(channelName, joinChannelCancellationToken.Token).Forget();
 7146        }
 147
 148        private void ShowErrorToast(string message)
 149        {
 3150            dataStore.notifications.DefaultErrorNotification.Set(message, true);
 3151        }
 152
 153        private void OpenChannelWindow(Channel channel)
 154        {
 4155            dataStore.channels.channelToBeOpened.Set(channel.ChannelId, true);
 4156        }
 157
 158        private ChannelLinkSource GetChannelLinkSource()
 159        {
 2160            if (dataStore.exploreV2.isOpen.Get()
 161                && dataStore.exploreV2.placesAndEventsVisible.Get()
 162                && dataStore.exploreV2.isSomeModalOpen.Get())
 163            {
 0164                switch (dataStore.exploreV2.currentVisibleModal.Get())
 165                {
 166                    case ExploreV2CurrentModal.Events:
 0167                        return ChannelLinkSource.Event;
 168                    case ExploreV2CurrentModal.Places:
 0169                        return ChannelLinkSource.Place;
 170                }
 171            }
 172
 2173            if (!string.IsNullOrEmpty(currentPlayerInfoCardId.Get().playerId))
 1174                return ChannelLinkSource.Profile;
 175
 1176            var visibleTaskbarPanels = dataStore.HUDs.visibleTaskbarPanels.Get();
 177
 2178            if (visibleTaskbarPanels.Any(panel => panel == "PrivateChatChannel"
 179                                                  || panel == "ChatChannel"
 180                                                  || panel == "PublicChatChannel"))
 1181                return ChannelLinkSource.Chat;
 182
 0183            return ChannelLinkSource.Unknown;
 184        }
 185    }
 186}