| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using DCL.Helpers; |
| | 4 | | using TMPro; |
| | 5 | | using UnityEngine; |
| | 6 | | using UnityEngine.UI; |
| | 7 | |
|
| | 8 | | internal class SectionSceneContributorsSettingsView : MonoBehaviour, IDisposable |
| | 9 | | { |
| | 10 | | [SerializeField] internal UsersSearchPromptView usersSearchPromptView; |
| | 11 | | [SerializeField] internal Button addUserButton; |
| | 12 | | [SerializeField] internal UserElementView userElementView; |
| | 13 | | [SerializeField] internal Transform usersContainer; |
| | 14 | | [SerializeField] internal GameObject emptyListContainer; |
| | 15 | | [SerializeField] internal TextMeshProUGUI labelContributor; |
| | 16 | |
|
| | 17 | | public event Action OnSearchUserButtonPressed; |
| | 18 | |
|
| 5 | 19 | | internal readonly Dictionary<string, UserElementView> userElementViews = new Dictionary<string, UserElementView>(); |
| 5 | 20 | | private readonly Queue<UserElementView> userElementViewsPool = new Queue<UserElementView>(); |
| | 21 | |
|
| | 22 | | private string contributorLabelFormat; |
| | 23 | | private bool isDestroyed = false; |
| | 24 | |
|
| | 25 | | private void Awake() |
| | 26 | | { |
| 4 | 27 | | addUserButton.onClick.AddListener(() => OnSearchUserButtonPressed?.Invoke()); |
| 4 | 28 | | PoolView(userElementView); |
| 4 | 29 | | contributorLabelFormat = labelContributor.text; |
| 4 | 30 | | } |
| | 31 | |
|
| 8 | 32 | | private void OnDestroy() { isDestroyed = true; } |
| | 33 | |
|
| | 34 | | public void Dispose() |
| | 35 | | { |
| 4 | 36 | | if (!isDestroyed) |
| | 37 | | { |
| 4 | 38 | | Destroy(gameObject); |
| | 39 | | } |
| 4 | 40 | | } |
| | 41 | |
|
| | 42 | | public void SetParent(Transform parent) |
| | 43 | | { |
| 0 | 44 | | transform.SetParent(parent); |
| 0 | 45 | | transform.ResetLocalTRS(); |
| 0 | 46 | | } |
| | 47 | |
|
| 0 | 48 | | public void SetActive(bool active) { gameObject.SetActive(active); } |
| | 49 | |
|
| 0 | 50 | | public UsersSearchPromptView GetSearchPromptView() { return usersSearchPromptView; } |
| | 51 | |
|
| | 52 | | public void SetEmptyList(bool isEmpty) |
| | 53 | | { |
| 5 | 54 | | usersContainer.gameObject.SetActive(!isEmpty); |
| 5 | 55 | | emptyListContainer.SetActive(isEmpty); |
| | 56 | |
|
| 5 | 57 | | if (isEmpty) |
| | 58 | | { |
| 4 | 59 | | foreach (UserElementView view in userElementViews.Values) |
| | 60 | | { |
| 0 | 61 | | PoolView(view); |
| | 62 | | } |
| 2 | 63 | | userElementViews.Clear(); |
| | 64 | | } |
| 5 | 65 | | } |
| | 66 | |
|
| 8 | 67 | | public void SetContributorsCount(int count) { labelContributor.text = string.Format(contributorLabelFormat, count); |
| | 68 | |
|
| | 69 | | public UserElementView AddUser(string userId) |
| | 70 | | { |
| 6 | 71 | | if (!userElementViews.TryGetValue(userId, out UserElementView view)) |
| | 72 | | { |
| 4 | 73 | | view = GetView(); |
| 4 | 74 | | view.SetUserName(userId); |
| 4 | 75 | | view.SetUserId(userId); |
| 4 | 76 | | view.SetAlwaysHighlighted(false); |
| 4 | 77 | | view.SetIsAdded(true); |
| 4 | 78 | | view.SetActive(true); |
| 4 | 79 | | userElementViews.Add(userId, view); |
| | 80 | | } |
| | 81 | |
|
| 6 | 82 | | bool isBlocked = UserProfile.GetOwnUserProfile().blocked.Contains(userId); |
| 6 | 83 | | view.SetBlocked(isBlocked); |
| 6 | 84 | | return view; |
| | 85 | | } |
| | 86 | |
|
| | 87 | | public bool RemoveUser(string userId) |
| | 88 | | { |
| 2 | 89 | | if (!userElementViews.TryGetValue(userId, out UserElementView view)) |
| | 90 | | { |
| 0 | 91 | | return false; |
| | 92 | | } |
| 2 | 93 | | userElementViews.Remove(userId); |
| 2 | 94 | | PoolView(view); |
| 2 | 95 | | return true; |
| | 96 | | } |
| | 97 | |
|
| | 98 | | void PoolView(UserElementView view) |
| | 99 | | { |
| 6 | 100 | | view.SetActive(false); |
| 6 | 101 | | userElementViewsPool.Enqueue(view); |
| 6 | 102 | | } |
| | 103 | |
|
| | 104 | | UserElementView GetView() |
| | 105 | | { |
| | 106 | | UserElementView userView; |
| | 107 | |
|
| 4 | 108 | | if (userElementViewsPool.Count > 0) |
| | 109 | | { |
| 2 | 110 | | userView = userElementViewsPool.Dequeue(); |
| 2 | 111 | | } |
| | 112 | | else |
| | 113 | | { |
| 2 | 114 | | userView = Instantiate(userElementView, usersContainer); |
| | 115 | | } |
| 4 | 116 | | userView.ClearThumbnail(); |
| 4 | 117 | | return userView; |
| | 118 | | } |
| | 119 | | } |