< Summary

Class:BIWSearchBarController
Assembly:BuildModeHUD
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/BuilderInWorld/HUD/BuildModeHUD/Scripts/Common/BIWSearchBarController.cs
Covered lines:49
Uncovered lines:31
Coverable lines:80
Total lines:188
Line coverage:61.2% (49 of 80)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
BIWSearchBarController()0%2100%
Initialize(...)0%330100%
Dispose()0%3.583060%
IsFilterActive()0%2100%
OnSearchInputChanged(...)0%42600%
ReleaseFilters()0%110100%
ChangeSmartItemFilter()0%3.713057.14%
FilterSmartItem()0%9.638070.59%
FilterAssets(...)0%8.257070.59%
MatchtFilter(...)0%550100%
AddNewSceneObjectCategoryToFilter(...)0%110100%
ReportSearchAnalytic()0%12300%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/BuilderInWorld/HUD/BuildModeHUD/Scripts/Common/BIWSearchBarController.cs

#LineLine coverage
 1using System;
 2using System.Collections;
 3using System.Collections.Generic;
 4using DCL;
 5using UnityEngine;
 6
 7public interface IBIWSearchBarController
 8{
 9    event Action<List<Dictionary<string, List<CatalogItem>>>> OnFilterChange;
 10    event Action OnFilterRemove;
 11    bool IsFilterActive();
 12    void Dispose();
 13    List<Dictionary<string, List<CatalogItem>>> FilterAssets(string nameToFilter);
 14}
 15
 16public class BIWSearchBarController : IBIWSearchBarController
 17{
 18    private const float SEARCH_DELAY_OFFSET = 1f;
 19    public event Action<List<Dictionary<string, List<CatalogItem>>>> OnFilterChange;
 20    public event Action OnFilterRemove;
 21
 022    internal List<Dictionary<string, List<CatalogItem>>> filterObjects = new List<Dictionary<string, List<CatalogItem>>>
 23
 24    private IBIWSearchBarView view;
 25
 26    private bool isSmartItemFilterActive = false;
 27    private bool isFilterActive = false;
 28
 29    private string currentSearchFilter;
 30    private Coroutine searchApplyCoroutine;
 31
 32    public void Initialize(ISceneCatalogView sceneCatalogView)
 33    {
 3134        this.view = sceneCatalogView.searchBarView;
 3135        if (this.view.searchInput != null)
 136            this.view.searchInput.onValueChanged.AddListener(OnSearchInputChanged);
 37
 3138        if (this.view.smartItemBtn != null)
 139            this.view.smartItemBtn.onClick.AddListener(ChangeSmartItemFilter);
 3140    }
 41
 42    public void Dispose()
 43    {
 3044        if (view.searchInput != null)
 045            view.searchInput.onValueChanged.RemoveListener(OnSearchInputChanged);
 46
 3047        if (view.smartItemBtn != null)
 048            view.smartItemBtn.onClick.RemoveListener(ChangeSmartItemFilter);
 3049    }
 50
 051    public bool IsFilterActive() { return isFilterActive; }
 52
 53    public void OnSearchInputChanged(string currentSearchInput)
 54    {
 055        if (string.IsNullOrEmpty(currentSearchInput))
 56        {
 057            OnFilterRemove?.Invoke();
 058            return;
 59        }
 60
 061        if (currentSearchInput.Length <= 1)
 062            return;
 063        currentSearchFilter = currentSearchInput;
 64
 065        isFilterActive = true;
 66
 067        FilterAssets(currentSearchInput);
 068        OnFilterChange?.Invoke(filterObjects);
 069        if (searchApplyCoroutine != null)
 070            CoroutineStarter.Stop(searchApplyCoroutine);
 071        searchApplyCoroutine = CoroutineStarter.Start(ReportSearchAnalytic());
 072    }
 73
 74    public void ReleaseFilters()
 75    {
 676        isSmartItemFilterActive = false;
 677        filterObjects.Clear();
 678        view.SetSmartItemPressStatus(false);
 679        view.SetEmptyFilter();
 80
 681        isFilterActive = false;
 682    }
 83
 84    public void ChangeSmartItemFilter()
 85    {
 286        isSmartItemFilterActive = !isSmartItemFilterActive;
 287        view.SetSmartItemPressStatus(isSmartItemFilterActive);
 88
 289        if (isSmartItemFilterActive)
 290            FilterSmartItem();
 91        else
 92        {
 093            isFilterActive = false;
 094            OnFilterRemove?.Invoke();
 95        }
 96
 097    }
 98
 99    public void FilterSmartItem()
 100    {
 2101        filterObjects.Clear();
 8102        foreach (CatalogItem catalogItem in DataStore.i.builderInWorld.catalogItemDict.GetValues())
 103        {
 2104            if (!catalogItem.IsSmartItem())
 105                continue;
 106
 1107            bool foundCategory = false;
 2108            foreach (Dictionary<string, List<CatalogItem>> groupedSceneObjects in filterObjects)
 109            {
 0110                if (groupedSceneObjects.ContainsKey(catalogItem.category))
 111                {
 0112                    foundCategory = true;
 0113                    if (!groupedSceneObjects[catalogItem.category].Contains(catalogItem))
 0114                        groupedSceneObjects[catalogItem.category].Add(catalogItem);
 115                }
 116            }
 1117            if (!foundCategory)
 118            {
 1119                AddNewSceneObjectCategoryToFilter(catalogItem);
 120            }
 121        }
 122
 2123        OnFilterChange?.Invoke(filterObjects);
 2124    }
 125
 126    public List<Dictionary<string, List<CatalogItem>>> FilterAssets(string nameToFilter)
 127    {
 6128        StringComparison comparison = StringComparison.OrdinalIgnoreCase;
 6129        filterObjects.Clear();
 24130        foreach (CatalogItem catalogItem in DataStore.i.builderInWorld.catalogItemDict.GetValues())
 131        {
 6132            if (!MatchtFilter(catalogItem, nameToFilter, comparison))
 133                continue;
 134
 3135            bool foundCategory = false;
 6136            foreach (Dictionary<string, List<CatalogItem>> groupedSceneObjects in filterObjects)
 137            {
 0138                if (groupedSceneObjects.ContainsKey(catalogItem.category))
 139                {
 0140                    foundCategory = true;
 0141                    if (!groupedSceneObjects[catalogItem.category].Contains(catalogItem))
 0142                        groupedSceneObjects[catalogItem.category].Add(catalogItem);
 143                }
 144            }
 3145            if (!foundCategory)
 146            {
 3147                AddNewSceneObjectCategoryToFilter(catalogItem);
 148            }
 149        }
 6150        return filterObjects;
 151    }
 152
 153    private bool MatchtFilter(CatalogItem catalogItem, string nameToFilter, StringComparison comparison)
 154    {
 6155        string nameToFilterToLower = nameToFilter.ToLower();
 156
 157        // NOTE: Due to an Unity known issue, the use of 'StringComparison.OrdinalIgnoreCase' in WebGL is case sensitive
 158        // Absurd as it may seem, Unity says it is working in this way "by design", so it seems they're not going to fix
 159        // A work-around is to use '.ToLower()' in both strings.
 160        // More info: https://issuetracker.unity3d.com/issues/webgl-build-system-dot-stringcomparison-dot-ordinalignorec
 6161        if (catalogItem.category.ToLower().IndexOf(nameToFilterToLower, comparison) != -1 ||
 162            catalogItem.name.ToLower().IndexOf(nameToFilterToLower, comparison) != -1)
 2163            return true;
 164
 21165        foreach (string tag in catalogItem.tags)
 166        {
 7167            if (tag.ToLower().IndexOf(nameToFilterToLower, comparison) != -1)
 168            {
 1169                return true;
 170            }
 171        }
 172
 3173        return false;
 1174    }
 175
 176    internal void AddNewSceneObjectCategoryToFilter(CatalogItem catalogItem)
 177    {
 5178        Dictionary<string, List<CatalogItem>> groupedCatalogItems = new Dictionary<string, List<CatalogItem>>();
 5179        groupedCatalogItems.Add(catalogItem.category, new List<CatalogItem>() { catalogItem });
 5180        filterObjects.Add(groupedCatalogItems);
 5181    }
 182
 183    IEnumerator ReportSearchAnalytic()
 184    {
 0185        yield return new WaitForSecondsRealtime(SEARCH_DELAY_OFFSET);
 0186        BIWAnalytics.CatalogItemSearched(currentSearchFilter, filterObjects.Count);
 0187    }
 188}