< 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:98
Uncovered lines:38
Coverable lines:136
Total lines:296
Line coverage:72% (98 of 136)
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        var 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        var 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.avatarImage = userProfile.faceSnapshot;
 138
 18139        userProfile.OnFaceSnapshotReadyEvent -= friendEntryModel.OnSpriteUpdate;
 18140        userProfile.OnFaceSnapshotReadyEvent += friendEntryModel.OnSpriteUpdate;
 141
 18142        if (ownUserProfile != null && ownUserProfile.blocked != null)
 18143            friendEntryModel.blocked = ownUserProfile.blocked.Contains(userId);
 144
 145        switch (friendshipAction)
 146        {
 147            case FriendshipAction.NONE:
 0148                userProfile.OnFaceSnapshotReadyEvent -= friendEntryModel.OnSpriteUpdate;
 0149                view.friendRequestsList.RemoveEntry(userId);
 0150                view.friendsList.RemoveEntry(userId);
 0151                break;
 152            case FriendshipAction.APPROVED:
 11153                view.friendRequestsList.RemoveEntry(userId);
 11154                view.friendsList.CreateOrUpdateEntryDeferred(userId, friendEntryModel);
 11155                break;
 156            case FriendshipAction.REJECTED:
 1157                userProfile.OnFaceSnapshotReadyEvent -= friendEntryModel.OnSpriteUpdate;
 1158                view.friendRequestsList.RemoveEntry(userId);
 1159                break;
 160            case FriendshipAction.CANCELLED:
 1161                userProfile.OnFaceSnapshotReadyEvent -= friendEntryModel.OnSpriteUpdate;
 1162                view.friendRequestsList.RemoveEntry(userId);
 1163                break;
 164            case FriendshipAction.REQUESTED_FROM:
 3165                view.friendRequestsList.CreateOrUpdateEntry(userId, friendEntryModel, true);
 3166                break;
 167            case FriendshipAction.REQUESTED_TO:
 1168                view.friendRequestsList.CreateOrUpdateEntry(userId,  friendEntryModel, false);
 1169                break;
 170            case FriendshipAction.DELETED:
 1171                userProfile.OnFaceSnapshotReadyEvent -= friendEntryModel.OnSpriteUpdate;
 1172                view.friendRequestsList.RemoveEntry(userId);
 1173                view.friendsList.RemoveEntry(userId);
 174                break;
 175        }
 176
 18177        UpdateNotificationsCounter();
 18178    }
 179
 180    private void UpdateNotificationsCounter()
 181    {
 182        //NOTE(Brian): If friends tab is already active, update and save this value instantly
 22183        if (view.friendsList.gameObject.activeInHierarchy)
 184        {
 16185            PlayerPrefsUtils.SetInt(PLAYER_PREFS_SEEN_FRIEND_COUNT, friendsController.friendCount);
 16186            PlayerPrefsUtils.Save();
 187        }
 188
 22189        var pendingFriendRequestsSO = NotificationScriptableObjects.pendingFriendRequests;
 22190        int receivedRequestsCount = view.friendRequestsList.receivedRequestsList.Count();
 191
 22192        if (pendingFriendRequestsSO != null)
 193        {
 22194            pendingFriendRequestsSO.Set(receivedRequestsCount);
 195        }
 196
 22197        int seenFriendsCount = PlayerPrefs.GetInt(PLAYER_PREFS_SEEN_FRIEND_COUNT, 0);
 22198        int friendsCount = friendsController.friendCount;
 199
 22200        int newFriends = friendsCount - seenFriendsCount;
 201
 202        //NOTE(Brian): If someone deletes you, don't show badge notification
 22203        if (newFriends < 0)
 1204            newFriends = 0;
 205
 22206        var newApprovedFriendsSO = NotificationScriptableObjects.newApprovedFriends;
 207
 22208        if (newApprovedFriendsSO != null)
 209        {
 22210            newApprovedFriendsSO.Set(newFriends);
 211        }
 22212    }
 213
 2214    private void Entry_OnWhisper(FriendEntry entry) { OnPressWhisper?.Invoke(entry.userId); }
 215
 216    private void Entry_OnDelete(string userId)
 217    {
 1218        WebInterface.UpdateFriendshipStatus(
 219            new FriendsController.FriendshipUpdateStatusMessage()
 220            {
 221                action = FriendshipAction.DELETED,
 222                userId = userId
 223            });
 1224    }
 225
 226    private void Entry_OnRequestRejected(FriendRequestEntry entry)
 227    {
 1228        WebInterface.UpdateFriendshipStatus(
 229            new FriendsController.FriendshipUpdateStatusMessage()
 230            {
 231                action = FriendshipAction.REJECTED,
 232                userId = entry.userId
 233            });
 1234    }
 235
 236    private void Entry_OnRequestCancelled(FriendRequestEntry entry)
 237    {
 1238        WebInterface.UpdateFriendshipStatus(
 239            new FriendsController.FriendshipUpdateStatusMessage()
 240            {
 241                action = FriendshipAction.CANCELLED,
 242                userId = entry.userId
 243            });
 1244    }
 245
 246    private void Entry_OnRequestAccepted(FriendRequestEntry entry)
 247    {
 0248        WebInterface.UpdateFriendshipStatus(
 249            new FriendsController.FriendshipUpdateStatusMessage()
 250            {
 251                action = FriendshipAction.APPROVED,
 252                userId = entry.userId
 253            });
 0254    }
 255
 256    public void Dispose()
 257    {
 28258        if (this.friendsController != null)
 259        {
 12260            this.friendsController.OnInitialized -= FriendsController_OnInitialized;
 12261            this.friendsController.OnUpdateFriendship -= OnUpdateFriendship;
 12262            this.friendsController.OnUpdateUserStatus -= OnUpdateUserStatus;
 263        }
 264
 28265        if (view != null)
 266        {
 22267            UnityEngine.Object.Destroy(view.gameObject);
 268        }
 269
 28270        if (this.ownUserProfile != null)
 12271            ownUserProfile.OnUpdate -= OnUserProfileUpdate;
 28272    }
 273
 274    public void SetVisibility(bool visible)
 275    {
 7276        view.gameObject.SetActive(visible);
 277
 7278        if (visible)
 279        {
 4280            UpdateNotificationsCounter();
 281
 4282            if (view.friendsButton.interactable)
 3283                view.friendsButton.onClick.Invoke();
 284
 4285            OnFriendsOpened?.Invoke();
 286
 4287            AudioScriptableObjects.dialogOpen.Play(true);
 4288        }
 289        else
 290        {
 3291            OnFriendsClosed?.Invoke();
 292
 3293            AudioScriptableObjects.dialogClose.Play(true);
 294        }
 3295    }
 296}