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