< Summary

Class:DCL.Social.Passports.PlayerPassportHUDController
Assembly:PassportHUD
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/Passport/Passport/PlayerPassportHUDController.cs
Covered lines:0
Uncovered lines:69
Coverable lines:69
Total lines:164
Line coverage:0% (0 of 69)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
PlayerPassportHUDController(...)0%2100%
ClosePassport()0%2100%
SetVisibility(...)0%2100%
OnCloseButtonPressed(...)0%2100%
Dispose()0%6200%
OnCurrentPlayerIdChanged(...)0%20400%
SetPassportPanelVisibility(...)0%2100%
QueryNftCollectionsAsync()0%42600%
ClickedBuyNft(...)0%12300%
UpdateUserProfile(...)0%2100%
UpdateUserProfileInSubpanels(...)0%2100%
RemoveCurrentPlayer()0%2100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/Passport/Passport/PlayerPassportHUDController.cs

#LineLine coverage
 1using System.Collections.Generic;
 2using System.Linq;
 3using UnityEngine;
 4using DCL.Interface;
 5using SocialFeaturesAnalytics;
 6using System.Threading.Tasks;
 7using System;
 8
 9namespace DCL.Social.Passports
 10{
 11    public class PlayerPassportHUDController : IHUD
 12    {
 13        internal readonly IPlayerPassportHUDView view;
 14        internal readonly StringVariable currentPlayerId;
 15        internal readonly IUserProfileBridge userProfileBridge;
 16        private readonly IPassportApiBridge passportApiBridge;
 17        private readonly ISocialAnalytics socialAnalytics;
 18
 19        internal UserProfile currentUserProfile;
 20
 21        private const string URL_BUY_SPECIFIC_COLLECTIBLE = "https://market.decentraland.org/contracts/{collectionId}/to
 22        private const string URL_COLLECTIBLE_GENERIC = "https://market.decentraland.org/account";
 23        private readonly InputAction_Trigger closeWindowTrigger;
 24
 25        private PassportPlayerInfoComponentController playerInfoController;
 26        private PassportPlayerPreviewComponentController playerPreviewController;
 27        private PassportNavigationComponentController passportNavigationController;
 28
 029        private List<Nft> ownedNftCollectionsL1 = new List<Nft>();
 030        private List<Nft> ownedNftCollectionsL2 = new List<Nft>();
 31
 032        public PlayerPassportHUDController(
 33            IPlayerPassportHUDView view,
 34            PassportPlayerInfoComponentController playerInfoController,
 35            PassportPlayerPreviewComponentController playerPreviewController,
 36            PassportNavigationComponentController passportNavigationController,
 37            StringVariable currentPlayerId,
 38            IUserProfileBridge userProfileBridge,
 39            IPassportApiBridge passportApiBridge,
 40            ISocialAnalytics socialAnalytics)
 41        {
 042            this.view = view;
 043            this.playerInfoController = playerInfoController;
 044            this.playerPreviewController = playerPreviewController;
 045            this.passportNavigationController = passportNavigationController;
 046            this.currentPlayerId = currentPlayerId;
 047            this.userProfileBridge = userProfileBridge;
 048            this.passportApiBridge = passportApiBridge;
 049            this.socialAnalytics = socialAnalytics;
 50
 051            view.Initialize();
 052            view.OnClose += RemoveCurrentPlayer;
 53
 054            closeWindowTrigger = Resources.Load<InputAction_Trigger>("CloseWindow");
 055            closeWindowTrigger.OnTriggered -= OnCloseButtonPressed;
 056            closeWindowTrigger.OnTriggered += OnCloseButtonPressed;
 57
 058            passportNavigationController.OnClickBuyNft += ClickedBuyNft;
 59
 060            currentPlayerId.OnChange += OnCurrentPlayerIdChanged;
 061            OnCurrentPlayerIdChanged(currentPlayerId, null);
 62
 063            playerInfoController.OnClosePassport += ClosePassport;
 064        }
 65
 66        private void ClosePassport()
 67        {
 068            RemoveCurrentPlayer();
 069        }
 70
 71        /// <summary>
 72        /// Called from <see cref="HUDBridge"/>
 73        /// so it just should control the root object visibility
 74        /// </summary>
 75        public void SetVisibility(bool visible)
 76        {
 077            view.SetVisibility(visible);
 078        }
 79
 80        private void OnCloseButtonPressed(DCLAction_Trigger action = DCLAction_Trigger.CloseWindow)
 81        {
 082            ClosePassport();
 083        }
 84
 85        public void Dispose()
 86        {
 087            closeWindowTrigger.OnTriggered -= OnCloseButtonPressed;
 088            currentPlayerId.OnChange -= OnCurrentPlayerIdChanged;
 089            playerInfoController.OnClosePassport -= ClosePassport;
 90
 091            playerPreviewController.Dispose();
 92
 093            if (view != null)
 094                view.Dispose();
 095        }
 96
 97        private void OnCurrentPlayerIdChanged(string current, string previous)
 98        {
 099            if (currentUserProfile != null)
 0100                currentUserProfile.OnUpdate -= UpdateUserProfile;
 101
 0102            ownedNftCollectionsL1 = new List<Nft>();
 0103            ownedNftCollectionsL2 = new List<Nft>();
 0104            currentUserProfile = string.IsNullOrEmpty(current)
 105                ? null
 106                : userProfileBridge.Get(current);
 107
 0108            if (currentUserProfile == null)
 109            {
 0110                SetPassportPanelVisibility(false);
 111            }
 112            else
 113            {
 0114                SetPassportPanelVisibility(true);
 0115                QueryNftCollectionsAsync(currentUserProfile.userId);
 0116                userProfileBridge.RequestFullUserProfile(currentUserProfile.userId);
 0117                currentUserProfile.OnUpdate += UpdateUserProfile;
 0118                UpdateUserProfileInSubpanels(currentUserProfile);
 119            }
 0120        }
 121
 122        private void SetPassportPanelVisibility(bool visible)
 123        {
 0124            view.SetPassportPanelVisibility(visible);
 0125            playerPreviewController.SetPassportPanelVisibility(visible);
 0126        }
 127
 128        private async Task QueryNftCollectionsAsync(string userId)
 129        {
 0130            if (string.IsNullOrEmpty(userId))
 0131                return;
 132
 0133            ownedNftCollectionsL1 = await passportApiBridge.QueryNftCollectionsEthereum(userId);
 0134            ownedNftCollectionsL2 = await passportApiBridge.QueryNftCollectionsMatic(userId);
 0135        }
 136
 137        private void ClickedBuyNft(string wearableId)
 138        {
 0139            var ownedCollectible = ownedNftCollectionsL1.FirstOrDefault(nft => nft.urn == wearableId);
 0140            if (ownedCollectible == null)
 0141                ownedCollectible = ownedNftCollectionsL2.FirstOrDefault(nft => nft.urn == wearableId);
 142
 0143            if (ownedCollectible != null)
 0144                WebInterface.OpenURL(URL_BUY_SPECIFIC_COLLECTIBLE.Replace("{collectionId}", ownedCollectible.collectionI
 145            else
 0146                WebInterface.OpenURL(URL_COLLECTIBLE_GENERIC);
 0147        }
 148
 0149        private void UpdateUserProfile(UserProfile userProfile) => UpdateUserProfileInSubpanels(userProfile);
 150
 151        private void UpdateUserProfileInSubpanels(UserProfile userProfile)
 152        {
 0153            playerInfoController.UpdateWithUserProfile(userProfile);
 0154            passportNavigationController.UpdateWithUserProfile(userProfile);
 0155            playerPreviewController.UpdateWithUserProfile(userProfile);
 0156        }
 157
 158        private void RemoveCurrentPlayer()
 159        {
 0160            currentPlayerId.Set(null);
 0161        }
 162
 163    }
 164}