< 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:45
Uncovered lines:0
Coverable lines:45
Total lines:107
Line coverage:100% (45 of 45)
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%
SendAsync()0%4.14081.82%
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 System;
 2using Cysharp.Threading.Tasks;
 3using DCl.Social.Friends;
 4using SocialFeaturesAnalytics;
 5using UnityEngine;
 6
 7namespace 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
 620        public SendFriendRequestHUDController(
 21            ISendFriendRequestHUDView view,
 22            DataStore dataStore,
 23            IUserProfileBridge userProfileBridge,
 24            IFriendsController friendsController,
 25            ISocialAnalytics socialAnalytics)
 26        {
 627            this.view = view;
 628            this.dataStore = dataStore;
 629            this.userProfileBridge = userProfileBridge;
 630            this.friendsController = friendsController;
 631            this.socialAnalytics = socialAnalytics;
 32
 633            dataStore.HUDs.sendFriendRequest.OnChange += ShowOrHide;
 34
 635            view.OnMessageBodyChanged += OnMessageBodyChanged;
 636            view.OnSend += Send;
 637            view.OnCancel += Hide;
 38
 639            view.Close();
 640        }
 41
 42        public void Dispose()
 43        {
 644            dataStore.HUDs.sendFriendRequest.OnChange -= ShowOrHide;
 645            view.OnMessageBodyChanged -= OnMessageBodyChanged;
 646            view.OnSend -= Send;
 647            view.OnCancel -= Hide;
 648            view.Dispose();
 649        }
 50
 51        private void ShowOrHide(string current, string previous)
 52        {
 653            if (string.IsNullOrEmpty(current))
 154                Hide();
 55            else
 556                Show(current);
 557        }
 58
 59        private void Show(string recipient)
 60        {
 561            messageBody = "";
 562            recipientId = recipient;
 563            var userProfile = userProfileBridge.Get(recipient);
 564            if (userProfile == null) return;
 565            view.SetName(userProfile.userName);
 66
 67            // must send the snapshot observer, otherwise the faceUrl is invalid and the texture never loads
 568            view.SetProfilePicture(userProfile.snapshotObserver);
 569            view.ClearInputField();
 570            view.Show();
 571        }
 72
 73        private void Send() =>
 474            SendAsync().Forget();
 75
 76        private async UniTaskVoid SendAsync()
 77        {
 478            view.ShowPendingToSend();
 79
 80            try
 81            {
 482                await friendsController.RequestFriendshipAsync(recipientId, messageBody)
 83                                       .Timeout(TimeSpan.FromSeconds(10));
 84
 385                socialAnalytics.SendFriendRequestSent(userProfileBridge.GetOwn().userId, recipientId, messageBody.Length
 86                    (PlayerActionSource)dataStore.HUDs.sendFriendRequestSource.Get());
 87
 388                view.ShowSendSuccess();
 389            }
 90            catch (Exception e)
 91            {
 92                // TODO: track error to analytics
 193                Debug.LogException(e);
 194                view.ShowSendFailed();
 195            }
 496        }
 97
 98        private void Hide()
 99        {
 1100            dataStore.HUDs.sendFriendRequest.Set(null, false);
 1101            view.Close();
 1102        }
 103
 104        private void OnMessageBodyChanged(string body) =>
 4105            messageBody = body;
 106    }
 107}