< Summary

Class:UIComponents.CollapsableSortedList.CollapsableSortedListComponentView[K,V]
Assembly:UIComponents
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/UIComponents/Scripts/Components/CollapsableSortedList/CollapsableSortedListComponentView.cs
Covered lines:68
Uncovered lines:25
Coverable lines:93
Total lines:182
Line coverage:73.1% (68 of 93)
Covered branches:0
Total branches:0
Covered methods:20
Total methods:23
Method coverage:86.9% (20 of 23)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
CollapsableSortedListComponentView()0%220100%
OnEnable()0%110100%
Update()0%220100%
RefreshControl()0%12300%
Contains(...)0%110100%
Expand()0%110100%
Collapse()0%2100%
Count()0%110100%
Show(...)0%110100%
Hide(...)0%110100%
Get(...)0%220100%
Add(...)0%22090%
Remove(...)0%220100%
Clear()0%220100%
Filter(...)0%330100%
FilterAsync()0%42600%
Sort()0%220100%
UpdateEmptyState()0%220100%
UpdateLayout()0%110100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/UIComponents/Scripts/Components/CollapsableSortedList/CollapsableSortedListComponentView.cs

#LineLine coverage
 1using System;
 2using System.Collections;
 3using System.Collections.Generic;
 4using System.Linq;
 5using DCL.Helpers;
 6using UnityEngine;
 7
 8namespace UIComponents.CollapsableSortedList
 9{
 10    public class CollapsableSortedListComponentView<K, V> : BaseComponentView
 11        where V : Component
 12    {
 26713        private readonly Dictionary<K, V> entries = new Dictionary<K, V>();
 26714        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
 94424        public Dictionary<K, V> Entries => entries;
 25
 43226        public Comparison<V> SortingMethod { get; set; } = (model, model1) => 0;
 27
 2928        public override bool isVisible => gameObject.activeInHierarchy;
 29
 30        public override void OnEnable()
 31        {
 9932            base.OnEnable();
 9933            UpdateEmptyState();
 9934            UpdateLayout();
 9935        }
 36
 37        public void Update()
 38        {
 44739            if (isLayoutDirty)
 4940                Utils.ForceRebuildLayoutImmediate((RectTransform) container);
 44741            isLayoutDirty = false;
 44742        }
 43
 44        public override void RefreshControl()
 45        {
 046            Clear();
 47
 048            if (model.isExpanded)
 049                Expand();
 50            else
 051                Collapse();
 52
 053            if (model.isVisible)
 054                Show();
 55            else
 056                Hide();
 057        }
 58
 8659        public virtual bool Contains(K key) => entries.ContainsKey(key);
 60
 61        public virtual void Expand()
 62        {
 463            toggleButton.Toggle(true);
 464            model.isExpanded = true;
 465        }
 66
 67        public virtual void Collapse()
 68        {
 069            toggleButton.Toggle(false);
 070            model.isExpanded = false;
 071        }
 72
 139473        public virtual int Count() => entries.Count - filteredCount;
 74
 75        public override void Show(bool instant = false)
 76        {
 1677            base.Show(instant);
 1678            gameObject.SetActive(true);
 1679            model.isVisible = true;
 1680        }
 81
 82        public override void Hide(bool instant = false)
 83        {
 1884            base.Hide(instant);
 1885            gameObject.SetActive(false);
 1886            model.isVisible = false;
 1887        }
 88
 20289        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        {
 6993            if (entries.ContainsKey(key)) return;
 6994            entries.Add(key, value);
 6995            sortedEntries.Add(key);
 6996            var entryTransform = value.transform;
 6997            entryTransform.SetParent(container, false);
 6998            entryTransform.localScale = Vector3.one;
 6999            UpdateEmptyState();
 69100            UpdateLayout();
 69101        }
 102
 103        public virtual V Remove(K key)
 104        {
 29105            if (!entries.ContainsKey(key)) return default;
 25106            var entry = entries[key];
 25107            entries.Remove(key);
 59108            sortedEntries.RemoveAll(k => k.Equals(key));
 25109            UpdateEmptyState();
 25110            UpdateLayout();
 25111            return entry;
 112        }
 113
 114        public virtual void Clear()
 115        {
 120116            foreach (var key in entries.Keys.ToList())
 22117                Remove(key);
 118
 38119            entries.Clear();
 38120            sortedEntries.Clear();
 38121            UpdateEmptyState();
 38122            UpdateLayout();
 38123        }
 124
 125        public virtual void Filter(Func<V, bool> comparision)
 126        {
 4127            filteredCount = 0;
 128
 16129            foreach (var entry in entries)
 130            {
 4131                var isMatch = comparision.Invoke(entry.Value);
 4132                entry.Value.gameObject.SetActive(isMatch);
 133
 4134                if (!isMatch)
 2135                    filteredCount++;
 136            }
 137
 4138            UpdateLayout();
 4139            UpdateEmptyState();
 4140        }
 141
 142        public IEnumerator FilterAsync(Func<V, bool> comparision, int throttlingBudget = 10)
 143        {
 0144            filteredCount = 0;
 0145            var iterations = 0;
 146
 0147            foreach (var entry in entries)
 148            {
 0149                iterations++;
 0150                if (iterations % throttlingBudget == 0)
 0151                    yield return null;
 152
 0153                var isMatch = comparision.Invoke(entry.Value);
 0154                entry.Value.gameObject.SetActive(isMatch);
 155
 0156                if (!isMatch)
 0157                    filteredCount++;
 0158            }
 159
 0160            UpdateLayout();
 0161            UpdateEmptyState();
 0162        }
 163
 164        public virtual void Sort()
 165        {
 140166            sortedEntries.Sort((k, y) => SortingMethod.Invoke(entries[k], entries[y]));
 342167            for (var i = 0; i < sortedEntries.Count; i++)
 168            {
 78169                var key = sortedEntries[i];
 78170                entries[key].transform.SetAsLastSibling();
 171            }
 93172        }
 173
 174        protected virtual void UpdateEmptyState()
 175        {
 368176            if (emptyStateContainer == null) return;
 202177            emptyStateContainer.SetActive(Count() == 0);
 202178        }
 179
 235180        private void UpdateLayout() => isLayoutDirty = true;
 181    }
 182}