< Summary

Class:FriendsHUDController
Assembly:FriendsHUD
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/FriendsHUD/Scripts/FriendsHUDController.cs
Covered lines:93
Uncovered lines:37
Coverable lines:130
Total lines:289
Line coverage:71.5% (93 of 130)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
Initialize(...)0%550100%
FriendsController_OnInitialized()0%2100%
OnUserProfileUpdate(...)0%12300%
Entry_OnRequestSent(...)0%110100%
OnUpdateUserStatus(...)0%20400%
OnFriendNotFound(...)0%110100%
OnUpdateFriendship(...)0%13.7813083.33%
UpdateNotificationsCounter()0%550100%
Entry_OnWhisper(...)0%220100%
Entry_OnDelete(...)0%110100%
Entry_OnRequestRejected(...)0%110100%
Entry_OnRequestCancelled(...)0%110100%
Entry_OnRequestAccepted(...)0%2100%
Dispose()0%440100%
SetVisibility(...)0%550100%

File(s)

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

#LineLine coverage
 1using DCL.Helpers;
 2using DCL.Interface;
 3using System.Collections.Generic;
 4using UnityEngine;
 5
 6public class FriendsHUDController : IHUD
 7{
 8    internal const string PLAYER_PREFS_SEEN_FRIEND_COUNT = "SeenFriendsCount";
 09    public FriendsHUDView view { get; private set; }
 10
 11    IFriendsController friendsController;
 12    public event System.Action<string> OnPressWhisper;
 13    public event System.Action OnFriendsOpened;
 14    public event System.Action OnFriendsClosed;
 15
 16    UserProfile ownUserProfile;
 17
 18    public void Initialize(IFriendsController friendsController, UserProfile ownUserProfile)
 19    {
 2220        view = FriendsHUDView.Create(this);
 2221        this.friendsController = friendsController;
 22
 2223        if (this.friendsController != null)
 24        {
 1225            this.friendsController.OnUpdateFriendship += OnUpdateFriendship;
 1226            this.friendsController.OnUpdateUserStatus += OnUpdateUserStatus;
 1227            this.friendsController.OnFriendNotFound += OnFriendNotFound;
 28        }
 29
 2230        view.friendRequestsList.OnFriendRequestApproved += Entry_OnRequestAccepted;
 2231        view.friendRequestsList.OnCancelConfirmation += Entry_OnRequestCancelled;
 2232        view.friendRequestsList.OnRejectConfirmation += Entry_OnRequestRejected;
 2233        view.friendRequestsList.OnFriendRequestSent += Entry_OnRequestSent;
 34
 2235        view.friendsList.OnWhisper += Entry_OnWhisper;
 36
 2237        view.friendsList.OnDeleteConfirmation += Entry_OnDelete;
 38
 2239        if (ownUserProfile != null)
 40        {
 1241            this.ownUserProfile = ownUserProfile;
 1242            ownUserProfile.OnUpdate += OnUserProfileUpdate;
 43        }
 44
 2245        if (friendsController != null)
 46        {
 1247            if (friendsController.isInitialized)
 48            {
 1149                view.HideSpinner();
 1150            }
 51            else
 52            {
 153                view.ShowSpinner();
 154                friendsController.OnInitialized -= FriendsController_OnInitialized;
 155                friendsController.OnInitialized += FriendsController_OnInitialized;
 56            }
 57        }
 1158    }
 59
 60    private void FriendsController_OnInitialized()
 61    {
 062        friendsController.OnInitialized -= FriendsController_OnInitialized;
 063        view.HideSpinner();
 064    }
 65
 66    private void OnUserProfileUpdate(UserProfile profile)
 67    {
 68        //NOTE(Brian): HashSet to check Contains quicker.
 69        HashSet<string> allBlockedUsers;
 70
 071        if (profile.blocked != null)
 072            allBlockedUsers = new HashSet<string>(profile.blocked);
 73        else
 074            allBlockedUsers = new HashSet<string>();
 75
 076        var entries = view.GetAllEntries();
 077        int entriesCount = entries.Count;
 78
 079        for (int i = 0; i < entriesCount; i++)
 80        {
 081            entries[i].model.blocked = allBlockedUsers.Contains(entries[i].userId);
 082            entries[i].Populate(entries[i].model);
 83        }
 084    }
 85
 286    private void Entry_OnRequestSent(string userId) { WebInterface.UpdateFriendshipStatus(new FriendsController.Friendsh
 87
 88    private void OnUpdateUserStatus(string userId, FriendsController.UserStatus newStatus)
 89    {
 090        var model = new FriendEntry.Model();
 91
 092        FriendEntryBase entry = view.friendsList.GetEntry(userId) ?? view.friendRequestsList.GetEntry(userId);
 93
 094        if (entry != null)
 095            model = entry.model;
 96
 097        model.status = newStatus.presence;
 098        model.coords = newStatus.position;
 99
 0100        if (newStatus.realm != null)
 101        {
 0102            model.realm = $"{newStatus.realm.serverName.ToUpperFirst()} {newStatus.realm.layer.ToUpperFirst()}";
 0103            model.realmServerName = newStatus.realm.serverName;
 0104            model.realmLayerName = newStatus.realm.layer;
 0105        }
 106        else
 107        {
 0108            model.realm = string.Empty;
 0109            model.realmServerName = string.Empty;
 0110            model.realmLayerName = string.Empty;
 111        }
 112
 0113        view.friendsList.UpdateEntry(userId, model);
 0114        view.friendRequestsList.UpdateEntry(userId, model);
 0115    }
 116
 2117    void OnFriendNotFound(string name) { view.friendRequestsList.DisplayFriendUserNotFound(); }
 118
 119    private void OnUpdateFriendship(string userId, FriendshipAction friendshipAction)
 120    {
 18121        UserProfile userProfile = UserProfileController.userProfilesCatalog.Get(userId);
 122
 18123        if (userProfile == null)
 124        {
 0125            Debug.LogError($"UserProfile is null for {userId}! ... friendshipAction {friendshipAction}");
 0126            return;
 127        }
 128
 18129        FriendEntryBase.Model friendEntryModel = new FriendEntry.Model();
 130
 18131        FriendEntryBase entry = view.friendsList.GetEntry(userId) ?? view.friendRequestsList.GetEntry(userId);
 132
 18133        if (entry != null)
 6134            friendEntryModel = entry.model;
 135
 18136        friendEntryModel.userName = userProfile.userName;
 18137        friendEntryModel.avatarSnapshotObserver = userProfile.snapshotObserver;
 138
 18139        if (ownUserProfile != null && ownUserProfile.blocked != null)
 18140            friendEntryModel.blocked = ownUserProfile.blocked.Contains(userId);
 141
 142        switch (friendshipAction)
 143        {
 144            case FriendshipAction.NONE:
 0145                view.friendRequestsList.RemoveEntry(userId);
 0146                view.friendsList.RemoveEntry(userId);
 0147                break;
 148            case FriendshipAction.APPROVED:
 11149                view.friendRequestsList.RemoveEntry(userId);
 11150                view.friendsList.CreateOrUpdateEntryDeferred(userId, friendEntryModel);
 11151                break;
 152            case FriendshipAction.REJECTED:
 1153                view.friendRequestsList.RemoveEntry(userId);
 1154                break;
 155            case FriendshipAction.CANCELLED:
 1156                view.friendRequestsList.RemoveEntry(userId);
 1157                break;
 158            case FriendshipAction.REQUESTED_FROM:
 3159                view.friendRequestsList.CreateOrUpdateEntry(userId, friendEntryModel, true);
 3160                break;
 161            case FriendshipAction.REQUESTED_TO:
 1162                view.friendRequestsList.CreateOrUpdateEntry(userId,  friendEntryModel, false);
 1163                break;
 164            case FriendshipAction.DELETED:
 1165                view.friendRequestsList.RemoveEntry(userId);
 1166                view.friendsList.RemoveEntry(userId);
 167                break;
 168        }
 169
 18170        UpdateNotificationsCounter();
 18171    }
 172
 173    private void UpdateNotificationsCounter()
 174    {
 175        //NOTE(Brian): If friends tab is already active, update and save this value instantly
 22176        if (view.friendsList.gameObject.activeInHierarchy)
 177        {
 16178            PlayerPrefsUtils.SetInt(PLAYER_PREFS_SEEN_FRIEND_COUNT, friendsController.friendCount);
 16179            PlayerPrefsUtils.Save();
 180        }
 181
 22182        var pendingFriendRequestsSO = NotificationScriptableObjects.pendingFriendRequests;
 22183        int receivedRequestsCount = view.friendRequestsList.receivedRequestsList.Count();
 184
 22185        if (pendingFriendRequestsSO != null)
 186        {
 22187            pendingFriendRequestsSO.Set(receivedRequestsCount);
 188        }
 189
 22190        int seenFriendsCount = PlayerPrefs.GetInt(PLAYER_PREFS_SEEN_FRIEND_COUNT, 0);
 22191        int friendsCount = friendsController.friendCount;
 192
 22193        int newFriends = friendsCount - seenFriendsCount;
 194
 195        //NOTE(Brian): If someone deletes you, don't show badge notification
 22196        if (newFriends < 0)
 1197            newFriends = 0;
 198
 22199        var newApprovedFriendsSO = NotificationScriptableObjects.newApprovedFriends;
 200
 22201        if (newApprovedFriendsSO != null)
 202        {
 22203            newApprovedFriendsSO.Set(newFriends);
 204        }
 22205    }
 206
 2207    private void Entry_OnWhisper(FriendEntry entry) { OnPressWhisper?.Invoke(entry.userId); }
 208
 209    private void Entry_OnDelete(string userId)
 210    {
 1211        WebInterface.UpdateFriendshipStatus(
 212            new FriendsController.FriendshipUpdateStatusMessage()
 213            {
 214                action = FriendshipAction.DELETED,
 215                userId = userId
 216            });
 1217    }
 218
 219    private void Entry_OnRequestRejected(FriendRequestEntry entry)
 220    {
 1221        WebInterface.UpdateFriendshipStatus(
 222            new FriendsController.FriendshipUpdateStatusMessage()
 223            {
 224                action = FriendshipAction.REJECTED,
 225                userId = entry.userId
 226            });
 1227    }
 228
 229    private void Entry_OnRequestCancelled(FriendRequestEntry entry)
 230    {
 1231        WebInterface.UpdateFriendshipStatus(
 232            new FriendsController.FriendshipUpdateStatusMessage()
 233            {
 234                action = FriendshipAction.CANCELLED,
 235                userId = entry.userId
 236            });
 1237    }
 238
 239    private void Entry_OnRequestAccepted(FriendRequestEntry entry)
 240    {
 0241        WebInterface.UpdateFriendshipStatus(
 242            new FriendsController.FriendshipUpdateStatusMessage()
 243            {
 244                action = FriendshipAction.APPROVED,
 245                userId = entry.userId
 246            });
 0247    }
 248
 249    public void Dispose()
 250    {
 28251        if (this.friendsController != null)
 252        {
 12253            this.friendsController.OnInitialized -= FriendsController_OnInitialized;
 12254            this.friendsController.OnUpdateFriendship -= OnUpdateFriendship;
 12255            this.friendsController.OnUpdateUserStatus -= OnUpdateUserStatus;
 256        }
 257
 28258        if (view != null)
 259        {
 22260            UnityEngine.Object.Destroy(view.gameObject);
 261        }
 262
 28263        if (this.ownUserProfile != null)
 12264            ownUserProfile.OnUpdate -= OnUserProfileUpdate;
 28265    }
 266
 267    public void SetVisibility(bool visible)
 268    {
 7269        view.gameObject.SetActive(visible);
 270
 7271        if (visible)
 272        {
 4273            UpdateNotificationsCounter();
 274
 4275            if (view.friendsButton.interactable)
 3276                view.friendsButton.onClick.Invoke();
 277
 4278            OnFriendsOpened?.Invoke();
 279
 4280            AudioScriptableObjects.dialogOpen.Play(true);
 4281        }
 282        else
 283        {
 3284            OnFriendsClosed?.Invoke();
 285
 3286            AudioScriptableObjects.dialogClose.Play(true);
 287        }
 3288    }
 289}