| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Linq; |
| | 4 | | using UnityEngine; |
| | 5 | | using Object = UnityEngine.Object; |
| | 6 | |
|
| | 7 | | internal class UsersSearchUserViewsHandler : IDisposable |
| | 8 | | { |
| | 9 | | public event Action<string> OnRemoveUser; |
| | 10 | | public event Action<string> OnAddUser; |
| | 11 | |
|
| 26 | 12 | | internal readonly Dictionary<string, UserElementView> userElementViews = new Dictionary<string, UserElementView>(); |
| 26 | 13 | | private readonly Queue<UserElementView> viewPool = new Queue<UserElementView>(); |
| | 14 | |
|
| | 15 | | private readonly UserElementView userElementViewBase; |
| | 16 | | private readonly Transform elementsParent; |
| | 17 | |
|
| | 18 | | private List<string> usersInRolList; |
| | 19 | |
|
| 0 | 20 | | public int userElementViewCount => userElementViews.Count; |
| | 21 | |
|
| 26 | 22 | | public UsersSearchUserViewsHandler(UserElementView userElementViewBase, Transform elementsParent) |
| | 23 | | { |
| 26 | 24 | | this.userElementViewBase = userElementViewBase; |
| 26 | 25 | | this.elementsParent = elementsParent; |
| 26 | 26 | | PoolUserView(userElementViewBase); |
| 26 | 27 | | } |
| | 28 | |
|
| | 29 | | public void Dispose() |
| | 30 | | { |
| 80 | 31 | | foreach (UserElementView userView in userElementViews.Values) |
| | 32 | | { |
| 14 | 33 | | userView.Dispose(); |
| | 34 | | } |
| | 35 | |
|
| 26 | 36 | | userElementViews.Clear(); |
| | 37 | |
|
| 46 | 38 | | while (viewPool.Count > 0) |
| | 39 | | { |
| 20 | 40 | | viewPool.Dequeue().Dispose(); |
| | 41 | | } |
| 26 | 42 | | } |
| | 43 | |
|
| | 44 | | public void SetUserViewsList(List<UserProfile> profiles) |
| | 45 | | { |
| | 46 | | UserProfile profile; |
| 32 | 47 | | for (int i = 0; i < profiles.Count; i++) |
| | 48 | | { |
| 11 | 49 | | profile = profiles[i]; |
| 11 | 50 | | if (profile == null) |
| | 51 | | continue; |
| | 52 | |
|
| 11 | 53 | | SetUserViewList(profile.userId, |
| | 54 | | view => |
| | 55 | | { |
| 11 | 56 | | view.SetUserProfile(profile); |
| 11 | 57 | | }); |
| | 58 | | } |
| 5 | 59 | | } |
| | 60 | |
|
| | 61 | | public void SetUserViewsList(UserProfileModel[] profiles) |
| | 62 | | { |
| 1 | 63 | | List<UserElementView> newUserList = new List<UserElementView>(); |
| | 64 | |
|
| | 65 | | UserProfileModel profile; |
| 8 | 66 | | for (int i = 0; i < profiles.Length; i++) |
| | 67 | | { |
| 3 | 68 | | profile = profiles[i]; |
| 3 | 69 | | if (profile == null) |
| | 70 | | continue; |
| | 71 | |
|
| 3 | 72 | | var userElementView = SetUserViewList(profile.userId, |
| | 73 | | view => |
| | 74 | | { |
| 3 | 75 | | view.SetUserProfileModel(profile); |
| 3 | 76 | | }); |
| 3 | 77 | | newUserList.Add(userElementView); |
| | 78 | | } |
| | 79 | |
|
| 1 | 80 | | SetVisibleList(newUserList); |
| 1 | 81 | | } |
| | 82 | |
|
| | 83 | | public void RemoveUserView(string userId) |
| | 84 | | { |
| 0 | 85 | | if (userElementViews.TryGetValue(userId, out UserElementView userElementView)) |
| | 86 | | { |
| 0 | 87 | | PoolUserView(userElementView); |
| | 88 | | } |
| 0 | 89 | | } |
| | 90 | |
|
| | 91 | | public void SetVisibleList(List<UserElementView> viewsList) |
| | 92 | | { |
| 12 | 93 | | if (viewsList == null) |
| | 94 | | { |
| 5 | 95 | | return; |
| | 96 | | } |
| | 97 | |
|
| 48 | 98 | | foreach (UserElementView userElementView in userElementViews.Values) |
| | 99 | | { |
| 17 | 100 | | userElementView.SetActive(false); |
| | 101 | | } |
| | 102 | |
|
| 44 | 103 | | for (int i = 0; i < viewsList.Count; i++) |
| | 104 | | { |
| 15 | 105 | | viewsList[i].SetActive(true); |
| 15 | 106 | | viewsList[i].SetOrder(i); |
| | 107 | | } |
| 7 | 108 | | } |
| | 109 | |
|
| | 110 | | public void SetUsersInRolList(List<string> usersId) |
| | 111 | | { |
| 15 | 112 | | usersInRolList = usersId; |
| 36 | 113 | | foreach (KeyValuePair<string, UserElementView> keyValuePair in userElementViews) |
| | 114 | | { |
| 3 | 115 | | keyValuePair.Value.SetIsAdded(usersId.Contains(keyValuePair.Key)); |
| | 116 | | } |
| 15 | 117 | | } |
| | 118 | |
|
| 5 | 119 | | public List<UserElementView> GetUserElementViews() { return userElementViews.Values.ToList(); } |
| | 120 | |
|
| | 121 | | private void PoolUserView(UserElementView userView) |
| | 122 | | { |
| 26 | 123 | | userView.SetActive(false); |
| 26 | 124 | | viewPool.Enqueue(userView); |
| 26 | 125 | | } |
| | 126 | |
|
| | 127 | | private UserElementView GetUserView() |
| | 128 | | { |
| | 129 | | UserElementView userView; |
| | 130 | |
|
| 14 | 131 | | if (viewPool.Count > 0) |
| | 132 | | { |
| 6 | 133 | | userView = viewPool.Dequeue(); |
| 6 | 134 | | } |
| | 135 | | else |
| | 136 | | { |
| 8 | 137 | | userView = Object.Instantiate(userElementViewBase, elementsParent); |
| | 138 | | } |
| | 139 | |
|
| 14 | 140 | | userView.ClearThumbnail(); |
| | 141 | |
|
| 14 | 142 | | userView.OnAddPressed -= OnAddUserPressed; |
| 14 | 143 | | userView.OnRemovePressed -= OnRemoveUserPressed; |
| 14 | 144 | | userView.OnAddPressed += OnAddUserPressed; |
| 14 | 145 | | userView.OnRemovePressed += OnRemoveUserPressed; |
| | 146 | |
|
| 14 | 147 | | return userView; |
| | 148 | | } |
| | 149 | |
|
| | 150 | | private UserElementView SetUserViewList(string userId, Action<UserElementView> OnAddNewElement) |
| | 151 | | { |
| 14 | 152 | | bool isBlocked = UserProfile.GetOwnUserProfile().blocked.Contains(userId); |
| 14 | 153 | | bool isAddedRol = usersInRolList?.Contains(userId) ?? false; |
| | 154 | |
|
| 14 | 155 | | if (!userElementViews.TryGetValue(userId, out UserElementView userElementView)) |
| | 156 | | { |
| 14 | 157 | | userElementView = GetUserView(); |
| 14 | 158 | | OnAddNewElement?.Invoke(userElementView); |
| 14 | 159 | | userElementView.SetAlwaysHighlighted(true); |
| 14 | 160 | | userElementViews.Add(userId, userElementView); |
| | 161 | | } |
| | 162 | |
|
| 14 | 163 | | userElementView.SetBlocked(isBlocked); |
| 14 | 164 | | userElementView.SetIsAdded(isAddedRol); |
| 14 | 165 | | return userElementView; |
| | 166 | | } |
| | 167 | |
|
| 2 | 168 | | void OnAddUserPressed(string userId) { OnAddUser?.Invoke(userId); } |
| | 169 | |
|
| 2 | 170 | | void OnRemoveUserPressed(string userId) { OnRemoveUser?.Invoke(userId); } |
| | 171 | | } |