< 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:69
Uncovered lines:25
Coverable lines:94
Total lines:184
Line coverage:73.4% (69 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%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    {
 39413        private readonly Dictionary<K, V> entries = new Dictionary<K, V>();
 39414        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
 1125024        public Dictionary<K, V> Entries => entries;
 25
 62526        public Comparison<V> SortingMethod { get; set; } = (model, model1) => 0;
 27
 2928        public override bool isVisible => gameObject.activeInHierarchy;
 29
 30        public override void OnEnable()
 31        {
 10932            base.OnEnable();
 10933            UpdateEmptyState();
 10934            UpdateLayout();
 10935        }
 36
 37        public override void Update()
 38        {
 686539            base.Update();
 40
 686541            if (isLayoutDirty)
 6042                Utils.ForceRebuildLayoutImmediate((RectTransform) container);
 686543            isLayoutDirty = false;
 686544        }
 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
 9961        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
 1223975        public virtual int Count() => entries.Count - filteredCount;
 76
 77        public override void Show(bool instant = false)
 78        {
 1679            base.Show(instant);
 1680            gameObject.SetActive(true);
 1681            model.isVisible = true;
 1682        }
 83
 84        public override void Hide(bool instant = false)
 85        {
 1886            base.Hide(instant);
 1887            gameObject.SetActive(false);
 1888            model.isVisible = false;
 1889        }
 90
 31591        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        {
 8095            if (entries.ContainsKey(key)) return;
 8096            entries.Add(key, value);
 8097            sortedEntries.Add(key);
 8098            var entryTransform = value.transform;
 8099            entryTransform.SetParent(container, false);
 80100            entryTransform.localScale = Vector3.one;
 80101            UpdateEmptyState();
 80102            UpdateLayout();
 80103        }
 104
 105        public virtual V Remove(K key)
 106        {
 107107            if (!entries.ContainsKey(key)) return default;
 27108            var entry = entries[key];
 27109            entries.Remove(key);
 63110            sortedEntries.RemoveAll(k => k.Equals(key));
 27111            UpdateEmptyState();
 27112            UpdateLayout();
 27113            return entry;
 114        }
 115
 116        public virtual void Clear()
 117        {
 110118            foreach (var key in entries.Keys.ToList())
 22119                Remove(key);
 120
 33121            entries.Clear();
 33122            sortedEntries.Clear();
 33123            UpdateEmptyState();
 33124            UpdateLayout();
 33125        }
 126
 127        public virtual void Filter(Func<V, bool> comparision)
 128        {
 4129            filteredCount = 0;
 130
 16131            foreach (var entry in entries)
 132            {
 4133                var isMatch = comparision.Invoke(entry.Value);
 4134                entry.Value.gameObject.SetActive(isMatch);
 135
 4136                if (!isMatch)
 2137                    filteredCount++;
 138            }
 139
 4140            UpdateLayout();
 4141            UpdateEmptyState();
 4142        }
 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        {
 142168            sortedEntries.Sort((k, y) => SortingMethod.Invoke(entries[k], entries[y]));
 346169            for (var i = 0; i < sortedEntries.Count; i++)
 170            {
 78171                var key = sortedEntries[i];
 78172                entries[key].transform.SetAsLastSibling();
 173            }
 95174        }
 175
 176        protected virtual void UpdateEmptyState()
 177        {
 386178            if (emptyStateContainer == null) return;
 220179            emptyStateContainer.SetActive(Count() == 0);
 220180        }
 181
 253182        private void UpdateLayout() => isLayoutDirty = true;
 183    }
 184}