< 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:0
Uncovered lines:65
Coverable lines:65
Total lines:142
Line coverage:0% (0 of 65)
Covered branches:0
Total branches:0
Covered methods:0
Total methods:9
Method coverage:0% (0 of 9)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
SendFriendRequestHUDController(...)0%2100%
Dispose()0%2100%
ShowOrHide(...)0%6200%
<Show()0%56700%
Show(...)0%2100%
Send()0%2100%
<Send()0%72800%
Hide()0%2100%
OnMessageBodyChanged(...)0%2100%

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;
 022        private CancellationTokenSource friendOperationsCancellationToken = new ();
 023        private CancellationTokenSource showCancellationToken = new ();
 24
 025        public SendFriendRequestHUDController(
 26            ISendFriendRequestHUDView view,
 27            FriendRequestHUDController friendRequestHUDController,
 28            DataStore dataStore,
 29            IUserProfileBridge userProfileBridge,
 30            IFriendsController friendsController,
 31            ISocialAnalytics socialAnalytics)
 32        {
 033            this.view = view;
 034            this.friendRequestHUDController = friendRequestHUDController;
 035            this.dataStore = dataStore;
 036            this.userProfileBridge = userProfileBridge;
 037            this.friendsController = friendsController;
 038            this.socialAnalytics = socialAnalytics;
 39
 040            dataStore.HUDs.sendFriendRequest.OnChange += ShowOrHide;
 41
 042            view.OnMessageBodyChanged += OnMessageBodyChanged;
 043            view.OnSend += Send;
 044            view.OnCancel += Hide;
 45
 046            view.Close();
 047        }
 48
 49        public void Dispose()
 50        {
 051            friendOperationsCancellationToken.SafeCancelAndDispose();
 052            showCancellationToken.SafeCancelAndDispose();
 053            dataStore.HUDs.sendFriendRequest.OnChange -= ShowOrHide;
 054            view.OnMessageBodyChanged -= OnMessageBodyChanged;
 055            view.OnSend -= Send;
 056            view.OnCancel -= Hide;
 057            view.Dispose();
 058            friendRequestHUDController.Dispose();
 059        }
 60
 61        private void ShowOrHide(string current, string previous)
 62        {
 063            if (string.IsNullOrEmpty(current))
 064                Hide();
 65            else
 066                Show(current);
 067        }
 68
 69        private void Show(string recipient)
 70        {
 71            async UniTaskVoid ShowAsync(string recipient, CancellationToken cancellationToken)
 72            {
 073                messageBody = "";
 074                recipientId = recipient;
 075                var userProfile = userProfileBridge.Get(recipient);
 76
 077                try { userProfile ??= await userProfileBridge.RequestFullUserProfileAsync(recipient, cancellationToken);
 078                catch (Exception e) when (e is not OperationCanceledException)
 79                {
 080                    view.SetName(recipient);
 081                    view.ClearInputField();
 082                    view.Show();
 083                    throw;
 84                }
 85
 086                view.SetName(userProfile.userName);
 87                // must send the snapshot observer, otherwise the faceUrl is invalid and the texture never loads
 088                view.SetProfilePicture(userProfile.snapshotObserver);
 089                view.ClearInputField();
 090                view.Show();
 091            }
 92
 093            showCancellationToken = showCancellationToken.SafeRestart();
 094            ShowAsync(recipient, showCancellationToken.Token).Forget();
 095        }
 96
 97        private void Send()
 98        {
 099            friendOperationsCancellationToken = friendOperationsCancellationToken.SafeRestart();
 100
 101            async UniTaskVoid SendAsync(CancellationToken cancellationToken)
 102            {
 0103                view.ShowPendingToSend();
 104
 105                try
 106                {
 0107                    var friendRequest = await friendsController.RequestFriendshipAsync(recipientId, messageBody, cancell
 108
 0109                    socialAnalytics.SendFriendRequestSent(userProfileBridge.GetOwn().userId,
 110                        recipientId, messageBody.Length,
 111                        (PlayerActionSource)dataStore.HUDs.sendFriendRequestSource.Get(), friendRequest.FriendRequestId)
 112
 0113                    view.ShowSendSuccess();
 114
 0115                    await friendRequestHUDController.HideWithDelay(cancellationToken: cancellationToken);
 0116                }
 0117                catch (Exception e) when (e is not OperationCanceledException)
 118                {
 0119                    e.ReportFriendRequestErrorToAnalyticsAsSender(recipientId, dataStore.HUDs.sendFriendRequestSource.Ge
 120                        userProfileBridge, socialAnalytics);
 121
 0122                    view.Show();
 0123                    dataStore.notifications.DefaultErrorNotification.Set(PROCESS_REQUEST_ERROR_MESSAGE, true);
 0124                    throw;
 125                }
 0126            }
 127
 0128            SendAsync(friendOperationsCancellationToken.Token)
 129               .Forget();
 0130        }
 131
 132        private void Hide()
 133        {
 0134            showCancellationToken.SafeCancelAndDispose();
 0135            dataStore.HUDs.sendFriendRequest.Set(null, false);
 0136            friendRequestHUDController.Hide();
 0137        }
 138
 139        private void OnMessageBodyChanged(string body) =>
 0140            messageBody = body;
 141    }
 142}