| | 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 SectionSceneAdminsSettingsView : MonoBehaviour, IDisposable |
| | 9 | | { |
| | 10 | | [SerializeField] internal UsersSearchPromptView adminsSearchPromptView; |
| | 11 | | [SerializeField] internal UsersSearchPromptView blockedSearchPromptView; |
| | 12 | | [SerializeField] internal Button addAdminButton; |
| | 13 | | [SerializeField] internal Button addBlockedButton; |
| | 14 | | [SerializeField] internal UserElementView adminElementView; |
| | 15 | | [SerializeField] internal UserElementView blockedElementView; |
| | 16 | | [SerializeField] internal Transform adminsContainer; |
| | 17 | | [SerializeField] internal Transform blockedContainer; |
| | 18 | | [SerializeField] internal GameObject adminsEmptyListContainer; |
| | 19 | | [SerializeField] internal GameObject blockedEmptyListContainer; |
| | 20 | | [SerializeField] internal TextMeshProUGUI labelAdmins; |
| | 21 | | [SerializeField] internal TextMeshProUGUI labelBlocked; |
| | 22 | |
|
| | 23 | | public event Action OnSearchFriendButtonPressed; |
| | 24 | | public event Action OnSearchUserButtonPressed; |
| | 25 | |
|
| 9 | 26 | | internal readonly Dictionary<string, UserElementView> adminsElementViews = new Dictionary<string, UserElementView>() |
| 9 | 27 | | internal readonly Dictionary<string, UserElementView> bannedUsersElementViews = new Dictionary<string, UserElementVi |
| 9 | 28 | | private readonly Queue<UserElementView> userElementViewsPool = new Queue<UserElementView>(); |
| | 29 | |
|
| | 30 | | private string adminsLabelFormat; |
| | 31 | | private string blockedLabelFormat; |
| | 32 | | private bool isDestroyed; |
| | 33 | |
|
| | 34 | | private void Awake() |
| | 35 | | { |
| 8 | 36 | | addAdminButton.onClick.AddListener(() => OnSearchFriendButtonPressed?.Invoke()); |
| 8 | 37 | | addBlockedButton.onClick.AddListener(() => OnSearchUserButtonPressed?.Invoke()); |
| 8 | 38 | | PoolView(adminElementView); |
| 8 | 39 | | PoolView(blockedElementView); |
| 8 | 40 | | adminsLabelFormat = labelAdmins.text; |
| 8 | 41 | | blockedLabelFormat = labelBlocked.text; |
| 8 | 42 | | } |
| | 43 | |
|
| 16 | 44 | | private void OnDestroy() { isDestroyed = true; } |
| | 45 | |
|
| | 46 | | public void Dispose() |
| | 47 | | { |
| 8 | 48 | | if (!isDestroyed) |
| | 49 | | { |
| 8 | 50 | | Destroy(gameObject); |
| | 51 | | } |
| 8 | 52 | | } |
| | 53 | |
|
| | 54 | | public void SetParent(Transform parent) |
| | 55 | | { |
| 0 | 56 | | transform.SetParent(parent); |
| 0 | 57 | | transform.ResetLocalTRS(); |
| 0 | 58 | | } |
| | 59 | |
|
| 0 | 60 | | public void SetActive(bool active) { gameObject.SetActive(active); } |
| | 61 | |
|
| 0 | 62 | | public UsersSearchPromptView GetAdminsSearchPromptView() { return adminsSearchPromptView; } |
| | 63 | |
|
| 0 | 64 | | public UsersSearchPromptView GetBlockedSearchPromptView() { return blockedSearchPromptView; } |
| | 65 | |
|
| | 66 | | public void SetAdminsEmptyList(bool isEmpty) |
| | 67 | | { |
| 6 | 68 | | adminsContainer.gameObject.SetActive(!isEmpty); |
| 6 | 69 | | adminsEmptyListContainer.SetActive(isEmpty); |
| | 70 | |
|
| 6 | 71 | | if (isEmpty) |
| | 72 | | { |
| 2 | 73 | | ClearElementViewsDictionary(adminsElementViews); |
| | 74 | | } |
| 6 | 75 | | } |
| | 76 | |
|
| | 77 | | public void SetBannedUsersEmptyList(bool isEmpty) |
| | 78 | | { |
| 6 | 79 | | blockedContainer.gameObject.SetActive(!isEmpty); |
| 6 | 80 | | blockedEmptyListContainer.SetActive(isEmpty); |
| | 81 | |
|
| 6 | 82 | | if (isEmpty) |
| | 83 | | { |
| 2 | 84 | | ClearElementViewsDictionary(bannedUsersElementViews); |
| | 85 | | } |
| 6 | 86 | | } |
| | 87 | |
|
| 10 | 88 | | public void SetAdminsCount(int count) { labelAdmins.text = string.Format(adminsLabelFormat, count); } |
| | 89 | |
|
| 10 | 90 | | public void SetBannedUsersCount(int count) { labelBlocked.text = string.Format(blockedLabelFormat, count); } |
| | 91 | |
|
| 7 | 92 | | public UserElementView AddAdmin(string userId) { return AddUser(userId, true); } |
| | 93 | |
|
| 9 | 94 | | public UserElementView AddBannedUser(string userId) { return AddUser(userId, false); } |
| | 95 | |
|
| 2 | 96 | | public bool RemoveAdmin(string userId) { return RemoveUser(userId, true); } |
| | 97 | |
|
| 1 | 98 | | public bool RemoveBannedUser(string userId) { return RemoveUser(userId, false); } |
| | 99 | |
|
| | 100 | | UserElementView AddUser(string userId, bool addAsAdmin) |
| | 101 | | { |
| 16 | 102 | | var dictionary = addAsAdmin ? adminsElementViews : bannedUsersElementViews; |
| 16 | 103 | | if (!dictionary.TryGetValue(userId, out UserElementView view)) |
| | 104 | | { |
| 12 | 105 | | view = GetView(); |
| 12 | 106 | | view.SetUserName(userId); |
| 12 | 107 | | view.SetUserId(userId); |
| 12 | 108 | | view.SetAlwaysHighlighted(false); |
| 12 | 109 | | view.SetIsAdded(true); |
| 12 | 110 | | view.SetActive(true); |
| 12 | 111 | | dictionary.Add(userId, view); |
| | 112 | | } |
| | 113 | |
|
| 16 | 114 | | bool isBlocked = UserProfile.GetOwnUserProfile().blocked.Contains(userId); |
| 16 | 115 | | view.SetBlocked(isBlocked); |
| 16 | 116 | | view.SetParent(addAsAdmin ? adminsContainer : blockedContainer); |
| 16 | 117 | | return view; |
| | 118 | | } |
| | 119 | |
|
| | 120 | | bool RemoveUser(string userId, bool removeAsAdmin) |
| | 121 | | { |
| 3 | 122 | | var dictionary = removeAsAdmin ? adminsElementViews : bannedUsersElementViews; |
| 3 | 123 | | if (!dictionary.TryGetValue(userId, out UserElementView view)) |
| | 124 | | { |
| 0 | 125 | | return false; |
| | 126 | | } |
| | 127 | |
|
| 3 | 128 | | Transform container = removeAsAdmin ? adminsContainer : blockedContainer; |
| | 129 | |
|
| 3 | 130 | | if (view.GetParent() != container) |
| | 131 | | { |
| 0 | 132 | | return false; |
| | 133 | | } |
| | 134 | |
|
| 3 | 135 | | dictionary.Remove(userId); |
| 3 | 136 | | PoolView(view); |
| 3 | 137 | | return true; |
| | 138 | | } |
| | 139 | |
|
| | 140 | | void PoolView(UserElementView view) |
| | 141 | | { |
| 19 | 142 | | view.SetActive(false); |
| 19 | 143 | | userElementViewsPool.Enqueue(view); |
| 19 | 144 | | } |
| | 145 | |
|
| | 146 | | UserElementView GetView() |
| | 147 | | { |
| | 148 | | UserElementView userView; |
| | 149 | |
|
| 12 | 150 | | if (userElementViewsPool.Count > 0) |
| | 151 | | { |
| 8 | 152 | | userView = userElementViewsPool.Dequeue(); |
| 8 | 153 | | } |
| | 154 | | else |
| | 155 | | { |
| 4 | 156 | | userView = Instantiate(adminElementView, adminsContainer); |
| | 157 | | } |
| 12 | 158 | | userView.ClearThumbnail(); |
| 12 | 159 | | return userView; |
| | 160 | | } |
| | 161 | |
|
| | 162 | | void ClearElementViewsDictionary(Dictionary<string, UserElementView> dictionary) |
| | 163 | | { |
| 8 | 164 | | foreach (UserElementView view in dictionary.Values) |
| | 165 | | { |
| 0 | 166 | | PoolView(view); |
| | 167 | | } |
| 4 | 168 | | dictionary.Clear(); |
| 4 | 169 | | } |
| | 170 | | } |