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