< Summary

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

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
CancelFriendRequestHUDController(...)0%110100%
Dispose()0%110100%
ShowOrHide(...)0%220100%
Show(...)0%3.063081.25%
Cancel()0%220100%
CancelAsync()0%6.846071.43%
Hide()0%110100%
OpenProfile()0%6200%

File(s)

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

#LineLine coverage
 1using Cysharp.Threading.Tasks;
 2using SocialFeaturesAnalytics;
 3using System;
 4using System.Threading;
 5using UnityEngine;
 6
 7namespace 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
 418        private CancellationTokenSource cancellationToken = new ();
 19        private string friendRequestId;
 20
 421        public CancelFriendRequestHUDController(
 22            ICancelFriendRequestHUDView view,
 23            DataStore dataStore,
 24            IUserProfileBridge userProfileBridge,
 25            IFriendsController friendsController,
 26            ISocialAnalytics socialAnalytics,
 27            StringVariable openPassportVariable)
 28        {
 429            this.view = view;
 430            this.dataStore = dataStore;
 431            this.userProfileBridge = userProfileBridge;
 432            this.friendsController = friendsController;
 433            this.socialAnalytics = socialAnalytics;
 434            this.openPassportVariable = openPassportVariable;
 35
 436            dataStore.HUDs.openSentFriendRequestDetail.OnChange += ShowOrHide;
 437            view.OnCancel += Cancel;
 438            view.OnClose += Hide;
 439            view.OnOpenProfile += OpenProfile;
 440            view.Close();
 441        }
 42
 43        public void Dispose()
 44        {
 445            dataStore.HUDs.openSentFriendRequestDetail.OnChange -= ShowOrHide;
 446            view.OnCancel -= Cancel;
 447            view.OnClose -= Hide;
 448            view.OnOpenProfile += OpenProfile;
 449            view.Dispose();
 450        }
 51
 52        private void ShowOrHide(string current, string previous)
 53        {
 454            if (string.IsNullOrEmpty(current))
 155                Hide();
 56            else
 357                Show(current);
 358        }
 59
 60        private void Show(string friendRequestId)
 61        {
 362            this.friendRequestId = friendRequestId;
 363            FriendRequest friendRequest = friendsController.GetAllocatedFriendRequest(this.friendRequestId);
 64
 365            if (friendRequest == null)
 66            {
 067                Debug.LogError($"Cannot display friend request {friendRequestId}, is not allocated");
 068                return;
 69            }
 70
 371            view.SetBodyMessage(friendRequest.MessageBody);
 372            view.SetTimestamp(DateTimeOffset.FromUnixTimeMilliseconds(friendRequest.Timestamp).DateTime);
 73
 374            var recipientProfile = userProfileBridge.Get(friendRequest.To);
 75
 376            if (recipientProfile != null)
 77            {
 378                view.SetRecipientName(recipientProfile.userName);
 379                view.SetRecipientProfilePicture(recipientProfile.snapshotObserver);
 80            }
 81            else
 082                Debug.LogError($"Cannot display user profile {friendRequest.To}, is not allocated");
 83
 384            var ownProfile = userProfileBridge.GetOwn();
 385            view.SetSenderProfilePicture(ownProfile.snapshotObserver);
 86
 387            view.Show();
 388        }
 89
 90        private void Cancel()
 91        {
 292            cancellationToken?.Cancel();
 293            cancellationToken = new CancellationTokenSource();
 294            CancelAsync(cancellationToken.Token).Forget();
 295        }
 96
 97        private async UniTask CancelAsync(CancellationToken cancellationToken = default)
 98        {
 299            view.ShowPendingToCancel();
 100
 101            try
 102            {
 2103                await friendsController.CancelRequestAsync(friendRequestId)
 104                                       .Timeout(TimeSpan.FromSeconds(10));
 1105                if (cancellationToken.IsCancellationRequested) return;
 106
 107                // TODO: send analytics
 108
 1109                view.Close();
 1110            }
 1111            catch (Exception)
 112            {
 1113                if (cancellationToken.IsCancellationRequested) return;
 114                // TODO: track error to analytics
 1115                view.ShowCancelFailed();
 1116                throw;
 117            }
 1118        }
 119
 120        private void Hide()
 121        {
 1122            dataStore.HUDs.openSentFriendRequestDetail.Set(null, false);
 1123            view.Close();
 1124        }
 125
 126        private void OpenProfile()
 127        {
 0128            FriendRequest friendRequest = friendsController.GetAllocatedFriendRequest(friendRequestId);
 129
 0130            if (friendRequest == null)
 131            {
 0132                Debug.LogError($"Cannot open passport {friendRequestId}, is not allocated");
 0133                return;
 134            }
 135
 0136            openPassportVariable.Set(friendRequest.To);
 0137        }
 138    }
 139}