< Summary

Class:LimitInputField
Assembly:BuilderLimitInputField
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/BuilderInWorld/HUD/ProjectsPanelHUD/Scripts/LimitInputField/LimitInputField.cs
Covered lines:47
Uncovered lines:37
Coverable lines:84
Total lines:181
Line coverage:55.9% (47 of 84)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
LimitInputField()0%110100%
Start()0%330100%
HasFocus()0%2100%
SetCharacterLimit(...)0%2100%
SetText(...)0%3.043083.33%
SetError()0%2100%
InputLostFocus(...)0%30500%
InputFocused(...)0%20400%
InputChanged(...)0%110100%
InputChanged(...)0%14.189060%
IsInputAvailable()0%2100%
InputAvailable()0%6.446076.92%
Empty()0%3.073080%
LimitReached()0%4.024088.89%
GetValue()0%2100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/BuilderInWorld/HUD/ProjectsPanelHUD/Scripts/LimitInputField/LimitInputField.cs

#LineLine coverage
 1using System;
 2using System.Collections;
 3using System.Collections.Generic;
 4using TMPro;
 5using UnityEngine;
 6using UnityEngine.UI;
 7
 8public class LimitInputField : MonoBehaviour
 9{
 10    public event System.Action OnLimitReached;
 11    public event System.Action OnEmptyValue;
 12    public event System.Action OnInputAvailable;
 13    public event System.Action OnInputFocused;
 14    public event System.Action OnInputLostFocus;
 15
 16    public event System.Action<string> OnInputChange;
 17
 18    [SerializeField] private int characterLimit;
 19    [SerializeField] private bool isMandatory = false;
 20    [SerializeField] private bool stopInputFromLimit = false;
 7221    [SerializeField] private string placeHolderText = "";
 22    [SerializeField] private Color normalColor;
 23    [SerializeField] private Color errorColor;
 24    [SerializeField] private Color focusColor;
 25
 26    [SerializeField] private Image inputFieldbackgroundImg;
 27    [SerializeField] private TMP_InputField inputField;
 28    [SerializeField] private TextMeshProUGUI inputFieldPlaceHolderText;
 29    [SerializeField] private TextMeshProUGUI limitText;
 30
 31    internal bool hasPassedLimit = false;
 32    internal bool hasBeenEmpty = false;
 33    internal bool hasFocus = false;
 7234    private string currentValue = "";
 35
 36    private void Start()
 37    {
 3838        inputField.onSelect.AddListener(InputFocused);
 3839        inputField.onDeselect.AddListener(InputLostFocus);
 3840        inputField.onValueChanged.AddListener(InputChanged);
 3841        limitText?.SetText( currentValue.Length + "/" + characterLimit);
 3842        if (isMandatory)
 2943            hasBeenEmpty = true;
 3844    }
 45
 046    public bool HasFocus() => hasFocus;
 47
 48    public void SetCharacterLimit(int limit)
 49    {
 050        characterLimit = limit;
 051    }
 52
 53    public void SetText(string value)
 54    {
 255        if(string.IsNullOrEmpty(value) && inputFieldPlaceHolderText != null)
 056            inputFieldPlaceHolderText.text = placeHolderText;
 57
 258        currentValue = value;
 259        inputField.SetTextWithoutNotify(value);
 260        InputChanged(value, false);
 261    }
 62
 063    public void SetError() { LimitReached(); }
 64
 65    private void InputLostFocus(string value)
 66    {
 067        hasFocus = false;
 068        if(string.IsNullOrEmpty(currentValue) && inputFieldPlaceHolderText != null)
 069            inputFieldPlaceHolderText.text = placeHolderText;
 70
 071        if (hasPassedLimit)
 72        {
 073            inputFieldbackgroundImg.enabled = true;
 074            inputFieldbackgroundImg.color = errorColor;
 075        }
 76        else
 77        {
 078            inputFieldbackgroundImg.enabled = false;
 79        }
 80
 081        OnInputLostFocus?.Invoke();
 082    }
 83
 84    private void InputFocused(string value)
 85    {
 086        if(inputFieldPlaceHolderText != null)
 087            inputFieldPlaceHolderText.text = "";
 088        hasFocus = true;
 89
 090        inputFieldbackgroundImg.enabled = true;
 91
 092        if (hasPassedLimit)
 093            inputFieldbackgroundImg.color = errorColor;
 94        else
 095            inputFieldbackgroundImg.color = focusColor;
 96
 097        OnInputFocused?.Invoke();
 098    }
 99
 100    internal void InputChanged(string newValue)
 101    {
 1102        InputChanged(newValue, true);
 1103    }
 104
 105    internal void InputChanged(string newValue, bool invokeCallback)
 106    {
 3107        if (hasPassedLimit && newValue.Length > currentValue.Length && stopInputFromLimit)
 108        {
 0109            inputField.SetTextWithoutNotify(currentValue);
 0110            return;
 111        }
 3112        currentValue = newValue;
 3113        limitText?.SetText(newValue.Length + "/" + characterLimit);
 3114        if (newValue.Length > characterLimit)
 115        {
 0116            LimitReached();
 0117        }
 3118        else if (currentValue.Length == 0)
 119        {
 0120            Empty();
 0121        }
 122        else
 123        {
 3124            InputAvailable();
 125        }
 126
 3127        if(invokeCallback)
 1128            OnInputChange?.Invoke(newValue);
 3129    }
 130
 0131    public bool IsInputAvailable() => !hasPassedLimit;
 132
 133    public void InputAvailable()
 134    {
 8135        if (!hasPassedLimit && !hasBeenEmpty)
 7136            return;
 137
 1138        if (limitText != null)
 1139            limitText.color = normalColor;
 140
 1141        if (hasFocus)
 142        {
 0143            inputFieldbackgroundImg.enabled = true;
 0144            inputFieldbackgroundImg.color = focusColor;
 0145        }
 146        else
 147        {
 1148            inputFieldbackgroundImg.enabled = false;
 149        }
 150
 1151        hasPassedLimit = false;
 1152        hasBeenEmpty = false;
 153
 1154        OnInputAvailable?.Invoke();
 1155    }
 156
 157    internal void Empty()
 158    {
 1159        if (hasBeenEmpty)
 0160            return;
 161
 1162        hasBeenEmpty = true;
 1163        OnEmptyValue?.Invoke();
 1164    }
 165
 166    public void LimitReached()
 167    {
 1168        if (hasPassedLimit)
 0169            return;
 170
 1171        if (limitText != null)
 1172            limitText.color = errorColor;
 1173        inputFieldbackgroundImg.enabled = true;
 1174        inputFieldbackgroundImg.color = errorColor;
 175
 1176        OnLimitReached?.Invoke();
 1177        hasPassedLimit = true;
 1178    }
 179
 0180    public string GetValue() { return currentValue; }
 181}