| | 1 | | using DCL.Helpers; |
| | 2 | | using System; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using UIComponents.Scripts.Components; |
| | 5 | | using UnityEngine; |
| | 6 | |
|
| | 7 | | namespace DCL.Backpack |
| | 8 | | { |
| | 9 | | public class NftBreadcrumbComponentView : BaseComponentView<NftBreadcrumbModel> |
| | 10 | | { |
| | 11 | | [SerializeField] internal NftSubCategoryFilterComponentView prefab; |
| | 12 | | [SerializeField] internal GameObject separatorPrefab; |
| | 13 | | [SerializeField] internal RectTransform container; |
| | 14 | | [SerializeField] internal RectTransform layoutContainer; |
| | 15 | | [SerializeField] internal NFTTypeIconsAndColors iconsByCategory; |
| | 16 | |
|
| 40 | 17 | | private readonly Dictionary<NftSubCategoryFilterComponentView, PoolableObject> pooledObjects = new (); |
| 40 | 18 | | private readonly Dictionary<GameObject, PoolableObject> pooledSeparators = new (); |
| | 19 | | private Pool pool; |
| | 20 | | private Pool separatorsPool; |
| | 21 | | private bool isLayoutDirty; |
| | 22 | |
|
| 1 | 23 | | internal NftBreadcrumbModel Model => model; |
| | 24 | |
|
| | 25 | | public event Action<string> OnFilterSelected; |
| | 26 | | public event Action<string> OnFilterRemoved; |
| | 27 | |
|
| | 28 | | public override void Awake() |
| | 29 | | { |
| 37 | 30 | | base.Awake(); |
| | 31 | |
|
| 37 | 32 | | pool = PoolManager.i.AddPool( |
| | 33 | | $"NftBreadcrumbComponentView_{GetInstanceID()}", |
| | 34 | | Instantiate(prefab).gameObject, |
| | 35 | | maxPrewarmCount: 5, |
| | 36 | | isPersistent: true); |
| | 37 | |
|
| 37 | 38 | | separatorsPool = PoolManager.i.AddPool( |
| | 39 | | $"NftBreadcrumbSeparator_{GetInstanceID()}", |
| | 40 | | Instantiate(separatorPrefab), |
| | 41 | | maxPrewarmCount: 5, |
| | 42 | | isPersistent: true); |
| 37 | 43 | | } |
| | 44 | |
|
| | 45 | | public override void RefreshControl() |
| | 46 | | { |
| 4 | 47 | | ClearInstances(); |
| | 48 | |
|
| 20 | 49 | | for (var i = 0; i < model.Path.Length; i++) |
| | 50 | | { |
| 6 | 51 | | (string Filter, string Name, string Type, bool Removable) subCategory = model.Path[i]; |
| 6 | 52 | | bool isLastItem = i == model.Path.Length - 1; |
| 6 | 53 | | bool isSelected = model.Current == i; |
| | 54 | |
|
| 6 | 55 | | CreateSubCategory(subCategory, false, isSelected); |
| | 56 | |
|
| 6 | 57 | | if (!isLastItem) |
| 2 | 58 | | CreateSeparator(); |
| | 59 | | } |
| | 60 | |
|
| 4 | 61 | | isLayoutDirty = true; |
| 4 | 62 | | } |
| | 63 | |
|
| | 64 | | private void Update() |
| | 65 | | { |
| 6 | 66 | | if (!isLayoutDirty) return; |
| 0 | 67 | | isLayoutDirty = false; |
| 0 | 68 | | layoutContainer.ForceUpdateLayout(false); |
| 0 | 69 | | } |
| | 70 | |
|
| | 71 | | private void CreateSeparator() |
| | 72 | | { |
| 2 | 73 | | PoolableObject poolObj = separatorsPool.Get(); |
| 2 | 74 | | GameObject separator = poolObj.gameObject; |
| 2 | 75 | | separator.transform.SetParent(container, false); |
| 2 | 76 | | pooledSeparators[separator] = poolObj; |
| 2 | 77 | | } |
| | 78 | |
|
| | 79 | | private NftSubCategoryFilterComponentView CreateSubCategory((string Filter, string Name, string Type, bool Remov |
| | 80 | | bool showResultCount, bool isSelected) |
| | 81 | | { |
| 6 | 82 | | PoolableObject poolObj = pool.Get(); |
| 6 | 83 | | NftSubCategoryFilterComponentView view = poolObj.gameObject.GetComponent<NftSubCategoryFilterComponentView>( |
| 6 | 84 | | Sprite icon = iconsByCategory.GetTypeImage(subCategory.Type); |
| | 85 | |
|
| 6 | 86 | | string categoryName = subCategory.Name; |
| 6 | 87 | | categoryName = WearableItem.CATEGORIES_READABLE_MAPPING.TryGetValue(categoryName, out string readableCategor |
| | 88 | |
|
| 6 | 89 | | view.SetModel(new NftSubCategoryFilterModel |
| | 90 | | { |
| | 91 | | Name = categoryName, |
| | 92 | | Filter = subCategory.Filter, |
| | 93 | | ResultCount = model.ResultCount, |
| | 94 | | ShowResultCount = showResultCount, |
| | 95 | | Icon = icon, |
| | 96 | | IsSelected = isSelected, |
| | 97 | | ShowRemoveButton = subCategory.Removable, |
| | 98 | | }); |
| | 99 | |
|
| 6 | 100 | | view.OnNavigate += ApplyFilter; |
| 6 | 101 | | view.OnExit += RemoveFilter; |
| 6 | 102 | | view.transform.SetParent(container, false); |
| | 103 | |
|
| 6 | 104 | | pooledObjects[view] = poolObj; |
| | 105 | |
|
| 6 | 106 | | return view; |
| | 107 | | } |
| | 108 | |
|
| | 109 | | private void ClearInstances() |
| | 110 | | { |
| 12 | 111 | | foreach ((NftSubCategoryFilterComponentView view, PoolableObject poolObj) in pooledObjects) |
| | 112 | | { |
| 2 | 113 | | poolObj.Release(); |
| 2 | 114 | | view.OnNavigate -= ApplyFilter; |
| 2 | 115 | | view.OnExit -= RemoveFilter; |
| | 116 | | } |
| | 117 | |
|
| 4 | 118 | | pooledObjects.Clear(); |
| | 119 | |
|
| 10 | 120 | | foreach ((GameObject go, PoolableObject poolObj) in pooledSeparators) |
| 1 | 121 | | poolObj.Release(); |
| | 122 | |
|
| 4 | 123 | | pooledSeparators.Clear(); |
| 4 | 124 | | } |
| | 125 | |
|
| | 126 | | private void RemoveFilter(NftSubCategoryFilterModel model) => |
| 0 | 127 | | OnFilterRemoved?.Invoke(model.Filter); |
| | 128 | |
|
| | 129 | | private void ApplyFilter(NftSubCategoryFilterModel model) => |
| 0 | 130 | | OnFilterSelected?.Invoke(model.Filter); |
| | 131 | | } |
| | 132 | | } |