| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | |
|
| | 4 | | public class SearchHandler<T> where T : ISearchable, ISortable<T> |
| | 5 | | { |
| | 6 | | public event Action<List<T>> OnSearchChanged; |
| | 7 | |
|
| 0 | 8 | | public string[] sortingTypes { get; } |
| 30 | 9 | | public bool isDescendingSortOrder { get; private set; } = true; |
| 0 | 10 | | public string currentSearchString { get; private set; } |
| 0 | 11 | | public string currentSortingType { get; private set; } |
| 0 | 12 | | 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 | |
|
| 30 | 24 | | public SearchHandler(string[] sortingTypes, Predicate<T> filterPredicate) |
| | 25 | | { |
| 30 | 26 | | this.sortingTypes = sortingTypes; |
| 30 | 27 | | this.currentFilterPredicate = filterPredicate; |
| 30 | 28 | | this.currentSortingType = sortingTypes?[0]; |
| 30 | 29 | | } |
| | 30 | |
|
| 34 | 31 | | public SearchHandler() : this(null, null) { } |
| | 32 | |
|
| | 33 | | public void SetSearchableList(List<T> list) |
| | 34 | | { |
| 19 | 35 | | originalList = list; |
| 19 | 36 | | searchResult = list; |
| 19 | 37 | | currentResult = list; |
| | 38 | |
|
| 19 | 39 | | searchStringChanged = true; |
| 19 | 40 | | searchFilterChanged = true; |
| 19 | 41 | | searchSortChanged = true; |
| | 42 | |
|
| 19 | 43 | | GetResult(OnSearchChanged); |
| 19 | 44 | | } |
| | 45 | |
|
| | 46 | | public void AddItem(T item) |
| | 47 | | { |
| 0 | 48 | | originalList.Add(item); |
| 0 | 49 | | bool matchSearch = MatchSearch(item); |
| 0 | 50 | | bool matchResult = matchSearch && MatchFilter(item); |
| | 51 | |
|
| 0 | 52 | | if (matchSearch) |
| | 53 | | { |
| 0 | 54 | | searchResult.Add(item); |
| | 55 | | } |
| | 56 | |
|
| 0 | 57 | | if (matchResult) |
| | 58 | | { |
| 0 | 59 | | currentResult.Add(item); |
| 0 | 60 | | searchSortChanged = true; |
| 0 | 61 | | GetResult(OnSearchChanged); |
| | 62 | | } |
| 0 | 63 | | } |
| | 64 | |
|
| | 65 | | public void RemoveItem(T item) |
| | 66 | | { |
| 0 | 67 | | originalList.Remove(item); |
| 0 | 68 | | searchResult.Remove(item); |
| 0 | 69 | | bool inResult = currentResult.Remove(item); |
| 0 | 70 | | if (inResult) |
| | 71 | | { |
| 0 | 72 | | OnSearchChanged?.Invoke(currentResult); |
| | 73 | | } |
| 0 | 74 | | } |
| | 75 | |
|
| | 76 | | public void GetResult(Action<List<T>> onResult) |
| | 77 | | { |
| 28 | 78 | | if (originalList == null) |
| | 79 | | { |
| 6 | 80 | | onResult?.Invoke(null); |
| 6 | 81 | | return; |
| | 82 | | } |
| | 83 | |
|
| 22 | 84 | | if (searchStringChanged) |
| | 85 | | { |
| 20 | 86 | | searchResult = string.IsNullOrEmpty(currentSearchString) ? originalList : SearchHelper.Search(currentSearchS |
| 20 | 87 | | currentResult = searchResult; |
| | 88 | | } |
| | 89 | |
|
| 22 | 90 | | if (searchFilterChanged || searchStringChanged) |
| | 91 | | { |
| 21 | 92 | | currentResult = currentFilterPredicate != null |
| | 93 | | ? SearchHelper.Filter(searchResult, currentFilterPredicate) |
| | 94 | | : searchResult; |
| | 95 | | } |
| | 96 | |
|
| 22 | 97 | | if ((searchSortChanged || searchStringChanged) && currentResult != null && currentSortingType != null) |
| | 98 | | { |
| 15 | 99 | | SearchHelper.Sort(currentSortingType, currentResult, isDescendingSortOrder); |
| | 100 | | } |
| | 101 | |
|
| 22 | 102 | | searchStringChanged = false; |
| 22 | 103 | | searchFilterChanged = false; |
| 22 | 104 | | searchSortChanged = false; |
| | 105 | |
|
| 22 | 106 | | onResult?.Invoke(currentResult); |
| 22 | 107 | | } |
| | 108 | |
|
| | 109 | | public void NotifySearchChanged(string searchText) |
| | 110 | | { |
| 6 | 111 | | searchStringChanged = searchStringChanged || searchText != currentSearchString; |
| 6 | 112 | | currentSearchString = searchText; |
| 6 | 113 | | GetResult(OnSearchChanged); |
| 6 | 114 | | } |
| | 115 | |
|
| | 116 | | public void NotifySortTypeChanged(string sortingType) |
| | 117 | | { |
| 1 | 118 | | searchSortChanged = searchSortChanged || currentSortingType != sortingType; |
| 1 | 119 | | currentSortingType = sortingType; |
| 1 | 120 | | GetResult(OnSearchChanged); |
| 1 | 121 | | } |
| | 122 | |
|
| | 123 | | public void NotifySortOrderChanged(bool isDescending) |
| | 124 | | { |
| 1 | 125 | | searchSortChanged = searchSortChanged || isDescendingSortOrder != isDescending; |
| 1 | 126 | | isDescendingSortOrder = isDescending; |
| 1 | 127 | | GetResult(OnSearchChanged); |
| 1 | 128 | | } |
| | 129 | |
|
| | 130 | | public void NotifyFilterChanged() |
| | 131 | | { |
| 1 | 132 | | searchFilterChanged = true; |
| 1 | 133 | | GetResult(OnSearchChanged); |
| 1 | 134 | | } |
| | 135 | |
|
| 0 | 136 | | public bool Match(T item) { return MatchSearch(item) && MatchFilter(item); } |
| | 137 | |
|
| 0 | 138 | | private bool MatchSearch(T item) { return SearchHelper.SearchMatchItem(currentSearchString, item); } |
| | 139 | |
|
| | 140 | | private bool MatchFilter(T item) |
| | 141 | | { |
| 0 | 142 | | if (currentFilterPredicate == null) |
| 0 | 143 | | return true; |
| | 144 | |
|
| 0 | 145 | | return currentFilterPredicate(item); |
| | 146 | | } |
| | 147 | | } |