< Summary

Class:SearchHandler[T]
Assembly:BuilderProjectsPanel
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/BuilderInWorld/HUD/ProjectsPanelHUD/Scripts/SearchHandler/SearchHandler.cs
Covered lines:45
Uncovered lines:25
Coverable lines:70
Total lines:147
Line coverage:64.2% (45 of 70)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
SearchHandler(...)0%330100%
SearchHandler()0%110100%
SetSearchableList(...)0%110100%
AddItem(...)0%20400%
RemoveItem(...)0%12300%
GetResult(...)0%14140100%
NotifySearchChanged(...)0%220100%
NotifySortTypeChanged(...)0%220100%
NotifySortOrderChanged(...)0%220100%
NotifyFilterChanged()0%110100%
Match(...)0%6200%
MatchSearch(...)0%2100%
MatchFilter(...)0%6200%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/BuilderInWorld/HUD/ProjectsPanelHUD/Scripts/SearchHandler/SearchHandler.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3
 4public class SearchHandler<T> where T : ISearchable, ISortable<T>
 5{
 6    public event Action<List<T>> OnSearchChanged;
 7
 08    public string[] sortingTypes { get; }
 309    public bool isDescendingSortOrder { get; private set; } = true;
 010    public string currentSearchString { get; private set; }
 011    public string currentSortingType { get; private set; }
 012    public int resultCount { get { return currentResult?.Count ?? 0; } }
 13
 14    private List<T> originalList;
 15    private List<T> searchResult;
 16    private List<T> currentResult;
 17
 18    private readonly Predicate<T> currentFilterPredicate;
 19
 20    private bool searchStringChanged = false;
 21    private bool searchSortChanged = false;
 22    private bool searchFilterChanged = false;
 23
 3024    public SearchHandler(string[] sortingTypes, Predicate<T> filterPredicate)
 25    {
 3026        this.sortingTypes = sortingTypes;
 3027        this.currentFilterPredicate = filterPredicate;
 3028        this.currentSortingType = sortingTypes?[0];
 3029    }
 30
 3431    public SearchHandler() : this(null, null) { }
 32
 33    public void SetSearchableList(List<T> list)
 34    {
 1935        originalList = list;
 1936        searchResult = list;
 1937        currentResult = list;
 38
 1939        searchStringChanged = true;
 1940        searchFilterChanged = true;
 1941        searchSortChanged = true;
 42
 1943        GetResult(OnSearchChanged);
 1944    }
 45
 46    public void AddItem(T item)
 47    {
 048        originalList.Add(item);
 049        bool matchSearch = MatchSearch(item);
 050        bool matchResult = matchSearch && MatchFilter(item);
 51
 052        if (matchSearch)
 53        {
 054            searchResult.Add(item);
 55        }
 56
 057        if (matchResult)
 58        {
 059            currentResult.Add(item);
 060            searchSortChanged = true;
 061            GetResult(OnSearchChanged);
 62        }
 063    }
 64
 65    public void RemoveItem(T item)
 66    {
 067        originalList.Remove(item);
 068        searchResult.Remove(item);
 069        bool inResult = currentResult.Remove(item);
 070        if (inResult)
 71        {
 072            OnSearchChanged?.Invoke(currentResult);
 73        }
 074    }
 75
 76    public void GetResult(Action<List<T>> onResult)
 77    {
 2878        if (originalList == null)
 79        {
 680            onResult?.Invoke(null);
 681            return;
 82        }
 83
 2284        if (searchStringChanged)
 85        {
 2086            searchResult = string.IsNullOrEmpty(currentSearchString) ? originalList : SearchHelper.Search(currentSearchS
 2087            currentResult = searchResult;
 88        }
 89
 2290        if (searchFilterChanged || searchStringChanged)
 91        {
 2192            currentResult = currentFilterPredicate != null
 93                ? SearchHelper.Filter(searchResult, currentFilterPredicate)
 94                : searchResult;
 95        }
 96
 2297        if ((searchSortChanged || searchStringChanged) && currentResult != null && currentSortingType != null)
 98        {
 1599            SearchHelper.Sort(currentSortingType, currentResult, isDescendingSortOrder);
 100        }
 101
 22102        searchStringChanged = false;
 22103        searchFilterChanged = false;
 22104        searchSortChanged = false;
 105
 22106        onResult?.Invoke(currentResult);
 22107    }
 108
 109    public void NotifySearchChanged(string searchText)
 110    {
 6111        searchStringChanged = searchStringChanged || searchText != currentSearchString;
 6112        currentSearchString = searchText;
 6113        GetResult(OnSearchChanged);
 6114    }
 115
 116    public void NotifySortTypeChanged(string sortingType)
 117    {
 1118        searchSortChanged = searchSortChanged || currentSortingType != sortingType;
 1119        currentSortingType = sortingType;
 1120        GetResult(OnSearchChanged);
 1121    }
 122
 123    public void NotifySortOrderChanged(bool isDescending)
 124    {
 1125        searchSortChanged = searchSortChanged || isDescendingSortOrder != isDescending;
 1126        isDescendingSortOrder = isDescending;
 1127        GetResult(OnSearchChanged);
 1128    }
 129
 130    public void NotifyFilterChanged()
 131    {
 1132        searchFilterChanged = true;
 1133        GetResult(OnSearchChanged);
 1134    }
 135
 0136    public bool Match(T item) { return MatchSearch(item) && MatchFilter(item); }
 137
 0138    private bool MatchSearch(T item) { return SearchHelper.SearchMatchItem(currentSearchString, item); }
 139
 140    private bool MatchFilter(T item)
 141    {
 0142        if (currentFilterPredicate == null)
 0143            return true;
 144
 0145        return currentFilterPredicate(item);
 146    }
 147}