| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using TMPro; |
| | 4 | | using UnityEngine; |
| | 5 | | using UnityEngine.Networking; |
| | 6 | | using UnityEngine.UI; |
| | 7 | |
|
| | 8 | | namespace DCL.MyAccount |
| | 9 | | { |
| | 10 | | public class MyProfileLinkListComponentView : MonoBehaviour |
| | 11 | | { |
| | 12 | | [SerializeField] private Button addButton; |
| | 13 | | [SerializeField] private TMP_InputField newLinkTitle; |
| | 14 | | [SerializeField] private TMP_InputField newLinkUrl; |
| | 15 | | [SerializeField] private MyProfileLinkComponentView linkPrefab; |
| | 16 | | [SerializeField] private RectTransform linksContainer; |
| | 17 | |
|
| 0 | 18 | | private readonly List<MyProfileLinkComponentView> links = new (); |
| | 19 | |
|
| 0 | 20 | | private bool isAddEnabled = true; |
| | 21 | |
|
| | 22 | | public event Action<(string title, string url)> OnAddedNew; |
| | 23 | | public event Action<(string title, string url)> OnRemoved; |
| | 24 | |
|
| | 25 | | private void Awake() |
| | 26 | | { |
| 0 | 27 | | addButton.onClick.AddListener(() => |
| | 28 | | { |
| 0 | 29 | | if (!newLinkUrl.text.StartsWith("http://", StringComparison.OrdinalIgnoreCase) |
| | 30 | | && !newLinkUrl.text.StartsWith("https://", StringComparison.OrdinalIgnoreCase)) |
| 0 | 31 | | newLinkUrl.text = $"https://{newLinkUrl.text}"; |
| | 32 | |
|
| 0 | 33 | | OnAddedNew?.Invoke(( |
| | 34 | | title: newLinkTitle.text, |
| | 35 | | url: newLinkUrl.text)); |
| 0 | 36 | | }); |
| | 37 | |
|
| 0 | 38 | | newLinkTitle.onValueChanged.AddListener(str => EnableOrDisableAddButton()); |
| 0 | 39 | | newLinkUrl.onValueChanged.AddListener(str => EnableOrDisableAddButton()); |
| | 40 | |
|
| 0 | 41 | | EnableOrDisableAddButton(); |
| 0 | 42 | | } |
| | 43 | |
|
| | 44 | | public void Add(string title, string url) |
| | 45 | | { |
| 0 | 46 | | MyProfileLinkComponentView linkComponent = Instantiate(linkPrefab, linksContainer); |
| 0 | 47 | | linkComponent.Set(title, UnityWebRequest.UnEscapeURL(url)); |
| 0 | 48 | | linkComponent.OnRemoved += OnRemoved; |
| 0 | 49 | | links.Add(linkComponent); |
| 0 | 50 | | } |
| | 51 | |
|
| | 52 | | public void Clear() |
| | 53 | | { |
| 0 | 54 | | foreach (MyProfileLinkComponentView linkComponent in links) |
| | 55 | | { |
| 0 | 56 | | linkComponent.OnRemoved -= OnRemoved; |
| 0 | 57 | | Destroy(linkComponent.gameObject); |
| | 58 | | } |
| | 59 | |
|
| 0 | 60 | | links.Clear(); |
| 0 | 61 | | } |
| | 62 | |
|
| | 63 | | public void ClearInput() |
| | 64 | | { |
| 0 | 65 | | newLinkTitle.text = ""; |
| 0 | 66 | | newLinkUrl.text = ""; |
| 0 | 67 | | } |
| | 68 | |
|
| | 69 | | public void EnableOrDisableAddNewLinkOption(bool enabled) |
| | 70 | | { |
| 0 | 71 | | isAddEnabled = enabled; |
| 0 | 72 | | EnableOrDisableAddButton(); |
| 0 | 73 | | } |
| | 74 | |
|
| | 75 | | private void EnableOrDisableAddButton() |
| | 76 | | { |
| 0 | 77 | | addButton.interactable = newLinkTitle.text.Length > 0 && newLinkUrl.text.Length > 0 |
| | 78 | | && LinkValidator.IsValid(newLinkUrl.text) |
| | 79 | | && isAddEnabled; |
| 0 | 80 | | } |
| | 81 | | } |
| | 82 | | } |