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