< Summary

Class:DCL.Backpack.BackpackFiltersController
Assembly:BackpackEditorHUDV2
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/BackpackEditorHUDV2/BackpackFiltersController.cs
Covered lines:67
Uncovered lines:4
Coverable lines:71
Total lines:155
Line coverage:94.3% (67 of 71)
Covered branches:0
Total branches:0
Covered methods:13
Total methods:13
Method coverage:100% (13 of 13)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
BackpackFiltersController(...)0%110100%
Dispose()0%110100%
LoadCollections()0%2.032080%
<LoadCollections()0%5.795068.42%
ClearTextSearch(...)0%110100%
SetTextSearch(...)0%110100%
SelectCollections(...)0%660100%
SetSorting(...)0%110100%
SetOnlyCollectibles(...)0%330100%
SetInternalStateOfCollectionsAndTriggerStateChange(...)0%110100%
SetInternalStateOfCollectionsAndTriggerStateChange(...)0%6.026091.67%
TriggerSorting(...)0%220100%
TriggerTextSearch(...)0%220100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/BackpackEditorHUDV2/BackpackFiltersController.cs

#LineLine coverage
 1using Cysharp.Threading.Tasks;
 2using DCL.Tasks;
 3using DCLServices.WearablesCatalogService;
 4using System;
 5using System.Collections.Generic;
 6using System.Threading;
 7using UnityEngine;
 8
 9namespace DCL.Backpack
 10{
 11    public class BackpackFiltersController
 12    {
 13        private const string DECENTRALAND_COLLECTION_ID = "decentraland";
 14
 15        public event Action<HashSet<string>> OnThirdPartyCollectionChanged;
 16        public event Action<(NftOrderByOperation type, bool directionAscendent)> OnSortByChanged;
 17        public event Action<string> OnSearchTextChanged;
 18        public event Action<NftCollectionType> OnCollectionTypeChanged;
 19
 20        private readonly IBackpackFiltersComponentView view;
 21        private readonly IWearablesCatalogService wearablesCatalogService;
 22        private bool collectionsAlreadyLoaded;
 7523        private HashSet<string> selectedCollections = new();
 7524        private HashSet<string> loadedCollections = new ();
 7525        private NftCollectionType collectionType = NftCollectionType.OnChain | NftCollectionType.Base;
 7526        private CancellationTokenSource loadThirdPartyCollectionsCancellationToken = new ();
 27
 7528        public BackpackFiltersController(
 29            IBackpackFiltersComponentView view,
 30            IWearablesCatalogService wearablesCatalogService)
 31        {
 7532            this.view = view;
 7533            this.wearablesCatalogService = wearablesCatalogService;
 34
 7535            view.OnOnlyCollectiblesChanged += SetOnlyCollectibles;
 7536            view.OnCollectionChanged += SetInternalStateOfCollectionsAndTriggerStateChange;
 7537            view.OnSortByChanged += TriggerSorting;
 7538            view.OnSearchTextChanged += TriggerTextSearch;
 7539        }
 40
 41        public void Dispose()
 42        {
 7543            view.OnOnlyCollectiblesChanged -= SetOnlyCollectibles;
 7544            view.OnCollectionChanged -= SetInternalStateOfCollectionsAndTriggerStateChange;
 7545            view.OnSortByChanged -= TriggerSorting;
 7546            view.OnSearchTextChanged -= TriggerTextSearch;
 47
 7548            view.Dispose();
 7549        }
 50
 51        public void LoadCollections()
 52        {
 1053            if (collectionsAlreadyLoaded)
 054                return;
 55
 56            async UniTaskVoid FetchTPWAndLoadDropdown(CancellationToken cancellationToken)
 57            {
 58                try
 59                {
 1060                    WearableCollectionsAPIData.Collection[] collections = await wearablesCatalogService.GetThirdPartyCol
 1061                    WearableCollectionsAPIData.Collection defaultCollection = new () { urn = DECENTRALAND_COLLECTION_ID,
 62
 1063                    loadedCollections.Clear();
 1064                    loadedCollections.Add(defaultCollection.urn);
 1065                    if (collections != null)
 66                    {
 867                        foreach (var collection in collections)
 368                            loadedCollections.Add(collection.urn);
 69                    }
 70
 1071                    view.LoadCollectionDropdown(collections, defaultCollection);
 1072                    collectionsAlreadyLoaded = true;
 1073                }
 074                catch (OperationCanceledException) { }
 075                catch (Exception e) { Debug.LogException(e); }
 1076            }
 77
 1078            loadThirdPartyCollectionsCancellationToken = loadThirdPartyCollectionsCancellationToken.SafeRestart();
 1079            FetchTPWAndLoadDropdown(loadThirdPartyCollectionsCancellationToken.Token).Forget();
 1080        }
 81
 82        public void ClearTextSearch(bool notify = true) =>
 4183            view.SetSearchText(null, notify);
 84
 85        public void SetTextSearch(string text, bool notify = true) =>
 586            view.SetSearchText(text, notify);
 87
 88        public void SelectCollections(NftCollectionType collectionTypeMask,
 89            ICollection<string> thirdPartyCollectionIdsFilter = null,
 90            bool notify = true)
 91        {
 4692            HashSet<string> newSelectedCollections = new ();
 93
 4694            if ((collectionTypeMask & NftCollectionType.OnChain) != 0)
 4695                newSelectedCollections.Add(DECENTRALAND_COLLECTION_ID);
 96
 4697            if ((collectionTypeMask & NftCollectionType.ThirdParty) != 0 && thirdPartyCollectionIdsFilter != null)
 898                foreach (string tpw in thirdPartyCollectionIdsFilter)
 399                    newSelectedCollections.Add(tpw);
 100
 46101            bool isOnlyCollectiblesOn = (collectionTypeMask & NftCollectionType.Base) == 0;
 46102            view.SetOnlyCollectiblesToggleIsOn(isOnlyCollectiblesOn, notify);
 46103            view.SelectDropdownCollections(newSelectedCollections, notify);
 104
 46105            collectionType = collectionTypeMask;
 46106            selectedCollections = newSelectedCollections;
 46107        }
 108
 109        public void SetSorting(NftOrderByOperation type, bool directionAscending, bool notify) =>
 23110            view.SetSorting(type, directionAscending, notify);
 111
 112        private void SetOnlyCollectibles(bool isOn)
 113        {
 2114            if (isOn)
 1115                collectionType &= ~NftCollectionType.Base;
 116            else
 1117                collectionType |= NftCollectionType.Base;
 118
 2119            OnCollectionTypeChanged?.Invoke(collectionType);
 2120        }
 121
 122        private void SetInternalStateOfCollectionsAndTriggerStateChange(HashSet<string> newSelectedCollections) =>
 2123            SetInternalStateOfCollectionsAndTriggerStateChange(newSelectedCollections, true);
 124
 125        private void SetInternalStateOfCollectionsAndTriggerStateChange(HashSet<string> newSelectedCollections, bool not
 126        {
 2127            if (newSelectedCollections.Contains(DECENTRALAND_COLLECTION_ID))
 128            {
 1129                collectionType |= NftCollectionType.OnChain;
 1130                newSelectedCollections.Remove(DECENTRALAND_COLLECTION_ID);
 131            }
 132            else
 1133                collectionType &= ~NftCollectionType.OnChain;
 134
 2135            selectedCollections = newSelectedCollections;
 136
 2137            if (newSelectedCollections.Count > 0)
 2138                collectionType |= NftCollectionType.ThirdParty;
 139            else
 0140                collectionType &= ~NftCollectionType.ThirdParty;
 141
 2142            if (notify)
 143            {
 2144                OnCollectionTypeChanged?.Invoke(collectionType);
 2145                OnThirdPartyCollectionChanged?.Invoke(selectedCollections);
 146            }
 2147        }
 148
 149        private void TriggerSorting((NftOrderByOperation type, bool directionAscendent) newSorting) =>
 7150            OnSortByChanged?.Invoke(newSorting);
 151
 152        private void TriggerTextSearch(string newText) =>
 2153            OnSearchTextChanged?.Invoke(newText);
 154    }
 155}