| | 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 | | /// <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 | |
|
| | 59 | | internal Coroutine searchWhileTypingRoutine; |
| | 60 | | internal float lastValueChangeTime = 0; |
| | 61 | |
|
| 0 | 62 | | public string Text => inputField.text; |
| | 63 | |
|
| | 64 | | public override void Awake() |
| | 65 | | { |
| 119 | 66 | | base.Awake(); |
| | 67 | |
|
| 119 | 68 | | inputField.onValueChanged.AddListener(OnValueChanged); |
| 119 | 69 | | inputField.onSubmit.AddListener(SubmitSearch); |
| 119 | 70 | | clearSearchButton.onClick.AddListener(() => ClearSearch()); |
| | 71 | |
|
| 119 | 72 | | SetClearMode(); |
| 119 | 73 | | } |
| | 74 | |
|
| | 75 | | public void Configure(SearchBarComponentModel newModel) |
| | 76 | | { |
| 32 | 77 | | model = newModel; |
| 32 | 78 | | RefreshControl(); |
| 32 | 79 | | } |
| | 80 | |
|
| | 81 | | public override void RefreshControl() |
| | 82 | | { |
| 32 | 83 | | if (model == null) |
| 0 | 84 | | return; |
| | 85 | |
|
| 32 | 86 | | SetPlaceHolderText(model.placeHolderText); |
| 32 | 87 | | } |
| | 88 | |
|
| | 89 | | public void SetPlaceHolderText(string value) |
| | 90 | | { |
| 158 | 91 | | model.placeHolderText = value; |
| | 92 | |
|
| 158 | 93 | | if (placeHolderText == null) |
| 0 | 94 | | return; |
| | 95 | |
|
| 158 | 96 | | placeHolderText.text = value; |
| 158 | 97 | | } |
| | 98 | |
|
| | 99 | | public void SubmitSearch(string value) |
| | 100 | | { |
| 5 | 101 | | StopSearchCoroutine(); |
| | 102 | |
|
| 5 | 103 | | inputField.text = value; |
| 5 | 104 | | SetSearchMode(); |
| 5 | 105 | | OnSearchText?.Invoke(value); |
| 5 | 106 | | OnSubmit?.Invoke(value); |
| 0 | 107 | | } |
| | 108 | |
|
| | 109 | | public void ClearSearch(bool notify = true) |
| | 110 | | { |
| 155 | 111 | | StopSearchCoroutine(); |
| | 112 | |
|
| 155 | 113 | | inputField.SetTextWithoutNotify(string.Empty); |
| 155 | 114 | | SetClearMode(); |
| | 115 | |
|
| 155 | 116 | | if (notify) |
| 151 | 117 | | OnSearchText?.Invoke(string.Empty); |
| 31 | 118 | | } |
| | 119 | |
|
| 0 | 120 | | public void SetIdleSearchTime(float idleSearchTime) { model.idleTimeToTriggerSearch = idleSearchTime; } |
| | 121 | |
|
| | 122 | | public void SetFocus() |
| | 123 | | { |
| 8 | 124 | | inputField.Select(); |
| 8 | 125 | | } |
| | 126 | |
|
| | 127 | | public override void Dispose() |
| | 128 | | { |
| 127 | 129 | | base.Dispose(); |
| | 130 | |
|
| 127 | 131 | | inputField.onValueChanged.RemoveAllListeners(); |
| 127 | 132 | | inputField.onSubmit.RemoveAllListeners(); |
| 127 | 133 | | inputField.onSelect.RemoveAllListeners(); |
| 127 | 134 | | inputField.onDeselect.RemoveAllListeners(); |
| 127 | 135 | | clearSearchButton.onClick.RemoveAllListeners(); |
| | 136 | |
|
| 127 | 137 | | StopSearchCoroutine(); |
| 127 | 138 | | } |
| | 139 | |
|
| | 140 | | internal void OnValueChanged(string value) |
| | 141 | | { |
| 10 | 142 | | if (model.idleTimeToTriggerSearch < 0) |
| 0 | 143 | | return; |
| | 144 | |
|
| 10 | 145 | | lastValueChangeTime = Time.unscaledTime; |
| | 146 | |
|
| 10 | 147 | | if (searchWhileTypingRoutine == null) |
| 10 | 148 | | searchWhileTypingRoutine = StartCoroutine(SearchWhileTyping()); |
| 10 | 149 | | } |
| | 150 | |
|
| | 151 | | internal IEnumerator SearchWhileTyping() |
| | 152 | | { |
| 10 | 153 | | SetTypingMode(); |
| | 154 | |
|
| 10 | 155 | | while ((Time.unscaledTime - lastValueChangeTime) < model.idleTimeToTriggerSearch) |
| | 156 | | { |
| 8 | 157 | | yield return null; |
| | 158 | | } |
| | 159 | |
|
| 2 | 160 | | string value = inputField.text; |
| 2 | 161 | | if (string.IsNullOrEmpty(value)) |
| | 162 | | { |
| 0 | 163 | | SetClearMode(); |
| 0 | 164 | | } |
| | 165 | | else |
| | 166 | | { |
| 2 | 167 | | SetSearchMode(); |
| | 168 | | } |
| | 169 | |
|
| 2 | 170 | | OnSearchText?.Invoke(value); |
| 2 | 171 | | searchWhileTypingRoutine = null; |
| 2 | 172 | | } |
| | 173 | |
|
| | 174 | | internal void StopSearchCoroutine() |
| | 175 | | { |
| 287 | 176 | | if (searchWhileTypingRoutine != null) |
| | 177 | | { |
| 8 | 178 | | StopCoroutine(searchWhileTypingRoutine); |
| 8 | 179 | | searchWhileTypingRoutine = null; |
| | 180 | | } |
| 287 | 181 | | } |
| | 182 | |
|
| | 183 | | internal void SetTypingMode() |
| | 184 | | { |
| 11 | 185 | | clearSearchButton.gameObject.SetActive(false); |
| 11 | 186 | | searchSpinner.SetActive(true); |
| 11 | 187 | | } |
| | 188 | |
|
| | 189 | | internal void SetSearchMode() |
| | 190 | | { |
| 8 | 191 | | clearSearchButton.gameObject.SetActive(true); |
| 8 | 192 | | searchSpinner.SetActive(false); |
| 8 | 193 | | } |
| | 194 | |
|
| | 195 | | internal void SetClearMode() |
| | 196 | | { |
| 275 | 197 | | clearSearchButton.gameObject.SetActive(false); |
| 275 | 198 | | searchSpinner.SetActive(false); |
| 275 | 199 | | } |
| | 200 | | } |