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