| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using DCL.Helpers; |
| | 4 | | using UnityEngine; |
| | 5 | |
|
| | 6 | | internal class UsersSearchPromptController : IDisposable |
| | 7 | | { |
| | 8 | | private const int MAX_USERS_RESULT = 10; |
| | 9 | |
|
| | 10 | | public event Action<string> OnRemoveUser; |
| | 11 | | public event Action<string> OnAddUser; |
| | 12 | |
|
| | 13 | | private readonly UsersSearchPromptView view; |
| | 14 | | internal readonly UsersSearchUserViewsHandler userViewsHandler; |
| | 15 | | private readonly UsersSearcher usersSearcher; |
| | 16 | |
|
| | 17 | | internal Promise<UserProfileModel[]> usersSearchPromise = null; |
| | 18 | |
|
| 9 | 19 | | public UsersSearchPromptController(UsersSearchPromptView promptView) |
| | 20 | | { |
| 9 | 21 | | view = promptView; |
| 9 | 22 | | view.SetIdleSearchTime(1.5f); |
| | 23 | |
|
| 9 | 24 | | userViewsHandler = new UsersSearchUserViewsHandler(view.GetUsersBaseElement(), view.GetUserElementsParent()); |
| 9 | 25 | | usersSearcher = new UsersSearcher(); |
| | 26 | |
|
| 9 | 27 | | view.OnSearchText += OnSearchText; |
| 9 | 28 | | view.OnShouldHide += OnShouldHidePrompt; |
| 9 | 29 | | userViewsHandler.OnAddUser += OnAddUserPressed; |
| 9 | 30 | | userViewsHandler.OnRemoveUser += OnRemoveUserPressed; |
| 9 | 31 | | } |
| | 32 | |
|
| | 33 | | public void Dispose() |
| | 34 | | { |
| 9 | 35 | | view.OnSearchText -= OnSearchText; |
| 9 | 36 | | view.OnShouldHide -= OnShouldHidePrompt; |
| 9 | 37 | | userViewsHandler.OnAddUser -= OnAddUserPressed; |
| 9 | 38 | | userViewsHandler.OnRemoveUser -= OnRemoveUserPressed; |
| | 39 | |
|
| 9 | 40 | | usersSearchPromise?.Dispose(); |
| 9 | 41 | | userViewsHandler.Dispose(); |
| 9 | 42 | | view.Dispose(); |
| 9 | 43 | | } |
| | 44 | |
|
| | 45 | | public void Show() |
| | 46 | | { |
| 1 | 47 | | view.ClearSearch(); |
| 1 | 48 | | view.SetFriendListEmpty(true); |
| 1 | 49 | | view.Show(); |
| 1 | 50 | | } |
| | 51 | |
|
| | 52 | | public void Hide() |
| | 53 | | { |
| 0 | 54 | | view.Hide(); |
| 0 | 55 | | usersSearchPromise?.Dispose(); |
| 0 | 56 | | } |
| | 57 | |
|
| 10 | 58 | | public void SetUsersInRolList(List<string> usersId) { userViewsHandler.SetUsersInRolList(usersId); } |
| | 59 | |
|
| | 60 | | private void OnSearchText(string searchText) |
| | 61 | | { |
| 2 | 62 | | usersSearchPromise?.Dispose(); |
| | 63 | |
|
| 2 | 64 | | view.ShowSearchSpinner(); |
| 2 | 65 | | usersSearchPromise = usersSearcher.SearchUser(searchText, MAX_USERS_RESULT); |
| 2 | 66 | | usersSearchPromise.Then( |
| | 67 | | result => |
| | 68 | | { |
| 1 | 69 | | view.ShowClearButton(); |
| 1 | 70 | | if (result == null || result.Length == 0) |
| | 71 | | { |
| 0 | 72 | | view.SetFriendListEmpty(true); |
| 0 | 73 | | } |
| | 74 | | else |
| | 75 | | { |
| 1 | 76 | | view.SetFriendListEmpty(false); |
| 1 | 77 | | userViewsHandler.SetUserViewsList(result); |
| | 78 | | } |
| 1 | 79 | | }); |
| 2 | 80 | | } |
| | 81 | |
|
| 0 | 82 | | private void OnShouldHidePrompt() { Hide(); } |
| | 83 | |
|
| 0 | 84 | | private void OnAddUserPressed(string userId) { OnAddUser?.Invoke(userId); } |
| | 85 | |
|
| 0 | 86 | | private void OnRemoveUserPressed(string userId) { OnRemoveUser?.Invoke(userId); } |
| | 87 | | } |