| | 1 | | using System; |
| | 2 | |
|
| | 3 | | namespace DCL.Social.Chat |
| | 4 | | { |
| | 5 | | public class NearbyMembersHUDController : IDisposable |
| | 6 | | { |
| | 7 | | private readonly IChannelMembersComponentView view; |
| | 8 | | private readonly DataStore_Player playerDataStore; |
| | 9 | | private readonly IUserProfileBridge userProfileBridge; |
| | 10 | | private bool isVisible; |
| | 11 | | private string currentSearchText; |
| | 12 | | private readonly UserProfile ownUserProfile; |
| | 13 | |
|
| 1 | 14 | | public IChannelMembersComponentView View => view; |
| | 15 | |
|
| 25 | 16 | | public NearbyMembersHUDController( |
| | 17 | | IChannelMembersComponentView view, |
| | 18 | | DataStore_Player playerDataStore, |
| | 19 | | IUserProfileBridge userProfileBridge) |
| | 20 | | { |
| 25 | 21 | | this.view = view; |
| 25 | 22 | | this.playerDataStore = playerDataStore; |
| 25 | 23 | | this.userProfileBridge = userProfileBridge; |
| | 24 | |
|
| 25 | 25 | | ownUserProfile = userProfileBridge.GetOwn(); |
| | 26 | |
|
| 25 | 27 | | playerDataStore.otherPlayers.OnAdded += OnNearbyPlayersAdded; |
| 25 | 28 | | playerDataStore.otherPlayers.OnRemoved += OnNearbyPlayersRemoved; |
| 25 | 29 | | view.OnSearchUpdated += OnSearchPlayers; |
| 25 | 30 | | } |
| | 31 | |
|
| | 32 | | public void SetVisibility(bool visible) |
| | 33 | | { |
| 6 | 34 | | isVisible = visible; |
| | 35 | |
|
| 6 | 36 | | if (visible) |
| | 37 | | { |
| 5 | 38 | | view.Show(); |
| 5 | 39 | | view.ClearSearchInput(false); |
| 5 | 40 | | LoadCurrentNearbyPlayers(); |
| | 41 | | } |
| | 42 | | else |
| 1 | 43 | | view.Hide(); |
| 1 | 44 | | } |
| | 45 | |
|
| | 46 | | public void ClearSearch() => |
| 7 | 47 | | view.ClearSearchInput(); |
| | 48 | |
|
| | 49 | | public void Dispose() |
| | 50 | | { |
| 25 | 51 | | playerDataStore.otherPlayers.OnAdded -= OnNearbyPlayersAdded; |
| 25 | 52 | | playerDataStore.otherPlayers.OnRemoved -= OnNearbyPlayersRemoved; |
| 25 | 53 | | view.OnSearchUpdated -= OnSearchPlayers; |
| 25 | 54 | | view.Dispose(); |
| 25 | 55 | | } |
| | 56 | |
|
| | 57 | | private void LoadCurrentNearbyPlayers(string textFilter = "") |
| | 58 | | { |
| 7 | 59 | | view.ClearAllEntries(); |
| | 60 | |
|
| 16 | 61 | | foreach (var player in playerDataStore.otherPlayers.Get()) |
| | 62 | | { |
| 1 | 63 | | if (!string.IsNullOrEmpty(textFilter) && |
| | 64 | | !player.Value.name.Contains(textFilter, StringComparison.OrdinalIgnoreCase)) |
| | 65 | | continue; |
| | 66 | |
|
| 1 | 67 | | OnNearbyPlayersAdded(player.Key, player.Value); |
| | 68 | | } |
| 7 | 69 | | } |
| | 70 | |
|
| | 71 | | private void OnNearbyPlayersAdded(string userId, Player player) |
| | 72 | | { |
| 3 | 73 | | if (!isVisible) |
| 0 | 74 | | return; |
| | 75 | |
|
| 3 | 76 | | if (!string.IsNullOrEmpty(currentSearchText) && |
| | 77 | | !player.name.Contains(currentSearchText, StringComparison.OrdinalIgnoreCase)) |
| 0 | 78 | | return; |
| | 79 | |
|
| 3 | 80 | | var otherProfile = userProfileBridge.Get(userId); |
| | 81 | |
|
| 3 | 82 | | if (otherProfile == null) |
| 0 | 83 | | return; |
| | 84 | |
|
| 3 | 85 | | ChannelMemberEntryModel userToAdd = new ChannelMemberEntryModel |
| | 86 | | { |
| | 87 | | isOnline = true, |
| | 88 | | thumnailUrl = otherProfile.face256SnapshotURL, |
| | 89 | | userId = otherProfile.userId, |
| | 90 | | userName = otherProfile.userName, |
| | 91 | | isOptionsButtonHidden = otherProfile.userId == userProfileBridge.GetOwn().userId, |
| | 92 | | blocked = IsUserBlocked(otherProfile.userId) |
| | 93 | | }; |
| | 94 | |
|
| 3 | 95 | | view.Set(userToAdd); |
| 3 | 96 | | } |
| | 97 | |
|
| | 98 | | private bool IsUserBlocked(string userId) |
| | 99 | | { |
| 3 | 100 | | if (ownUserProfile != null && ownUserProfile.blocked != null) |
| 3 | 101 | | return ownUserProfile.blocked.Contains(userId); |
| | 102 | |
|
| 0 | 103 | | return false; |
| | 104 | | } |
| | 105 | |
|
| | 106 | | private void OnNearbyPlayersRemoved(string userId, Player _) |
| | 107 | | { |
| 1 | 108 | | if (!isVisible) |
| 0 | 109 | | return; |
| | 110 | |
|
| 1 | 111 | | view.Remove(userId); |
| 1 | 112 | | } |
| | 113 | |
|
| | 114 | | private void OnSearchPlayers(string searchText) |
| | 115 | | { |
| 2 | 116 | | currentSearchText = searchText; |
| 2 | 117 | | LoadCurrentNearbyPlayers(searchText); |
| 2 | 118 | | } |
| | 119 | | } |
| | 120 | | } |