| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using DCL.Tasks; |
| | 3 | | using SocialFeaturesAnalytics; |
| | 4 | | using System; |
| | 5 | | using System.Threading; |
| | 6 | | using UnityEngine; |
| | 7 | |
|
| | 8 | | namespace DCL.Social.Friends |
| | 9 | | { |
| | 10 | | public class SentFriendRequestHUDController |
| | 11 | | { |
| | 12 | | private const string PROCESS_REQUEST_ERROR_MESSAGE = "There was an error while trying to process your request. P |
| | 13 | | private const string OPEN_PASSPORT_SOURCE = "FriendRequest"; |
| | 14 | |
|
| | 15 | | private readonly ISentFriendRequestHUDView view; |
| | 16 | | private readonly DataStore dataStore; |
| | 17 | | private readonly IUserProfileBridge userProfileBridge; |
| | 18 | | private readonly IFriendsController friendsController; |
| | 19 | | private readonly ISocialAnalytics socialAnalytics; |
| | 20 | | private readonly BaseVariable<(string playerId, string source)> openPassportVariable; |
| | 21 | |
|
| 0 | 22 | | private CancellationTokenSource friendRequestOperationsCancellationToken = new (); |
| | 23 | | private string friendRequestId; |
| | 24 | |
|
| 0 | 25 | | public SentFriendRequestHUDController( |
| | 26 | | ISentFriendRequestHUDView view, |
| | 27 | | DataStore dataStore, |
| | 28 | | IUserProfileBridge userProfileBridge, |
| | 29 | | IFriendsController friendsController, |
| | 30 | | ISocialAnalytics socialAnalytics) |
| | 31 | | { |
| 0 | 32 | | this.view = view; |
| 0 | 33 | | this.dataStore = dataStore; |
| 0 | 34 | | this.userProfileBridge = userProfileBridge; |
| 0 | 35 | | this.friendsController = friendsController; |
| 0 | 36 | | this.socialAnalytics = socialAnalytics; |
| 0 | 37 | | this.openPassportVariable = dataStore.HUDs.currentPlayerId; |
| | 38 | |
|
| 0 | 39 | | dataStore.HUDs.openSentFriendRequestDetail.OnChange += ShowOrHide; |
| 0 | 40 | | view.OnCancel += Cancel; |
| 0 | 41 | | view.OnClose += Hide; |
| 0 | 42 | | view.OnOpenProfile += OpenProfile; |
| 0 | 43 | | view.Close(); |
| 0 | 44 | | } |
| | 45 | |
|
| | 46 | | public void Dispose() |
| | 47 | | { |
| 0 | 48 | | friendRequestOperationsCancellationToken.SafeCancelAndDispose(); |
| 0 | 49 | | dataStore.HUDs.openSentFriendRequestDetail.OnChange -= ShowOrHide; |
| 0 | 50 | | view.OnCancel -= Cancel; |
| 0 | 51 | | view.OnClose -= Hide; |
| 0 | 52 | | view.OnOpenProfile += OpenProfile; |
| 0 | 53 | | view.Dispose(); |
| 0 | 54 | | } |
| | 55 | |
|
| | 56 | | private void ShowOrHide(string current, string previous) |
| | 57 | | { |
| 0 | 58 | | if (string.IsNullOrEmpty(current)) |
| 0 | 59 | | Hide(); |
| | 60 | | else |
| 0 | 61 | | Show(current); |
| 0 | 62 | | } |
| | 63 | |
|
| | 64 | | private void Show(string friendRequestId) |
| | 65 | | { |
| 0 | 66 | | this.friendRequestId = friendRequestId; |
| 0 | 67 | | bool wasFound = friendsController.TryGetAllocatedFriendRequest(friendRequestId, out FriendRequest friendRequ |
| | 68 | |
|
| 0 | 69 | | if (!wasFound) |
| | 70 | | { |
| 0 | 71 | | Debug.LogError($"Cannot display friend request {friendRequestId}, is not allocated"); |
| 0 | 72 | | return; |
| | 73 | | } |
| | 74 | |
|
| 0 | 75 | | view.SetBodyMessage(friendRequest.MessageBody); |
| 0 | 76 | | view.SetTimestamp(friendRequest.Timestamp); |
| | 77 | |
|
| 0 | 78 | | var recipientProfile = userProfileBridge.Get(friendRequest.To); |
| | 79 | |
|
| 0 | 80 | | if (recipientProfile != null) |
| | 81 | | { |
| 0 | 82 | | view.SetRecipientName(recipientProfile.userName); |
| 0 | 83 | | view.SetRecipientProfilePicture(recipientProfile.snapshotObserver); |
| | 84 | | } |
| | 85 | | else |
| 0 | 86 | | Debug.LogError($"Cannot display user profile {friendRequest.To}, is not allocated"); |
| | 87 | |
|
| 0 | 88 | | var ownProfile = userProfileBridge.GetOwn(); |
| 0 | 89 | | view.SetSenderProfilePicture(ownProfile.snapshotObserver); |
| | 90 | |
|
| 0 | 91 | | view.SetSortingOrder(dataStore.HUDs.currentPassportSortingOrder.Get() + 1); |
| 0 | 92 | | view.Show(); |
| 0 | 93 | | } |
| | 94 | |
|
| | 95 | | private void Cancel() |
| | 96 | | { |
| 0 | 97 | | friendRequestOperationsCancellationToken = friendRequestOperationsCancellationToken.SafeRestart(); |
| | 98 | |
|
| | 99 | | async UniTask CancelAsync(CancellationToken cancellationToken = default) |
| | 100 | | { |
| 0 | 101 | | view.ShowPendingToCancel(); |
| | 102 | |
|
| | 103 | | try |
| | 104 | | { |
| 0 | 105 | | FriendRequest request = await friendsController.CancelRequestAsync(friendRequestId, cancellationToke |
| | 106 | |
|
| 0 | 107 | | socialAnalytics.SendFriendRequestCancelled(request.From, request.To, "modal", request.FriendRequestI |
| | 108 | |
|
| 0 | 109 | | view.Close(); |
| 0 | 110 | | } |
| 0 | 111 | | catch (Exception e) when (e is not OperationCanceledException) |
| | 112 | | { |
| 0 | 113 | | e.ReportFriendRequestErrorToAnalyticsByRequestId(friendRequestId, "modal", friendsController, social |
| 0 | 114 | | view.Show(); |
| 0 | 115 | | dataStore.notifications.DefaultErrorNotification.Set(PROCESS_REQUEST_ERROR_MESSAGE, true); |
| 0 | 116 | | throw; |
| | 117 | | } |
| 0 | 118 | | } |
| | 119 | |
|
| 0 | 120 | | CancelAsync(friendRequestOperationsCancellationToken.Token).Forget(); |
| 0 | 121 | | } |
| | 122 | |
|
| | 123 | | private void Hide() |
| | 124 | | { |
| 0 | 125 | | dataStore.HUDs.openSentFriendRequestDetail.Set(null, false); |
| 0 | 126 | | view.Close(); |
| 0 | 127 | | } |
| | 128 | |
|
| | 129 | | private void OpenProfile() |
| | 130 | | { |
| 0 | 131 | | bool wasFound = friendsController.TryGetAllocatedFriendRequest(friendRequestId, out FriendRequest friendRequ |
| | 132 | |
|
| 0 | 133 | | if (!wasFound) |
| | 134 | | { |
| 0 | 135 | | Debug.LogError($"Cannot open passport {friendRequestId}, is not allocated"); |
| 0 | 136 | | return; |
| | 137 | | } |
| | 138 | |
|
| 0 | 139 | | openPassportVariable.Set((friendRequest.To, OPEN_PASSPORT_SOURCE)); |
| 0 | 140 | | view.SetSortingOrder(dataStore.HUDs.currentPassportSortingOrder.Get() - 1); |
| 0 | 141 | | } |
| | 142 | | } |
| | 143 | | } |