< 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:67
Uncovered lines:27
Coverable lines:94
Total lines:184
Line coverage:71.2% (67 of 94)
Covered branches:0
Total branches:0

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%220100%
Remove(...)0%220100%
Clear()0%220100%
Filter(...)0%330100%
FilterAsync()0%42600%
Sort()0%220100%
UpdateEmptyState()0%220100%
UpdateLayout()0%2100%

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    {
 26913        private readonly Dictionary<K, V> entries = new Dictionary<K, V>();
 26914        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
 024        public Dictionary<K, V> Entries => entries;
 25
 26926        public Comparison<V> SortingMethod { get; set; } = (model, model1) => 0;
 27
 2728        public override bool isVisible => gameObject.activeInHierarchy;
 29
 30        public override void OnEnable()
 31        {
 7532            base.OnEnable();
 7533            UpdateEmptyState();
 7534            UpdateLayout();
 7535        }
 36
 37        public override void Update()
 38        {
 8739            base.Update();
 40
 8741            if (isLayoutDirty)
 4942                Utils.ForceRebuildLayoutImmediate((RectTransform) container);
 8743            isLayoutDirty = false;
 8744        }
 45
 46        public override void RefreshControl()
 47        {
 048            Clear();
 49
 050            if (model.isExpanded)
 051                Expand();
 52            else
 053                Collapse();
 54
 055            if (model.isVisible)
 056                Show();
 57            else
 058                Hide();
 059        }
 60
 6261        public virtual bool Contains(K key) => entries.ContainsKey(key);
 62
 63        public virtual void Expand()
 64        {
 4865            toggleButton.Toggle(true);
 4866            model.isExpanded = true;
 4867        }
 68
 69        public virtual void Collapse()
 70        {
 071            toggleButton.Toggle(false);
 072            model.isExpanded = false;
 073        }
 74
 50775        public virtual int Count() => entries.Count - filteredCount;
 76
 77        public override void Show(bool instant = false)
 78        {
 1479            base.Show(instant);
 1480            gameObject.SetActive(true);
 1481            model.isVisible = true;
 1482        }
 83
 84        public override void Hide(bool instant = false)
 85        {
 1586            base.Hide(instant);
 1587            gameObject.SetActive(false);
 1588            model.isVisible = false;
 1589        }
 90
 9091        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        {
 7495            if (entries.ContainsKey(key)) return;
 6696            entries.Add(key, value);
 6697            sortedEntries.Add(key);
 6698            var entryTransform = value.transform;
 6699            entryTransform.SetParent(container, false);
 66100            entryTransform.localScale = Vector3.one;
 66101            UpdateEmptyState();
 66102            UpdateLayout();
 66103        }
 104
 105        public virtual V Remove(K key)
 106        {
 72107            if (!entries.ContainsKey(key)) return default;
 36108            var entry = entries[key];
 36109            entries.Remove(key);
 93110            sortedEntries.RemoveAll(k => k.Equals(key));
 36111            UpdateEmptyState();
 36112            UpdateLayout();
 36113            return entry;
 114        }
 115
 116        public virtual void Clear()
 117        {
 100118            foreach (var key in entries.Keys.ToList())
 32119                Remove(key);
 120
 18121            entries.Clear();
 18122            sortedEntries.Clear();
 18123            UpdateEmptyState();
 18124            UpdateLayout();
 18125        }
 126
 127        public virtual void Filter(Func<V, bool> comparision)
 128        {
 18129            filteredCount = 0;
 130
 108131            foreach (var entry in entries)
 132            {
 36133                var isMatch = comparision.Invoke(entry.Value);
 36134                entry.Value.gameObject.SetActive(isMatch);
 135
 36136                if (!isMatch)
 6137                    filteredCount++;
 138            }
 139
 18140            UpdateLayout();
 18141            UpdateEmptyState();
 18142        }
 143
 144        public IEnumerator FilterAsync(Func<V, bool> comparision, int throttlingBudget = 10)
 145        {
 0146            filteredCount = 0;
 0147            var iterations = 0;
 148
 0149            foreach (var entry in entries)
 150            {
 0151                iterations++;
 0152                if (iterations % throttlingBudget == 0)
 0153                    yield return null;
 154
 0155                var isMatch = comparision.Invoke(entry.Value);
 0156                entry.Value.gameObject.SetActive(isMatch);
 157
 0158                if (!isMatch)
 0159                    filteredCount++;
 0160            }
 161
 0162            UpdateLayout();
 0163            UpdateEmptyState();
 0164        }
 165
 166        public virtual void Sort()
 167        {
 78168            sortedEntries.Sort((k, y) => SortingMethod.Invoke(entries[k], entries[y]));
 190169            for (var i = 0; i < sortedEntries.Count; i++)
 170            {
 39171                var key = sortedEntries[i];
 39172                entries[key].transform.SetAsLastSibling();
 173            }
 56174        }
 175
 176        protected virtual void UpdateEmptyState()
 177        {
 290178            if (emptyStateContainer == null) return;
 182179            emptyStateContainer.SetActive(Count() == 0);
 182180        }
 181
 0182        private void UpdateLayout() => isLayoutDirty = true;
 183    }
 184}