< Summary

Class:FriendRequestsTabView
Assembly:FriendsHUD
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/FriendsHUD/Scripts/Tabs/FriendRequestsTabView.cs
Covered lines:69
Uncovered lines:20
Coverable lines:89
Total lines:201
Line coverage:77.5% (69 of 89)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
FriendRequestsTabView()0%110100%
Initialize(...)0%110100%
OnDisable()0%220100%
CreateOrUpdateEntry(...)0%110100%
CreateEntry(...)0%3.13077.78%
RemoveEntry(...)0%220100%
UpdateEntry(...)0%4.024088.89%
SendFriendRequest(...)0%4.254075%
AlreadyFriends(...)0%330100%
DisplayFriendUserNotFound()0%110100%
OnSearchInputValueChanged(...)0%20400%
OnFriendRequestReceivedAccepted(...)0%6200%
OnEntryRejectButtonPressed(...)0%110100%
OnEntryCancelButtonPressed(...)0%110100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/FriendsHUD/Scripts/Tabs/FriendRequestsTabView.cs

#LineLine coverage
 1using TMPro;
 2using UnityEngine;
 3using UnityEngine.UI;
 4
 5public class FriendRequestsTabView : FriendsTabViewBase
 6{
 447    [SerializeField] internal EntryList receivedRequestsList = new EntryList();
 448    [SerializeField] internal EntryList sentRequestsList = new EntryList();
 9
 10    [SerializeField] internal TMP_InputField friendSearchInputField;
 11    [SerializeField] internal Button addFriendButton;
 12
 13    [Header("Notifications")] [SerializeField]
 14    internal Notification requestSentNotification;
 15
 16    [SerializeField] internal Notification friendSearchFailedNotification;
 17    [SerializeField] internal Notification acceptedFriendNotification;
 18    [SerializeField] internal Notification alreadyFriendsNotification;
 19
 20    public event System.Action<FriendRequestEntry> OnCancelConfirmation;
 21    public event System.Action<FriendRequestEntry> OnRejectConfirmation;
 22    public event System.Action<FriendRequestEntry> OnFriendRequestApproved;
 23    public event System.Action<string> OnFriendRequestSent;
 24
 25    public override void Initialize(FriendsHUDView owner, int preinstantiatedEntries)
 26    {
 2227        base.Initialize(owner, preinstantiatedEntries);
 28
 2229        receivedRequestsList.toggleTextPrefix = "RECEIVED";
 2230        sentRequestsList.toggleTextPrefix = "SENT";
 31
 2232        requestSentNotification.model.timer = owner.notificationsDuration;
 2233        requestSentNotification.model.groupID = FriendsHUDView.NOTIFICATIONS_ID;
 34
 2235        friendSearchFailedNotification.model.timer = owner.notificationsDuration;
 2236        friendSearchFailedNotification.model.groupID = FriendsHUDView.NOTIFICATIONS_ID;
 37
 2238        acceptedFriendNotification.model.timer = owner.notificationsDuration;
 2239        acceptedFriendNotification.model.groupID = FriendsHUDView.NOTIFICATIONS_ID;
 40
 2241        alreadyFriendsNotification.model.timer = owner.notificationsDuration;
 2242        alreadyFriendsNotification.model.groupID = FriendsHUDView.NOTIFICATIONS_ID;
 43
 2244        friendSearchInputField.onSubmit.AddListener(SendFriendRequest);
 2245        friendSearchInputField.onValueChanged.AddListener(OnSearchInputValueChanged);
 2246        addFriendButton.onClick.AddListener(() => friendSearchInputField.OnSubmit(null));
 2247    }
 48
 49    protected override void OnDisable()
 50    {
 151        base.OnDisable();
 52
 153        NotificationsController.i?.DismissAllNotifications(FriendsHUDView.NOTIFICATIONS_ID);
 154    }
 55
 56    public void CreateOrUpdateEntry(string userId, FriendEntryBase.Model model, bool isReceived)
 57    {
 1158        CreateEntry(userId);
 1159        UpdateEntry(userId, model, isReceived);
 1160    }
 61
 62    protected override bool CreateEntry(string userId)
 63    {
 1164        if (!base.CreateEntry(userId))
 065            return false;
 66
 1167        var entry = GetEntry(userId) as FriendRequestEntry;
 68
 1169        if (entry == null)
 070            return false;
 71
 1172        entry.OnAccepted += OnFriendRequestReceivedAccepted;
 1173        entry.OnRejected += OnEntryRejectButtonPressed;
 1174        entry.OnCancelled += OnEntryCancelButtonPressed;
 75
 1176        return true;
 77    }
 78
 79    public override bool RemoveEntry(string userId)
 80    {
 1781        if (!base.RemoveEntry(userId))
 1182            return false;
 83
 684        receivedRequestsList.Remove(userId);
 685        sentRequestsList.Remove(userId);
 686        return true;
 87    }
 88
 89    public bool UpdateEntry(string userId, FriendEntry.Model model, bool? isReceived = null)
 90    {
 1291        if (!base.UpdateEntry(userId, model))
 092            return false;
 93
 1294        var entry = entries[userId] as FriendRequestEntry;
 95
 1296        if (isReceived.HasValue)
 97        {
 1298            entry.SetReceived(isReceived.Value);
 99
 12100            if (isReceived.Value)
 8101                receivedRequestsList.Add(userId, entry);
 102            else
 4103                sentRequestsList.Add(userId, entry);
 104        }
 105
 12106        return true;
 107    }
 108
 109    void SendFriendRequest(string friendUserName)
 110    {
 1111        if (string.IsNullOrEmpty(friendUserName))
 0112            return;
 113
 1114        friendSearchInputField.placeholder.enabled = true;
 1115        friendSearchInputField.text = string.Empty;
 116
 1117        addFriendButton.gameObject.SetActive(false);
 118
 1119        if (!AlreadyFriends(friendUserName))
 120        {
 1121            requestSentNotification.model.message = $"Your request to {friendUserName} successfully sent!";
 1122            NotificationsController.i.ShowNotification(requestSentNotification);
 123
 1124            OnFriendRequestSent?.Invoke(friendUserName);
 1125        }
 126        else
 127        {
 0128            NotificationsController.i.ShowNotification(alreadyFriendsNotification);
 129        }
 0130    }
 131
 132    bool AlreadyFriends(string friendUserName)
 133    {
 1134        var friendUserProfile = UserProfileController.GetProfileByName(friendUserName);
 135
 1136        return friendUserProfile != null
 137               && FriendsController.i.friends.ContainsKey(friendUserProfile.userId)
 138               && FriendsController.i.friends[friendUserProfile.userId].friendshipStatus == FriendshipStatus.FRIEND;
 139    }
 140
 141    public void DisplayFriendUserNotFound()
 142    {
 1143        NotificationsController.i.ShowNotification(friendSearchFailedNotification);
 1144        addFriendButton.interactable = false;
 1145    }
 146
 147    void OnSearchInputValueChanged(string friendUserName)
 148    {
 0149        if (!addFriendButton.gameObject.activeSelf)
 0150            addFriendButton.gameObject.SetActive(true);
 151
 0152        if (!addFriendButton.interactable)
 0153            addFriendButton.interactable = true;
 154
 0155        if (!string.IsNullOrEmpty(friendUserName))
 0156            NotificationsController.i.DismissAllNotifications(FriendsHUDView.NOTIFICATIONS_ID);
 0157    }
 158
 159    void OnFriendRequestReceivedAccepted(FriendRequestEntry requestEntry)
 160    {
 161        // Add placeholder friend to avoid affecting UX by roundtrip with kernel
 0162        FriendsController.i.UpdateFriendshipStatus(new FriendsController.FriendshipUpdateStatusMessage()
 163        {
 164            userId = requestEntry.userId,
 165            action = FriendshipAction.APPROVED
 166        });
 167
 0168        FriendsController.i.UpdateUserStatus(new FriendsController.UserStatus()
 169        {
 170            userId = requestEntry.userId,
 171            presence = PresenceStatus.OFFLINE
 172        });
 173
 0174        acceptedFriendNotification.model.message = $"You and {requestEntry.model.userName} are now friends!";
 0175        NotificationsController.i.ShowNotification(acceptedFriendNotification);
 176
 0177        RemoveEntry(requestEntry.userId);
 178
 0179        OnFriendRequestApproved?.Invoke(requestEntry);
 0180    }
 181
 182    void OnEntryRejectButtonPressed(FriendRequestEntry requestEntry)
 183    {
 3184        confirmationDialog.SetText($"Are you sure you want to reject {requestEntry.model.userName} friend request?");
 3185        confirmationDialog.Show(() =>
 186        {
 1187            RemoveEntry(requestEntry.userId);
 1188            OnRejectConfirmation?.Invoke(requestEntry as FriendRequestEntry);
 1189        });
 3190    }
 191
 192    void OnEntryCancelButtonPressed(FriendRequestEntry requestEntry)
 193    {
 3194        confirmationDialog.SetText($"Are you sure you want to cancel {requestEntry.model.userName} friend request?");
 3195        confirmationDialog.Show(() =>
 196        {
 1197            RemoveEntry(requestEntry.userId);
 1198            OnCancelConfirmation?.Invoke(requestEntry as FriendRequestEntry);
 1199        });
 3200    }
 201}