| | 1 | | using System; |
| | 2 | | using System.Collections; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using DCL; |
| | 5 | | using UnityEngine; |
| | 6 | |
|
| | 7 | | public 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 | |
|
| | 16 | | public 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 | |
|
| 0 | 22 | | 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 | | { |
| 31 | 34 | | this.view = sceneCatalogView.searchBarView; |
| 31 | 35 | | if (this.view.searchInput != null) |
| 1 | 36 | | this.view.searchInput.onValueChanged.AddListener(OnSearchInputChanged); |
| | 37 | |
|
| 31 | 38 | | if (this.view.smartItemBtn != null) |
| 1 | 39 | | this.view.smartItemBtn.onClick.AddListener(ChangeSmartItemFilter); |
| 31 | 40 | | } |
| | 41 | |
|
| | 42 | | public void Dispose() |
| | 43 | | { |
| 30 | 44 | | if (view.searchInput != null) |
| 0 | 45 | | view.searchInput.onValueChanged.RemoveListener(OnSearchInputChanged); |
| | 46 | |
|
| 30 | 47 | | if (view.smartItemBtn != null) |
| 0 | 48 | | view.smartItemBtn.onClick.RemoveListener(ChangeSmartItemFilter); |
| 30 | 49 | | } |
| | 50 | |
|
| 0 | 51 | | public bool IsFilterActive() { return isFilterActive; } |
| | 52 | |
|
| | 53 | | public void OnSearchInputChanged(string currentSearchInput) |
| | 54 | | { |
| 0 | 55 | | if (string.IsNullOrEmpty(currentSearchInput)) |
| | 56 | | { |
| 0 | 57 | | OnFilterRemove?.Invoke(); |
| 0 | 58 | | return; |
| | 59 | | } |
| | 60 | |
|
| 0 | 61 | | if (currentSearchInput.Length <= 1) |
| 0 | 62 | | return; |
| 0 | 63 | | currentSearchFilter = currentSearchInput; |
| | 64 | |
|
| 0 | 65 | | isFilterActive = true; |
| | 66 | |
|
| 0 | 67 | | FilterAssets(currentSearchInput); |
| 0 | 68 | | OnFilterChange?.Invoke(filterObjects); |
| 0 | 69 | | if (searchApplyCoroutine != null) |
| 0 | 70 | | CoroutineStarter.Stop(searchApplyCoroutine); |
| 0 | 71 | | searchApplyCoroutine = CoroutineStarter.Start(ReportSearchAnalytic()); |
| 0 | 72 | | } |
| | 73 | |
|
| | 74 | | public void ReleaseFilters() |
| | 75 | | { |
| 6 | 76 | | isSmartItemFilterActive = false; |
| 6 | 77 | | filterObjects.Clear(); |
| 6 | 78 | | view.SetSmartItemPressStatus(false); |
| 6 | 79 | | view.SetEmptyFilter(); |
| | 80 | |
|
| 6 | 81 | | isFilterActive = false; |
| 6 | 82 | | } |
| | 83 | |
|
| | 84 | | public void ChangeSmartItemFilter() |
| | 85 | | { |
| 2 | 86 | | isSmartItemFilterActive = !isSmartItemFilterActive; |
| 2 | 87 | | view.SetSmartItemPressStatus(isSmartItemFilterActive); |
| | 88 | |
|
| 2 | 89 | | if (isSmartItemFilterActive) |
| 2 | 90 | | FilterSmartItem(); |
| | 91 | | else |
| | 92 | | { |
| 0 | 93 | | isFilterActive = false; |
| 0 | 94 | | OnFilterRemove?.Invoke(); |
| | 95 | | } |
| | 96 | |
|
| 0 | 97 | | } |
| | 98 | |
|
| | 99 | | public void FilterSmartItem() |
| | 100 | | { |
| 2 | 101 | | filterObjects.Clear(); |
| 8 | 102 | | foreach (CatalogItem catalogItem in DataStore.i.builderInWorld.catalogItemDict.GetValues()) |
| | 103 | | { |
| 2 | 104 | | if (!catalogItem.IsSmartItem()) |
| | 105 | | continue; |
| | 106 | |
|
| 1 | 107 | | bool foundCategory = false; |
| 2 | 108 | | foreach (Dictionary<string, List<CatalogItem>> groupedSceneObjects in filterObjects) |
| | 109 | | { |
| 0 | 110 | | if (groupedSceneObjects.ContainsKey(catalogItem.category)) |
| | 111 | | { |
| 0 | 112 | | foundCategory = true; |
| 0 | 113 | | if (!groupedSceneObjects[catalogItem.category].Contains(catalogItem)) |
| 0 | 114 | | groupedSceneObjects[catalogItem.category].Add(catalogItem); |
| | 115 | | } |
| | 116 | | } |
| 1 | 117 | | if (!foundCategory) |
| | 118 | | { |
| 1 | 119 | | AddNewSceneObjectCategoryToFilter(catalogItem); |
| | 120 | | } |
| | 121 | | } |
| | 122 | |
|
| 2 | 123 | | OnFilterChange?.Invoke(filterObjects); |
| 2 | 124 | | } |
| | 125 | |
|
| | 126 | | public List<Dictionary<string, List<CatalogItem>>> FilterAssets(string nameToFilter) |
| | 127 | | { |
| 6 | 128 | | StringComparison comparison = StringComparison.OrdinalIgnoreCase; |
| 6 | 129 | | filterObjects.Clear(); |
| 24 | 130 | | foreach (CatalogItem catalogItem in DataStore.i.builderInWorld.catalogItemDict.GetValues()) |
| | 131 | | { |
| 6 | 132 | | if (!MatchtFilter(catalogItem, nameToFilter, comparison)) |
| | 133 | | continue; |
| | 134 | |
|
| 3 | 135 | | bool foundCategory = false; |
| 6 | 136 | | foreach (Dictionary<string, List<CatalogItem>> groupedSceneObjects in filterObjects) |
| | 137 | | { |
| 0 | 138 | | if (groupedSceneObjects.ContainsKey(catalogItem.category)) |
| | 139 | | { |
| 0 | 140 | | foundCategory = true; |
| 0 | 141 | | if (!groupedSceneObjects[catalogItem.category].Contains(catalogItem)) |
| 0 | 142 | | groupedSceneObjects[catalogItem.category].Add(catalogItem); |
| | 143 | | } |
| | 144 | | } |
| 3 | 145 | | if (!foundCategory) |
| | 146 | | { |
| 3 | 147 | | AddNewSceneObjectCategoryToFilter(catalogItem); |
| | 148 | | } |
| | 149 | | } |
| 6 | 150 | | return filterObjects; |
| | 151 | | } |
| | 152 | |
|
| | 153 | | private bool MatchtFilter(CatalogItem catalogItem, string nameToFilter, StringComparison comparison) |
| | 154 | | { |
| 6 | 155 | | 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 |
| 6 | 161 | | if (catalogItem.category.ToLower().IndexOf(nameToFilterToLower, comparison) != -1 || |
| | 162 | | catalogItem.name.ToLower().IndexOf(nameToFilterToLower, comparison) != -1) |
| 2 | 163 | | return true; |
| | 164 | |
|
| 21 | 165 | | foreach (string tag in catalogItem.tags) |
| | 166 | | { |
| 7 | 167 | | if (tag.ToLower().IndexOf(nameToFilterToLower, comparison) != -1) |
| | 168 | | { |
| 1 | 169 | | return true; |
| | 170 | | } |
| | 171 | | } |
| | 172 | |
|
| 3 | 173 | | return false; |
| 1 | 174 | | } |
| | 175 | |
|
| | 176 | | internal void AddNewSceneObjectCategoryToFilter(CatalogItem catalogItem) |
| | 177 | | { |
| 5 | 178 | | Dictionary<string, List<CatalogItem>> groupedCatalogItems = new Dictionary<string, List<CatalogItem>>(); |
| 5 | 179 | | groupedCatalogItems.Add(catalogItem.category, new List<CatalogItem>() { catalogItem }); |
| 5 | 180 | | filterObjects.Add(groupedCatalogItems); |
| 5 | 181 | | } |
| | 182 | |
|
| | 183 | | IEnumerator ReportSearchAnalytic() |
| | 184 | | { |
| 0 | 185 | | yield return new WaitForSecondsRealtime(SEARCH_DELAY_OFFSET); |
| 0 | 186 | | BIWAnalytics.CatalogItemSearched(currentSearchFilter, filterObjects.Count); |
| 0 | 187 | | } |
| | 188 | | } |