| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using SocialFeaturesAnalytics; |
| | 3 | | using System; |
| | 4 | | using System.Threading; |
| | 5 | | using UnityEngine; |
| | 6 | |
|
| | 7 | | namespace DCL.Social.Friends |
| | 8 | | { |
| | 9 | | public class CancelFriendRequestHUDController |
| | 10 | | { |
| | 11 | | private readonly ICancelFriendRequestHUDView view; |
| | 12 | | private readonly DataStore dataStore; |
| | 13 | | private readonly IUserProfileBridge userProfileBridge; |
| | 14 | | private readonly IFriendsController friendsController; |
| | 15 | | private readonly ISocialAnalytics socialAnalytics; |
| | 16 | | private readonly StringVariable openPassportVariable; |
| | 17 | |
|
| 4 | 18 | | private CancellationTokenSource cancellationToken = new (); |
| | 19 | | private string friendRequestId; |
| | 20 | |
|
| 4 | 21 | | public CancelFriendRequestHUDController( |
| | 22 | | ICancelFriendRequestHUDView view, |
| | 23 | | DataStore dataStore, |
| | 24 | | IUserProfileBridge userProfileBridge, |
| | 25 | | IFriendsController friendsController, |
| | 26 | | ISocialAnalytics socialAnalytics, |
| | 27 | | StringVariable openPassportVariable) |
| | 28 | | { |
| 4 | 29 | | this.view = view; |
| 4 | 30 | | this.dataStore = dataStore; |
| 4 | 31 | | this.userProfileBridge = userProfileBridge; |
| 4 | 32 | | this.friendsController = friendsController; |
| 4 | 33 | | this.socialAnalytics = socialAnalytics; |
| 4 | 34 | | this.openPassportVariable = openPassportVariable; |
| | 35 | |
|
| 4 | 36 | | dataStore.HUDs.openSentFriendRequestDetail.OnChange += ShowOrHide; |
| 4 | 37 | | view.OnCancel += Cancel; |
| 4 | 38 | | view.OnClose += Hide; |
| 4 | 39 | | view.OnOpenProfile += OpenProfile; |
| 4 | 40 | | view.Close(); |
| 4 | 41 | | } |
| | 42 | |
|
| | 43 | | public void Dispose() |
| | 44 | | { |
| 4 | 45 | | dataStore.HUDs.openSentFriendRequestDetail.OnChange -= ShowOrHide; |
| 4 | 46 | | view.OnCancel -= Cancel; |
| 4 | 47 | | view.OnClose -= Hide; |
| 4 | 48 | | view.OnOpenProfile += OpenProfile; |
| 4 | 49 | | view.Dispose(); |
| 4 | 50 | | } |
| | 51 | |
|
| | 52 | | private void ShowOrHide(string current, string previous) |
| | 53 | | { |
| 4 | 54 | | if (string.IsNullOrEmpty(current)) |
| 1 | 55 | | Hide(); |
| | 56 | | else |
| 3 | 57 | | Show(current); |
| 3 | 58 | | } |
| | 59 | |
|
| | 60 | | private void Show(string friendRequestId) |
| | 61 | | { |
| 3 | 62 | | this.friendRequestId = friendRequestId; |
| 3 | 63 | | FriendRequest friendRequest = friendsController.GetAllocatedFriendRequest(this.friendRequestId); |
| | 64 | |
|
| 3 | 65 | | if (friendRequest == null) |
| | 66 | | { |
| 0 | 67 | | Debug.LogError($"Cannot display friend request {friendRequestId}, is not allocated"); |
| 0 | 68 | | return; |
| | 69 | | } |
| | 70 | |
|
| 3 | 71 | | view.SetBodyMessage(friendRequest.MessageBody); |
| 3 | 72 | | view.SetTimestamp(DateTimeOffset.FromUnixTimeMilliseconds(friendRequest.Timestamp).DateTime); |
| | 73 | |
|
| 3 | 74 | | var recipientProfile = userProfileBridge.Get(friendRequest.To); |
| | 75 | |
|
| 3 | 76 | | if (recipientProfile != null) |
| | 77 | | { |
| 3 | 78 | | view.SetRecipientName(recipientProfile.userName); |
| 3 | 79 | | view.SetRecipientProfilePicture(recipientProfile.snapshotObserver); |
| | 80 | | } |
| | 81 | | else |
| 0 | 82 | | Debug.LogError($"Cannot display user profile {friendRequest.To}, is not allocated"); |
| | 83 | |
|
| 3 | 84 | | var ownProfile = userProfileBridge.GetOwn(); |
| 3 | 85 | | view.SetSenderProfilePicture(ownProfile.snapshotObserver); |
| | 86 | |
|
| 3 | 87 | | view.Show(); |
| 3 | 88 | | } |
| | 89 | |
|
| | 90 | | private void Cancel() |
| | 91 | | { |
| 2 | 92 | | cancellationToken?.Cancel(); |
| 2 | 93 | | cancellationToken = new CancellationTokenSource(); |
| 2 | 94 | | CancelAsync(cancellationToken.Token).Forget(); |
| 2 | 95 | | } |
| | 96 | |
|
| | 97 | | private async UniTask CancelAsync(CancellationToken cancellationToken = default) |
| | 98 | | { |
| 2 | 99 | | view.ShowPendingToCancel(); |
| | 100 | |
|
| | 101 | | try |
| | 102 | | { |
| 2 | 103 | | await friendsController.CancelRequestAsync(friendRequestId) |
| | 104 | | .Timeout(TimeSpan.FromSeconds(10)); |
| 1 | 105 | | if (cancellationToken.IsCancellationRequested) return; |
| | 106 | |
|
| | 107 | | // TODO: send analytics |
| | 108 | |
|
| 1 | 109 | | view.Close(); |
| 1 | 110 | | } |
| 1 | 111 | | catch (Exception) |
| | 112 | | { |
| 1 | 113 | | if (cancellationToken.IsCancellationRequested) return; |
| | 114 | | // TODO: track error to analytics |
| 1 | 115 | | view.ShowCancelFailed(); |
| 1 | 116 | | throw; |
| | 117 | | } |
| 1 | 118 | | } |
| | 119 | |
|
| | 120 | | private void Hide() |
| | 121 | | { |
| 1 | 122 | | dataStore.HUDs.openSentFriendRequestDetail.Set(null, false); |
| 1 | 123 | | view.Close(); |
| 1 | 124 | | } |
| | 125 | |
|
| | 126 | | private void OpenProfile() |
| | 127 | | { |
| 0 | 128 | | FriendRequest friendRequest = friendsController.GetAllocatedFriendRequest(friendRequestId); |
| | 129 | |
|
| 0 | 130 | | if (friendRequest == null) |
| | 131 | | { |
| 0 | 132 | | Debug.LogError($"Cannot open passport {friendRequestId}, is not allocated"); |
| 0 | 133 | | return; |
| | 134 | | } |
| | 135 | |
|
| 0 | 136 | | openPassportVariable.Set(friendRequest.To); |
| 0 | 137 | | } |
| | 138 | | } |
| | 139 | | } |