< Summary

Class:DCL.Social.Chat.ChannelLinkDetector
Assembly:WorldChatWindowHUD
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/WorldChatWindowHUD/ChannelLinkDetector.cs
Covered lines:26
Uncovered lines:18
Coverable lines:44
Total lines:112
Line coverage:59% (26 of 44)
Covered branches:0
Total branches:0
Covered methods:5
Total methods:7
Method coverage:71.4% (5 of 7)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
ChannelLinkDetector()0%110100%
Awake()0%440100%
OnDestroy()0%2.022083.33%
OnPointerClick(...)0%30500%
OnTextComponentPreRenderText(...)0%3.033085.71%
RefreshChannelPatterns()0%660100%
GetChannelLinkByPointerPosition(...)0%12300%

File(s)

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

#LineLine coverage
 1using Cysharp.Threading.Tasks;
 2using DCL.Chat;
 3using DCL.Chat.Channels;
 4using System.Collections.Generic;
 5using System.Threading;
 6using TMPro;
 7using UnityEngine;
 8using UnityEngine.EventSystems;
 9
 10namespace DCL.Social.Chat
 11{
 12    // TODO: refactor into MVC
 13    public class ChannelLinkDetector : MonoBehaviour, IPointerClickHandler
 14    {
 15        [SerializeField] internal TMP_Text textComponent;
 16
 64417        private readonly CancellationTokenSource cancellationToken = new ();
 18
 19        internal string currentText;
 20        internal bool hasNoParseLabel;
 64421        internal List<string> channelsFoundInText = new ();
 64422        private bool isAllowedToCreateChannels = true;
 23
 24        private void Awake()
 25        {
 60026            if (textComponent == null)
 227                return;
 28
 59829            textComponent.OnPreRenderText += OnTextComponentPreRenderText;
 30
 59831            if (Environment.i != null
 32                && Environment.i.serviceLocator.Get<IChannelsFeatureFlagService>() != null)
 33            {
 934                var channelsFeatureFlagService = Environment.i.serviceLocator.Get<IChannelsFeatureFlagService>();
 935                isAllowedToCreateChannels = channelsFeatureFlagService.IsChannelsFeatureEnabled();
 36            }
 59837        }
 38
 39        private void OnDestroy()
 40        {
 60041            cancellationToken.Cancel();
 60042            cancellationToken.Dispose();
 43
 60044            if (textComponent == null)
 045                return;
 46
 60047            textComponent.OnPreRenderText -= OnTextComponentPreRenderText;
 60048        }
 49
 50        public void OnPointerClick(PointerEventData eventData)
 51        {
 052            if (!isAllowedToCreateChannels) return;
 53
 054            if (eventData.button != PointerEventData.InputButton.Left) return;
 055            string clickedLink = GetChannelLinkByPointerPosition(eventData.position);
 56
 057            if (!ChannelUtils.IsAChannel(clickedLink)) return;
 58
 059            if (UserProfile.GetOwnUserProfile().isGuest)
 060                DataStore.i.HUDs.connectWalletModalVisible.Set(true);
 61            else
 62            {
 063                DataStore.i.channels.channelJoinedSource.Set(ChannelJoinedSource.Link);
 064                DataStore.i.channels.currentJoinChannelModal.Set(clickedLink.ToLower(), true);
 65            }
 066        }
 67
 68        private void OnTextComponentPreRenderText(TMP_TextInfo textInfo)
 69        {
 25270            if (currentText == textComponent.text) return;
 26171            if (!isAllowedToCreateChannels) return;
 72
 24373            hasNoParseLabel = textInfo.textComponent.text.ToLower().Contains("<noparse>");
 24374            RefreshChannelPatterns(cancellationToken.Token).Forget();
 24375        }
 76
 77        internal async UniTask RefreshChannelPatterns(CancellationToken cancellationToken)
 78        {
 49279            await UniTask.WaitForEndOfFrame(this, cancellationToken);
 80
 281            channelsFoundInText = ChannelUtils.ExtractChannelIdsFromText(textComponent.text);
 82
 2083            foreach (string channelFound in channelsFoundInText)
 84            {
 885                textComponent.text = textComponent.text.Replace(
 86                    channelFound,
 87                    hasNoParseLabel ?
 88                        $"</noparse><link={channelFound}><color=#4886E3><u>{channelFound}</u></color></link><noparse>" :
 89                        $"<link={channelFound}><color=#4886E3><u>{channelFound}</u></color></link>");
 90            }
 91
 292            currentText = textComponent.text;
 293        }
 94
 95        private string GetChannelLinkByPointerPosition(Vector2 pointerPosition)
 96        {
 097            if (textComponent == null)
 098                return "";
 99
 0100            string result = string.Empty;
 0101            int linkIndex = TMP_TextUtilities.FindIntersectingLink(textComponent, pointerPosition, textComponent.canvas.
 102
 0103            if (linkIndex != -1)
 104            {
 0105                TMP_LinkInfo linkInfo = textComponent.textInfo.linkInfo[linkIndex];
 0106                result = linkInfo.GetLinkID();
 107            }
 108
 0109            return result;
 110        }
 111    }
 112}