| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | |
|
| | 4 | | internal class FriendsSearchPromptController : IDisposable |
| | 5 | | { |
| | 6 | | private const float IDLE_TIME_TRIGGER_SEARCH = 0.5f; |
| | 7 | |
|
| | 8 | | public event Action<string> OnRemoveUser; |
| | 9 | | public event Action<string> OnAddUser; |
| | 10 | |
|
| | 11 | | private readonly UsersSearchPromptView view; |
| | 12 | |
|
| 17 | 13 | | private readonly SearchHandler<UserElementView> searchHandler = new SearchHandler<UserElementView>(); |
| | 14 | | private readonly UsersSearchFriendsHandler friendsHandler; |
| | 15 | | internal readonly UsersSearchUserViewsHandler userViewsHandler; |
| | 16 | |
|
| 17 | 17 | | public FriendsSearchPromptController(UsersSearchPromptView promptView, IFriendsController friendsController) |
| | 18 | | { |
| 17 | 19 | | view = promptView; |
| 17 | 20 | | view.SetIdleSearchTime(IDLE_TIME_TRIGGER_SEARCH); |
| | 21 | |
|
| 17 | 22 | | friendsHandler = new UsersSearchFriendsHandler(friendsController); |
| 17 | 23 | | userViewsHandler = new UsersSearchUserViewsHandler(view.GetUsersBaseElement(), view.GetUserElementsParent()); |
| | 24 | |
|
| 17 | 25 | | friendsHandler.OnFriendRemoved += OnFriendRemoved; |
| 17 | 26 | | searchHandler.OnSearchChanged += OnSearchResult; |
| 17 | 27 | | view.OnSearchText += OnSearchText; |
| 17 | 28 | | view.OnShouldHide += OnShouldHidePrompt; |
| 17 | 29 | | userViewsHandler.OnAddUser += OnAddUserPressed; |
| 17 | 30 | | userViewsHandler.OnRemoveUser += OnRemoveUserPressed; |
| 17 | 31 | | } |
| | 32 | |
|
| | 33 | | public void Dispose() |
| | 34 | | { |
| 17 | 35 | | friendsHandler.OnFriendRemoved -= OnFriendRemoved; |
| 17 | 36 | | searchHandler.OnSearchChanged -= OnSearchResult; |
| 17 | 37 | | view.OnSearchText -= OnSearchText; |
| 17 | 38 | | view.OnShouldHide -= OnShouldHidePrompt; |
| 17 | 39 | | userViewsHandler.OnAddUser -= OnAddUserPressed; |
| 17 | 40 | | userViewsHandler.OnRemoveUser -= OnRemoveUserPressed; |
| | 41 | |
|
| 17 | 42 | | friendsHandler.Dispose(); |
| 17 | 43 | | userViewsHandler.Dispose(); |
| 17 | 44 | | view.Dispose(); |
| 17 | 45 | | } |
| | 46 | |
|
| | 47 | | public void Show() |
| | 48 | | { |
| 5 | 49 | | view.ClearSearch(); |
| 5 | 50 | | view.SetFriendListEmpty(userViewsHandler.userElementViewCount == 0); |
| | 51 | |
|
| 5 | 52 | | if (friendsHandler.isFriendlistDirty) |
| | 53 | | { |
| 5 | 54 | | friendsHandler.GetFriendList().Then(OnFriendList); |
| | 55 | | } |
| | 56 | |
|
| 5 | 57 | | view.Show(); |
| 5 | 58 | | } |
| | 59 | |
|
| 0 | 60 | | public void Hide() { view.Hide(); } |
| | 61 | |
|
| 20 | 62 | | public void SetUsersInRolList(List<string> usersId) { userViewsHandler.SetUsersInRolList(usersId); } |
| | 63 | |
|
| 12 | 64 | | private void OnSearchText(string searchText) { searchHandler.NotifySearchChanged(searchText); } |
| | 65 | |
|
| 0 | 66 | | private void OnShouldHidePrompt() { Hide(); } |
| | 67 | |
|
| 22 | 68 | | private void OnSearchResult(List<UserElementView> viewsList) { userViewsHandler.SetVisibleList(viewsList); } |
| | 69 | |
|
| 0 | 70 | | private void OnFriendRemoved(string userId) { userViewsHandler.RemoveUserView(userId); } |
| | 71 | |
|
| | 72 | | private void OnFriendList(Dictionary<string, FriendsController.UserStatus> friendsDictionary) |
| | 73 | | { |
| 5 | 74 | | List<UserProfile> profiles = new List<UserProfile>(); |
| | 75 | |
|
| 32 | 76 | | foreach (KeyValuePair<string, FriendsController.UserStatus> keyValuePair in friendsDictionary) |
| | 77 | | { |
| 11 | 78 | | if (keyValuePair.Value.friendshipStatus != FriendshipStatus.FRIEND) |
| | 79 | | continue; |
| | 80 | |
|
| 11 | 81 | | UserProfile profile = UserProfileController.userProfilesCatalog.Get(keyValuePair.Key); |
| 11 | 82 | | if (profile) |
| | 83 | | { |
| 11 | 84 | | profiles.Add(profile); |
| | 85 | | } |
| | 86 | | } |
| | 87 | |
|
| 5 | 88 | | userViewsHandler.SetUserViewsList(profiles); |
| 5 | 89 | | view.SetFriendListEmpty(userViewsHandler.userElementViewCount == 0); |
| 5 | 90 | | searchHandler.SetSearchableList(userViewsHandler.GetUserElementViews()); |
| 5 | 91 | | } |
| | 92 | |
|
| 1 | 93 | | private void OnAddUserPressed(string userId) { OnAddUser?.Invoke(userId); } |
| | 94 | |
|
| 1 | 95 | | private void OnRemoveUserPressed(string userId) { OnRemoveUser?.Invoke(userId); } |
| | 96 | | } |