< Summary

Class:DCL.Social.Friends.SendFriendRequestHUDController
Assembly:FriendRequestHUD
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/FriendRequestHUD/SendFriendRequestHUDController.cs
Covered lines:54
Uncovered lines:0
Coverable lines:54
Total lines:125
Line coverage:100% (54 of 54)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
SendFriendRequestHUDController(...)0%110100%
Dispose()0%110100%
ShowOrHide(...)0%220100%
Show(...)0%22090%
Send()0%110100%
<Send()0%8.128087.5%
Hide()0%110100%
OnMessageBodyChanged(...)0%110100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/FriendRequestHUD/SendFriendRequestHUDController.cs

#LineLine coverage
 1using Cysharp.Threading.Tasks;
 2using DCL.Tasks;
 3using SocialFeaturesAnalytics;
 4using System;
 5using System.Threading;
 6
 7namespace 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;
 722        private CancellationTokenSource friendOperationsCancellationToken = new ();
 23
 724        public SendFriendRequestHUDController(
 25            ISendFriendRequestHUDView view,
 26            FriendRequestHUDController friendRequestHUDController,
 27            DataStore dataStore,
 28            IUserProfileBridge userProfileBridge,
 29            IFriendsController friendsController,
 30            ISocialAnalytics socialAnalytics)
 31        {
 732            this.view = view;
 733            this.friendRequestHUDController = friendRequestHUDController;
 734            this.dataStore = dataStore;
 735            this.userProfileBridge = userProfileBridge;
 736            this.friendsController = friendsController;
 737            this.socialAnalytics = socialAnalytics;
 38
 739            dataStore.HUDs.sendFriendRequest.OnChange += ShowOrHide;
 40
 741            view.OnMessageBodyChanged += OnMessageBodyChanged;
 742            view.OnSend += Send;
 743            view.OnCancel += Hide;
 44
 745            view.Close();
 746        }
 47
 48        public void Dispose()
 49        {
 750            friendOperationsCancellationToken.SafeCancelAndDispose();
 751            dataStore.HUDs.sendFriendRequest.OnChange -= ShowOrHide;
 752            view.OnMessageBodyChanged -= OnMessageBodyChanged;
 753            view.OnSend -= Send;
 754            view.OnCancel -= Hide;
 755            view.Dispose();
 756            friendRequestHUDController.Dispose();
 757        }
 58
 59        private void ShowOrHide(string current, string previous)
 60        {
 761            if (string.IsNullOrEmpty(current))
 162                Hide();
 63            else
 664                Show(current);
 665        }
 66
 67        private void Show(string recipient)
 68        {
 669            messageBody = "";
 670            recipientId = recipient;
 671            var userProfile = userProfileBridge.Get(recipient);
 672            if (userProfile == null) return;
 673            view.SetName(userProfile.userName);
 74
 75            // must send the snapshot observer, otherwise the faceUrl is invalid and the texture never loads
 676            view.SetProfilePicture(userProfile.snapshotObserver);
 677            view.ClearInputField();
 678            view.Show();
 679        }
 80
 81        private void Send()
 82        {
 583            friendOperationsCancellationToken = friendOperationsCancellationToken.SafeRestart();
 84
 85            async UniTaskVoid SendAsync(CancellationToken cancellationToken)
 86            {
 587                view.ShowPendingToSend();
 88
 89                try
 90                {
 591                    await friendsController.RequestFriendshipAsync(recipientId, messageBody, cancellationToken);
 92
 493                    socialAnalytics.SendFriendRequestSent(userProfileBridge.GetOwn().userId,
 94                        recipientId, messageBody.Length,
 95                        (PlayerActionSource)dataStore.HUDs.sendFriendRequestSource.Get());
 96
 497                    view.ShowSendSuccess();
 98
 1299                    await friendRequestHUDController.HideWithDelay(cancellationToken: cancellationToken);
 1100                }
 4101                catch (Exception e) when (e is not OperationCanceledException)
 102                {
 1103                    e.ReportFriendRequestErrorToAnalyticsAsSender(recipientId, dataStore.HUDs.sendFriendRequestSource.Ge
 104                        userProfileBridge, socialAnalytics);
 105
 1106                    view.Show();
 1107                    dataStore.notifications.DefaultErrorNotification.Set(PROCESS_REQUEST_ERROR_MESSAGE, true);
 1108                    throw;
 109                }
 1110            }
 111
 5112            SendAsync(friendOperationsCancellationToken.Token)
 113               .Forget();
 5114        }
 115
 116        private void Hide()
 117        {
 1118            dataStore.HUDs.sendFriendRequest.Set(null, false);
 1119            friendRequestHUDController.Hide();
 1120        }
 121
 122        private void OnMessageBodyChanged(string body) =>
 4123            messageBody = body;
 124    }
 125}