| | 1 | | using System; |
| | 2 | | using System.Collections; |
| | 3 | | using TMPro; |
| | 4 | | using UnityEngine; |
| | 5 | | using UnityEngine.UI; |
| | 6 | |
|
| | 7 | | public interface ISearchBarComponentView |
| | 8 | | { |
| | 9 | | string Text { get; } |
| | 10 | |
|
| | 11 | | /// <summary> |
| | 12 | | /// Event that will be triggered when a search is ordered in the search component. |
| | 13 | | /// </summary> |
| | 14 | | event Action<string> OnSearchText; |
| | 15 | |
|
| | 16 | | /// <summary> |
| | 17 | | /// Set the place holder text of the search component. |
| | 18 | | /// </summary> |
| | 19 | | /// <param name="value"></param> |
| | 20 | | void SetPlaceHolderText(string value); |
| | 21 | |
|
| | 22 | | /// <summary> |
| | 23 | | /// Order a specific search. |
| | 24 | | /// </summary> |
| | 25 | | /// <param name="value">Text to search.</param> |
| | 26 | | void SubmitSearch(string value); |
| | 27 | |
|
| | 28 | | /// <summary> |
| | 29 | | /// Clear the search component. |
| | 30 | | /// </summary> |
| | 31 | | void ClearSearch(bool notify = true); |
| | 32 | |
|
| | 33 | | /// <summary> |
| | 34 | | /// Set the idle time to search. |
| | 35 | | /// </summary> |
| | 36 | | /// <param name="idleSearchTime">Time in seconds.</param> |
| | 37 | | void SetIdleSearchTime(float idleSearchTime); |
| | 38 | | } |
| | 39 | |
|
| | 40 | | public class SearchBarComponentView : BaseComponentView, ISearchBarComponentView, IComponentModelConfig<SearchBarCompone |
| | 41 | | { |
| | 42 | | [Header("Prefab References")] |
| | 43 | | [SerializeField] internal TMP_InputField inputField; |
| | 44 | | [SerializeField] internal TMP_Text placeHolderText; |
| | 45 | | [SerializeField] internal GameObject searchSpinner; |
| | 46 | | [SerializeField] internal Button clearSearchButton; |
| | 47 | |
|
| | 48 | | [Header("Configuration")] |
| | 49 | | [SerializeField] internal SearchBarComponentModel model; |
| | 50 | |
|
| | 51 | | public event Action<string> OnSearchText; |
| | 52 | | public event Action<string> OnSubmit; |
| | 53 | |
|
| | 54 | | internal Coroutine searchWhileTypingRoutine; |
| | 55 | | internal float lastValueChangeTime = 0; |
| | 56 | |
|
| 0 | 57 | | public string Text => inputField.text; |
| | 58 | |
|
| | 59 | | public override void Awake() |
| | 60 | | { |
| 61 | 61 | | base.Awake(); |
| | 62 | |
|
| 61 | 63 | | inputField.onValueChanged.AddListener(OnValueChanged); |
| 61 | 64 | | inputField.onSubmit.AddListener(SubmitSearch); |
| 61 | 65 | | inputField.onSelect.AddListener(SelectInput); |
| 61 | 66 | | inputField.onDeselect.AddListener(DeselectInput); |
| 61 | 67 | | clearSearchButton.onClick.AddListener(() => ClearSearch()); |
| | 68 | |
|
| 61 | 69 | | SetClearMode(); |
| 61 | 70 | | } |
| | 71 | |
|
| | 72 | | public void Configure(SearchBarComponentModel newModel) |
| | 73 | | { |
| 32 | 74 | | model = newModel; |
| 32 | 75 | | RefreshControl(); |
| 32 | 76 | | } |
| | 77 | |
|
| | 78 | | public override void RefreshControl() |
| | 79 | | { |
| 32 | 80 | | if (model == null) |
| 0 | 81 | | return; |
| | 82 | |
|
| 32 | 83 | | SetPlaceHolderText(model.placeHolderText); |
| 32 | 84 | | } |
| | 85 | |
|
| | 86 | | public void SetPlaceHolderText(string value) |
| | 87 | | { |
| 158 | 88 | | model.placeHolderText = value; |
| | 89 | |
|
| 158 | 90 | | if (placeHolderText == null) |
| 0 | 91 | | return; |
| | 92 | |
|
| 158 | 93 | | placeHolderText.text = value; |
| 158 | 94 | | } |
| | 95 | |
|
| | 96 | | public void SubmitSearch(string value) |
| | 97 | | { |
| 2 | 98 | | StopSearchCoroutine(); |
| | 99 | |
|
| 2 | 100 | | inputField.text = value; |
| 2 | 101 | | SetSearchMode(); |
| 2 | 102 | | OnSearchText?.Invoke(value); |
| 2 | 103 | | OnSubmit?.Invoke(value); |
| 0 | 104 | | } |
| | 105 | |
|
| | 106 | | public void ClearSearch(bool notify = true) |
| | 107 | | { |
| 148 | 108 | | StopSearchCoroutine(); |
| | 109 | |
|
| 148 | 110 | | inputField.SetTextWithoutNotify(string.Empty); |
| 148 | 111 | | SetClearMode(); |
| | 112 | |
|
| 148 | 113 | | if (notify) |
| 148 | 114 | | OnSearchText?.Invoke(string.Empty); |
| 24 | 115 | | } |
| | 116 | |
|
| 0 | 117 | | public void SetIdleSearchTime(float idleSearchTime) { model.idleTimeToTriggerSearch = idleSearchTime; } |
| | 118 | |
|
| | 119 | | public override void Dispose() |
| | 120 | | { |
| 72 | 121 | | base.Dispose(); |
| | 122 | |
|
| 72 | 123 | | inputField.onValueChanged.RemoveAllListeners(); |
| 72 | 124 | | inputField.onSubmit.RemoveAllListeners(); |
| 72 | 125 | | inputField.onSelect.RemoveAllListeners(); |
| 72 | 126 | | inputField.onDeselect.RemoveAllListeners(); |
| 72 | 127 | | clearSearchButton.onClick.RemoveAllListeners(); |
| | 128 | |
|
| 72 | 129 | | StopSearchCoroutine(); |
| 72 | 130 | | } |
| | 131 | |
|
| | 132 | | internal void OnValueChanged(string value) |
| | 133 | | { |
| 3 | 134 | | if (model.idleTimeToTriggerSearch < 0) |
| 0 | 135 | | return; |
| | 136 | |
|
| 3 | 137 | | lastValueChangeTime = Time.unscaledTime; |
| | 138 | |
|
| 3 | 139 | | if (searchWhileTypingRoutine == null) |
| 3 | 140 | | searchWhileTypingRoutine = StartCoroutine(SearchWhileTyping()); |
| 3 | 141 | | } |
| | 142 | |
|
| | 143 | | internal IEnumerator SearchWhileTyping() |
| | 144 | | { |
| 3 | 145 | | SetTypingMode(); |
| | 146 | |
|
| 3 | 147 | | while ((Time.unscaledTime - lastValueChangeTime) < model.idleTimeToTriggerSearch) |
| | 148 | | { |
| 1 | 149 | | yield return null; |
| | 150 | | } |
| | 151 | |
|
| 2 | 152 | | string value = inputField.text; |
| 2 | 153 | | if (string.IsNullOrEmpty(value)) |
| | 154 | | { |
| 0 | 155 | | SetClearMode(); |
| 0 | 156 | | } |
| | 157 | | else |
| | 158 | | { |
| 2 | 159 | | SetSearchMode(); |
| | 160 | | } |
| | 161 | |
|
| 2 | 162 | | OnSearchText?.Invoke(value); |
| 2 | 163 | | searchWhileTypingRoutine = null; |
| 2 | 164 | | } |
| | 165 | |
|
| | 166 | | internal void StopSearchCoroutine() |
| | 167 | | { |
| 222 | 168 | | if (searchWhileTypingRoutine != null) |
| | 169 | | { |
| 1 | 170 | | StopCoroutine(searchWhileTypingRoutine); |
| 1 | 171 | | searchWhileTypingRoutine = null; |
| | 172 | | } |
| 222 | 173 | | } |
| | 174 | |
|
| | 175 | | internal void SetTypingMode() |
| | 176 | | { |
| 4 | 177 | | clearSearchButton.gameObject.SetActive(false); |
| 4 | 178 | | searchSpinner.SetActive(true); |
| 4 | 179 | | } |
| | 180 | |
|
| | 181 | | internal void SetSearchMode() |
| | 182 | | { |
| 5 | 183 | | clearSearchButton.gameObject.SetActive(true); |
| 5 | 184 | | searchSpinner.SetActive(false); |
| 5 | 185 | | } |
| | 186 | |
|
| | 187 | | internal void SetClearMode() |
| | 188 | | { |
| 210 | 189 | | clearSearchButton.gameObject.SetActive(false); |
| 210 | 190 | | searchSpinner.SetActive(false); |
| 210 | 191 | | } |
| | 192 | |
|
| | 193 | | internal void SelectInput(string value) |
| | 194 | | { |
| 1 | 195 | | placeHolderText.gameObject.SetActive(false); |
| 1 | 196 | | } |
| | 197 | |
|
| | 198 | | internal void DeselectInput(string value) |
| | 199 | | { |
| 2 | 200 | | placeHolderText.gameObject.SetActive(true); |
| 2 | 201 | | } |
| | 202 | | } |