| | 1 | | using DCLServices.WearablesCatalogService; |
| | 2 | | using System; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using System.Linq; |
| | 5 | | using UnityEngine; |
| | 6 | |
|
| | 7 | | namespace DCL.Backpack |
| | 8 | | { |
| | 9 | | public class BackpackFiltersComponentView : MonoBehaviour, IBackpackFiltersComponentView |
| | 10 | | { |
| | 11 | | public event Action<bool> OnOnlyCollectiblesChanged; |
| | 12 | | public event Action<HashSet<string>> OnCollectionChanged; |
| | 13 | | public event Action<(NftOrderByOperation type, bool directionAscendent)> OnSortByChanged; |
| | 14 | | public event Action<string> OnSearchTextChanged; |
| | 15 | |
|
| | 16 | | public List<ToggleComponentModel> loadedFilters; |
| | 17 | |
|
| | 18 | | [SerializeField] internal ToggleComponentView onlyCollectiblesToggle; |
| | 19 | | [SerializeField] internal DropdownComponentView collectionDropdown; |
| | 20 | | [SerializeField] internal DropdownComponentView sortByDropdown; |
| | 21 | | [SerializeField] internal SearchBarComponentView searchBar; |
| | 22 | |
|
| 52 | 23 | | private readonly HashSet<string> selectedCollections = new (); |
| | 24 | |
|
| | 25 | | private const string NEWEST_FILTER_ID = "newest"; |
| | 26 | | private const string OLDEST_FILTER_ID = "oldest"; |
| | 27 | | private const string RAREST_FILTER_ID = "rarest"; |
| | 28 | | private const string LESS_RARE_FILTER_ID = "less_rare"; |
| | 29 | | private const string NAME_AZ_FILTER_ID = "name_az"; |
| | 30 | | private const string NAME_ZA_FILTER_ID = "name_za"; |
| | 31 | |
|
| | 32 | | private void Awake() |
| | 33 | | { |
| 49 | 34 | | LoadSortByDropdown(); |
| | 35 | |
|
| 49 | 36 | | onlyCollectiblesToggle.OnSelectedChanged += OnOnlyCollectiblesToggleChanged; |
| 49 | 37 | | collectionDropdown.OnOptionSelectionChanged += OnCollectionDropdownChanged; |
| 49 | 38 | | sortByDropdown.OnOptionSelectionChanged += OnSortByDropdownChanged; |
| 49 | 39 | | searchBar.OnSearchText += OnSearchBarChanged; |
| 49 | 40 | | } |
| | 41 | |
|
| | 42 | | public void Dispose() |
| | 43 | | { |
| 14 | 44 | | onlyCollectiblesToggle.OnSelectedChanged -= OnOnlyCollectiblesToggleChanged; |
| 14 | 45 | | collectionDropdown.OnOptionSelectionChanged -= OnCollectionDropdownChanged; |
| 14 | 46 | | sortByDropdown.OnOptionSelectionChanged -= OnSortByDropdownChanged; |
| 14 | 47 | | searchBar.OnSearchText -= OnSearchBarChanged; |
| 14 | 48 | | } |
| | 49 | |
|
| | 50 | | public void LoadCollectionDropdown( |
| | 51 | | WearableCollectionsAPIData.Collection[] collections, |
| | 52 | | WearableCollectionsAPIData.Collection defaultCollection = null) |
| | 53 | | { |
| 5 | 54 | | List<ToggleComponentModel> collectionsToAdd = new (); |
| | 55 | |
|
| 5 | 56 | | if (defaultCollection != null) |
| | 57 | | { |
| 1 | 58 | | ToggleComponentModel defaultToggle = new () |
| | 59 | | { |
| | 60 | | id = defaultCollection.urn, |
| | 61 | | text = defaultCollection.name, |
| | 62 | | isOn = true, |
| | 63 | | isTextActive = true, |
| | 64 | | changeTextColorOnSelect = true, |
| | 65 | | }; |
| | 66 | |
|
| 1 | 67 | | collectionsToAdd.Add(defaultToggle); |
| 1 | 68 | | selectedCollections.Clear(); |
| 1 | 69 | | selectedCollections.Add(defaultToggle.id); |
| | 70 | | } |
| | 71 | |
|
| 40 | 72 | | foreach (var collection in collections) |
| | 73 | | { |
| 15 | 74 | | ToggleComponentModel newCollectionModel = new ToggleComponentModel |
| | 75 | | { |
| | 76 | | id = collection.urn, |
| | 77 | | text = collection.name, |
| | 78 | | isOn = false, |
| | 79 | | isTextActive = true, |
| | 80 | | changeTextColorOnSelect = true, |
| | 81 | | }; |
| | 82 | |
|
| 15 | 83 | | collectionsToAdd.Add(newCollectionModel); |
| | 84 | | } |
| | 85 | |
|
| 5 | 86 | | if (collectionsToAdd.Count > 0) |
| 5 | 87 | | collectionDropdown.SetTitle(collectionsToAdd[0].text); |
| | 88 | |
|
| 5 | 89 | | collectionDropdown.SetOptions(collectionsToAdd); |
| 5 | 90 | | loadedFilters = collectionsToAdd; |
| 5 | 91 | | } |
| | 92 | |
|
| | 93 | | public void SetSearchText(string text, bool notify) => |
| 0 | 94 | | searchBar.SubmitSearch(text, notify, notify); |
| | 95 | |
|
| | 96 | | public void SetOnlyCollectiblesToggleIsOn(bool isOn, bool notify) |
| | 97 | | { |
| 0 | 98 | | if (notify) |
| 0 | 99 | | onlyCollectiblesToggle.isOn = isOn; |
| | 100 | | else |
| 0 | 101 | | onlyCollectiblesToggle.SetIsOnWithoutNotify(isOn); |
| 0 | 102 | | } |
| | 103 | |
|
| | 104 | | public void SetSorting(NftOrderByOperation type, bool directionAscending, bool notify) |
| | 105 | | { |
| 0 | 106 | | IToggleComponentView option = sortByDropdown.SelectOption(OrderByToOptionId(type, directionAscending), notif |
| 0 | 107 | | sortByDropdown.SetTitle(option.title); |
| 0 | 108 | | } |
| | 109 | |
|
| | 110 | | public void SelectDropdownCollections(HashSet<string> collections, bool notify) |
| | 111 | | { |
| 0 | 112 | | var isFirstAssigned = false; |
| | 113 | |
|
| 0 | 114 | | foreach (string collection in collections) |
| | 115 | | { |
| 0 | 116 | | collectionDropdown.SelectOption(collection, notify); |
| | 117 | |
|
| 0 | 118 | | if (isFirstAssigned) continue; |
| 0 | 119 | | IToggleComponentView option = collectionDropdown.GetOption(collection); |
| 0 | 120 | | if (option == null) continue; |
| 0 | 121 | | collectionDropdown.SetTitle(option.title); |
| 0 | 122 | | isFirstAssigned = true; |
| | 123 | | } |
| 0 | 124 | | } |
| | 125 | |
|
| | 126 | | private string OrderByToOptionId(NftOrderByOperation type, bool directionAscending) |
| | 127 | | { |
| | 128 | | switch (type) |
| | 129 | | { |
| | 130 | | case NftOrderByOperation.Date: |
| 0 | 131 | | return directionAscending ? OLDEST_FILTER_ID : NEWEST_FILTER_ID; |
| | 132 | | case NftOrderByOperation.Rarity: |
| 0 | 133 | | return directionAscending ? LESS_RARE_FILTER_ID : RAREST_FILTER_ID; |
| | 134 | | case NftOrderByOperation.Name: |
| 0 | 135 | | return directionAscending ? NAME_AZ_FILTER_ID : NAME_ZA_FILTER_ID; |
| | 136 | | default: |
| 0 | 137 | | throw new ArgumentOutOfRangeException($"Unsupported order type operation: {type}, direction: {(direc |
| | 138 | | } |
| | 139 | | } |
| | 140 | |
|
| | 141 | | private void LoadSortByDropdown() |
| | 142 | | { |
| 49 | 143 | | List<ToggleComponentModel> sortingMethodsToAdd = new List<ToggleComponentModel> |
| | 144 | | { |
| | 145 | | new () { id = NEWEST_FILTER_ID, text = "Newest", isOn = false, isTextActive = true, changeTextColorOnSel |
| | 146 | | new () { id = OLDEST_FILTER_ID, text = "Oldest", isOn = false, isTextActive = true, changeTextColorOnSel |
| | 147 | | new () { id = RAREST_FILTER_ID, text = "Rarest", isOn = false, isTextActive = true, changeTextColorOnSel |
| | 148 | | new () { id = LESS_RARE_FILTER_ID, text = "Less rare", isOn = false, isTextActive = true, changeTextColo |
| | 149 | | new () { id = NAME_AZ_FILTER_ID, text = "Name A-Z", isOn = true, isTextActive = true, changeTextColorOnS |
| | 150 | | new () { id = NAME_ZA_FILTER_ID, text = "Name Z-A", isOn = false, isTextActive = true, changeTextColorOn |
| | 151 | | }; |
| | 152 | |
|
| 49 | 153 | | sortByDropdown.SetTitle(sortingMethodsToAdd[4].text); |
| 49 | 154 | | sortByDropdown.SetOptions(sortingMethodsToAdd); |
| 49 | 155 | | } |
| | 156 | |
|
| | 157 | | private void OnOnlyCollectiblesToggleChanged(bool isOn, string optionId, string optionText) => |
| 1 | 158 | | OnOnlyCollectiblesChanged?.Invoke(isOn); |
| | 159 | |
|
| | 160 | | private void OnCollectionDropdownChanged(bool isOn, string optionId, string optionName) |
| | 161 | | { |
| 3 | 162 | | if (!isOn) |
| 0 | 163 | | return; |
| | 164 | |
|
| 3 | 165 | | collectionDropdown.SetTitle(optionName); |
| 3 | 166 | | selectedCollections.Clear(); |
| 3 | 167 | | selectedCollections.Add(optionId); |
| | 168 | |
|
| | 169 | | // need to make a copy of the collection because it may be modified in the event subscription |
| 3 | 170 | | OnCollectionChanged?.Invoke(selectedCollections.ToHashSet()); |
| 3 | 171 | | } |
| | 172 | |
|
| | 173 | | private void OnSortByDropdownChanged(bool isOn, string optionId, string optionName) |
| | 174 | | { |
| 12 | 175 | | if (!isOn) |
| 6 | 176 | | return; |
| | 177 | |
|
| 6 | 178 | | sortByDropdown.SetTitle(optionName); |
| | 179 | |
|
| | 180 | | switch (optionId) |
| | 181 | | { |
| | 182 | | case OLDEST_FILTER_ID: |
| 1 | 183 | | OnSortByChanged?.Invoke((NftOrderByOperation.Date, true)); |
| 1 | 184 | | break; |
| | 185 | | case NEWEST_FILTER_ID: |
| 1 | 186 | | OnSortByChanged?.Invoke((NftOrderByOperation.Date, false)); |
| 1 | 187 | | break; |
| | 188 | | case LESS_RARE_FILTER_ID: |
| 1 | 189 | | OnSortByChanged?.Invoke((NftOrderByOperation.Rarity, true)); |
| 1 | 190 | | break; |
| | 191 | | case RAREST_FILTER_ID: |
| 1 | 192 | | OnSortByChanged?.Invoke((NftOrderByOperation.Rarity, false)); |
| 1 | 193 | | break; |
| | 194 | | case NAME_AZ_FILTER_ID: |
| 1 | 195 | | OnSortByChanged?.Invoke((NftOrderByOperation.Name, true)); |
| 1 | 196 | | break; |
| | 197 | | case NAME_ZA_FILTER_ID: |
| 1 | 198 | | OnSortByChanged?.Invoke((NftOrderByOperation.Name, false)); |
| | 199 | | break; |
| | 200 | | } |
| 1 | 201 | | } |
| | 202 | |
|
| | 203 | | private void OnSearchBarChanged(string newText) => |
| 1 | 204 | | OnSearchTextChanged?.Invoke(newText); |
| | 205 | | } |
| | 206 | | } |