< Summary

Class:PlayerInfoCardHUDView
Assembly:PlayerInfoCardHUD
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/PlayerInfoCardHUD/PlayerInfoCardHUDView.cs
Covered lines:85
Uncovered lines:51
Coverable lines:136
Total lines:323
Line coverage:62.5% (85 of 136)
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%
SetFaceSnapshot(...)0%2100%
SetUserProfile(...)0%3.013091.67%
UpdateFriendButton()0%37.239029.63%
SetIsBlocked(...)0%110100%
SetVisibility(...)0%5.673033.33%
ClearCollectibles()0%2.752042.86%
IsBlocked(...)0%8.125050%
OnPointerDown()0%2100%
OnDestroy()0%330100%

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();
 1262    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    {
 8144        if (active && mouseCatcher != null)
 145        {
 3146            mouseCatcher.UnlockCursor();
 3147        }
 5148        else if (!active)
 149        {
 5150            CatalogController.RemoveWearablesInUse(loadedWearables);
 5151            loadedWearables.Clear();
 152        }
 153
 8154        cardCanvas.enabled = active;
 8155        CommonScriptableObjects.playerInfoCardVisibleState.Set(active);
 8156    }
 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
 0166    public void SetFaceSnapshot(Texture2D texture) { avatarPicture.texture = texture; }
 167
 168    public void SetUserProfile(UserProfile userProfile)
 169    {
 4170        Assert.IsTrue(userProfile != null, "userProfile can't be null");
 171
 4172        name.text = userProfile.userName;
 4173        description.text = userProfile.description;
 174
 4175        ClearCollectibles();
 176
 4177        CatalogController.RequestOwnedWearables(userProfile.userId)
 178                         .Then((ownedWearables) =>
 179                         {
 0180                             userProfile.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
 4204        SetIsBlocked(IsBlocked(userProfile.userId));
 205
 206        // Remove old profile listener and set the new one
 4207        if ( currentUserProfile != null )
 0208            currentUserProfile.snapshotObserver.RemoveListener(SetFaceSnapshot);
 209
 4210        userProfile.snapshotObserver.AddListener(SetFaceSnapshot);
 211
 4212        currentUserProfile = userProfile;
 213
 4214        UpdateFriendButton();
 4215    }
 216
 217    private void UpdateFriendButton()
 218    {
 4219        if (currentUserProfile == null)
 220        {
 0221            requestReceivedContainer.SetActive(false);
 0222            addFriendButton.gameObject.SetActive(false);
 0223            alreadyFriendsContainer.SetActive(false);
 0224            requestSentButton.gameObject.SetActive(false);
 0225            return;
 226        }
 227
 4228        bool canUseFriendButton = FriendsController.i != null && FriendsController.i.isInitialized && currentUserProfile
 229
 4230        friendStatusContainer.SetActive(canUseFriendButton);
 231
 4232        if (!canUseFriendButton)
 233        {
 4234            addFriendButton.gameObject.SetActive(false);
 4235            alreadyFriendsContainer.SetActive(false);
 4236            requestSentButton.gameObject.SetActive(false);
 4237            return;
 238        }
 239
 0240        var status = FriendsController.i.GetUserStatus(currentUserProfile.userId);
 241
 0242        addFriendButton.gameObject.SetActive(false);
 0243        alreadyFriendsContainer.SetActive(false);
 0244        requestReceivedContainer.SetActive(false);
 0245        requestSentButton.gameObject.SetActive(false);
 246
 0247        switch (status.friendshipStatus)
 248        {
 249            case FriendshipStatus.NONE:
 0250                addFriendButton.gameObject.SetActive(true);
 0251                break;
 252            case FriendshipStatus.FRIEND:
 0253                alreadyFriendsContainer.SetActive(true);
 0254                break;
 255            case FriendshipStatus.REQUESTED_FROM:
 0256                requestReceivedContainer.SetActive(true);
 0257                break;
 258            case FriendshipStatus.REQUESTED_TO:
 0259                requestSentButton.gameObject.SetActive(true);
 260                break;
 261        }
 0262    }
 263
 264    public void SetIsBlocked(bool isBlocked)
 265    {
 4266        unblockPlayerButton.gameObject.SetActive(isBlocked);
 4267        blockedAvatarOverlay.gameObject.SetActive(isBlocked);
 4268    }
 269
 270    public void SetVisibility(bool visible)
 271    {
 1272        if (gameObject.activeSelf != visible)
 273        {
 0274            if ( visible )
 275            {
 0276                AudioScriptableObjects.dialogOpen.Play(true);
 0277                currentUserProfile.snapshotObserver.AddListener(SetFaceSnapshot);
 0278            }
 279            else
 280            {
 0281                AudioScriptableObjects.dialogClose.Play(true);
 0282                currentUserProfile.snapshotObserver.RemoveListener(SetFaceSnapshot);
 283            }
 284        }
 285
 1286        gameObject.SetActive(visible);
 1287    }
 288
 289    private void ClearCollectibles()
 290    {
 8291        for (var i = playerInfoCollectibles.Count - 1; i >= 0; i--)
 292        {
 0293            var playerInfoCollectible = playerInfoCollectibles[i];
 0294            playerInfoCollectibles.RemoveAt(i);
 0295            Destroy(playerInfoCollectible.gameObject);
 296        }
 4297    }
 298
 299    internal bool IsBlocked(string userId)
 300    {
 4301        if (ownUserProfile == null || ownUserProfile.blocked == null)
 0302            return false;
 303
 8304        for (int i = 0; i < ownUserProfile.blocked.Count; i++)
 305        {
 0306            if (ownUserProfile.blocked[i] == userId)
 0307                return true;
 308        }
 309
 4310        return false;
 311    }
 312
 0313    private void OnPointerDown() { hideCardButton.onClick.Invoke(); }
 314
 315    private void OnDestroy()
 316    {
 13317        if ( currentUserProfile != null )
 4318            currentUserProfile.snapshotObserver.RemoveListener(SetFaceSnapshot);
 319
 13320        if (mouseCatcher != null)
 13321            mouseCatcher.OnMouseDown -= OnPointerDown;
 13322    }
 323}