< Summary

Class:DCL.Social.Friends.SentFriendRequestHUDController
Assembly:FriendRequestHUD
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/FriendRequestHUD/SentFriendRequestHUDController.cs
Covered lines:56
Uncovered lines:10
Coverable lines:66
Total lines:143
Line coverage:84.8% (56 of 66)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
SentFriendRequestHUDController(...)0%110100%
Dispose()0%110100%
ShowOrHide(...)0%220100%
Show(...)0%3.053082.35%
Cancel()0%110100%
<Cancel()0%6.136084.62%
Hide()0%110100%
OpenProfile()0%6200%

File(s)

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

#LineLine coverage
 1using Cysharp.Threading.Tasks;
 2using DCL.Tasks;
 3using SocialFeaturesAnalytics;
 4using System;
 5using System.Threading;
 6using UnityEngine;
 7
 8namespace DCL.Social.Friends
 9{
 10    public class SentFriendRequestHUDController
 11    {
 12        private const string PROCESS_REQUEST_ERROR_MESSAGE = "There was an error while trying to process your request. P
 13
 14        private readonly ISentFriendRequestHUDView view;
 15        private readonly DataStore dataStore;
 16        private readonly IUserProfileBridge userProfileBridge;
 17        private readonly IFriendsController friendsController;
 18        private readonly ISocialAnalytics socialAnalytics;
 19        private readonly StringVariable openPassportVariable;
 20
 421        private CancellationTokenSource friendRequestOperationsCancellationToken = new ();
 22        private string friendRequestId;
 23
 424        public SentFriendRequestHUDController(
 25            ISentFriendRequestHUDView view,
 26            DataStore dataStore,
 27            IUserProfileBridge userProfileBridge,
 28            IFriendsController friendsController,
 29            ISocialAnalytics socialAnalytics,
 30            StringVariable openPassportVariable)
 31        {
 432            this.view = view;
 433            this.dataStore = dataStore;
 434            this.userProfileBridge = userProfileBridge;
 435            this.friendsController = friendsController;
 436            this.socialAnalytics = socialAnalytics;
 437            this.openPassportVariable = openPassportVariable;
 38
 439            dataStore.HUDs.openSentFriendRequestDetail.OnChange += ShowOrHide;
 440            view.OnCancel += Cancel;
 441            view.OnClose += Hide;
 442            view.OnOpenProfile += OpenProfile;
 443            view.Close();
 444        }
 45
 46        public void Dispose()
 47        {
 448            friendRequestOperationsCancellationToken.SafeCancelAndDispose();
 449            dataStore.HUDs.openSentFriendRequestDetail.OnChange -= ShowOrHide;
 450            view.OnCancel -= Cancel;
 451            view.OnClose -= Hide;
 452            view.OnOpenProfile += OpenProfile;
 453            view.Dispose();
 454        }
 55
 56        private void ShowOrHide(string current, string previous)
 57        {
 458            if (string.IsNullOrEmpty(current))
 159                Hide();
 60            else
 361                Show(current);
 362        }
 63
 64        private void Show(string friendRequestId)
 65        {
 366            this.friendRequestId = friendRequestId;
 367            FriendRequest friendRequest = friendsController.GetAllocatedFriendRequest(this.friendRequestId);
 68
 369            if (friendRequest == null)
 70            {
 071                Debug.LogError($"Cannot display friend request {friendRequestId}, is not allocated");
 072                return;
 73            }
 74
 375            view.SetBodyMessage(friendRequest.MessageBody);
 376            view.SetTimestamp(DateTimeOffset.FromUnixTimeMilliseconds(friendRequest.Timestamp).DateTime);
 77
 378            var recipientProfile = userProfileBridge.Get(friendRequest.To);
 79
 380            if (recipientProfile != null)
 81            {
 382                view.SetRecipientName(recipientProfile.userName);
 383                view.SetRecipientProfilePicture(recipientProfile.snapshotObserver);
 84            }
 85            else
 086                Debug.LogError($"Cannot display user profile {friendRequest.To}, is not allocated");
 87
 388            var ownProfile = userProfileBridge.GetOwn();
 389            view.SetSenderProfilePicture(ownProfile.snapshotObserver);
 90
 391            view.SetSortingOrder(dataStore.HUDs.currentPassportSortingOrder.Get() + 1);
 392            view.Show();
 393        }
 94
 95        private void Cancel()
 96        {
 297            friendRequestOperationsCancellationToken = friendRequestOperationsCancellationToken.SafeRestart();
 98
 99            async UniTask CancelAsync(CancellationToken cancellationToken = default)
 100            {
 2101                view.ShowPendingToCancel();
 102
 103                try
 104                {
 2105                    FriendRequest request = await friendsController.CancelRequestAsync(friendRequestId, cancellationToke
 106
 1107                    socialAnalytics.SendFriendRequestCancelled(request.From, request.To, "modal");
 108
 1109                    view.Close();
 1110                }
 1111                catch (Exception e) when (e is not OperationCanceledException)
 112                {
 1113                    e.ReportFriendRequestErrorToAnalyticsByRequestId(friendRequestId, "modal", friendsController, social
 1114                    view.Show();
 1115                    dataStore.notifications.DefaultErrorNotification.Set(PROCESS_REQUEST_ERROR_MESSAGE, true);
 1116                    throw;
 117                }
 1118            }
 119
 2120            CancelAsync(friendRequestOperationsCancellationToken.Token).Forget();
 2121        }
 122
 123        private void Hide()
 124        {
 1125            dataStore.HUDs.openSentFriendRequestDetail.Set(null, false);
 1126            view.Close();
 1127        }
 128
 129        private void OpenProfile()
 130        {
 0131            FriendRequest friendRequest = friendsController.GetAllocatedFriendRequest(friendRequestId);
 132
 0133            if (friendRequest == null)
 134            {
 0135                Debug.LogError($"Cannot open passport {friendRequestId}, is not allocated");
 0136                return;
 137            }
 138
 0139            openPassportVariable.Set(friendRequest.To);
 0140            view.SetSortingOrder(dataStore.HUDs.currentPassportSortingOrder.Get() - 1);
 0141        }
 142    }
 143}