| | 1 | | using System; |
| | 2 | | using System.Collections; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using System.Linq; |
| | 5 | | using DCL.Helpers; |
| | 6 | | using UnityEngine; |
| | 7 | |
|
| | 8 | | namespace UIComponents.CollapsableSortedList |
| | 9 | | { |
| | 10 | | public class CollapsableSortedListComponentView<K, V> : BaseComponentView |
| | 11 | | where V : Component |
| | 12 | | { |
| 289 | 13 | | private readonly Dictionary<K, V> entries = new Dictionary<K, V>(); |
| 289 | 14 | | private readonly List<K> sortedEntries = new List<K>(); |
| | 15 | |
|
| | 16 | | [SerializeField] private Transform container; |
| | 17 | | [SerializeField] private CollapsableListToggleButton toggleButton; |
| | 18 | | [SerializeField] private GameObject emptyStateContainer; |
| | 19 | | [SerializeField] private CollapsableSortedListModel model; |
| | 20 | |
|
| | 21 | | private int filteredCount; |
| | 22 | | private bool isLayoutDirty; |
| | 23 | |
|
| 0 | 24 | | public Dictionary<K, V> Entries => entries; |
| | 25 | |
|
| 289 | 26 | | public Comparison<V> SortingMethod { get; set; } = (model, model1) => 0; |
| | 27 | |
|
| 29 | 28 | | public override bool isVisible => gameObject.activeInHierarchy; |
| | 29 | |
|
| | 30 | | public override void OnEnable() |
| | 31 | | { |
| 85 | 32 | | base.OnEnable(); |
| 85 | 33 | | UpdateEmptyState(); |
| 85 | 34 | | UpdateLayout(); |
| 85 | 35 | | } |
| | 36 | |
|
| | 37 | | public override void Update() |
| | 38 | | { |
| 1213 | 39 | | base.Update(); |
| | 40 | |
|
| 1213 | 41 | | if (isLayoutDirty) |
| 55 | 42 | | Utils.ForceRebuildLayoutImmediate((RectTransform) container); |
| 1213 | 43 | | isLayoutDirty = false; |
| 1213 | 44 | | } |
| | 45 | |
|
| | 46 | | public override void RefreshControl() |
| | 47 | | { |
| 0 | 48 | | Clear(); |
| | 49 | |
|
| 0 | 50 | | if (model.isExpanded) |
| 0 | 51 | | Expand(); |
| | 52 | | else |
| 0 | 53 | | Collapse(); |
| | 54 | |
|
| 0 | 55 | | if (model.isVisible) |
| 0 | 56 | | Show(); |
| | 57 | | else |
| 0 | 58 | | Hide(); |
| 0 | 59 | | } |
| | 60 | |
|
| 73 | 61 | | public virtual bool Contains(K key) => entries.ContainsKey(key); |
| | 62 | |
|
| | 63 | | public virtual void Expand() |
| | 64 | | { |
| 48 | 65 | | toggleButton.Toggle(true); |
| 48 | 66 | | model.isExpanded = true; |
| 48 | 67 | | } |
| | 68 | |
|
| | 69 | | public virtual void Collapse() |
| | 70 | | { |
| 0 | 71 | | toggleButton.Toggle(false); |
| 0 | 72 | | model.isExpanded = false; |
| 0 | 73 | | } |
| | 74 | |
|
| 1733 | 75 | | public virtual int Count() => entries.Count - filteredCount; |
| | 76 | |
|
| | 77 | | public override void Show(bool instant = false) |
| | 78 | | { |
| 15 | 79 | | base.Show(instant); |
| 15 | 80 | | gameObject.SetActive(true); |
| 15 | 81 | | model.isVisible = true; |
| 15 | 82 | | } |
| | 83 | |
|
| | 84 | | public override void Hide(bool instant = false) |
| | 85 | | { |
| 17 | 86 | | base.Hide(instant); |
| 17 | 87 | | gameObject.SetActive(false); |
| 17 | 88 | | model.isVisible = false; |
| 17 | 89 | | } |
| | 90 | |
|
| 236 | 91 | | public virtual V Get(K key) => entries.TryGetValue(key, out var value) ? value : null; |
| | 92 | |
|
| | 93 | | public virtual void Add(K key, V value) |
| | 94 | | { |
| 56 | 95 | | if (entries.ContainsKey(key)) return; |
| 56 | 96 | | entries.Add(key, value); |
| 56 | 97 | | sortedEntries.Add(key); |
| 56 | 98 | | var entryTransform = value.transform; |
| 56 | 99 | | entryTransform.SetParent(container, false); |
| 56 | 100 | | entryTransform.localScale = Vector3.one; |
| 56 | 101 | | UpdateEmptyState(); |
| 56 | 102 | | UpdateLayout(); |
| 56 | 103 | | } |
| | 104 | |
|
| | 105 | | public virtual V Remove(K key) |
| | 106 | | { |
| 101 | 107 | | if (!entries.ContainsKey(key)) return default; |
| 21 | 108 | | var entry = entries[key]; |
| 21 | 109 | | entries.Remove(key); |
| 51 | 110 | | sortedEntries.RemoveAll(k => k.Equals(key)); |
| 21 | 111 | | UpdateEmptyState(); |
| 21 | 112 | | UpdateLayout(); |
| 21 | 113 | | return entry; |
| | 114 | | } |
| | 115 | |
|
| | 116 | | public virtual void Clear() |
| | 117 | | { |
| 82 | 118 | | foreach (var key in entries.Keys.ToList()) |
| 17 | 119 | | Remove(key); |
| | 120 | |
|
| 24 | 121 | | entries.Clear(); |
| 24 | 122 | | sortedEntries.Clear(); |
| 24 | 123 | | UpdateEmptyState(); |
| 24 | 124 | | UpdateLayout(); |
| 24 | 125 | | } |
| | 126 | |
|
| | 127 | | public virtual void Filter(Func<V, bool> comparision) |
| | 128 | | { |
| 0 | 129 | | filteredCount = 0; |
| | 130 | |
|
| 0 | 131 | | foreach (var entry in entries) |
| | 132 | | { |
| 0 | 133 | | var isMatch = comparision.Invoke(entry.Value); |
| 0 | 134 | | entry.Value.gameObject.SetActive(isMatch); |
| | 135 | |
|
| 0 | 136 | | if (!isMatch) |
| 0 | 137 | | filteredCount++; |
| | 138 | | } |
| | 139 | |
|
| 0 | 140 | | UpdateLayout(); |
| 0 | 141 | | UpdateEmptyState(); |
| 0 | 142 | | } |
| | 143 | |
|
| | 144 | | public IEnumerator FilterAsync(Func<V, bool> comparision, int throttlingBudget = 10) |
| | 145 | | { |
| 0 | 146 | | filteredCount = 0; |
| 0 | 147 | | var iterations = 0; |
| | 148 | |
|
| 0 | 149 | | foreach (var entry in entries) |
| | 150 | | { |
| 0 | 151 | | iterations++; |
| 0 | 152 | | if (iterations % throttlingBudget == 0) |
| 0 | 153 | | yield return null; |
| | 154 | |
|
| 0 | 155 | | var isMatch = comparision.Invoke(entry.Value); |
| 0 | 156 | | entry.Value.gameObject.SetActive(isMatch); |
| | 157 | |
|
| 0 | 158 | | if (!isMatch) |
| 0 | 159 | | filteredCount++; |
| 0 | 160 | | } |
| | 161 | |
|
| 0 | 162 | | UpdateLayout(); |
| 0 | 163 | | UpdateEmptyState(); |
| 0 | 164 | | } |
| | 165 | |
|
| | 166 | | public virtual void Sort() |
| | 167 | | { |
| 115 | 168 | | sortedEntries.Sort((k, y) => SortingMethod.Invoke(entries[k], entries[y])); |
| 274 | 169 | | for (var i = 0; i < sortedEntries.Count; i++) |
| | 170 | | { |
| 68 | 171 | | var key = sortedEntries[i]; |
| 68 | 172 | | entries[key].transform.SetAsLastSibling(); |
| | 173 | | } |
| 69 | 174 | | } |
| | 175 | |
|
| | 176 | | protected virtual void UpdateEmptyState() |
| | 177 | | { |
| 285 | 178 | | if (emptyStateContainer == null) return; |
| 159 | 179 | | emptyStateContainer.SetActive(Count() == 0); |
| 159 | 180 | | } |
| | 181 | |
|
| 0 | 182 | | private void UpdateLayout() => isLayoutDirty = true; |
| | 183 | | } |
| | 184 | | } |