< 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:58
Uncovered lines:36
Coverable lines:94
Total lines:184
Line coverage:61.7% (58 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%12300%
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    {
 28913        private readonly Dictionary<K, V> entries = new Dictionary<K, V>();
 28914        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
 28926        public Comparison<V> SortingMethod { get; set; } = (model, model1) => 0;
 27
 2928        public override bool isVisible => gameObject.activeInHierarchy;
 29
 30        public override void OnEnable()
 31        {
 8532            base.OnEnable();
 8533            UpdateEmptyState();
 8534            UpdateLayout();
 8535        }
 36
 37        public override void Update()
 38        {
 121339            base.Update();
 40
 121341            if (isLayoutDirty)
 5542                Utils.ForceRebuildLayoutImmediate((RectTransform) container);
 121343            isLayoutDirty = false;
 121344        }
 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
 7361        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
 173375        public virtual int Count() => entries.Count - filteredCount;
 76
 77        public override void Show(bool instant = false)
 78        {
 1579            base.Show(instant);
 1580            gameObject.SetActive(true);
 1581            model.isVisible = true;
 1582        }
 83
 84        public override void Hide(bool instant = false)
 85        {
 1786            base.Hide(instant);
 1787            gameObject.SetActive(false);
 1788            model.isVisible = false;
 1789        }
 90
 23691        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        {
 5695            if (entries.ContainsKey(key)) return;
 5696            entries.Add(key, value);
 5697            sortedEntries.Add(key);
 5698            var entryTransform = value.transform;
 5699            entryTransform.SetParent(container, false);
 56100            entryTransform.localScale = Vector3.one;
 56101            UpdateEmptyState();
 56102            UpdateLayout();
 56103        }
 104
 105        public virtual V Remove(K key)
 106        {
 101107            if (!entries.ContainsKey(key)) return default;
 21108            var entry = entries[key];
 21109            entries.Remove(key);
 51110            sortedEntries.RemoveAll(k => k.Equals(key));
 21111            UpdateEmptyState();
 21112            UpdateLayout();
 21113            return entry;
 114        }
 115
 116        public virtual void Clear()
 117        {
 82118            foreach (var key in entries.Keys.ToList())
 17119                Remove(key);
 120
 24121            entries.Clear();
 24122            sortedEntries.Clear();
 24123            UpdateEmptyState();
 24124            UpdateLayout();
 24125        }
 126
 127        public virtual void Filter(Func<V, bool> comparision)
 128        {
 0129            filteredCount = 0;
 130
 0131            foreach (var entry in entries)
 132            {
 0133                var isMatch = comparision.Invoke(entry.Value);
 0134                entry.Value.gameObject.SetActive(isMatch);
 135
 0136                if (!isMatch)
 0137                    filteredCount++;
 138            }
 139
 0140            UpdateLayout();
 0141            UpdateEmptyState();
 0142        }
 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        {
 115168            sortedEntries.Sort((k, y) => SortingMethod.Invoke(entries[k], entries[y]));
 274169            for (var i = 0; i < sortedEntries.Count; i++)
 170            {
 68171                var key = sortedEntries[i];
 68172                entries[key].transform.SetAsLastSibling();
 173            }
 69174        }
 175
 176        protected virtual void UpdateEmptyState()
 177        {
 285178            if (emptyStateContainer == null) return;
 159179            emptyStateContainer.SetActive(Count() == 0);
 159180        }
 181
 0182        private void UpdateLayout() => isLayoutDirty = true;
 183    }
 184}