< Summary

Class:BIWSearchBarController
Assembly:BuildModeHUD
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/BuilderInWorld/HUD/BuildModeHUD/Scripts/Common/BIWSearchBarController.cs
Covered lines:50
Uncovered lines:32
Coverable lines:82
Total lines:191
Line coverage:60.9% (50 of 82)
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%5.264057.14%
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 == null)
 045            return;
 46
 3047        if (view.searchInput != null)
 048            view.searchInput.onValueChanged.RemoveListener(OnSearchInputChanged);
 49
 3050        if (view.smartItemBtn != null)
 051            view.smartItemBtn.onClick.RemoveListener(ChangeSmartItemFilter);
 3052    }
 53
 054    public bool IsFilterActive() { return isFilterActive; }
 55
 56    public void OnSearchInputChanged(string currentSearchInput)
 57    {
 058        if (string.IsNullOrEmpty(currentSearchInput))
 59        {
 060            OnFilterRemove?.Invoke();
 061            return;
 62        }
 63
 064        if (currentSearchInput.Length <= 1)
 065            return;
 066        currentSearchFilter = currentSearchInput;
 67
 068        isFilterActive = true;
 69
 070        FilterAssets(currentSearchInput);
 071        OnFilterChange?.Invoke(filterObjects);
 072        if (searchApplyCoroutine != null)
 073            CoroutineStarter.Stop(searchApplyCoroutine);
 074        searchApplyCoroutine = CoroutineStarter.Start(ReportSearchAnalytic());
 075    }
 76
 77    public void ReleaseFilters()
 78    {
 679        isSmartItemFilterActive = false;
 680        filterObjects.Clear();
 681        view.SetSmartItemPressStatus(false);
 682        view.SetEmptyFilter();
 83
 684        isFilterActive = false;
 685    }
 86
 87    public void ChangeSmartItemFilter()
 88    {
 289        isSmartItemFilterActive = !isSmartItemFilterActive;
 290        view.SetSmartItemPressStatus(isSmartItemFilterActive);
 91
 292        if (isSmartItemFilterActive)
 293            FilterSmartItem();
 94        else
 95        {
 096            isFilterActive = false;
 097            OnFilterRemove?.Invoke();
 98        }
 99
 0100    }
 101
 102    public void FilterSmartItem()
 103    {
 2104        filterObjects.Clear();
 8105        foreach (CatalogItem catalogItem in DataStore.i.builderInWorld.catalogItemDict.GetValues())
 106        {
 2107            if (!catalogItem.IsSmartItem())
 108                continue;
 109
 1110            bool foundCategory = false;
 2111            foreach (Dictionary<string, List<CatalogItem>> groupedSceneObjects in filterObjects)
 112            {
 0113                if (groupedSceneObjects.ContainsKey(catalogItem.category))
 114                {
 0115                    foundCategory = true;
 0116                    if (!groupedSceneObjects[catalogItem.category].Contains(catalogItem))
 0117                        groupedSceneObjects[catalogItem.category].Add(catalogItem);
 118                }
 119            }
 1120            if (!foundCategory)
 121            {
 1122                AddNewSceneObjectCategoryToFilter(catalogItem);
 123            }
 124        }
 125
 2126        OnFilterChange?.Invoke(filterObjects);
 2127    }
 128
 129    public List<Dictionary<string, List<CatalogItem>>> FilterAssets(string nameToFilter)
 130    {
 6131        StringComparison comparison = StringComparison.OrdinalIgnoreCase;
 6132        filterObjects.Clear();
 24133        foreach (CatalogItem catalogItem in DataStore.i.builderInWorld.catalogItemDict.GetValues())
 134        {
 6135            if (!MatchtFilter(catalogItem, nameToFilter, comparison))
 136                continue;
 137
 3138            bool foundCategory = false;
 6139            foreach (Dictionary<string, List<CatalogItem>> groupedSceneObjects in filterObjects)
 140            {
 0141                if (groupedSceneObjects.ContainsKey(catalogItem.category))
 142                {
 0143                    foundCategory = true;
 0144                    if (!groupedSceneObjects[catalogItem.category].Contains(catalogItem))
 0145                        groupedSceneObjects[catalogItem.category].Add(catalogItem);
 146                }
 147            }
 3148            if (!foundCategory)
 149            {
 3150                AddNewSceneObjectCategoryToFilter(catalogItem);
 151            }
 152        }
 6153        return filterObjects;
 154    }
 155
 156    private bool MatchtFilter(CatalogItem catalogItem, string nameToFilter, StringComparison comparison)
 157    {
 6158        string nameToFilterToLower = nameToFilter.ToLower();
 159
 160        // NOTE: Due to an Unity known issue, the use of 'StringComparison.OrdinalIgnoreCase' in WebGL is case sensitive
 161        // Absurd as it may seem, Unity says it is working in this way "by design", so it seems they're not going to fix
 162        // A work-around is to use '.ToLower()' in both strings.
 163        // More info: https://issuetracker.unity3d.com/issues/webgl-build-system-dot-stringcomparison-dot-ordinalignorec
 6164        if (catalogItem.category.ToLower().IndexOf(nameToFilterToLower, comparison) != -1 ||
 165            catalogItem.name.ToLower().IndexOf(nameToFilterToLower, comparison) != -1)
 2166            return true;
 167
 21168        foreach (string tag in catalogItem.tags)
 169        {
 7170            if (tag.ToLower().IndexOf(nameToFilterToLower, comparison) != -1)
 171            {
 1172                return true;
 173            }
 174        }
 175
 3176        return false;
 1177    }
 178
 179    internal void AddNewSceneObjectCategoryToFilter(CatalogItem catalogItem)
 180    {
 5181        Dictionary<string, List<CatalogItem>> groupedCatalogItems = new Dictionary<string, List<CatalogItem>>();
 5182        groupedCatalogItems.Add(catalogItem.category, new List<CatalogItem>() { catalogItem });
 5183        filterObjects.Add(groupedCatalogItems);
 5184    }
 185
 186    IEnumerator ReportSearchAnalytic()
 187    {
 0188        yield return new WaitForSecondsRealtime(SEARCH_DELAY_OFFSET);
 0189        BIWAnalytics.CatalogItemSearched(currentSearchFilter, filterObjects.Count);
 0190    }
 191}