< Summary

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

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
PassportNavigationComponentController(...)0%2100%
>c__DisplayClass28_0/<<UpdateWithUserProfile()0%42600%
UpdateWithUserProfile(...)0%12300%
CloseAllNFTItemInfos()0%2100%
SetViewInitialPage()0%2100%
Dispose()0%20400%
LoadAndDisplayEquippedWearablesAsync()0%56700%
LoadAndShowOwnedWearables(...)0%2100%
LoadAndShowOwnedEmotes()0%30500%
LoadAndShowOwnedNamesAsync()0%56700%
LoadAndShowOwnedLandsAsync()0%56700%
FilterContentAsync()0%20400%
IsProfanityFilteringEnabled()0%2100%

File(s)

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

#LineLine coverage
 1using AvatarSystem;
 2using Cysharp.Threading.Tasks;
 3using DCL.Helpers;
 4using DCL.ProfanityFiltering;
 5using DCLServices.Lambdas.LandsService;
 6using DCLServices.Lambdas.NamesService;
 7using System;
 8using System.Collections.Generic;
 9using System.Linq;
 10using System.Threading;
 11using UnityEngine;
 12
 13namespace DCL.Social.Passports
 14{
 15    public class PassportNavigationComponentController : IDisposable
 16    {
 17        private const int MAX_NFT_COUNT = 40;
 18
 19        private readonly IProfanityFilter profanityFilter;
 20        private readonly IWearableItemResolver wearableItemResolver;
 21        private readonly IWearableCatalogBridge wearableCatalogBridge;
 22        private readonly IEmotesCatalogService emotesCatalogService;
 23        private readonly INamesService namesService;
 24        private readonly ILandsService landsService;
 25        private readonly IUserProfileBridge userProfileBridge;
 26        private readonly DataStore dataStore;
 27        private string currentUserId;
 28
 029        private UserProfile ownUserProfile => userProfileBridge.GetOwn();
 30        private readonly IPassportNavigationComponentView view;
 031        private HashSet<string> cachedAvatarEquippedWearables = new ();
 032        private readonly List<string> loadedWearables = new List<string>();
 33        public event Action<string, string> OnClickBuyNft;
 34        public event Action OnClickedLink;
 35        public event Action OnClickCollectibles;
 036        private CancellationTokenSource cts = new CancellationTokenSource();
 37        private Promise<WearableItem[]> wearablesPromise;
 38        private Promise<WearableItem[]> emotesPromise;
 39
 040        public PassportNavigationComponentController(
 41            IPassportNavigationComponentView view,
 42            IProfanityFilter profanityFilter,
 43            IWearableItemResolver wearableItemResolver,
 44            IWearableCatalogBridge wearableCatalogBridge,
 45            IEmotesCatalogService emotesCatalogService,
 46            INamesService namesService,
 47            ILandsService landsService,
 48            IUserProfileBridge userProfileBridge,
 49            DataStore dataStore)
 50        {
 051            this.view = view;
 052            this.profanityFilter = profanityFilter;
 053            this.wearableItemResolver = wearableItemResolver;
 054            this.wearableCatalogBridge = wearableCatalogBridge;
 055            this.emotesCatalogService = emotesCatalogService;
 056            this.namesService = namesService;
 057            this.landsService = landsService;
 058            this.userProfileBridge = userProfileBridge;
 059            this.dataStore = dataStore;
 060            view.OnClickBuyNft += (wearableId, wearableType) => OnClickBuyNft?.Invoke(wearableType is "name" or "parcel"
 061            view.OnClickCollectibles += () => OnClickCollectibles?.Invoke();
 062        }
 63
 64        public void UpdateWithUserProfile(UserProfile userProfile)
 65        {
 66            async UniTaskVoid UpdateWithUserProfileAsync()
 67            {
 068                var ct = cts.Token;
 069                currentUserId = userProfile.userId;
 070                wearableCatalogBridge.RemoveWearablesInUse(loadedWearables);
 071                string filteredName = await FilterContentAsync(userProfile.userName).AttachExternalCancellation(ct);
 072                view.SetGuestUser(userProfile.isGuest);
 073                view.SetName(filteredName);
 074                view.SetOwnUserTexts(userProfile.userId == ownUserProfile.userId);
 75
 076                if (!userProfile.isGuest)
 77                {
 078                    string filteredDescription = await FilterContentAsync(userProfile.description).AttachExternalCancell
 079                    view.SetDescription(filteredDescription);
 080                    view.SetHasBlockedOwnUser(userProfile.IsBlocked(ownUserProfile.userId));
 081                    LoadAndShowOwnedNamesAsync(userProfile, ct).Forget();
 082                    LoadAndShowOwnedLandsAsync(userProfile, ct).Forget();
 083                    LoadAndDisplayEquippedWearablesAsync(userProfile, ct).Forget();
 84                }
 085            }
 86
 087            cts?.Cancel();
 088            cts?.Dispose();
 089            cts = new CancellationTokenSource();
 090            UpdateWithUserProfileAsync().Forget();
 091        }
 92
 093        public void CloseAllNFTItemInfos() => view.CloseAllNFTItemInfos();
 94
 95        public void SetViewInitialPage() =>
 096            view.SetInitialPage();
 97
 98        public void Dispose()
 99        {
 0100            cts?.Cancel();
 0101            cts?.Dispose();
 0102            cts = null;
 103
 0104            wearablesPromise?.Dispose();
 0105        }
 106
 107        private async UniTask LoadAndDisplayEquippedWearablesAsync(UserProfile userProfile, CancellationToken ct)
 108        {
 0109            foreach (var t in userProfile.avatar.wearables)
 110            {
 0111                if (!cachedAvatarEquippedWearables.Contains(t))
 112                {
 0113                    view.InitializeView();
 0114                    cachedAvatarEquippedWearables = new HashSet<string>(userProfile.avatar.wearables);
 0115                    LoadAndShowOwnedWearables(userProfile);
 0116                    LoadAndShowOwnedEmotes(userProfile).Forget();
 117
 0118                    WearableItem[] wearableItems =  await wearableItemResolver.Resolve(userProfile.avatar.wearables, ct)
 0119                    view.SetEquippedWearables(wearableItems, userProfile.avatar.bodyShape);
 0120                    return;
 121                }
 122            }
 0123        }
 124
 125        private void LoadAndShowOwnedWearables(UserProfile userProfile)
 126        {
 0127            view.SetCollectibleWearablesLoadingActive(true);
 0128            wearablesPromise = wearableCatalogBridge.RequestOwnedWearables(userProfile.userId).Then(wearables =>
 129            {
 0130                IGrouping<string, WearableItem>[] wearableItems = wearables.GroupBy(i => i.id).ToArray();
 0131                string[] wearableIds = wearableItems.Select(g => g.First().id).Take(MAX_NFT_COUNT).ToArray();
 0132                userProfile.SetInventory(wearableIds);
 0133                loadedWearables.AddRange(wearableIds);
 134
 0135                var containedWearables = wearableItems
 0136                                        .Select(g => g.First())
 137                                        .Take(MAX_NFT_COUNT)
 0138                                        .Where(wearable => wearableCatalogBridge.IsValidWearable(wearable.id));
 139
 0140                view.SetCollectibleWearables(containedWearables.ToArray());
 0141                view.SetCollectibleWearablesLoadingActive(false);
 0142            });
 143
 0144            wearablesPromise.Catch(Debug.LogError);
 0145        }
 146
 147        private async UniTask LoadAndShowOwnedEmotes(UserProfile userProfile)
 148        {
 0149            view.SetCollectibleEmotesLoadingActive(true);
 150
 0151            WearableItem[] emotes = await emotesCatalogService.RequestOwnedEmotesAsync(userProfile.userId, cts.Token);
 0152            WearableItem[] emoteItems = emotes.GroupBy(i => i.id).Select(g => g.First()).Take(MAX_NFT_COUNT).ToArray();
 0153            view.SetCollectibleEmotes(emoteItems);
 0154            view.SetCollectibleEmotesLoadingActive(false);
 0155        }
 156
 157        private async UniTask LoadAndShowOwnedNamesAsync(UserProfile userProfile, CancellationToken ct)
 158        {
 0159            view.SetCollectibleNamesLoadingActive(true);
 0160            using var pagePointer = namesService.GetPaginationPointer(userProfile.userId, MAX_NFT_COUNT, CancellationTok
 0161            var response = await pagePointer.GetPageAsync(1, ct);
 0162            var namesResult = Array.Empty<NamesResponse.NameEntry>();
 163
 0164            if (response.success)
 0165                namesResult = response.response.Names.ToArray();
 166            else
 0167                Debug.LogError("Error requesting names lambdas!");
 168
 0169            view.SetCollectibleNames(namesResult);
 0170            view.SetCollectibleNamesLoadingActive(false);
 0171        }
 172
 173        private async UniTask LoadAndShowOwnedLandsAsync(UserProfile userProfile, CancellationToken ct)
 174        {
 0175            view.SetCollectibleLandsLoadingActive(true);
 176            // TODO (Santi): Use userProfile.userId here!!
 0177            using var pagePointer = landsService.GetPaginationPointer(userProfile.userId, MAX_NFT_COUNT, CancellationTok
 0178            var response = await pagePointer.GetPageAsync(1, ct);
 0179            var landsResult = Array.Empty<LandsResponse.LandEntry>();
 180
 0181            if (response.success)
 0182                landsResult = response.response.Lands.ToArray();
 183            else
 0184                Debug.LogError("Error requesting lands lambdas!");
 185
 0186            view.SetCollectibleLands(landsResult);
 0187            view.SetCollectibleLandsLoadingActive(false);
 0188        }
 189
 190        private async UniTask<string> FilterContentAsync(string filterContent) =>
 0191            IsProfanityFilteringEnabled()
 192                ? await profanityFilter.Filter(filterContent)
 193                : filterContent;
 194
 195        private bool IsProfanityFilteringEnabled() =>
 0196            dataStore.settings.profanityChatFilteringEnabled.Get();
 197    }
 198}