| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using DCL.Tasks; |
| | 3 | | using SocialFeaturesAnalytics; |
| | 4 | | using System; |
| | 5 | | using System.Threading; |
| | 6 | |
|
| | 7 | | namespace DCL.Social.Friends |
| | 8 | | { |
| | 9 | | public class SendFriendRequestHUDController |
| | 10 | | { |
| | 11 | | private const string PROCESS_REQUEST_ERROR_MESSAGE = "There was an error while trying to process your request. P |
| | 12 | |
|
| | 13 | | private readonly ISendFriendRequestHUDView view; |
| | 14 | | private readonly FriendRequestHUDController friendRequestHUDController; |
| | 15 | | private readonly DataStore dataStore; |
| | 16 | | private readonly IUserProfileBridge userProfileBridge; |
| | 17 | | private readonly IFriendsController friendsController; |
| | 18 | | private readonly ISocialAnalytics socialAnalytics; |
| | 19 | |
|
| | 20 | | private string messageBody; |
| | 21 | | private string recipientId; |
| 7 | 22 | | private CancellationTokenSource friendOperationsCancellationToken = new (); |
| | 23 | |
|
| 7 | 24 | | public SendFriendRequestHUDController( |
| | 25 | | ISendFriendRequestHUDView view, |
| | 26 | | FriendRequestHUDController friendRequestHUDController, |
| | 27 | | DataStore dataStore, |
| | 28 | | IUserProfileBridge userProfileBridge, |
| | 29 | | IFriendsController friendsController, |
| | 30 | | ISocialAnalytics socialAnalytics) |
| | 31 | | { |
| 7 | 32 | | this.view = view; |
| 7 | 33 | | this.friendRequestHUDController = friendRequestHUDController; |
| 7 | 34 | | this.dataStore = dataStore; |
| 7 | 35 | | this.userProfileBridge = userProfileBridge; |
| 7 | 36 | | this.friendsController = friendsController; |
| 7 | 37 | | this.socialAnalytics = socialAnalytics; |
| | 38 | |
|
| 7 | 39 | | dataStore.HUDs.sendFriendRequest.OnChange += ShowOrHide; |
| | 40 | |
|
| 7 | 41 | | view.OnMessageBodyChanged += OnMessageBodyChanged; |
| 7 | 42 | | view.OnSend += Send; |
| 7 | 43 | | view.OnCancel += Hide; |
| | 44 | |
|
| 7 | 45 | | view.Close(); |
| 7 | 46 | | } |
| | 47 | |
|
| | 48 | | public void Dispose() |
| | 49 | | { |
| 7 | 50 | | friendOperationsCancellationToken.SafeCancelAndDispose(); |
| 7 | 51 | | dataStore.HUDs.sendFriendRequest.OnChange -= ShowOrHide; |
| 7 | 52 | | view.OnMessageBodyChanged -= OnMessageBodyChanged; |
| 7 | 53 | | view.OnSend -= Send; |
| 7 | 54 | | view.OnCancel -= Hide; |
| 7 | 55 | | view.Dispose(); |
| 7 | 56 | | friendRequestHUDController.Dispose(); |
| 7 | 57 | | } |
| | 58 | |
|
| | 59 | | private void ShowOrHide(string current, string previous) |
| | 60 | | { |
| 7 | 61 | | if (string.IsNullOrEmpty(current)) |
| 1 | 62 | | Hide(); |
| | 63 | | else |
| 6 | 64 | | Show(current); |
| 6 | 65 | | } |
| | 66 | |
|
| | 67 | | private void Show(string recipient) |
| | 68 | | { |
| 6 | 69 | | messageBody = ""; |
| 6 | 70 | | recipientId = recipient; |
| 6 | 71 | | var userProfile = userProfileBridge.Get(recipient); |
| 6 | 72 | | if (userProfile == null) return; |
| 6 | 73 | | view.SetName(userProfile.userName); |
| | 74 | |
|
| | 75 | | // must send the snapshot observer, otherwise the faceUrl is invalid and the texture never loads |
| 6 | 76 | | view.SetProfilePicture(userProfile.snapshotObserver); |
| 6 | 77 | | view.ClearInputField(); |
| 6 | 78 | | view.Show(); |
| 6 | 79 | | } |
| | 80 | |
|
| | 81 | | private void Send() |
| | 82 | | { |
| 5 | 83 | | friendOperationsCancellationToken = friendOperationsCancellationToken.SafeRestart(); |
| | 84 | |
|
| | 85 | | async UniTaskVoid SendAsync(CancellationToken cancellationToken) |
| | 86 | | { |
| 5 | 87 | | view.ShowPendingToSend(); |
| | 88 | |
|
| | 89 | | try |
| | 90 | | { |
| 5 | 91 | | await friendsController.RequestFriendshipAsync(recipientId, messageBody, cancellationToken); |
| | 92 | |
|
| 4 | 93 | | socialAnalytics.SendFriendRequestSent(userProfileBridge.GetOwn().userId, |
| | 94 | | recipientId, messageBody.Length, |
| | 95 | | (PlayerActionSource)dataStore.HUDs.sendFriendRequestSource.Get()); |
| | 96 | |
|
| 4 | 97 | | view.ShowSendSuccess(); |
| | 98 | |
|
| 12 | 99 | | await friendRequestHUDController.HideWithDelay(cancellationToken: cancellationToken); |
| 1 | 100 | | } |
| 4 | 101 | | catch (Exception e) when (e is not OperationCanceledException) |
| | 102 | | { |
| 1 | 103 | | e.ReportFriendRequestErrorToAnalyticsAsSender(recipientId, dataStore.HUDs.sendFriendRequestSource.Ge |
| | 104 | | userProfileBridge, socialAnalytics); |
| | 105 | |
|
| 1 | 106 | | view.Show(); |
| 1 | 107 | | dataStore.notifications.DefaultErrorNotification.Set(PROCESS_REQUEST_ERROR_MESSAGE, true); |
| 1 | 108 | | throw; |
| | 109 | | } |
| 1 | 110 | | } |
| | 111 | |
|
| 5 | 112 | | SendAsync(friendOperationsCancellationToken.Token) |
| | 113 | | .Forget(); |
| 5 | 114 | | } |
| | 115 | |
|
| | 116 | | private void Hide() |
| | 117 | | { |
| 1 | 118 | | dataStore.HUDs.sendFriendRequest.Set(null, false); |
| 1 | 119 | | friendRequestHUDController.Hide(); |
| 1 | 120 | | } |
| | 121 | |
|
| | 122 | | private void OnMessageBodyChanged(string body) => |
| 4 | 123 | | messageBody = body; |
| | 124 | | } |
| | 125 | | } |