< 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:118
Coverable lines:118
Total lines:256
Line coverage:0% (0 of 118)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
PlayerPassportHUDController()0%2100%
PlayerPassportHUDController(...)0%2100%
ClosedGuestWalletPanel(...)0%6200%
ClosePassport()0%6200%
SetVisibility(...)0%2100%
OnCloseButtonPressed(...)0%2100%
Dispose()0%20400%
OnCurrentPlayerIdChanged(...)0%20400%
SetPassportPanelVisibility(...)0%12300%
QueryNftCollectionsAsync()0%20400%
ClickedLink()0%2100%
>c__DisplayClass33_0/<<ClickedBuyNft()0%56700%
ClickedBuyNft(...)0%72800%
OpenNftMarketUrl(...)0%2100%
ClickedCollectibles()0%2100%
UpdateUserProfile(...)0%2100%
UpdateUserProfile(...)0%2100%

File(s)

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

#LineLine coverage
 1using Cysharp.Threading.Tasks;
 2using DCL.Interface;
 3using SocialFeaturesAnalytics;
 4using System.Collections.Generic;
 5using System.Linq;
 6using System.Threading;
 7using UnityEngine;
 8
 9namespace DCL.Social.Passports
 10{
 11    public class PlayerPassportHUDController : IHUD
 12    {
 13        private const string URL_COLLECTIBLE_NAME = "https://market.decentraland.org/accounts/{userId}?section=ens";
 14        private const string URL_COLLECTIBLE_LAND = "https://market.decentraland.org/accounts/{userId}?section=land";
 15        private const string URL_BUY_SPECIFIC_COLLECTIBLE = "https://market.decentraland.org/contracts/{collectionId}/to
 16        private const string URL_COLLECTIBLE_GENERIC = "https://market.decentraland.org?utm_source=dcl_explorer";
 17        private const string NAME_TYPE = "name";
 018        private static readonly string[] ALLOWED_TYPES = { NAME_TYPE, "parcel", "estate" };
 19
 20        private readonly IPlayerPassportHUDView view;
 21        private readonly StringVariable currentPlayerId;
 22        private readonly IUserProfileBridge userProfileBridge;
 23        private readonly IPassportApiBridge passportApiBridge;
 24        private readonly ISocialAnalytics socialAnalytics;
 25        private readonly DataStore dataStore;
 26        private readonly InputAction_Trigger closeWindowTrigger;
 27        private readonly PassportPlayerInfoComponentController playerInfoController;
 28        private readonly PassportPlayerPreviewComponentController playerPreviewController;
 29        private readonly PassportNavigationComponentController passportNavigationController;
 30        private readonly BooleanVariable playerInfoCardVisibleState;
 31
 32        private UserProfile currentUserProfile;
 033        private List<Nft> ownedNftCollectionsL1 = new ();
 034        private List<Nft> ownedNftCollectionsL2 = new ();
 35        private double passportOpenStartTime;
 036        private CancellationTokenSource cts = new CancellationTokenSource();
 37
 38        private bool isOpen;
 39
 040        public PlayerPassportHUDController(
 41            IPlayerPassportHUDView view,
 42            PassportPlayerInfoComponentController playerInfoController,
 43            PassportPlayerPreviewComponentController playerPreviewController,
 44            PassportNavigationComponentController passportNavigationController,
 45            StringVariable currentPlayerId,
 46            IUserProfileBridge userProfileBridge,
 47            IPassportApiBridge passportApiBridge,
 48            ISocialAnalytics socialAnalytics,
 49            DataStore dataStore,
 50            MouseCatcher mouseCatcher,
 51            BooleanVariable playerInfoCardVisibleState)
 52        {
 053            this.view = view;
 054            this.playerInfoController = playerInfoController;
 055            this.playerPreviewController = playerPreviewController;
 056            this.passportNavigationController = passportNavigationController;
 057            this.currentPlayerId = currentPlayerId;
 058            this.userProfileBridge = userProfileBridge;
 059            this.passportApiBridge = passportApiBridge;
 060            this.socialAnalytics = socialAnalytics;
 061            this.dataStore = dataStore;
 062            this.playerInfoCardVisibleState = playerInfoCardVisibleState;
 63
 064            view.Initialize(mouseCatcher);
 065            view.OnClose += ClosePassport;
 66
 067            closeWindowTrigger = Resources.Load<InputAction_Trigger>("CloseWindow");
 068            closeWindowTrigger.OnTriggered -= OnCloseButtonPressed;
 069            closeWindowTrigger.OnTriggered += OnCloseButtonPressed;
 70
 071            passportNavigationController.OnClickBuyNft += ClickedBuyNft;
 072            passportNavigationController.OnClickedLink += ClickedLink;
 073            passportNavigationController.OnClickCollectibles += ClickedCollectibles;
 74
 075            currentPlayerId.OnChange += OnCurrentPlayerIdChanged;
 076            OnCurrentPlayerIdChanged(currentPlayerId, null);
 77
 078            playerInfoController.OnClosePassport += ClosePassport;
 079            dataStore.HUDs.closedWalletModal.OnChange += ClosedGuestWalletPanel;
 080            dataStore.HUDs.currentPassportSortingOrder.Set(view.PassportCurrentSortingOrder);
 081        }
 82
 83        private void ClosedGuestWalletPanel(bool current, bool previous)
 84        {
 085            if (current)
 86            {
 087                ClosePassport();
 088                dataStore.HUDs.closedWalletModal.Set(false, false);
 89            }
 090        }
 91
 92        private void ClosePassport()
 93        {
 094            if(!isOpen)
 095                return;
 96
 097            isOpen = false;
 98
 099            passportNavigationController.CloseAllNFTItemInfos();
 0100            passportNavigationController.SetViewInitialPage();
 0101            playerInfoController.ClosePassport();
 0102            currentPlayerId.Set(null);
 0103        }
 104
 105        /// <summary>
 106        /// Called from <see cref="HUDBridge"/>
 107        /// so it just should control the root object visibility
 108        /// </summary>
 109        public void SetVisibility(bool visible)
 110        {
 0111            view.SetVisibility(visible);
 0112        }
 113
 114        private void OnCloseButtonPressed(DCLAction_Trigger action = DCLAction_Trigger.CloseWindow)
 115        {
 0116            ClosePassport();
 0117        }
 118
 119        public void Dispose()
 120        {
 0121            cts?.Cancel();
 0122            cts?.Dispose();
 0123            cts = null;
 124
 0125            closeWindowTrigger.OnTriggered -= OnCloseButtonPressed;
 0126            currentPlayerId.OnChange -= OnCurrentPlayerIdChanged;
 0127            playerInfoController.OnClosePassport -= ClosePassport;
 0128            dataStore.HUDs.closedWalletModal.OnChange -= ClosedGuestWalletPanel;
 129
 0130            playerInfoController.Dispose();
 0131            playerPreviewController.Dispose();
 0132            passportNavigationController.Dispose();
 133
 0134            view?.Dispose();
 0135        }
 136
 137        private void OnCurrentPlayerIdChanged(string current, string previous)
 138        {
 0139            if (currentUserProfile != null)
 0140                currentUserProfile.OnUpdate -= UpdateUserProfile;
 141
 0142            ownedNftCollectionsL1 = new List<Nft>();
 0143            ownedNftCollectionsL2 = new List<Nft>();
 0144            currentUserProfile = string.IsNullOrEmpty(current)
 145                ? null
 146                : userProfileBridge.Get(current);
 147
 0148            if (currentUserProfile == null)
 149            {
 0150                socialAnalytics.SendPassportClose(Time.realtimeSinceStartup - passportOpenStartTime);
 0151                SetPassportPanelVisibility(false);
 152            }
 153            else
 154            {
 0155                SetPassportPanelVisibility(true);
 0156                passportOpenStartTime = Time.realtimeSinceStartup;
 0157                socialAnalytics.SendPassportOpen();
 0158                QueryNftCollectionsAsync(currentUserProfile.userId).Forget();
 0159                userProfileBridge.RequestFullUserProfile(currentUserProfile.userId);
 0160                currentUserProfile.OnUpdate += UpdateUserProfile;
 0161                UpdateUserProfile(currentUserProfile, true);
 162            }
 0163        }
 164
 165        private void SetPassportPanelVisibility(bool visible)
 166        {
 0167            isOpen = visible;
 168
 0169            if (visible && userProfileBridge.GetOwn().isGuest)
 170            {
 0171                dataStore.HUDs.connectWalletModalVisible.Set(true);
 172            }
 0173            playerInfoCardVisibleState.Set(visible);
 0174            view.SetPassportPanelVisibility(visible);
 0175            playerPreviewController.SetPassportPanelVisibility(visible);
 0176        }
 177
 178        private async UniTask QueryNftCollectionsAsync(string userId)
 179        {
 0180            if (string.IsNullOrEmpty(userId))
 0181                return;
 182
 0183            (ownedNftCollectionsL1, ownedNftCollectionsL2) = await UniTask.WhenAll(passportApiBridge.QueryNftCollections
 0184        }
 185
 186        private void ClickedLink()
 187        {
 0188            socialAnalytics.SendLinkClick(PlayerActionSource.Passport);
 0189        }
 190
 191        private void ClickedBuyNft(string id, string wearableType)
 192        {
 193            async UniTaskVoid QueryNftCollectionByUrnAsync(string urn)
 194            {
 0195                var nft = await passportApiBridge.QueryNftCollectionAsync(currentUserProfile.userId, urn, NftCollections
 196
 0197                if (nft == null)
 0198                    nft = await passportApiBridge.QueryNftCollectionAsync(currentUserProfile.userId, urn, NftCollections
 199
 0200                if (nft != null)
 0201                    OpenNftMarketUrl(nft);
 202                else
 0203                    passportApiBridge.OpenURL(URL_COLLECTIBLE_GENERIC);
 0204            }
 205
 0206            if (ALLOWED_TYPES.Contains(wearableType))
 207            {
 0208                passportApiBridge.OpenURL((wearableType is NAME_TYPE ? URL_COLLECTIBLE_NAME : URL_COLLECTIBLE_LAND).Repl
 0209                socialAnalytics.SendNftBuy(PlayerActionSource.Passport);
 0210                return;
 211            }
 212
 0213            var ownedCollectible = ownedNftCollectionsL1.FirstOrDefault(nft => nft.urn == id);
 0214            if (ownedCollectible == null)
 0215                ownedCollectible = ownedNftCollectionsL2.FirstOrDefault(nft => nft.urn == id);
 216
 0217            if (ownedCollectible != null)
 0218                OpenNftMarketUrl(ownedCollectible);
 219            else
 220            {
 0221                cts?.Cancel();
 0222                cts?.Dispose();
 0223                cts = new CancellationTokenSource();
 224
 225                // In case the NFT's information is not found neither on ownedNftCollectionsL1 nor or ownedNftCollection
 226                // to the TheGraph queries only return a maximum of 100 entries by default), we request the information 
 0227                QueryNftCollectionByUrnAsync(id).Forget();
 228            }
 0229        }
 230
 231        private void OpenNftMarketUrl(Nft nft)
 232        {
 0233            passportApiBridge.OpenURL(URL_BUY_SPECIFIC_COLLECTIBLE.Replace("{collectionId}", nft.collectionId).Replace("
 234            //TODO: integrate ItemType itemType once new lambdas are active
 0235            socialAnalytics.SendNftBuy(PlayerActionSource.Passport);
 0236        }
 237
 238        private void ClickedCollectibles()
 239        {
 0240            socialAnalytics.SendClickedOnCollectibles();
 0241        }
 242
 243        private void UpdateUserProfile(UserProfile userProfile)
 244        {
 0245            UpdateUserProfile(userProfile, false);
 0246        }
 247
 248        private void UpdateUserProfile(UserProfile userProfile, bool activateLoading)
 249        {
 0250            playerPreviewController.UpdateWithUserProfile(userProfile, activateLoading);
 0251            playerInfoController.UpdateWithUserProfile(userProfile);
 0252            passportNavigationController.UpdateWithUserProfile(userProfile);
 0253        }
 254
 255    }
 256}