| | 1 | | using System; |
| | 2 | | using Cysharp.Threading.Tasks; |
| | 3 | | using DCl.Social.Friends; |
| | 4 | | using SocialFeaturesAnalytics; |
| | 5 | | using UnityEngine; |
| | 6 | |
|
| | 7 | | namespace DCL.Social.Friends |
| | 8 | | { |
| | 9 | | public class SendFriendRequestHUDController |
| | 10 | | { |
| | 11 | | private readonly ISendFriendRequestHUDView view; |
| | 12 | | private readonly DataStore dataStore; |
| | 13 | | private readonly IUserProfileBridge userProfileBridge; |
| | 14 | | private readonly IFriendsController friendsController; |
| | 15 | | private readonly ISocialAnalytics socialAnalytics; |
| | 16 | |
|
| | 17 | | private string messageBody; |
| | 18 | | private string recipientId; |
| | 19 | |
|
| 6 | 20 | | public SendFriendRequestHUDController( |
| | 21 | | ISendFriendRequestHUDView view, |
| | 22 | | DataStore dataStore, |
| | 23 | | IUserProfileBridge userProfileBridge, |
| | 24 | | IFriendsController friendsController, |
| | 25 | | ISocialAnalytics socialAnalytics) |
| | 26 | | { |
| 6 | 27 | | this.view = view; |
| 6 | 28 | | this.dataStore = dataStore; |
| 6 | 29 | | this.userProfileBridge = userProfileBridge; |
| 6 | 30 | | this.friendsController = friendsController; |
| 6 | 31 | | this.socialAnalytics = socialAnalytics; |
| | 32 | |
|
| 6 | 33 | | dataStore.HUDs.sendFriendRequest.OnChange += ShowOrHide; |
| | 34 | |
|
| 6 | 35 | | view.OnMessageBodyChanged += OnMessageBodyChanged; |
| 6 | 36 | | view.OnSend += Send; |
| 6 | 37 | | view.OnCancel += Hide; |
| | 38 | |
|
| 6 | 39 | | view.Close(); |
| 6 | 40 | | } |
| | 41 | |
|
| | 42 | | public void Dispose() |
| | 43 | | { |
| 6 | 44 | | dataStore.HUDs.sendFriendRequest.OnChange -= ShowOrHide; |
| 6 | 45 | | view.OnMessageBodyChanged -= OnMessageBodyChanged; |
| 6 | 46 | | view.OnSend -= Send; |
| 6 | 47 | | view.OnCancel -= Hide; |
| 6 | 48 | | view.Dispose(); |
| 6 | 49 | | } |
| | 50 | |
|
| | 51 | | private void ShowOrHide(string current, string previous) |
| | 52 | | { |
| 6 | 53 | | if (string.IsNullOrEmpty(current)) |
| 1 | 54 | | Hide(); |
| | 55 | | else |
| 5 | 56 | | Show(current); |
| 5 | 57 | | } |
| | 58 | |
|
| | 59 | | private void Show(string recipient) |
| | 60 | | { |
| 5 | 61 | | messageBody = ""; |
| 5 | 62 | | recipientId = recipient; |
| 5 | 63 | | var userProfile = userProfileBridge.Get(recipient); |
| 5 | 64 | | if (userProfile == null) return; |
| 5 | 65 | | view.SetName(userProfile.userName); |
| | 66 | |
|
| | 67 | | // must send the snapshot observer, otherwise the faceUrl is invalid and the texture never loads |
| 5 | 68 | | view.SetProfilePicture(userProfile.snapshotObserver); |
| 5 | 69 | | view.ClearInputField(); |
| 5 | 70 | | view.Show(); |
| 5 | 71 | | } |
| | 72 | |
|
| | 73 | | private void Send() => |
| 4 | 74 | | SendAsync().Forget(); |
| | 75 | |
|
| | 76 | | private async UniTaskVoid SendAsync() |
| | 77 | | { |
| 4 | 78 | | view.ShowPendingToSend(); |
| | 79 | |
|
| | 80 | | try |
| | 81 | | { |
| 4 | 82 | | await friendsController.RequestFriendshipAsync(recipientId, messageBody) |
| | 83 | | .Timeout(TimeSpan.FromSeconds(10)); |
| | 84 | |
|
| 3 | 85 | | socialAnalytics.SendFriendRequestSent(userProfileBridge.GetOwn().userId, recipientId, messageBody.Length |
| | 86 | | (PlayerActionSource)dataStore.HUDs.sendFriendRequestSource.Get()); |
| | 87 | |
|
| 3 | 88 | | view.ShowSendSuccess(); |
| 3 | 89 | | } |
| | 90 | | catch (Exception e) |
| | 91 | | { |
| | 92 | | // TODO: track error to analytics |
| 1 | 93 | | Debug.LogException(e); |
| 1 | 94 | | view.ShowSendFailed(); |
| 1 | 95 | | } |
| 4 | 96 | | } |
| | 97 | |
|
| | 98 | | private void Hide() |
| | 99 | | { |
| 1 | 100 | | dataStore.HUDs.sendFriendRequest.Set(null, false); |
| 1 | 101 | | view.Close(); |
| 1 | 102 | | } |
| | 103 | |
|
| | 104 | | private void OnMessageBodyChanged(string body) => |
| 4 | 105 | | messageBody = body; |
| | 106 | | } |
| | 107 | | } |