| | 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, bool notifyInputField = true, bool notify = true); |
| | 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 | | /// <summary> |
| | 40 | | /// Set the focus on the text input. |
| | 41 | | /// </summary> |
| | 42 | | void SetFocus(); |
| | 43 | | } |
| | 44 | |
|
| | 45 | | public class SearchBarComponentView : BaseComponentView, ISearchBarComponentView, IComponentModelConfig<SearchBarCompone |
| | 46 | | { |
| | 47 | | [Header("Prefab References")] |
| | 48 | | [SerializeField] internal TMP_InputField inputField; |
| | 49 | | [SerializeField] internal TMP_Text placeHolderText; |
| | 50 | | [SerializeField] internal GameObject searchSpinner; |
| | 51 | | [SerializeField] internal Button clearSearchButton; |
| | 52 | |
|
| | 53 | | [Header("Configuration")] |
| | 54 | | [SerializeField] internal SearchBarComponentModel model; |
| | 55 | |
|
| | 56 | | public event Action<string> OnSearchText; |
| | 57 | | public event Action<string> OnSubmit; |
| | 58 | | public event Action<string> OnSearchValueChanged; |
| | 59 | | public event Action<bool> OnSelected; |
| | 60 | |
|
| | 61 | | internal Coroutine searchWhileTypingRoutine; |
| | 62 | | internal float lastValueChangeTime = 0; |
| | 63 | |
|
| 30 | 64 | | public string Text => inputField.text; |
| | 65 | |
|
| | 66 | | public override void Awake() |
| | 67 | | { |
| 204 | 68 | | base.Awake(); |
| | 69 | |
|
| 204 | 70 | | inputField.onValueChanged.AddListener(OnValueChanged); |
| 204 | 71 | | inputField.onSubmit.AddListener(s => SubmitSearch(s)); |
| 204 | 72 | | clearSearchButton.onClick.AddListener(() => ClearSearch()); |
| 204 | 73 | | inputField.onSelect.RemoveAllListeners(); |
| 204 | 74 | | inputField.onSelect.AddListener((text)=>OnSelected?.Invoke(true)); |
| | 75 | |
|
| 204 | 76 | | SetClearMode(); |
| 204 | 77 | | } |
| | 78 | |
|
| | 79 | | public void Configure(SearchBarComponentModel newModel) |
| | 80 | | { |
| 2 | 81 | | model = newModel; |
| 2 | 82 | | RefreshControl(); |
| 2 | 83 | | } |
| | 84 | |
|
| | 85 | | public override void RefreshControl() |
| | 86 | | { |
| 2 | 87 | | if (model == null) |
| 0 | 88 | | return; |
| | 89 | |
|
| 2 | 90 | | SetPlaceHolderText(model.placeHolderText); |
| 2 | 91 | | } |
| | 92 | |
|
| | 93 | | public void SetPlaceHolderText(string value) |
| | 94 | | { |
| 334 | 95 | | model.placeHolderText = value; |
| | 96 | |
|
| 334 | 97 | | if (placeHolderText == null) |
| 0 | 98 | | return; |
| | 99 | |
|
| 334 | 100 | | placeHolderText.text = value; |
| 334 | 101 | | } |
| | 102 | |
|
| | 103 | | public void SubmitSearch(string value, bool notifyInputField = true, bool notify = true) |
| | 104 | | { |
| 6 | 105 | | StopSearchCoroutine(); |
| | 106 | |
|
| 6 | 107 | | if (notifyInputField) |
| 6 | 108 | | inputField.text = value; |
| | 109 | | else |
| 0 | 110 | | inputField.SetTextWithoutNotify(value); |
| | 111 | |
|
| 6 | 112 | | SetSearchMode(); |
| | 113 | |
|
| 6 | 114 | | if (notify) |
| | 115 | | { |
| 6 | 116 | | OnSearchText?.Invoke(value); |
| 6 | 117 | | OnSubmit?.Invoke(value); |
| | 118 | | } |
| 0 | 119 | | } |
| | 120 | |
|
| | 121 | | public void ClearSearch(bool notify = true) |
| | 122 | | { |
| 401 | 123 | | StopSearchCoroutine(); |
| | 124 | |
|
| 401 | 125 | | inputField.SetTextWithoutNotify(string.Empty); |
| 401 | 126 | | SetClearMode(); |
| | 127 | |
|
| 401 | 128 | | if (notify) |
| 358 | 129 | | OnSearchText?.Invoke(string.Empty); |
| 71 | 130 | | } |
| | 131 | |
|
| 2 | 132 | | public void SetIdleSearchTime(float idleSearchTime) { model.idleTimeToTriggerSearch = idleSearchTime; } |
| | 133 | |
|
| | 134 | | public void SetFocus() |
| | 135 | | { |
| 8 | 136 | | inputField.Select(); |
| 8 | 137 | | } |
| | 138 | |
|
| | 139 | | public override void Dispose() |
| | 140 | | { |
| 225 | 141 | | base.Dispose(); |
| | 142 | |
|
| 225 | 143 | | inputField.onValueChanged.RemoveAllListeners(); |
| 225 | 144 | | inputField.onSubmit.RemoveAllListeners(); |
| 225 | 145 | | inputField.onSelect.RemoveAllListeners(); |
| 225 | 146 | | inputField.onDeselect.RemoveAllListeners(); |
| 225 | 147 | | clearSearchButton.onClick.RemoveAllListeners(); |
| | 148 | |
|
| 225 | 149 | | StopSearchCoroutine(); |
| 225 | 150 | | } |
| | 151 | |
|
| | 152 | | internal void OnValueChanged(string value) |
| | 153 | | { |
| 12 | 154 | | OnSearchValueChanged?.Invoke(value); |
| 12 | 155 | | AudioScriptableObjects.input.Play(true); |
| 12 | 156 | | if (model.idleTimeToTriggerSearch < 0) |
| 0 | 157 | | return; |
| | 158 | |
|
| 12 | 159 | | lastValueChangeTime = Time.unscaledTime; |
| | 160 | |
|
| 12 | 161 | | if (searchWhileTypingRoutine == null) |
| 12 | 162 | | searchWhileTypingRoutine = StartCoroutine(SearchWhileTyping()); |
| 12 | 163 | | } |
| | 164 | |
|
| | 165 | | internal IEnumerator SearchWhileTyping() |
| | 166 | | { |
| 12 | 167 | | SetTypingMode(); |
| | 168 | |
|
| 12 | 169 | | while ((Time.unscaledTime - lastValueChangeTime) < model.idleTimeToTriggerSearch) |
| | 170 | | { |
| 9 | 171 | | yield return null; |
| | 172 | | } |
| | 173 | |
|
| 3 | 174 | | string value = inputField.text; |
| 3 | 175 | | if (string.IsNullOrEmpty(value)) |
| | 176 | | { |
| 0 | 177 | | SetClearMode(); |
| | 178 | | } |
| | 179 | | else |
| | 180 | | { |
| 3 | 181 | | SetSearchMode(); |
| | 182 | | } |
| | 183 | |
|
| 3 | 184 | | OnSearchText?.Invoke(value); |
| 3 | 185 | | searchWhileTypingRoutine = null; |
| 3 | 186 | | } |
| | 187 | |
|
| | 188 | | internal void StopSearchCoroutine() |
| | 189 | | { |
| 632 | 190 | | if (searchWhileTypingRoutine != null) |
| | 191 | | { |
| 9 | 192 | | StopCoroutine(searchWhileTypingRoutine); |
| 9 | 193 | | searchWhileTypingRoutine = null; |
| | 194 | | } |
| 632 | 195 | | } |
| | 196 | |
|
| | 197 | | internal void SetTypingMode() |
| | 198 | | { |
| 13 | 199 | | clearSearchButton.gameObject.SetActive(false); |
| 13 | 200 | | searchSpinner.SetActive(true); |
| 13 | 201 | | } |
| | 202 | |
|
| | 203 | | internal void SetSearchMode() |
| | 204 | | { |
| 11 | 205 | | clearSearchButton.gameObject.SetActive(!string.IsNullOrEmpty(inputField.text)); |
| 11 | 206 | | searchSpinner.SetActive(false); |
| 11 | 207 | | } |
| | 208 | |
|
| | 209 | | internal void SetClearMode() |
| | 210 | | { |
| 606 | 211 | | clearSearchButton.gameObject.SetActive(false); |
| 606 | 212 | | searchSpinner.SetActive(false); |
| 606 | 213 | | } |
| | 214 | | } |