| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using DCL.Chat; |
| | 3 | | using DCL.Chat.Channels; |
| | 4 | | using System.Collections.Generic; |
| | 5 | | using System.Threading; |
| | 6 | | using TMPro; |
| | 7 | | using UnityEngine; |
| | 8 | | using UnityEngine.EventSystems; |
| | 9 | |
|
| | 10 | | namespace DCL.Social.Chat |
| | 11 | | { |
| | 12 | | // TODO: refactor into MVC |
| | 13 | | public class ChannelLinkDetector : MonoBehaviour, IPointerClickHandler |
| | 14 | | { |
| | 15 | | [SerializeField] internal TMP_Text textComponent; |
| | 16 | |
|
| 644 | 17 | | private readonly CancellationTokenSource cancellationToken = new (); |
| | 18 | |
|
| | 19 | | internal string currentText; |
| | 20 | | internal bool hasNoParseLabel; |
| 644 | 21 | | internal List<string> channelsFoundInText = new (); |
| 644 | 22 | | private bool isAllowedToCreateChannels = true; |
| | 23 | |
|
| | 24 | | private void Awake() |
| | 25 | | { |
| 600 | 26 | | if (textComponent == null) |
| 2 | 27 | | return; |
| | 28 | |
|
| 598 | 29 | | textComponent.OnPreRenderText += OnTextComponentPreRenderText; |
| | 30 | |
|
| 598 | 31 | | if (Environment.i != null |
| | 32 | | && Environment.i.serviceLocator.Get<IChannelsFeatureFlagService>() != null) |
| | 33 | | { |
| 9 | 34 | | var channelsFeatureFlagService = Environment.i.serviceLocator.Get<IChannelsFeatureFlagService>(); |
| 9 | 35 | | isAllowedToCreateChannels = channelsFeatureFlagService.IsChannelsFeatureEnabled(); |
| | 36 | | } |
| 598 | 37 | | } |
| | 38 | |
|
| | 39 | | private void OnDestroy() |
| | 40 | | { |
| 600 | 41 | | cancellationToken.Cancel(); |
| 600 | 42 | | cancellationToken.Dispose(); |
| | 43 | |
|
| 600 | 44 | | if (textComponent == null) |
| 0 | 45 | | return; |
| | 46 | |
|
| 600 | 47 | | textComponent.OnPreRenderText -= OnTextComponentPreRenderText; |
| 600 | 48 | | } |
| | 49 | |
|
| | 50 | | public void OnPointerClick(PointerEventData eventData) |
| | 51 | | { |
| 0 | 52 | | if (!isAllowedToCreateChannels) return; |
| | 53 | |
|
| 0 | 54 | | if (eventData.button != PointerEventData.InputButton.Left) return; |
| 0 | 55 | | string clickedLink = GetChannelLinkByPointerPosition(eventData.position); |
| | 56 | |
|
| 0 | 57 | | if (!ChannelUtils.IsAChannel(clickedLink)) return; |
| | 58 | |
|
| 0 | 59 | | if (UserProfile.GetOwnUserProfile().isGuest) |
| 0 | 60 | | DataStore.i.HUDs.connectWalletModalVisible.Set(true); |
| | 61 | | else |
| | 62 | | { |
| 0 | 63 | | DataStore.i.channels.channelJoinedSource.Set(ChannelJoinedSource.Link); |
| 0 | 64 | | DataStore.i.channels.currentJoinChannelModal.Set(clickedLink.ToLower(), true); |
| | 65 | | } |
| 0 | 66 | | } |
| | 67 | |
|
| | 68 | | private void OnTextComponentPreRenderText(TMP_TextInfo textInfo) |
| | 69 | | { |
| 252 | 70 | | if (currentText == textComponent.text) return; |
| 261 | 71 | | if (!isAllowedToCreateChannels) return; |
| | 72 | |
|
| 243 | 73 | | hasNoParseLabel = textInfo.textComponent.text.ToLower().Contains("<noparse>"); |
| 243 | 74 | | RefreshChannelPatterns(cancellationToken.Token).Forget(); |
| 243 | 75 | | } |
| | 76 | |
|
| | 77 | | internal async UniTask RefreshChannelPatterns(CancellationToken cancellationToken) |
| | 78 | | { |
| 492 | 79 | | await UniTask.WaitForEndOfFrame(this, cancellationToken); |
| | 80 | |
|
| 2 | 81 | | channelsFoundInText = ChannelUtils.ExtractChannelIdsFromText(textComponent.text); |
| | 82 | |
|
| 20 | 83 | | foreach (string channelFound in channelsFoundInText) |
| | 84 | | { |
| 8 | 85 | | 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 | |
|
| 2 | 92 | | currentText = textComponent.text; |
| 2 | 93 | | } |
| | 94 | |
|
| | 95 | | private string GetChannelLinkByPointerPosition(Vector2 pointerPosition) |
| | 96 | | { |
| 0 | 97 | | if (textComponent == null) |
| 0 | 98 | | return ""; |
| | 99 | |
|
| 0 | 100 | | string result = string.Empty; |
| 0 | 101 | | int linkIndex = TMP_TextUtilities.FindIntersectingLink(textComponent, pointerPosition, textComponent.canvas. |
| | 102 | |
|
| 0 | 103 | | if (linkIndex != -1) |
| | 104 | | { |
| 0 | 105 | | TMP_LinkInfo linkInfo = textComponent.textInfo.linkInfo[linkIndex]; |
| 0 | 106 | | result = linkInfo.GetLinkID(); |
| | 107 | | } |
| | 108 | |
|
| 0 | 109 | | return result; |
| | 110 | | } |
| | 111 | | } |
| | 112 | | } |