< Summary

Class:ViewAllComponentController
Assembly:PassportHUD
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/Passport/Passport/PassportNavigation/ViewAllComponentController.cs
Covered lines:0
Uncovered lines:97
Coverable lines:97
Total lines:269
Line coverage:0% (0 of 97)
Covered branches:0
Total branches:0
Covered methods:0
Total methods:15
Method coverage:0% (0 of 15)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
ViewAllComponentController(...)0%2100%
OpenViewAllSection(...)0%2100%
SetViewAllVisibility(...)0%6200%
BackFromViewAll()0%6200%
ShowNftIcons(...)0%2100%
Dispose()0%2100%
RequestCollectibleElements(...)0%30500%
RequestOwnedWearablesAsync()0%30500%
RequestOwnedNamesAsync()0%30500%
RequestOwnedLandsAsync()0%30500%
ProcessReceivedWearables(...)0%12300%
ProcessReceivedEmotes(...)0%6200%
ProcessReceivedNames(...)0%12300%
ProcessReceivedLands(...)0%20400%
ShowErrorAndGoBack()0%2100%

File(s)

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

#LineLine coverage
 1using Cysharp.Threading.Tasks;
 2using DCL;
 3using DCL.NotificationModel;
 4using DCL.Tasks;
 5using DCLServices.Lambdas.LandsService;
 6using DCLServices.Lambdas.NamesService;
 7using DCLServices.WearablesCatalogService;
 8using System;
 9using System.Collections.Generic;
 10using System.Threading;
 11using Type = DCL.NotificationModel.Type;
 12
 13public class ViewAllComponentController : IDisposable
 14{
 15    private const string NAME_TYPE = "name";
 16    private const string EMOTE_TYPE = "emote";
 17    private const string LAND_TYPE = "land";
 18    private const string REQUEST_ERROR_MESSAGE = "There was an error while trying to process your request. Please try ag
 19
 20    public event Action OnBackFromViewAll;
 21    public delegate void ClickBuyNftDelegate(NftInfo nftInfo);
 22    public event ClickBuyNftDelegate OnClickBuyNft;
 23
 24
 25    private readonly IWearablesCatalogService wearablesCatalogService;
 26    private readonly IViewAllComponentView view;
 27    private readonly DataStore_HUDs hudsDataStore;
 28    private readonly ILandsService landsService;
 29    private readonly INamesService namesService;
 30    private readonly NotificationsController notificationsController;
 031    private CancellationTokenSource sectionsCts = new ();
 32    private bool cleanCachedWearablesPages;
 33
 034    public ViewAllComponentController(
 35        IViewAllComponentView view,
 36        DataStore_HUDs hudsDataStore,
 37        IWearablesCatalogService wearablesCatalogService,
 38        ILandsService landsService,
 39        INamesService namesService,
 40        NotificationsController notificationsController)
 41    {
 042        this.view = view;
 043        this.hudsDataStore = hudsDataStore;
 044        this.wearablesCatalogService = wearablesCatalogService;
 045        this.landsService = landsService;
 046        this.namesService = namesService;
 047        this.notificationsController = notificationsController;
 048        view.OnBackFromViewAll += BackFromViewAll;
 049        view.OnRequestCollectibleElements += RequestCollectibleElements;
 050        view.OnClickBuyNft += (nftId) => OnClickBuyNft?.Invoke(nftId);
 051    }
 52
 53    public void OpenViewAllSection(PassportSection section)
 54    {
 055        view.Initialize(section);
 056    }
 57
 58    public void SetViewAllVisibility(bool isVisible)
 59    {
 060        view.SetVisible(isVisible);
 61
 062        if (isVisible)
 63        {
 064            sectionsCts = sectionsCts.SafeRestart();
 065            cleanCachedWearablesPages = true;
 66        }
 67        else
 68        {
 069            sectionsCts.SafeCancelAndDispose();
 070            sectionsCts = null;
 71
 072            view.SetLoadingActive(false);
 73        }
 074    }
 75
 76    private void BackFromViewAll()
 77    {
 078        OnBackFromViewAll?.Invoke();
 079        SetViewAllVisibility(false);
 080        view.CloseAllNftItemInfos();
 081    }
 82
 83    private void ShowNftIcons(List<(NFTIconComponentModel model, WearableItem wearable)> iconsWithWearables)
 84    {
 085        view.ShowNftIcons(iconsWithWearables);
 086    }
 87
 88    public void Dispose()
 89    {
 090        view.OnBackFromViewAll -= BackFromViewAll;
 091        view.OnRequestCollectibleElements -= RequestCollectibleElements;
 92
 093        sectionsCts.SafeCancelAndDispose();
 094        sectionsCts = null;
 095    }
 96
 97    private void RequestCollectibleElements(PassportSection section, int pageNumber, int pageSize)
 98    {
 099        view.CloseAllNftItemInfos();
 100
 101        switch (section)
 102        {
 103            case PassportSection.Wearables:
 0104                RequestOwnedWearablesAsync(hudsDataStore.currentPlayerId.Get().playerId, pageNumber, pageSize, sectionsC
 0105                break;
 106            case PassportSection.Names:
 0107                RequestOwnedNamesAsync(hudsDataStore.currentPlayerId.Get().playerId, pageNumber, pageSize, sectionsCts.T
 0108                break;
 109            case PassportSection.Lands:
 0110                RequestOwnedLandsAsync(hudsDataStore.currentPlayerId.Get().playerId, pageNumber, pageSize, sectionsCts.T
 111                break;
 112        }
 0113    }
 114
 115    private async UniTask RequestOwnedWearablesAsync(string userId, int pageNumber, int pageSize, CancellationToken ct)
 116    {
 117        try
 118        {
 0119            view.SetLoadingActive(true);
 120
 0121            (IReadOnlyList<WearableItem> wearables, int totalAmount) ownedWearableItems =
 122                await wearablesCatalogService.RequestOwnedWearablesAsync(userId, pageNumber, pageSize, cleanCachedWearab
 123
 0124            cleanCachedWearablesPages = false;
 0125            ProcessReceivedWearables(ownedWearableItems.wearables);
 0126            view.SetTotalElements(ownedWearableItems.totalAmount);
 0127            view.SetLoadingActive(false);
 0128        }
 0129        catch (Exception e) when (e is not OperationCanceledException)
 130        {
 0131            ShowErrorAndGoBack();
 0132        }
 0133    }
 134
 135    private async UniTask RequestOwnedNamesAsync(string userId, int pageNumber, int pageSize, CancellationToken ct)
 136    {
 137        try
 138        {
 0139            view.SetLoadingActive(true);
 140
 0141            (IReadOnlyList<NamesResponse.NameEntry> names, int totalAmount) ownedNamesItems =
 142                await namesService.RequestOwnedNamesAsync(userId, pageNumber, pageSize, cleanCachedWearablesPages, ct);
 143
 0144            cleanCachedWearablesPages = false;
 0145            ProcessReceivedNames(ownedNamesItems.names);
 0146            view.SetTotalElements(ownedNamesItems.totalAmount);
 0147            view.SetLoadingActive(false);
 0148        }
 0149        catch (Exception e) when (e is not OperationCanceledException)
 150        {
 0151            ShowErrorAndGoBack();
 0152        }
 0153    }
 154
 155    private async UniTask RequestOwnedLandsAsync(string userId, int pageNumber, int pageSize, CancellationToken ct)
 156    {
 157        try
 158        {
 0159            view.SetLoadingActive(true);
 160
 0161            (IReadOnlyList<LandsResponse.LandEntry> lands, int totalAmount) ownedLandsItems =
 162                await landsService.RequestOwnedLandsAsync(userId, pageNumber, pageSize, cleanCachedWearablesPages, ct);
 163
 0164            cleanCachedWearablesPages = false;
 0165            ProcessReceivedLands(ownedLandsItems.lands);
 0166            view.SetTotalElements(ownedLandsItems.totalAmount);
 0167            view.SetLoadingActive(false);
 0168        }
 0169        catch (Exception e) when (e is not OperationCanceledException)
 170        {
 0171            ShowErrorAndGoBack();
 0172        }
 0173    }
 174
 175    private void ProcessReceivedWearables(IReadOnlyList<WearableItem> wearables)
 176    {
 0177        List<(NFTIconComponentModel Model, WearableItem w)> wearableModels = new List<(NFTIconComponentModel Model, Wear
 0178        foreach (var wearable in wearables)
 179        {
 0180            bool isWearableCollectible = wearable.IsCollectible();
 181
 0182            wearableModels.Add((new NFTIconComponentModel
 183            {
 184                showMarketplaceButton = isWearableCollectible,
 185                showType = isWearableCollectible,
 186                type = wearable.data.category,
 187                marketplaceURI = "",
 188                name = wearable.GetName(),
 189                rarity = wearable.rarity,
 190                imageURI = wearable.ComposeThumbnailUrl(),
 191                nftInfo = wearable.GetNftInfo(),
 192            }, wearable));
 193        }
 0194        ShowNftIcons(wearableModels);
 0195    }
 196
 197    private void ProcessReceivedEmotes(WearableItem[] emotes)
 198    {
 0199        List<(NFTIconComponentModel Model, WearableItem w)> emoteModels = new List<(NFTIconComponentModel Model, Wearabl
 0200        foreach (var emote in emotes)
 201        {
 0202            emoteModels.Add((new NFTIconComponentModel
 203            {
 204                showMarketplaceButton = true,
 205                showType = true,
 206                type = EMOTE_TYPE,
 207                marketplaceURI = "",
 208                name = emote.GetName(),
 209                rarity = emote.rarity,
 210                imageURI = emote.ComposeThumbnailUrl(),
 211                nftInfo = emote.GetNftInfo(),
 212            }, emote));
 213        }
 0214        ShowNftIcons(emoteModels);
 0215    }
 216
 217    private void ProcessReceivedNames(IReadOnlyList<NamesResponse.NameEntry> names)
 218    {
 0219        List<(NFTIconComponentModel Model, WearableItem w)> nameModels = new List<(NFTIconComponentModel Model, Wearable
 0220        foreach (var name in names)
 221        {
 0222            nameModels.Add((new NFTIconComponentModel
 223            {
 224                showMarketplaceButton = true,
 225                showType = true,
 226                type = NAME_TYPE,
 227                marketplaceURI = "",
 228                name = name.Name,
 229                rarity = NAME_TYPE,
 230                imageURI = "",
 231                nftInfo = name.GetNftInfo(),
 232            }, null));
 233        }
 0234        ShowNftIcons(nameModels);
 0235    }
 236
 237    private void ProcessReceivedLands(IReadOnlyList<LandsResponse.LandEntry> lands)
 238    {
 0239        List<(NFTIconComponentModel Model, WearableItem w)> landModels = new List<(NFTIconComponentModel Model, Wearable
 0240        foreach (var land in lands)
 241        {
 0242            landModels.Add((new()
 243            {
 244                showMarketplaceButton = true,
 245                showType = true,
 246                type = land.Category,
 247                marketplaceURI = "",
 248                name = !string.IsNullOrEmpty(land.Name) ? land.Name : land.Category,
 249                rarity = LAND_TYPE,
 250                imageURI = land.Image,
 251                nftInfo = land.GetNftInfo(),
 252            }, null));
 253        }
 0254        ShowNftIcons(landModels);
 0255    }
 256
 257    private void ShowErrorAndGoBack()
 258    {
 0259        notificationsController.ShowNotification(new Model
 260        {
 261            message = REQUEST_ERROR_MESSAGE,
 262            type = Type.ERROR,
 263            timer = 10f,
 264            destroyOnFinish = true,
 265        });
 266
 0267        BackFromViewAll();
 0268    }
 269}