| | 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.RefreshFriendsTab(); |
| 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 += HandleFriendshipUpdated; |
| 32 | 73 | | friendsController.OnUpdateUserStatus += HandleUserStatusUpdated; |
| 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 -= HandleFriendshipUpdated; |
| 33 | 98 | | friendsController.OnUpdateUserStatus -= HandleUserStatusUpdated; |
| | 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 | | { |
| 106 | 121 | | foreach (var friendId in friends.Keys) |
| | 122 | | { |
| 20 | 123 | | var profile = userProfileBridge.Get(friendId); |
| 20 | 124 | | if (profile == null) continue; |
| 18 | 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.UpdateBlockStatus(friendId, model.blocked); |
| | 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 HandleUserStatusUpdated(string userId, FriendsController.UserStatus status) => |
| 8 | 212 | | UpdateUserStatus(userId, status, ShouldBeDisplayed(status)); |
| | 213 | |
|
| | 214 | | private void UpdateUserStatus(string userId, FriendsController.UserStatus status, bool shouldDisplay) |
| | 215 | | { |
| 9 | 216 | | switch (status.friendshipStatus) |
| | 217 | | { |
| | 218 | | case FriendshipStatus.FRIEND: |
| 4 | 219 | | var friend = friends.ContainsKey(userId) |
| | 220 | | ? new FriendEntryModel(friends[userId]) |
| | 221 | | : new FriendEntryModel(); |
| 4 | 222 | | friend.CopyFrom(status); |
| 4 | 223 | | friend.blocked = IsUserBlocked(userId); |
| 4 | 224 | | friends[userId] = friend; |
| 4 | 225 | | if (shouldDisplay) |
| 3 | 226 | | View.Set(userId, friend); |
| 3 | 227 | | break; |
| | 228 | | case FriendshipStatus.NOT_FRIEND: |
| 0 | 229 | | View.Remove(userId); |
| 0 | 230 | | friends.Remove(userId); |
| 0 | 231 | | break; |
| | 232 | | case FriendshipStatus.REQUESTED_TO: |
| 2 | 233 | | var sentRequest = friends.ContainsKey(userId) |
| | 234 | | ? new FriendRequestEntryModel(friends[userId], false) |
| | 235 | | : new FriendRequestEntryModel {isReceived = false}; |
| 2 | 236 | | sentRequest.CopyFrom(status); |
| 2 | 237 | | sentRequest.blocked = IsUserBlocked(userId); |
| 2 | 238 | | friends[userId] = sentRequest; |
| 2 | 239 | | if (shouldDisplay) |
| 2 | 240 | | View.Set(userId, sentRequest); |
| 2 | 241 | | break; |
| | 242 | | case FriendshipStatus.REQUESTED_FROM: |
| 3 | 243 | | var receivedRequest = friends.ContainsKey(userId) |
| | 244 | | ? new FriendRequestEntryModel(friends[userId], true) |
| | 245 | | : new FriendRequestEntryModel {isReceived = true}; |
| 3 | 246 | | receivedRequest.CopyFrom(status); |
| 3 | 247 | | receivedRequest.blocked = IsUserBlocked(userId); |
| 3 | 248 | | friends[userId] = receivedRequest; |
| 3 | 249 | | if (shouldDisplay) |
| 2 | 250 | | View.Set(userId, receivedRequest); |
| | 251 | | break; |
| | 252 | | } |
| | 253 | |
|
| 9 | 254 | | if (!shouldDisplay) |
| 2 | 255 | | EnqueueOnPendingToLoad(userId, status); |
| 9 | 256 | | } |
| | 257 | |
|
| | 258 | | private void HandleFriendshipUpdated(string userId, FriendshipAction friendshipAction) |
| | 259 | | { |
| 17 | 260 | | var userProfile = userProfileBridge.Get(userId); |
| | 261 | |
|
| 17 | 262 | | if (userProfile == null) |
| | 263 | | { |
| 0 | 264 | | Debug.LogError($"UserProfile is null for {userId}! ... friendshipAction {friendshipAction}"); |
| 0 | 265 | | return; |
| | 266 | | } |
| | 267 | |
|
| 17 | 268 | | userProfile.OnUpdate -= HandleFriendProfileUpdated; |
| | 269 | |
|
| 17 | 270 | | var shouldDisplay = ShouldBeDisplayed(userId, friendshipAction); |
| | 271 | |
|
| | 272 | | switch (friendshipAction) |
| | 273 | | { |
| | 274 | | case FriendshipAction.NONE: |
| | 275 | | case FriendshipAction.REJECTED: |
| | 276 | | case FriendshipAction.CANCELLED: |
| | 277 | | case FriendshipAction.DELETED: |
| 3 | 278 | | friends.Remove(userId); |
| 3 | 279 | | View.Remove(userId); |
| 3 | 280 | | break; |
| | 281 | | case FriendshipAction.APPROVED: |
| 7 | 282 | | var approved = friends.ContainsKey(userId) |
| | 283 | | ? new FriendEntryModel(friends[userId]) |
| | 284 | | : new FriendEntryModel(); |
| 7 | 285 | | approved.CopyFrom(userProfile); |
| 7 | 286 | | approved.blocked = IsUserBlocked(userId); |
| 7 | 287 | | friends[userId] = approved; |
| 7 | 288 | | if (shouldDisplay) |
| 5 | 289 | | View.Set(userId, approved); |
| 7 | 290 | | userProfile.OnUpdate += HandleFriendProfileUpdated; |
| 7 | 291 | | break; |
| | 292 | | case FriendshipAction.REQUESTED_FROM: |
| 4 | 293 | | var requestReceived = friends.ContainsKey(userId) |
| | 294 | | ? new FriendRequestEntryModel(friends[userId], true) |
| | 295 | | : new FriendRequestEntryModel {isReceived = true}; |
| 4 | 296 | | requestReceived.CopyFrom(userProfile); |
| 4 | 297 | | requestReceived.blocked = IsUserBlocked(userId); |
| 4 | 298 | | friends[userId] = requestReceived; |
| 4 | 299 | | if (shouldDisplay) |
| 2 | 300 | | View.Set(userId, requestReceived); |
| 4 | 301 | | userProfile.OnUpdate += HandleFriendProfileUpdated; |
| 4 | 302 | | break; |
| | 303 | | case FriendshipAction.REQUESTED_TO: |
| 3 | 304 | | var requestSent = friends.ContainsKey(userId) |
| | 305 | | ? new FriendRequestEntryModel(friends[userId], false) |
| | 306 | | : new FriendRequestEntryModel {isReceived = false}; |
| 3 | 307 | | requestSent.CopyFrom(userProfile); |
| 3 | 308 | | requestSent.blocked = IsUserBlocked(userId); |
| 3 | 309 | | friends[userId] = requestSent; |
| 3 | 310 | | if (shouldDisplay) |
| 3 | 311 | | View.Set(userId, requestSent); |
| 3 | 312 | | userProfile.OnUpdate += HandleFriendProfileUpdated; |
| | 313 | | break; |
| | 314 | | } |
| | 315 | |
|
| 17 | 316 | | if (shouldDisplay) |
| 13 | 317 | | UpdateNotificationsCounter(); |
| | 318 | | else |
| 4 | 319 | | EnqueueOnPendingToLoad(userId, friendshipAction); |
| 4 | 320 | | } |
| | 321 | |
|
| | 322 | | private void HandleFriendProfileUpdated(UserProfile profile) |
| | 323 | | { |
| 1 | 324 | | var userId = profile.userId; |
| 1 | 325 | | if (!friends.ContainsKey(userId)) return; |
| 1 | 326 | | friends[userId].CopyFrom(profile); |
| | 327 | |
|
| 1 | 328 | | var status = friendsController.GetUserStatus(profile.userId); |
| 1 | 329 | | if (status == null) return; |
| | 330 | |
|
| 1 | 331 | | UpdateUserStatus(userId, status, ShouldBeDisplayed(status)); |
| 1 | 332 | | } |
| | 333 | |
|
| | 334 | | private bool IsUserBlocked(string userId) |
| | 335 | | { |
| 23 | 336 | | if (ownUserProfile != null && ownUserProfile.blocked != null) |
| 23 | 337 | | return ownUserProfile.blocked.Contains(userId); |
| 0 | 338 | | return false; |
| | 339 | | } |
| | 340 | |
|
| | 341 | | private void EnqueueOnPendingToLoad(string userId, FriendsController.UserStatus newStatus) |
| | 342 | | { |
| 2 | 343 | | switch (newStatus.friendshipStatus) |
| | 344 | | { |
| | 345 | | case FriendshipStatus.FRIEND: |
| 1 | 346 | | pendingFriends.Enqueue(userId); |
| 1 | 347 | | View.ShowMoreFriendsToLoadHint(pendingFriends.Count); |
| 1 | 348 | | break; |
| | 349 | | case FriendshipStatus.REQUESTED_FROM: |
| 1 | 350 | | pendingRequests.Enqueue(userId); |
| 1 | 351 | | View.ShowMoreRequestsToLoadHint(pendingRequests.Count); |
| | 352 | | break; |
| | 353 | | } |
| 1 | 354 | | } |
| | 355 | |
|
| | 356 | | private void EnqueueOnPendingToLoad(string userId, FriendshipAction friendshipAction) |
| | 357 | | { |
| | 358 | | switch (friendshipAction) |
| | 359 | | { |
| | 360 | | case FriendshipAction.APPROVED: |
| 2 | 361 | | pendingFriends.Enqueue(userId); |
| 2 | 362 | | View.ShowMoreFriendsToLoadHint(pendingFriends.Count); |
| 2 | 363 | | break; |
| | 364 | | case FriendshipAction.REQUESTED_FROM: |
| 2 | 365 | | pendingRequests.Enqueue(userId); |
| 2 | 366 | | View.ShowMoreRequestsToLoadHint(pendingRequests.Count); |
| | 367 | | break; |
| | 368 | | } |
| 2 | 369 | | } |
| | 370 | |
|
| | 371 | | private bool ShouldBeDisplayed(string userId, FriendshipAction friendshipAction) |
| | 372 | | { |
| 17 | 373 | | return friendshipAction switch |
| | 374 | | { |
| | 375 | | FriendshipAction.APPROVED => View.FriendCount <= INITIAL_DISPLAYED_FRIEND_COUNT || |
| | 376 | | View.ContainsFriend(userId), |
| | 377 | | FriendshipAction.REQUESTED_FROM => View.FriendRequestCount <= INITIAL_DISPLAYED_FRIEND_COUNT || |
| | 378 | | View.ContainsFriendRequest(userId), |
| | 379 | | _ => true |
| | 380 | | }; |
| | 381 | | } |
| | 382 | |
|
| | 383 | | private bool ShouldBeDisplayed(FriendsController.UserStatus status) |
| | 384 | | { |
| 14 | 385 | | if (status.presence == PresenceStatus.ONLINE) return true; |
| | 386 | |
|
| 4 | 387 | | return status.friendshipStatus switch |
| | 388 | | { |
| | 389 | | FriendshipStatus.FRIEND => View.FriendCount < INITIAL_DISPLAYED_FRIEND_COUNT || |
| | 390 | | View.ContainsFriend(status.userId), |
| | 391 | | FriendshipStatus.REQUESTED_FROM => View.FriendRequestCount < INITIAL_DISPLAYED_FRIEND_COUNT || |
| | 392 | | View.ContainsFriendRequest(status.userId), |
| | 393 | | _ => true |
| | 394 | | }; |
| | 395 | | } |
| | 396 | |
|
| | 397 | | private void OnFriendNotFound(string name) |
| | 398 | | { |
| 1 | 399 | | View.DisplayFriendUserNotFound(); |
| 1 | 400 | | } |
| | 401 | |
|
| | 402 | | private void UpdateNotificationsCounter() |
| | 403 | | { |
| 17 | 404 | | if (View.IsActive()) |
| 5 | 405 | | dataStore.friendNotifications.seenFriends.Set(friendsController.friendCount); |
| | 406 | |
|
| 17 | 407 | | dataStore.friendNotifications.seenRequests.Set(friendsController.ReceivedRequestCount); |
| 17 | 408 | | } |
| | 409 | |
|
| 1 | 410 | | private void HandleOpenWhisperChat(FriendEntryModel entry) => OnPressWhisper?.Invoke(entry.userId); |
| | 411 | |
|
| 0 | 412 | | private void HandleUnfriend(string userId) => friendsController.RemoveFriend(userId); |
| | 413 | |
|
| | 414 | | private void HandleRequestRejected(FriendRequestEntryModel entry) |
| | 415 | | { |
| 0 | 416 | | friendsController.RejectFriendship(entry.userId); |
| | 417 | |
|
| 0 | 418 | | UpdateNotificationsCounter(); |
| | 419 | |
|
| 0 | 420 | | if (ownUserProfile != null) |
| 0 | 421 | | socialAnalytics.SendFriendRequestRejected(ownUserProfile.userId, entry.userId, |
| | 422 | | PlayerActionSource.FriendsHUD); |
| 0 | 423 | | } |
| | 424 | |
|
| | 425 | | private void HandleRequestCancelled(FriendRequestEntryModel entry) |
| | 426 | | { |
| 0 | 427 | | friendsController.CancelRequest(entry.userId); |
| | 428 | |
|
| 0 | 429 | | if (ownUserProfile != null) |
| 0 | 430 | | socialAnalytics.SendFriendRequestCancelled(ownUserProfile.userId, entry.userId, |
| | 431 | | PlayerActionSource.FriendsHUD); |
| 0 | 432 | | } |
| | 433 | |
|
| | 434 | | private void HandleRequestAccepted(FriendRequestEntryModel entry) |
| | 435 | | { |
| 0 | 436 | | friendsController.AcceptFriendship(entry.userId); |
| | 437 | |
|
| 0 | 438 | | if (ownUserProfile != null) |
| 0 | 439 | | socialAnalytics.SendFriendRequestApproved(ownUserProfile.userId, entry.userId, |
| | 440 | | PlayerActionSource.FriendsHUD); |
| 0 | 441 | | } |
| | 442 | |
|
| | 443 | | private void DisplayMoreFriends() |
| | 444 | | { |
| 0 | 445 | | for (var i = 0; i < LOAD_FRIENDS_ON_DEMAND_COUNT && pendingFriends.Count > 0; i++) |
| | 446 | | { |
| 0 | 447 | | var userId = pendingFriends.Dequeue(); |
| 0 | 448 | | var status = friendsController.GetUserStatus(userId); |
| 0 | 449 | | if (status == null) continue; |
| 0 | 450 | | UpdateUserStatus(userId, status, true); |
| | 451 | | } |
| | 452 | |
|
| 0 | 453 | | ShowOrHideMoreFriendsToLoadHint(); |
| 0 | 454 | | } |
| | 455 | |
|
| | 456 | | private void DisplayMoreFriendRequests() |
| | 457 | | { |
| 0 | 458 | | for (var i = 0; i < LOAD_FRIENDS_ON_DEMAND_COUNT && pendingRequests.Count > 0; i++) |
| | 459 | | { |
| 0 | 460 | | var userId = pendingRequests.Dequeue(); |
| 0 | 461 | | var status = friendsController.GetUserStatus(userId); |
| 0 | 462 | | if (status == null) continue; |
| 0 | 463 | | HandleUserStatusUpdated(userId, status); |
| | 464 | | } |
| | 465 | |
|
| 0 | 466 | | ShowOrHideMoreFriendRequestsToLoadHint(); |
| 0 | 467 | | } |
| | 468 | |
|
| | 469 | | private void ShowOrHideMoreFriendRequestsToLoadHint() |
| | 470 | | { |
| 32 | 471 | | if (pendingRequests.Count == 0) |
| 32 | 472 | | View.HideMoreRequestsToLoadHint(); |
| | 473 | | else |
| 0 | 474 | | View.ShowMoreRequestsToLoadHint(pendingRequests.Count); |
| 0 | 475 | | } |
| | 476 | |
|
| | 477 | | private void ShowOrHideMoreFriendsToLoadHint() |
| | 478 | | { |
| 32 | 479 | | if (pendingFriends.Count == 0) |
| 32 | 480 | | View.HideMoreFriendsToLoadHint(); |
| | 481 | | else |
| 0 | 482 | | View.ShowMoreFriendsToLoadHint(pendingFriends.Count); |
| 0 | 483 | | } |
| | 484 | |
|
| | 485 | | private void SearchFriends(string search) |
| | 486 | | { |
| 0 | 487 | | if (string.IsNullOrEmpty(search)) |
| | 488 | | { |
| 0 | 489 | | View.ClearFriendFilter(); |
| 0 | 490 | | ShowOrHideMoreFriendsToLoadHint(); |
| 0 | 491 | | return; |
| | 492 | | } |
| | 493 | |
|
| | 494 | | Dictionary<string, FriendEntryModel> FilterFriendsByUserNameAndUserId(string search) |
| | 495 | | { |
| 0 | 496 | | var regex = new Regex(search, RegexOptions.IgnoreCase); |
| | 497 | |
|
| 0 | 498 | | return friends.Values.Where(model => |
| | 499 | | { |
| 0 | 500 | | var status = friendsController.GetUserStatus(model.userId); |
| 0 | 501 | | if (status == null) return false; |
| 0 | 502 | | if (status.friendshipStatus != FriendshipStatus.FRIEND) return false; |
| 0 | 503 | | if (regex.IsMatch(model.userId)) return true; |
| 0 | 504 | | return !string.IsNullOrEmpty(model.userName) && regex.IsMatch(model.userName); |
| 0 | 505 | | }).Take(MAX_SEARCHED_FRIENDS).ToDictionary(model => model.userId, model => model); |
| | 506 | | } |
| | 507 | |
|
| | 508 | | void DisplayMissingFriends(IEnumerable<FriendEntryModel> filteredFriends) |
| | 509 | | { |
| 0 | 510 | | foreach (var model in filteredFriends) |
| | 511 | | { |
| 0 | 512 | | if (View.ContainsFriend(model.userId)) return; |
| 0 | 513 | | var status = friendsController.GetUserStatus(model.userId); |
| 0 | 514 | | if (status == null) continue; |
| 0 | 515 | | UpdateUserStatus(model.userId, status, true); |
| | 516 | | } |
| 0 | 517 | | } |
| | 518 | |
|
| 0 | 519 | | var filteredFriends = FilterFriendsByUserNameAndUserId(search); |
| 0 | 520 | | DisplayMissingFriends(filteredFriends.Values); |
| 0 | 521 | | View.FilterFriends(filteredFriends); |
| 0 | 522 | | } |
| | 523 | | } |