< Summary

Class:SearchBarComponentView
Assembly:UIComponents
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/UIComponents/Scripts/Components/SearchBar/SearchBarComponentView.cs
Covered lines:72
Uncovered lines:6
Coverable lines:78
Total lines:214
Line coverage:92.3% (72 of 78)
Covered branches:0
Total branches:0
Covered methods:16
Total methods:16
Method coverage:100% (16 of 16)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
Awake()0%110100%
Configure(...)0%110100%
RefreshControl()0%2.062075%
SetPlaceHolderText(...)0%2.032080%
SubmitSearch(...)0%5.275077.78%
ClearSearch(...)0%330100%
SetIdleSearchTime(...)0%110100%
SetFocus()0%110100%
Dispose()0%110100%
OnValueChanged(...)0%4.034087.5%
SearchWhileTyping()0%6.046090%
StopSearchCoroutine()0%220100%
SetTypingMode()0%110100%
SetSearchMode()0%110100%
SetClearMode()0%110100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/UIComponents/Scripts/Components/SearchBar/SearchBarComponentView.cs

#LineLine coverage
 1using System;
 2using System.Collections;
 3using TMPro;
 4using UnityEngine;
 5using UnityEngine.UI;
 6
 7public 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
 45public 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
 3064    public string Text => inputField.text;
 65
 66    public override void Awake()
 67    {
 20468        base.Awake();
 69
 20470        inputField.onValueChanged.AddListener(OnValueChanged);
 20471        inputField.onSubmit.AddListener(s => SubmitSearch(s));
 20472        clearSearchButton.onClick.AddListener(() => ClearSearch());
 20473        inputField.onSelect.RemoveAllListeners();
 20474        inputField.onSelect.AddListener((text)=>OnSelected?.Invoke(true));
 75
 20476        SetClearMode();
 20477    }
 78
 79    public void Configure(SearchBarComponentModel newModel)
 80    {
 281        model = newModel;
 282        RefreshControl();
 283    }
 84
 85    public override void RefreshControl()
 86    {
 287        if (model == null)
 088            return;
 89
 290        SetPlaceHolderText(model.placeHolderText);
 291    }
 92
 93    public void SetPlaceHolderText(string value)
 94    {
 33495        model.placeHolderText = value;
 96
 33497        if (placeHolderText == null)
 098            return;
 99
 334100        placeHolderText.text = value;
 334101    }
 102
 103    public void SubmitSearch(string value, bool notifyInputField = true, bool notify = true)
 104    {
 6105        StopSearchCoroutine();
 106
 6107        if (notifyInputField)
 6108            inputField.text = value;
 109        else
 0110            inputField.SetTextWithoutNotify(value);
 111
 6112        SetSearchMode();
 113
 6114        if (notify)
 115        {
 6116            OnSearchText?.Invoke(value);
 6117            OnSubmit?.Invoke(value);
 118        }
 0119    }
 120
 121    public void ClearSearch(bool notify = true)
 122    {
 401123        StopSearchCoroutine();
 124
 401125        inputField.SetTextWithoutNotify(string.Empty);
 401126        SetClearMode();
 127
 401128        if (notify)
 358129            OnSearchText?.Invoke(string.Empty);
 71130    }
 131
 2132    public void SetIdleSearchTime(float idleSearchTime) { model.idleTimeToTriggerSearch = idleSearchTime; }
 133
 134    public void SetFocus()
 135    {
 8136        inputField.Select();
 8137    }
 138
 139    public override void Dispose()
 140    {
 225141        base.Dispose();
 142
 225143        inputField.onValueChanged.RemoveAllListeners();
 225144        inputField.onSubmit.RemoveAllListeners();
 225145        inputField.onSelect.RemoveAllListeners();
 225146        inputField.onDeselect.RemoveAllListeners();
 225147        clearSearchButton.onClick.RemoveAllListeners();
 148
 225149        StopSearchCoroutine();
 225150    }
 151
 152    internal void OnValueChanged(string value)
 153    {
 12154        OnSearchValueChanged?.Invoke(value);
 12155        AudioScriptableObjects.input.Play(true);
 12156        if (model.idleTimeToTriggerSearch < 0)
 0157            return;
 158
 12159        lastValueChangeTime = Time.unscaledTime;
 160
 12161        if (searchWhileTypingRoutine == null)
 12162            searchWhileTypingRoutine = StartCoroutine(SearchWhileTyping());
 12163    }
 164
 165    internal IEnumerator SearchWhileTyping()
 166    {
 12167        SetTypingMode();
 168
 12169        while ((Time.unscaledTime - lastValueChangeTime) < model.idleTimeToTriggerSearch)
 170        {
 9171            yield return null;
 172        }
 173
 3174        string value = inputField.text;
 3175        if (string.IsNullOrEmpty(value))
 176        {
 0177            SetClearMode();
 178        }
 179        else
 180        {
 3181            SetSearchMode();
 182        }
 183
 3184        OnSearchText?.Invoke(value);
 3185        searchWhileTypingRoutine = null;
 3186    }
 187
 188    internal void StopSearchCoroutine()
 189    {
 632190        if (searchWhileTypingRoutine != null)
 191        {
 9192            StopCoroutine(searchWhileTypingRoutine);
 9193            searchWhileTypingRoutine = null;
 194        }
 632195    }
 196
 197    internal void SetTypingMode()
 198    {
 13199        clearSearchButton.gameObject.SetActive(false);
 13200        searchSpinner.SetActive(true);
 13201    }
 202
 203    internal void SetSearchMode()
 204    {
 11205        clearSearchButton.gameObject.SetActive(!string.IsNullOrEmpty(inputField.text));
 11206        searchSpinner.SetActive(false);
 11207    }
 208
 209    internal void SetClearMode()
 210    {
 606211        clearSearchButton.gameObject.SetActive(false);
 606212        searchSpinner.SetActive(false);
 606213    }
 214}