| | 1 | | using TMPro; |
| | 2 | | using UnityEngine; |
| | 3 | | using UnityEngine.UI; |
| | 4 | |
|
| | 5 | | internal class SearchBarConfig |
| | 6 | | { |
| | 7 | | public bool showFilterOwner; |
| | 8 | | public bool showFilterOperator; |
| | 9 | | public bool showFilterContributor; |
| | 10 | | public bool showResultLabel; |
| | 11 | | } |
| | 12 | |
|
| | 13 | | internal class SearchBarView : MonoBehaviour |
| | 14 | | { |
| | 15 | | private const string RESULT_FORMAT = "Results ({0})"; |
| | 16 | |
|
| | 17 | | [SerializeField] internal SearchInputField inputField; |
| | 18 | | [SerializeField] internal Button sortButton; |
| | 19 | | [SerializeField] internal TextMeshProUGUI sortTypeLabel; |
| | 20 | | [SerializeField] internal Toggle ownerToggle; |
| | 21 | | [SerializeField] internal Toggle operatorToggle; |
| | 22 | | [SerializeField] internal Toggle contributorToggle; |
| | 23 | | [SerializeField] private TextMeshProUGUI resultLabel; |
| | 24 | | [SerializeField] internal SortDropdownView sortDropdown; |
| | 25 | |
|
| | 26 | | private bool filterOwner = false; |
| | 27 | | private bool filterOperator = false; |
| | 28 | | private bool filterContributor = false; |
| | 29 | |
|
| | 30 | | private ISectionSearchHandler searchHandler; |
| | 31 | |
|
| | 32 | | private void Awake() |
| | 33 | | { |
| 11 | 34 | | sortButton.onClick.AddListener(OnSortButtonPressed); |
| | 35 | |
|
| 11 | 36 | | ownerToggle.onValueChanged.AddListener(OnToggleOwner); |
| 11 | 37 | | operatorToggle.onValueChanged.AddListener(OnToggleOperator); |
| 11 | 38 | | contributorToggle.onValueChanged.AddListener(OnToggleContributor); |
| | 39 | |
|
| 11 | 40 | | filterOwner = ownerToggle.isOn; |
| 11 | 41 | | filterOperator = operatorToggle.isOn; |
| 11 | 42 | | filterContributor = contributorToggle.isOn; |
| | 43 | |
|
| 14 | 44 | | inputField.OnSearchText += text => searchHandler?.SetSearchString(text); |
| 11 | 45 | | sortDropdown.OnSortTypeSelected += OnSortTypeSelected; |
| 11 | 46 | | } |
| | 47 | |
|
| 8 | 48 | | public void SetResultCount(int count) { resultLabel.text = string.Format(RESULT_FORMAT, count); } |
| | 49 | |
|
| | 50 | | public void ShowFilters(bool filterOwner, bool filterOperator, bool filterContributor) |
| | 51 | | { |
| 3 | 52 | | ownerToggle.gameObject.SetActive(filterOwner); |
| 3 | 53 | | operatorToggle.gameObject.SetActive(filterOperator); |
| 3 | 54 | | contributorToggle.gameObject.SetActive(filterContributor); |
| 3 | 55 | | } |
| | 56 | |
|
| 6 | 57 | | public void SetSortTypes(string[] types) { sortDropdown.AddSortType(types); } |
| | 58 | |
|
| | 59 | | private void OnSortButtonPressed() |
| | 60 | | { |
| 2 | 61 | | if (sortDropdown.GetSortTypesCount() > 1) |
| | 62 | | { |
| 1 | 63 | | sortDropdown.Show(); |
| | 64 | | } |
| 2 | 65 | | } |
| | 66 | |
|
| | 67 | | private void OnSortTypeSelected(string type) |
| | 68 | | { |
| 1 | 69 | | sortTypeLabel.text = type; |
| 1 | 70 | | searchHandler?.SetSortType(type); |
| 1 | 71 | | } |
| | 72 | |
|
| | 73 | | private void OnToggleOwner(bool isOn) |
| | 74 | | { |
| 2 | 75 | | filterOwner = isOn; |
| 2 | 76 | | ReportFilter(); |
| 2 | 77 | | } |
| | 78 | |
|
| | 79 | | private void OnToggleOperator(bool isOn) |
| | 80 | | { |
| 2 | 81 | | filterOperator = isOn; |
| 2 | 82 | | ReportFilter(); |
| 2 | 83 | | } |
| | 84 | |
|
| | 85 | | private void OnToggleContributor(bool isOn) |
| | 86 | | { |
| 2 | 87 | | filterContributor = isOn; |
| 2 | 88 | | ReportFilter(); |
| 2 | 89 | | } |
| | 90 | |
|
| 12 | 91 | | private void ReportFilter() { searchHandler?.SetFilter(filterOwner, filterOperator, filterContributor); } |
| | 92 | |
|
| | 93 | | public void SetSearchBar(ISectionSearchHandler handler, SearchBarConfig config) |
| | 94 | | { |
| 5 | 95 | | if (searchHandler != null) |
| | 96 | | { |
| 0 | 97 | | searchHandler.OnUpdated -= OnUpdateResultCount; |
| | 98 | | } |
| | 99 | |
|
| 5 | 100 | | searchHandler = handler; |
| | 101 | |
|
| 5 | 102 | | if (searchHandler == null) |
| | 103 | | { |
| 1 | 104 | | gameObject.SetActive(false); |
| 1 | 105 | | return; |
| | 106 | | } |
| 4 | 107 | | gameObject.SetActive(true); |
| | 108 | |
|
| 4 | 109 | | sortDropdown.Clear(); |
| 4 | 110 | | sortDropdown.AddSortType(handler.sortTypes); |
| | 111 | |
|
| 4 | 112 | | if (config != null) |
| | 113 | | { |
| 1 | 114 | | ShowFilters(config.showFilterOwner, config.showFilterOperator, config.showFilterContributor); |
| 1 | 115 | | resultLabel.gameObject.SetActive(config.showResultLabel); |
| | 116 | | } |
| | 117 | |
|
| 4 | 118 | | ownerToggle.SetIsOnWithoutNotify(handler.filterOwner); |
| 4 | 119 | | operatorToggle.SetIsOnWithoutNotify(handler.filterOperator); |
| 4 | 120 | | contributorToggle.SetIsOnWithoutNotify(handler.filterContributor); |
| 4 | 121 | | filterOwner = handler.filterOwner; |
| 4 | 122 | | filterOperator = handler.filterOperator; |
| 4 | 123 | | filterContributor = handler.filterContributor; |
| | 124 | |
|
| 4 | 125 | | sortTypeLabel.text = handler.sortType; |
| 4 | 126 | | inputField.inputField.SetTextWithoutNotify(handler.searchString); |
| 4 | 127 | | SetResultCount(handler.resultCount); |
| | 128 | |
|
| 4 | 129 | | searchHandler.OnUpdated += OnUpdateResultCount; |
| 4 | 130 | | } |
| | 131 | |
|
| | 132 | | private void OnUpdateResultCount() |
| | 133 | | { |
| 0 | 134 | | if (searchHandler == null) |
| 0 | 135 | | return; |
| 0 | 136 | | SetResultCount(searchHandler.resultCount); |
| 0 | 137 | | } |
| | 138 | | } |