< Summary

Class:LimitInputField
Assembly:BuilderLimitInputField
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/BuilderInWorld/HUD/ProjectsPanelHUD/Scripts/LimitInputField/LimitInputField.cs
Covered lines:35
Uncovered lines:15
Coverable lines:50
Total lines:113
Line coverage:70% (35 of 50)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
LimitInputField()0%110100%
Start()0%330100%
SetText(...)0%2100%
SetError()0%2100%
InputLostFocus(...)0%6200%
InputFocused(...)0%6200%
InputChanged(...)0%5.275077.78%
InputAvailable()0%550100%
Empty()0%3.073080%
LimitReached()0%4.034087.5%
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
 14    public event System.Action<string> OnInputChange;
 15
 16    [SerializeField] private int characterLimit;
 17    [SerializeField] private bool isMandatory = false;
 18    [SerializeField] private Color normalColor;
 19    [SerializeField] private Color errorColor;
 20
 21    [SerializeField] private Sprite inputFieldNormalBackgroundSprite;
 22    [SerializeField] private Sprite inputFieldErrorBackgroundSprite;
 23    [SerializeField] private Sprite inputFieldFocusBackgroundSprite;
 24
 25    [SerializeField] private Image inputFieldbackgroundImg;
 26    [SerializeField] private TMP_InputField inputField;
 27    [SerializeField] private TextMeshProUGUI limitText;
 28
 29    internal bool hasPassedLimit = false;
 30    internal bool hasBeenEmpty = false;
 5231    private string currentValue = "";
 32
 33    private void Start()
 34    {
 835        inputField.onSelect.AddListener(InputFocused);
 836        inputField.onDeselect.AddListener(InputLostFocus);
 837        inputField.onValueChanged.AddListener(InputChanged);
 838        limitText?.SetText( currentValue.Length + "/" + characterLimit);
 839        if (isMandatory)
 440            hasBeenEmpty = true;
 841    }
 42
 043    public void SetText(string value) { inputField.SetTextWithoutNotify(value); }
 44
 045    public void SetError() { LimitReached(); }
 46
 47    private void InputLostFocus(string value)
 48    {
 049        if (value.Length > characterLimit)
 050            inputFieldbackgroundImg.sprite = inputFieldErrorBackgroundSprite;
 51        else
 052            inputFieldbackgroundImg.sprite = inputFieldNormalBackgroundSprite;
 053    }
 54
 55    private void InputFocused(string value)
 56    {
 057        if (value.Length > characterLimit)
 058            inputFieldbackgroundImg.sprite = inputFieldErrorBackgroundSprite;
 59        else
 060            inputFieldbackgroundImg.sprite = inputFieldFocusBackgroundSprite;
 061    }
 62
 63    internal void InputChanged(string newValue)
 64    {
 165        currentValue = newValue;
 166        limitText?.SetText(newValue.Length + "/" + characterLimit);
 167        if (newValue.Length > characterLimit)
 068            LimitReached();
 169        else if (currentValue.Length == 0)
 070            Empty();
 71        else
 172            InputAvailable();
 73
 174        OnInputChange?.Invoke(newValue);
 175    }
 76
 77    internal void InputAvailable()
 78    {
 279        if (!hasPassedLimit && !hasBeenEmpty)
 180            return;
 81
 182        if (limitText != null)
 183            limitText.color = normalColor;
 184        inputFieldbackgroundImg.sprite = inputFieldFocusBackgroundSprite;
 185        OnInputAvailable?.Invoke();
 186        hasPassedLimit = false;
 187        hasBeenEmpty = false;
 188    }
 89
 90    internal void Empty()
 91    {
 192        if (hasBeenEmpty)
 093            return;
 94
 195        hasBeenEmpty = true;
 196        OnEmptyValue?.Invoke();
 197    }
 98
 99    internal void LimitReached()
 100    {
 1101        if (hasPassedLimit)
 0102            return;
 103
 1104        if (limitText != null)
 1105            limitText.color = errorColor;
 1106        inputFieldbackgroundImg.sprite = inputFieldErrorBackgroundSprite;
 107
 1108        OnLimitReached?.Invoke();
 1109        hasPassedLimit = true;
 1110    }
 111
 0112    public string GetValue() { return currentValue; }
 113}