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