| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Linq; |
| | 4 | | using System.Text.RegularExpressions; |
| | 5 | | using Cysharp.Threading.Tasks; |
| | 6 | | using DCL; |
| | 7 | | using SocialFeaturesAnalytics; |
| | 8 | | using UnityEngine; |
| | 9 | |
|
| | 10 | | public class FriendsHUDController : IHUD |
| | 11 | | { |
| | 12 | | private const int INITIAL_DISPLAYED_FRIEND_COUNT = 50; |
| | 13 | | private const int LOAD_FRIENDS_ON_DEMAND_COUNT = 30; |
| | 14 | | private const int MAX_SEARCHED_FRIENDS = 100; |
| | 15 | |
|
| 32 | 16 | | private readonly Dictionary<string, FriendEntryModel> friends = new Dictionary<string, FriendEntryModel>(); |
| 32 | 17 | | private readonly Queue<string> pendingFriends = new Queue<string>(); |
| 32 | 18 | | private readonly Queue<string> pendingRequests = new Queue<string>(); |
| | 19 | | private readonly DataStore dataStore; |
| | 20 | | private readonly IFriendsController friendsController; |
| | 21 | | private readonly IUserProfileBridge userProfileBridge; |
| | 22 | | private readonly ISocialAnalytics socialAnalytics; |
| | 23 | | private readonly IChatController chatController; |
| | 24 | | private readonly ILastReadMessagesService lastReadMessagesService; |
| | 25 | |
|
| | 26 | | private UserProfile ownUserProfile; |
| | 27 | |
|
| 0 | 28 | | public IFriendsHUDComponentView View { get; private set; } |
| | 29 | |
|
| | 30 | | public event Action<string> OnPressWhisper; |
| | 31 | | public event Action OnFriendsOpened; |
| | 32 | | public event Action OnFriendsClosed; |
| | 33 | |
|
| 32 | 34 | | public FriendsHUDController(DataStore dataStore, |
| | 35 | | IFriendsController friendsController, |
| | 36 | | IUserProfileBridge userProfileBridge, |
| | 37 | | ISocialAnalytics socialAnalytics, |
| | 38 | | IChatController chatController, |
| | 39 | | ILastReadMessagesService lastReadMessagesService) |
| | 40 | | { |
| 32 | 41 | | this.dataStore = dataStore; |
| 32 | 42 | | this.friendsController = friendsController; |
| 32 | 43 | | this.userProfileBridge = userProfileBridge; |
| 32 | 44 | | this.socialAnalytics = socialAnalytics; |
| 32 | 45 | | this.chatController = chatController; |
| 32 | 46 | | this.lastReadMessagesService = lastReadMessagesService; |
| 32 | 47 | | } |
| | 48 | |
|
| | 49 | | public void Initialize(IFriendsHUDComponentView view = null) |
| | 50 | | { |
| 32 | 51 | | view ??= FriendsHUDComponentView.Create(); |
| 32 | 52 | | View = view; |
| | 53 | |
|
| 32 | 54 | | view.Initialize(chatController, lastReadMessagesService, friendsController, socialAnalytics); |
| 32 | 55 | | view.ListByOnlineStatus = dataStore.featureFlags.flags.Get().IsFeatureEnabled("friends_by_online_status"); |
| 32 | 56 | | view.OnFriendRequestApproved += HandleRequestAccepted; |
| 32 | 57 | | view.OnCancelConfirmation += HandleRequestCancelled; |
| 32 | 58 | | view.OnRejectConfirmation += HandleRequestRejected; |
| 32 | 59 | | view.OnFriendRequestSent += HandleRequestSent; |
| 32 | 60 | | view.OnWhisper += HandleOpenWhisperChat; |
| 32 | 61 | | view.OnClose += HandleViewClosed; |
| 32 | 62 | | view.OnRequireMoreFriends += DisplayMoreFriends; |
| 32 | 63 | | view.OnRequireMoreFriendRequests += DisplayMoreFriendRequests; |
| 32 | 64 | | view.OnSearchFriendsRequested += SearchFriends; |
| | 65 | |
|
| 32 | 66 | | ownUserProfile = userProfileBridge.GetOwn(); |
| 32 | 67 | | ownUserProfile.OnUpdate -= HandleProfileUpdated; |
| 32 | 68 | | ownUserProfile.OnUpdate += HandleProfileUpdated; |
| | 69 | |
|
| 32 | 70 | | if (friendsController != null) |
| | 71 | | { |
| 32 | 72 | | friendsController.OnUpdateFriendship += OnUpdateFriendship; |
| 32 | 73 | | friendsController.OnUpdateUserStatus += OnUpdateUserStatus; |
| 32 | 74 | | friendsController.OnFriendNotFound += OnFriendNotFound; |
| | 75 | |
|
| 32 | 76 | | if (friendsController.isInitialized) |
| | 77 | | { |
| 2 | 78 | | view.HideLoadingSpinner(); |
| 2 | 79 | | } |
| | 80 | | else |
| | 81 | | { |
| 30 | 82 | | view.ShowLoadingSpinner(); |
| 30 | 83 | | friendsController.OnInitialized -= HandleFriendsInitialized; |
| 30 | 84 | | friendsController.OnInitialized += HandleFriendsInitialized; |
| | 85 | | } |
| | 86 | | } |
| | 87 | |
|
| 32 | 88 | | ShowOrHideMoreFriendsToLoadHint(); |
| 32 | 89 | | ShowOrHideMoreFriendRequestsToLoadHint(); |
| 32 | 90 | | } |
| | 91 | |
|
| | 92 | | public void Dispose() |
| | 93 | | { |
| 33 | 94 | | if (friendsController != null) |
| | 95 | | { |
| 33 | 96 | | friendsController.OnInitialized -= HandleFriendsInitialized; |
| 33 | 97 | | friendsController.OnUpdateFriendship -= OnUpdateFriendship; |
| 33 | 98 | | friendsController.OnUpdateUserStatus -= OnUpdateUserStatus; |
| | 99 | | } |
| | 100 | |
|
| 33 | 101 | | if (View != null) |
| | 102 | | { |
| 33 | 103 | | View.OnFriendRequestApproved -= HandleRequestAccepted; |
| 33 | 104 | | View.OnCancelConfirmation -= HandleRequestCancelled; |
| 33 | 105 | | View.OnRejectConfirmation -= HandleRequestRejected; |
| 33 | 106 | | View.OnFriendRequestSent -= HandleRequestSent; |
| 33 | 107 | | View.OnWhisper -= HandleOpenWhisperChat; |
| 33 | 108 | | View.OnDeleteConfirmation -= HandleUnfriend; |
| 33 | 109 | | View.OnClose -= HandleViewClosed; |
| 33 | 110 | | View.OnRequireMoreFriends -= DisplayMoreFriends; |
| 33 | 111 | | View.OnRequireMoreFriendRequests -= DisplayMoreFriendRequests; |
| 33 | 112 | | View.OnSearchFriendsRequested -= SearchFriends; |
| 33 | 113 | | View.Dispose(); |
| | 114 | | } |
| | 115 | |
|
| 33 | 116 | | if (ownUserProfile != null) |
| 33 | 117 | | ownUserProfile.OnUpdate -= HandleProfileUpdated; |
| | 118 | |
|
| 33 | 119 | | if (userProfileBridge != null) |
| | 120 | | { |
| 112 | 121 | | foreach (var friendId in friends.Keys) |
| | 122 | | { |
| 23 | 123 | | var profile = userProfileBridge.Get(friendId); |
| 23 | 124 | | if (profile == null) continue; |
| 21 | 125 | | profile.OnUpdate -= HandleFriendProfileUpdated; |
| | 126 | | } |
| | 127 | | } |
| 33 | 128 | | } |
| | 129 | |
|
| | 130 | | public void SetVisibility(bool visible) |
| | 131 | | { |
| 6 | 132 | | if (visible) |
| | 133 | | { |
| 4 | 134 | | View.Show(); |
| 4 | 135 | | UpdateNotificationsCounter(); |
| 4 | 136 | | OnFriendsOpened?.Invoke(); |
| 0 | 137 | | } |
| | 138 | | else |
| | 139 | | { |
| 2 | 140 | | View.Hide(); |
| 2 | 141 | | OnFriendsClosed?.Invoke(); |
| | 142 | | } |
| 0 | 143 | | } |
| | 144 | |
|
| 0 | 145 | | private void HandleViewClosed() => SetVisibility(false); |
| | 146 | |
|
| | 147 | | private void HandleFriendsInitialized() |
| | 148 | | { |
| 0 | 149 | | friendsController.OnInitialized -= HandleFriendsInitialized; |
| 0 | 150 | | View.HideLoadingSpinner(); |
| 0 | 151 | | } |
| | 152 | |
|
| 2 | 153 | | private void HandleProfileUpdated(UserProfile profile) => UpdateBlockStatus(profile).Forget(); |
| | 154 | |
|
| | 155 | | private async UniTask UpdateBlockStatus(UserProfile profile) |
| | 156 | | { |
| | 157 | | const int iterationsPerFrame = 10; |
| | 158 | |
|
| | 159 | | //NOTE(Brian): HashSet to check Contains quicker. |
| 2 | 160 | | var allBlockedUsers = profile.blocked != null |
| | 161 | | ? new HashSet<string>(profile.blocked) |
| | 162 | | : new HashSet<string>(); |
| | 163 | |
|
| 2 | 164 | | var iterations = 0; |
| | 165 | |
|
| 8 | 166 | | foreach (var friendPair in friends) |
| | 167 | | { |
| 2 | 168 | | var friendId = friendPair.Key; |
| 2 | 169 | | var model = friendPair.Value; |
| | 170 | |
|
| 2 | 171 | | model.blocked = allBlockedUsers.Contains(friendId); |
| 2 | 172 | | await UniTask.SwitchToMainThread(); |
| 2 | 173 | | View.Populate(friendId, model); |
| | 174 | |
|
| 2 | 175 | | iterations++; |
| 2 | 176 | | if (iterations > 0 && iterations % iterationsPerFrame == 0) |
| 0 | 177 | | await UniTask.NextFrame(); |
| 2 | 178 | | } |
| 2 | 179 | | } |
| | 180 | |
|
| | 181 | | private void HandleRequestSent(string userNameOrId) |
| | 182 | | { |
| 3 | 183 | | if (AreAlreadyFriends(userNameOrId)) |
| | 184 | | { |
| 1 | 185 | | View.ShowRequestSendError(FriendRequestError.AlreadyFriends); |
| 1 | 186 | | } |
| | 187 | | else |
| | 188 | | { |
| 2 | 189 | | friendsController.RequestFriendship(userNameOrId); |
| | 190 | |
|
| 2 | 191 | | if (ownUserProfile != null) |
| 2 | 192 | | socialAnalytics.SendFriendRequestSent(ownUserProfile.userId, userNameOrId, 0, |
| | 193 | | PlayerActionSource.FriendsHUD); |
| | 194 | |
|
| 2 | 195 | | View.ShowRequestSendSuccess(); |
| | 196 | | } |
| 2 | 197 | | } |
| | 198 | |
|
| | 199 | | private bool AreAlreadyFriends(string userNameOrId) |
| | 200 | | { |
| 3 | 201 | | var userId = userNameOrId; |
| 3 | 202 | | var profile = userProfileBridge.GetByName(userNameOrId); |
| | 203 | |
|
| 3 | 204 | | if (profile != default) |
| 1 | 205 | | userId = profile.userId; |
| | 206 | |
|
| 3 | 207 | | return friendsController != null |
| | 208 | | && friendsController.ContainsStatus(userId, FriendshipStatus.FRIEND); |
| | 209 | | } |
| | 210 | |
|
| | 211 | | private void OnUpdateUserStatus(string userId, FriendsController.UserStatus newStatus) |
| | 212 | | { |
| 8 | 213 | | var shouldDisplay = ShouldBeDisplayed(newStatus); |
| 8 | 214 | | var model = GetOrCreateModel(userId, newStatus); |
| 8 | 215 | | model.CopyFrom(newStatus); |
| | 216 | |
|
| 8 | 217 | | if (shouldDisplay) |
| 6 | 218 | | View.Set(userId, newStatus.friendshipStatus, model); |
| | 219 | | else |
| 2 | 220 | | EnqueueOnPendingToLoad(userId, newStatus); |
| 2 | 221 | | } |
| | 222 | |
|
| | 223 | | private void OnUpdateFriendship(string userId, FriendshipAction friendshipAction) |
| | 224 | | { |
| 17 | 225 | | var userProfile = userProfileBridge.Get(userId); |
| | 226 | |
|
| 17 | 227 | | if (userProfile == null) |
| | 228 | | { |
| 0 | 229 | | Debug.LogError($"UserProfile is null for {userId}! ... friendshipAction {friendshipAction}"); |
| 0 | 230 | | return; |
| | 231 | | } |
| | 232 | |
|
| 17 | 233 | | userProfile.OnUpdate -= HandleFriendProfileUpdated; |
| 17 | 234 | | userProfile.OnUpdate += HandleFriendProfileUpdated; |
| | 235 | |
|
| 17 | 236 | | var shouldDisplay = ShouldBeDisplayed(userId, friendshipAction); |
| 17 | 237 | | var model = GetOrCreateModel(userId, friendshipAction); |
| 17 | 238 | | model.CopyFrom(userProfile); |
| 17 | 239 | | model.blocked = IsUserBlocked(userId); |
| | 240 | |
|
| 17 | 241 | | if (shouldDisplay) |
| | 242 | | { |
| 13 | 243 | | View.Set(userId, friendshipAction, model); |
| 13 | 244 | | UpdateNotificationsCounter(); |
| 13 | 245 | | } |
| | 246 | | else |
| 4 | 247 | | EnqueueOnPendingToLoad(userId, friendshipAction); |
| 4 | 248 | | } |
| | 249 | |
|
| | 250 | | private void HandleFriendProfileUpdated(UserProfile profile) |
| | 251 | | { |
| 1 | 252 | | var userId = profile.userId; |
| 1 | 253 | | if (!friends.ContainsKey(userId)) return; |
| | 254 | |
|
| 1 | 255 | | var model = friends[userId]; |
| 1 | 256 | | model.CopyFrom(profile); |
| 1 | 257 | | model.blocked = IsUserBlocked(userId); |
| | 258 | |
|
| 1 | 259 | | View.Populate(userId, model); |
| 1 | 260 | | } |
| | 261 | |
|
| | 262 | | private bool IsUserBlocked(string userId) |
| | 263 | | { |
| 18 | 264 | | if (ownUserProfile != null && ownUserProfile.blocked != null) |
| 18 | 265 | | return ownUserProfile.blocked.Contains(userId); |
| 0 | 266 | | return false; |
| | 267 | | } |
| | 268 | |
|
| | 269 | | private FriendEntryModel GetOrCreateModel(string userId, FriendshipAction friendshipAction) |
| | 270 | | { |
| 17 | 271 | | if (!friends.ContainsKey(userId)) |
| | 272 | | { |
| 15 | 273 | | if (friendshipAction == FriendshipAction.REQUESTED_TO |
| | 274 | | || friendshipAction == FriendshipAction.REQUESTED_FROM |
| | 275 | | || friendshipAction == FriendshipAction.CANCELLED |
| | 276 | | || friendshipAction == FriendshipAction.REJECTED) |
| | 277 | | { |
| 8 | 278 | | friends[userId] = new FriendRequestEntryModel |
| | 279 | | { |
| | 280 | | isReceived = friendshipAction == FriendshipAction.REQUESTED_FROM |
| | 281 | | }; |
| 8 | 282 | | } |
| | 283 | | else |
| 7 | 284 | | friends[userId] = new FriendEntryModel(); |
| 7 | 285 | | } |
| | 286 | | else |
| | 287 | | { |
| 2 | 288 | | if (friendshipAction == FriendshipAction.REQUESTED_TO |
| | 289 | | || friendshipAction == FriendshipAction.REQUESTED_FROM |
| | 290 | | || friendshipAction == FriendshipAction.CANCELLED |
| | 291 | | || friendshipAction == FriendshipAction.REJECTED) |
| | 292 | | { |
| 1 | 293 | | friends[userId] = new FriendRequestEntryModel(friends[userId], |
| | 294 | | friendshipAction == FriendshipAction.REQUESTED_FROM); |
| 1 | 295 | | } |
| | 296 | | else |
| 1 | 297 | | friends[userId] = new FriendEntryModel(friends[userId]); |
| | 298 | | } |
| | 299 | |
|
| 17 | 300 | | return friends[userId]; |
| | 301 | | } |
| | 302 | |
|
| | 303 | | private FriendEntryModel GetOrCreateModel(string userId, FriendsController.UserStatus newStatus) |
| | 304 | | { |
| 8 | 305 | | if (!friends.ContainsKey(userId)) |
| | 306 | | { |
| 8 | 307 | | if (newStatus.friendshipStatus == FriendshipStatus.REQUESTED_TO |
| | 308 | | || newStatus.friendshipStatus == FriendshipStatus.REQUESTED_FROM) |
| | 309 | | { |
| 5 | 310 | | friends[userId] = new FriendRequestEntryModel |
| | 311 | | { |
| | 312 | | isReceived = newStatus.friendshipStatus == FriendshipStatus.REQUESTED_FROM |
| | 313 | | }; |
| 5 | 314 | | } |
| | 315 | | else |
| 3 | 316 | | friends[userId] = new FriendEntryModel(); |
| 3 | 317 | | } |
| | 318 | | else |
| | 319 | | { |
| 0 | 320 | | if (newStatus.friendshipStatus == FriendshipStatus.REQUESTED_TO |
| | 321 | | || newStatus.friendshipStatus == FriendshipStatus.REQUESTED_FROM) |
| 0 | 322 | | friends[userId] = new FriendRequestEntryModel(friends[userId], |
| | 323 | | newStatus.friendshipStatus == FriendshipStatus.REQUESTED_FROM); |
| | 324 | | else |
| 0 | 325 | | friends[userId] = new FriendEntryModel(friends[userId]); |
| | 326 | | } |
| | 327 | |
|
| 8 | 328 | | return friends[userId]; |
| | 329 | | } |
| | 330 | |
|
| | 331 | | private void EnqueueOnPendingToLoad(string userId, FriendsController.UserStatus newStatus) |
| | 332 | | { |
| 2 | 333 | | switch (newStatus.friendshipStatus) |
| | 334 | | { |
| | 335 | | case FriendshipStatus.FRIEND: |
| 1 | 336 | | pendingFriends.Enqueue(userId); |
| 1 | 337 | | View.ShowMoreFriendsToLoadHint(pendingFriends.Count); |
| 1 | 338 | | break; |
| | 339 | | case FriendshipStatus.REQUESTED_FROM: |
| 1 | 340 | | pendingRequests.Enqueue(userId); |
| 1 | 341 | | View.ShowMoreRequestsToLoadHint(pendingRequests.Count); |
| | 342 | | break; |
| | 343 | | } |
| 1 | 344 | | } |
| | 345 | |
|
| | 346 | | private void EnqueueOnPendingToLoad(string userId, FriendshipAction friendshipAction) |
| | 347 | | { |
| | 348 | | switch (friendshipAction) |
| | 349 | | { |
| | 350 | | case FriendshipAction.APPROVED: |
| 2 | 351 | | pendingFriends.Enqueue(userId); |
| 2 | 352 | | View.ShowMoreFriendsToLoadHint(pendingFriends.Count); |
| 2 | 353 | | break; |
| | 354 | | case FriendshipAction.REQUESTED_FROM: |
| 2 | 355 | | pendingRequests.Enqueue(userId); |
| 2 | 356 | | View.ShowMoreRequestsToLoadHint(pendingRequests.Count); |
| | 357 | | break; |
| | 358 | | } |
| 2 | 359 | | } |
| | 360 | |
|
| | 361 | | private bool ShouldBeDisplayed(string userId, FriendshipAction friendshipAction) |
| | 362 | | { |
| 17 | 363 | | return friendshipAction switch |
| | 364 | | { |
| | 365 | | FriendshipAction.APPROVED => View.FriendCount <= INITIAL_DISPLAYED_FRIEND_COUNT || View.ContainsFriend(userI |
| | 366 | | FriendshipAction.REQUESTED_FROM => View.FriendRequestCount <= INITIAL_DISPLAYED_FRIEND_COUNT || View.Contain |
| | 367 | | _ => true |
| | 368 | | }; |
| | 369 | | } |
| | 370 | |
|
| | 371 | | private bool ShouldBeDisplayed(FriendsController.UserStatus status) |
| | 372 | | { |
| 13 | 373 | | if (status.presence == PresenceStatus.ONLINE) return true; |
| | 374 | |
|
| 3 | 375 | | return status.friendshipStatus switch |
| | 376 | | { |
| | 377 | | FriendshipStatus.FRIEND => View.FriendCount < INITIAL_DISPLAYED_FRIEND_COUNT || View.ContainsFriend(status.u |
| | 378 | | FriendshipStatus.REQUESTED_FROM => View.FriendRequestCount < INITIAL_DISPLAYED_FRIEND_COUNT || View.Contains |
| | 379 | | _ => true |
| | 380 | | }; |
| | 381 | | } |
| | 382 | |
|
| | 383 | | private void OnFriendNotFound(string name) |
| | 384 | | { |
| 1 | 385 | | View.DisplayFriendUserNotFound(); |
| 1 | 386 | | } |
| | 387 | |
|
| | 388 | | private void UpdateNotificationsCounter() |
| | 389 | | { |
| 17 | 390 | | if (View.IsActive()) |
| 5 | 391 | | dataStore.friendNotifications.seenFriends.Set(friendsController.friendCount); |
| | 392 | |
|
| 17 | 393 | | dataStore.friendNotifications.seenRequests.Set(friendsController.ReceivedRequestCount); |
| 17 | 394 | | } |
| | 395 | |
|
| 1 | 396 | | private void HandleOpenWhisperChat(FriendEntryModel entry) => OnPressWhisper?.Invoke(entry.userId); |
| | 397 | |
|
| 0 | 398 | | private void HandleUnfriend(string userId) => friendsController.RemoveFriend(userId); |
| | 399 | |
|
| | 400 | | private void HandleRequestRejected(FriendRequestEntryModel entry) |
| | 401 | | { |
| 0 | 402 | | friendsController.RejectFriendship(entry.userId); |
| | 403 | |
|
| 0 | 404 | | UpdateNotificationsCounter(); |
| | 405 | |
|
| 0 | 406 | | if (ownUserProfile != null) |
| 0 | 407 | | socialAnalytics.SendFriendRequestRejected(ownUserProfile.userId, entry.userId, |
| | 408 | | PlayerActionSource.FriendsHUD); |
| 0 | 409 | | } |
| | 410 | |
|
| | 411 | | private void HandleRequestCancelled(FriendRequestEntryModel entry) |
| | 412 | | { |
| 0 | 413 | | friendsController.CancelRequest(entry.userId); |
| | 414 | |
|
| 0 | 415 | | if (ownUserProfile != null) |
| 0 | 416 | | socialAnalytics.SendFriendRequestCancelled(ownUserProfile.userId, entry.userId, |
| | 417 | | PlayerActionSource.FriendsHUD); |
| 0 | 418 | | } |
| | 419 | |
|
| | 420 | | private void HandleRequestAccepted(FriendRequestEntryModel entry) |
| | 421 | | { |
| 0 | 422 | | friendsController.AcceptFriendship(entry.userId); |
| | 423 | |
|
| 0 | 424 | | if (ownUserProfile != null) |
| 0 | 425 | | socialAnalytics.SendFriendRequestApproved(ownUserProfile.userId, entry.userId, |
| | 426 | | PlayerActionSource.FriendsHUD); |
| 0 | 427 | | } |
| | 428 | |
|
| | 429 | | private void DisplayMoreFriends() |
| | 430 | | { |
| 0 | 431 | | for (var i = 0; i < LOAD_FRIENDS_ON_DEMAND_COUNT && pendingFriends.Count > 0; i++) |
| | 432 | | { |
| 0 | 433 | | var userId = pendingFriends.Dequeue(); |
| 0 | 434 | | if (!friends.ContainsKey(userId)) continue; |
| 0 | 435 | | var model = friends[userId]; |
| 0 | 436 | | var status = friendsController.GetUserStatus(userId); |
| 0 | 437 | | if (status == null) continue; |
| 0 | 438 | | View.Set(userId, status.friendshipStatus, model); |
| | 439 | | } |
| | 440 | |
|
| 0 | 441 | | ShowOrHideMoreFriendsToLoadHint(); |
| 0 | 442 | | } |
| | 443 | |
|
| | 444 | | private void DisplayMoreFriendRequests() |
| | 445 | | { |
| 0 | 446 | | for (var i = 0; i < LOAD_FRIENDS_ON_DEMAND_COUNT && pendingRequests.Count > 0; i++) |
| | 447 | | { |
| 0 | 448 | | var userId = pendingRequests.Dequeue(); |
| 0 | 449 | | if (!friends.ContainsKey(userId)) continue; |
| 0 | 450 | | var model = friends[userId]; |
| 0 | 451 | | var status = friendsController.GetUserStatus(userId); |
| 0 | 452 | | if (status == null) continue; |
| 0 | 453 | | View.Set(userId, status.friendshipStatus, model); |
| | 454 | | } |
| | 455 | |
|
| 0 | 456 | | ShowOrHideMoreFriendRequestsToLoadHint(); |
| 0 | 457 | | } |
| | 458 | |
|
| | 459 | | private void ShowOrHideMoreFriendRequestsToLoadHint() |
| | 460 | | { |
| 32 | 461 | | if (pendingRequests.Count == 0) |
| 32 | 462 | | View.HideMoreRequestsToLoadHint(); |
| | 463 | | else |
| 0 | 464 | | View.ShowMoreRequestsToLoadHint(pendingRequests.Count); |
| 0 | 465 | | } |
| | 466 | |
|
| | 467 | | private void ShowOrHideMoreFriendsToLoadHint() |
| | 468 | | { |
| 32 | 469 | | if (pendingFriends.Count == 0) |
| 32 | 470 | | View.HideMoreFriendsToLoadHint(); |
| | 471 | | else |
| 0 | 472 | | View.ShowMoreFriendsToLoadHint(pendingFriends.Count); |
| 0 | 473 | | } |
| | 474 | |
|
| | 475 | | private void SearchFriends(string search) |
| | 476 | | { |
| 0 | 477 | | if (string.IsNullOrEmpty(search)) |
| | 478 | | { |
| 0 | 479 | | View.ClearFriendFilter(); |
| 0 | 480 | | ShowOrHideMoreFriendsToLoadHint(); |
| 0 | 481 | | return; |
| | 482 | | } |
| | 483 | |
|
| | 484 | | Dictionary<string, FriendEntryModel> FilterFriendsByUserNameAndUserId(string search) |
| | 485 | | { |
| 0 | 486 | | var regex = new Regex(search, RegexOptions.IgnoreCase); |
| | 487 | |
|
| 0 | 488 | | return friends.Values.Where(model => |
| | 489 | | { |
| 0 | 490 | | var status = friendsController.GetUserStatus(model.userId); |
| 0 | 491 | | if (status == null) return false; |
| 0 | 492 | | if (status.friendshipStatus != FriendshipStatus.FRIEND) return false; |
| 0 | 493 | | if (regex.IsMatch(model.userId)) return true; |
| 0 | 494 | | return !string.IsNullOrEmpty(model.userName) && regex.IsMatch(model.userName); |
| 0 | 495 | | }).Take(MAX_SEARCHED_FRIENDS).ToDictionary(model => model.userId, model => model); |
| | 496 | | } |
| | 497 | |
|
| | 498 | | void DisplayMissingFriends(IEnumerable<FriendEntryModel> filteredFriends) |
| | 499 | | { |
| 0 | 500 | | foreach (var model in filteredFriends) |
| | 501 | | { |
| 0 | 502 | | if (View.ContainsFriend(model.userId)) return; |
| 0 | 503 | | var status = friendsController.GetUserStatus(model.userId); |
| 0 | 504 | | if (status == null) continue; |
| 0 | 505 | | View.Set(model.userId, FriendshipStatus.FRIEND, model); |
| | 506 | | } |
| 0 | 507 | | } |
| | 508 | |
|
| 0 | 509 | | var filteredFriends = FilterFriendsByUserNameAndUserId(search); |
| 0 | 510 | | DisplayMissingFriends(filteredFriends.Values); |
| 0 | 511 | | View.FilterFriends(filteredFriends); |
| 0 | 512 | | } |
| | 513 | | } |