< Summary

Class:DCL.Social.Chat.Mentions.MentionLinkDetector
Assembly:Mentions
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/Mentions/MentionLinkDetector.cs
Covered lines:17
Uncovered lines:43
Coverable lines:60
Total lines:139
Line coverage:28.3% (17 of 60)
Covered branches:0
Total branches:0
Covered methods:6
Total methods:12
Method coverage:50% (6 of 12)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
MentionLinkDetector()0%110100%
Awake()0%2.062075%
Start()0%110100%
OnDestroy()0%2.022083.33%
OnPointerClick(...)0%20400%
SetContextMenu(...)0%110100%
GetUserNameByPointerPosition(...)0%30500%
ShowContextMenu(...)0%6200%
OnTextComponentPreRenderText(...)0%6.83025%
RefreshMentionPatterns()0%12300%
CheckMentionAsync()0%30500%
OnFeatureFlagsChanged(...)0%2100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/Mentions/MentionLinkDetector.cs

#LineLine coverage
 1using Cysharp.Threading.Tasks;
 2using DCL.Tasks;
 3using System;
 4using System.Threading;
 5using TMPro;
 6using UnityEngine;
 7using UnityEngine.EventSystems;
 8
 9namespace DCL.Social.Chat.Mentions
 10{
 11    public class MentionLinkDetector : MonoBehaviour, IPointerClickHandler
 12    {
 13        private const string MENTION_URL_PREFIX = "mention://";
 14
 15        public event Action OnPlayerMentioned;
 16
 17        [SerializeField] internal TMP_Text textComponent;
 18
 4319        internal bool isMentionsFeatureEnabled = true;
 20        internal string currentText;
 21        internal bool hasNoParseLabel;
 22        private UserContextMenu contextMenu;
 4323        private readonly CancellationTokenSource cancellationToken = new ();
 24
 25        private void Awake()
 26        {
 2727            if (textComponent == null)
 028                return;
 29
 2730            textComponent.OnPreRenderText += OnTextComponentPreRenderText;
 2731        }
 32
 33        private void Start()
 34        {
 1735            isMentionsFeatureEnabled = DataStore.i.featureFlags.flags.Get().IsFeatureEnabled("chat_mentions_enabled");
 1736            DataStore.i.featureFlags.flags.OnChange += OnFeatureFlagsChanged;
 1737        }
 38
 39        private void OnDestroy()
 40        {
 2741            if (textComponent == null)
 042                return;
 43
 2744            textComponent.OnPreRenderText -= OnTextComponentPreRenderText;
 2745            DataStore.i.featureFlags.flags.OnChange -= OnFeatureFlagsChanged;
 46
 2747            cancellationToken.SafeCancelAndDispose();
 2748        }
 49
 50        public void OnPointerClick(PointerEventData eventData)
 51        {
 052            if (!isMentionsFeatureEnabled)
 053                return;
 54
 055            if (eventData.button != PointerEventData.InputButton.Left)
 056                return;
 57
 058            string userName = GetUserNameByPointerPosition(eventData.position);
 059            if (string.IsNullOrEmpty(userName))
 060                return;
 61
 062            ShowContextMenu(userName);
 063        }
 64
 65        public void SetContextMenu(UserContextMenu userContextMenu)
 66        {
 567            this.contextMenu = userContextMenu;
 568        }
 69
 70        private string GetUserNameByPointerPosition(Vector2 pointerPosition)
 71        {
 072            if (textComponent == null || textComponent.canvas == null)
 073                return null;
 74
 075            int linkIndex = TMP_TextUtilities.FindIntersectingLink(textComponent, pointerPosition, textComponent.canvas.
 076            if (linkIndex == -1)
 077                return null;
 78
 079            TMP_LinkInfo linkInfo = textComponent.textInfo.linkInfo[linkIndex];
 80
 081            string mentionText = linkInfo.GetLinkText();
 082            string mentionLink = linkInfo.GetLinkID();
 83
 084            return !MentionsUtils.IsAMention(mentionText)
 85                ? null
 86                : mentionLink.Replace(MENTION_URL_PREFIX, string.Empty);
 87        }
 88
 89        private void ShowContextMenu(string userName)
 90        {
 091            if (contextMenu == null)
 092                return;
 93
 094            var menuTransform = (RectTransform)contextMenu.transform;
 095            menuTransform.position = textComponent.transform.position;
 096            contextMenu.ShowByUserName(userName);
 097        }
 98
 99        private void OnTextComponentPreRenderText(TMP_TextInfo textInfo)
 100        {
 19101            if (!isMentionsFeatureEnabled)
 19102                return;
 103
 0104            if (textInfo.textComponent.text == currentText)
 0105                return;
 106
 0107            hasNoParseLabel = textInfo.textComponent.text.Contains("<noparse>", StringComparison.OrdinalIgnoreCase);
 0108            RefreshMentionPatterns(cancellationToken.Token).Forget();
 0109            CheckMentionAsync(textInfo.textComponent, cancellationToken.Token).Forget();
 0110        }
 111
 112        private async UniTask RefreshMentionPatterns(CancellationToken cancellationToken)
 113        {
 0114            await UniTask.WaitForEndOfFrame(this, cancellationToken);
 115
 0116            textComponent.text = MentionsUtils.ReplaceMentionPattern(textComponent.text, mention =>
 117            {
 0118                string mentionWithoutSymbol = mention[1..];
 119
 0120                return hasNoParseLabel
 121                    ? $"</noparse><link={MENTION_URL_PREFIX}{mentionWithoutSymbol}><color=#4886E3><u>{mention}</u></colo
 122                    : $"<link={MENTION_URL_PREFIX}{mentionWithoutSymbol}><color=#4886E3><u>{mention}</u></color></link>"
 123            });
 124
 0125            currentText = textComponent.text;
 0126        }
 127
 128        private async UniTask CheckMentionAsync(TMP_Text textComp, CancellationToken ct)
 129        {
 0130            await UniTask.WaitForEndOfFrame(this, ct);
 131
 0132            if (MentionsUtils.TextContainsMention(textComp.text))
 0133                OnPlayerMentioned?.Invoke();
 0134        }
 135
 136        private void OnFeatureFlagsChanged(FeatureFlag current, FeatureFlag previous) =>
 0137            isMentionsFeatureEnabled = current.IsFeatureEnabled("chat_mentions_enabled");
 138    }
 139}