< Summary

Class:PlayerInfoCardHUDView
Assembly:PlayerInfoCardHUD
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/PlayerInfoCardHUD/PlayerInfoCardHUDView.cs
Covered lines:82
Uncovered lines:42
Coverable lines:124
Total lines:297
Line coverage:66.1% (82 of 124)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
PlayerInfoCardHUDView()0%110100%
CreateView()0%110100%
Initialize(...)0%66097.22%
OnFriendStatusUpdated(...)0%6200%
SetCardActive(...)0%440100%
UpdateTabs()0%220100%
SetUserProfile(...)0%220100%
UpdateFriendButton()0%36.279030.43%
SetIsBlocked(...)0%110100%
SetVisibility(...)0%4.594066.67%
ClearCollectibles()0%2.752042.86%
IsBlocked(...)0%8.125050%
OnPointerDown()0%2100%
OnDestroy()0%220100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/PlayerInfoCardHUD/PlayerInfoCardHUDView.cs

#LineLine coverage
 1using System;
 2using DCL.Helpers;
 3using System.Collections.Generic;
 4using DCL;
 5using DCL.Configuration;
 6using TMPro;
 7using UnityEngine;
 8using UnityEngine.Assertions;
 9using UnityEngine.Events;
 10using UnityEngine.EventSystems;
 11using UnityEngine.UI;
 12using System.Linq;
 13
 14public class PlayerInfoCardHUDView : MonoBehaviour
 15{
 16    private const string PREFAB_PATH = "PlayerInfoCardHUD";
 17
 18    public enum Tabs
 19    {
 20        Passport,
 21        Trade,
 22        Block
 23    }
 24
 25    [System.Serializable]
 26    internal class TabsMapping
 27    {
 28        public GameObject container;
 29        public Toggle toggle;
 30        public Tabs tab;
 31    }
 32
 33    [SerializeField] internal GenericFactory collectiblesFactory;
 34    [SerializeField] internal Canvas cardCanvas;
 35    [SerializeField] internal TabsMapping[] tabsMapping;
 36    [SerializeField] internal Button hideCardButton;
 37
 38    [Space] [SerializeField] internal RawImage avatarPicture;
 39    [SerializeField] internal Image blockedAvatarOverlay;
 40    [SerializeField] internal TextMeshProUGUI name;
 41
 42    [Header("Friends")] [SerializeField] internal GameObject friendStatusContainer;
 43    [SerializeField] internal Button requestSentButton;
 44    [SerializeField] internal Button addFriendButton;
 45    [SerializeField] internal GameObject alreadyFriendsContainer;
 46    [SerializeField] internal GameObject requestReceivedContainer;
 47    [SerializeField] internal Button acceptRequestButton;
 48    [SerializeField] internal Button rejectRequestButton;
 49
 50    [Header("Passport")] [SerializeField] internal TextMeshProUGUI description;
 51
 52    [Header("Trade")] [SerializeField] private RectTransform wearablesContainer;
 53    [SerializeField] private GameObject emptyCollectiblesImage;
 54
 55    [Header("Block")] [SerializeField] internal Button reportPlayerButton;
 56    [SerializeField] internal Button blockPlayerButton;
 57    [SerializeField] internal Button unblockPlayerButton;
 58
 2659    internal readonly List<PlayerInfoCollectibleItem> playerInfoCollectibles = new List<PlayerInfoCollectibleItem>(10);
 60    internal UserProfile currentUserProfile;
 461    private UnityAction<bool> toggleChangedDelegate => (x) => UpdateTabs();
 2162    private UserProfile ownUserProfile => UserProfile.GetOwnUserProfile();
 63
 64    private MouseCatcher mouseCatcher;
 2665    private List<string> loadedWearables = new List<string>();
 66
 1367    public static PlayerInfoCardHUDView CreateView() { return Instantiate(Resources.Load<GameObject>(PREFAB_PATH)).GetCo
 68
 69    public void Initialize(UnityAction cardClosedCallback,
 70        UnityAction reportPlayerCallback,
 71        UnityAction blockPlayerCallback,
 72        UnityAction unblockPlayerCallback,
 73        UnityAction addFriendCallback,
 74        UnityAction cancelInvitation,
 75        UnityAction acceptFriendRequest,
 76        UnityAction rejectFriendRequest)
 77    {
 1478        hideCardButton.onClick.RemoveAllListeners();
 1479        hideCardButton.onClick.AddListener(cardClosedCallback);
 80
 1481        reportPlayerButton.onClick.RemoveAllListeners();
 1482        reportPlayerButton.onClick.AddListener(reportPlayerCallback);
 83
 1484        blockPlayerButton.onClick.RemoveAllListeners();
 1485        blockPlayerButton.onClick.AddListener(blockPlayerCallback);
 86
 1487        unblockPlayerButton.onClick.RemoveAllListeners();
 1488        unblockPlayerButton.onClick.AddListener(unblockPlayerCallback);
 89
 1490        addFriendButton.onClick.RemoveAllListeners();
 1491        addFriendButton.onClick.AddListener(addFriendCallback);
 92
 1493        requestSentButton.onClick.RemoveAllListeners();
 1494        requestSentButton.onClick.AddListener(cancelInvitation);
 95
 1496        acceptRequestButton.onClick.RemoveAllListeners();
 1497        acceptRequestButton.onClick.AddListener(acceptFriendRequest);
 98
 1499        rejectRequestButton.onClick.RemoveAllListeners();
 14100        rejectRequestButton.onClick.AddListener(rejectFriendRequest);
 101
 112102        for (int index = 0; index < tabsMapping.Length; index++)
 103        {
 42104            var tab = tabsMapping[index];
 42105            tab.toggle.onValueChanged.RemoveListener(toggleChangedDelegate);
 42106            tab.toggle.onValueChanged.AddListener(toggleChangedDelegate);
 107        }
 108
 28109        for (int index = 0; index < tabsMapping.Length; index++)
 110        {
 14111            if (tabsMapping[index].tab == Tabs.Passport)
 112            {
 14113                tabsMapping[index].toggle.isOn = true;
 14114                break;
 115            }
 116        }
 117
 118
 14119        FriendsController.i.OnUpdateFriendship -= OnFriendStatusUpdated;
 14120        FriendsController.i.OnUpdateFriendship += OnFriendStatusUpdated;
 121
 14122        if (InitialSceneReferences.i != null)
 123        {
 14124            var mouseCatcher = DCL.InitialSceneReferences.i.mouseCatcher;
 125
 14126            if (mouseCatcher != null)
 127            {
 14128                this.mouseCatcher = mouseCatcher;
 14129                mouseCatcher.OnMouseDown += OnPointerDown;
 130            }
 131        }
 14132    }
 133
 134    private void OnFriendStatusUpdated(string userId, FriendshipAction action)
 135    {
 0136        if (currentUserProfile == null)
 0137            return;
 138
 0139        UpdateFriendButton();
 0140    }
 141
 142    public void SetCardActive(bool active)
 143    {
 7144        if (active && mouseCatcher != null)
 145        {
 6146            mouseCatcher.UnlockCursor();
 6147        }
 1148        else if (!active)
 149        {
 1150            CatalogController.RemoveWearablesInUse(loadedWearables);
 1151            loadedWearables.Clear();
 152        }
 153
 7154        cardCanvas.enabled = active;
 7155        CommonScriptableObjects.playerInfoCardVisibleState.Set(active);
 7156    }
 157
 158    private void UpdateTabs()
 159    {
 32160        for (int index = 0; index < tabsMapping.Length; index++)
 161        {
 12162            tabsMapping[index].container.SetActive(tabsMapping[index].toggle.isOn);
 163        }
 4164    }
 165
 166    public void SetUserProfile(UserProfile userProfile)
 167    {
 7168        Assert.IsTrue(userProfile != null, "userProfile can't be null");
 169
 7170        currentUserProfile = userProfile;
 7171        name.text = currentUserProfile.userName;
 7172        description.text = currentUserProfile.description;
 7173        avatarPicture.texture = currentUserProfile.faceSnapshot;
 174
 7175        ClearCollectibles();
 176
 7177        CatalogController.RequestOwnedWearables(userProfile.userId)
 178                         .Then((ownedWearables) =>
 179                         {
 0180                             currentUserProfile.SetInventory(ownedWearables.Select(x => x.id).ToArray());
 0181                             loadedWearables.AddRange(ownedWearables.Select(x => x.id));
 182
 0183                             var collectiblesIds = currentUserProfile.GetInventoryItemsIds();
 0184                             for (int index = 0; index < collectiblesIds.Length; index++)
 185                             {
 0186                                 string collectibleId = collectiblesIds[index];
 0187                                 CatalogController.wearableCatalog.TryGetValue(collectibleId, out WearableItem collectib
 0188                                 if (collectible == null)
 189                                     continue;
 190
 0191                                 var playerInfoCollectible =
 192                                     collectiblesFactory.Instantiate<PlayerInfoCollectibleItem>(collectible.rarity,
 193                                         wearablesContainer.transform);
 0194                                 if (playerInfoCollectible == null)
 195                                     continue;
 0196                                 playerInfoCollectibles.Add(playerInfoCollectible);
 0197                                 playerInfoCollectible.Initialize(collectible);
 198                             }
 199
 0200                             emptyCollectiblesImage.SetActive(collectiblesIds.Length == 0);
 0201                         })
 2202                         .Catch((error) => Debug.Log(error));
 203
 7204        SetIsBlocked(IsBlocked(userProfile.userId));
 205
 7206        UpdateFriendButton();
 7207    }
 208
 209    private void UpdateFriendButton()
 210    {
 7211        bool canUseFriendButton = FriendsController.i != null && FriendsController.i.isInitialized && currentUserProfile
 212
 7213        friendStatusContainer.SetActive(canUseFriendButton);
 7214        if (!canUseFriendButton)
 215        {
 7216            addFriendButton.gameObject.SetActive(false);
 7217            alreadyFriendsContainer.SetActive(false);
 7218            requestSentButton.gameObject.SetActive(false);
 7219            return;
 220        }
 221
 0222        if (currentUserProfile == null)
 0223            return;
 224
 0225        var status = FriendsController.i.GetUserStatus(currentUserProfile.userId);
 226
 0227        addFriendButton.gameObject.SetActive(false);
 0228        alreadyFriendsContainer.SetActive(false);
 0229        requestReceivedContainer.SetActive(false);
 0230        requestSentButton.gameObject.SetActive(false);
 231
 0232        switch (status.friendshipStatus)
 233        {
 234            case FriendshipStatus.NONE:
 0235                addFriendButton.gameObject.SetActive(true);
 0236                break;
 237            case FriendshipStatus.FRIEND:
 0238                alreadyFriendsContainer.SetActive(true);
 0239                break;
 240            case FriendshipStatus.REQUESTED_FROM:
 0241                requestReceivedContainer.SetActive(true);
 0242                break;
 243            case FriendshipStatus.REQUESTED_TO:
 0244                requestSentButton.gameObject.SetActive(true);
 245                break;
 246        }
 0247    }
 248
 249    public void SetIsBlocked(bool isBlocked)
 250    {
 7251        unblockPlayerButton.gameObject.SetActive(isBlocked);
 7252        blockedAvatarOverlay.gameObject.SetActive(isBlocked);
 7253    }
 254
 255    public void SetVisibility(bool visible)
 256    {
 1257        if (gameObject.activeSelf && !visible)
 0258            AudioScriptableObjects.dialogClose.Play(true);
 259
 1260        if (!gameObject.activeSelf && visible)
 0261            AudioScriptableObjects.dialogOpen.Play(true);
 262
 1263        gameObject.SetActive(visible);
 1264    }
 265
 266    private void ClearCollectibles()
 267    {
 14268        for (var i = playerInfoCollectibles.Count - 1; i >= 0; i--)
 269        {
 0270            var playerInfoCollectible = playerInfoCollectibles[i];
 0271            playerInfoCollectibles.RemoveAt(i);
 0272            Destroy(playerInfoCollectible.gameObject);
 273        }
 7274    }
 275
 276    internal bool IsBlocked(string userId)
 277    {
 7278        if (ownUserProfile == null || ownUserProfile.blocked == null)
 0279            return false;
 280
 14281        for (int i = 0; i < ownUserProfile.blocked.Count; i++)
 282        {
 0283            if (ownUserProfile.blocked[i] == userId)
 0284                return true;
 285        }
 286
 7287        return false;
 288    }
 289
 0290    private void OnPointerDown() { hideCardButton.onClick.Invoke(); }
 291
 292    private void OnDestroy()
 293    {
 13294        if (mouseCatcher != null)
 13295            mouseCatcher.OnMouseDown -= OnPointerDown;
 13296    }
 297}