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