| | 1 | | using System; |
| | 2 | | using System.Collections; |
| | 3 | | using TMPro; |
| | 4 | | using UnityEngine; |
| | 5 | | using UnityEngine.UI; |
| | 6 | |
|
| | 7 | | internal class SearchInputField : MonoBehaviour |
| | 8 | | { |
| | 9 | | public event Action<string> OnSearchText; |
| | 10 | |
|
| | 11 | | [SerializeField] internal TMP_InputField inputField; |
| | 12 | | [SerializeField] internal GameObject searchSpinner; |
| | 13 | | [SerializeField] internal Button clearSearchButton; |
| 57 | 14 | | [SerializeField] internal float idleTimeToTriggerSearch = 1; |
| | 15 | |
|
| | 16 | | private Coroutine searchWhileTypingRoutine; |
| | 17 | | private float lastValueChangeTime = 0; |
| | 18 | |
|
| 4 | 19 | | public void ShowSearchSpinner() { SetTypingMode(); } |
| | 20 | |
|
| 2 | 21 | | public void ShowSearchClearButton() { SetSearchMode(); } |
| | 22 | |
|
| 12 | 23 | | public void ClearSearch() { OnClear(); } |
| | 24 | |
|
| 0 | 25 | | public void SetIdleSearchTime(float idleSearchTime) { idleTimeToTriggerSearch = idleSearchTime; } |
| | 26 | |
|
| | 27 | | private void Awake() |
| | 28 | | { |
| 17 | 29 | | inputField.onValueChanged.AddListener(OnValueChanged); |
| 17 | 30 | | inputField.onSubmit.AddListener(OnSubmit); |
| 17 | 31 | | clearSearchButton.onClick.AddListener(OnClear); |
| | 32 | |
|
| 17 | 33 | | SetClearMode(); |
| 17 | 34 | | } |
| | 35 | |
|
| | 36 | | private void OnValueChanged(string value) |
| | 37 | | { |
| 2 | 38 | | if (idleTimeToTriggerSearch < 0) |
| 0 | 39 | | return; |
| | 40 | |
|
| 2 | 41 | | lastValueChangeTime = Time.unscaledTime; |
| 2 | 42 | | StartSearchWhileTyping(); |
| 2 | 43 | | } |
| | 44 | |
|
| | 45 | | internal void OnSubmit(string value) |
| | 46 | | { |
| 3 | 47 | | StopSearchWhileTyping(); |
| 3 | 48 | | SetSearchMode(); |
| 3 | 49 | | OnSearchText?.Invoke(value); |
| 3 | 50 | | } |
| | 51 | |
|
| | 52 | | private void OnClear() |
| | 53 | | { |
| 7 | 54 | | inputField.SetTextWithoutNotify(string.Empty); |
| 7 | 55 | | SetClearMode(); |
| 7 | 56 | | OnSearchText?.Invoke(string.Empty); |
| 7 | 57 | | } |
| | 58 | |
|
| | 59 | | private void StartSearchWhileTyping() |
| | 60 | | { |
| 2 | 61 | | if (searchWhileTypingRoutine != null) |
| 0 | 62 | | return; |
| | 63 | |
|
| 2 | 64 | | searchWhileTypingRoutine = StartCoroutine(SearchWhileTypingRoutine()); |
| 2 | 65 | | } |
| | 66 | |
|
| | 67 | | private void StopSearchWhileTyping() |
| | 68 | | { |
| 3 | 69 | | if (searchWhileTypingRoutine == null) |
| 2 | 70 | | return; |
| | 71 | |
|
| 1 | 72 | | StopCoroutine(searchWhileTypingRoutine); |
| 1 | 73 | | searchWhileTypingRoutine = null; |
| 1 | 74 | | } |
| | 75 | |
|
| | 76 | | private IEnumerator SearchWhileTypingRoutine() |
| | 77 | | { |
| 2 | 78 | | SetTypingMode(); |
| | 79 | |
|
| 217 | 80 | | while ((Time.unscaledTime - lastValueChangeTime) < idleTimeToTriggerSearch) |
| | 81 | | { |
| 216 | 82 | | yield return null; |
| | 83 | | } |
| | 84 | |
|
| 1 | 85 | | string value = inputField.text; |
| 1 | 86 | | if (string.IsNullOrEmpty(value)) |
| | 87 | | { |
| 0 | 88 | | SetClearMode(); |
| 0 | 89 | | } |
| | 90 | | else |
| | 91 | | { |
| 1 | 92 | | SetSearchMode(); |
| | 93 | | } |
| | 94 | |
|
| 1 | 95 | | OnSearchText?.Invoke(value); |
| 1 | 96 | | searchWhileTypingRoutine = null; |
| 1 | 97 | | } |
| | 98 | |
|
| | 99 | | private void SetTypingMode() |
| | 100 | | { |
| 4 | 101 | | clearSearchButton.gameObject.SetActive(false); |
| 4 | 102 | | searchSpinner.SetActive(true); |
| 4 | 103 | | } |
| | 104 | |
|
| | 105 | | private void SetSearchMode() |
| | 106 | | { |
| 5 | 107 | | clearSearchButton.gameObject.SetActive(true); |
| 5 | 108 | | searchSpinner.SetActive(false); |
| 5 | 109 | | } |
| | 110 | |
|
| | 111 | | private void SetClearMode() |
| | 112 | | { |
| 24 | 113 | | clearSearchButton.gameObject.SetActive(false); |
| 24 | 114 | | searchSpinner.SetActive(false); |
| 24 | 115 | | } |
| | 116 | | } |