using System.Globalization; using InABox.Core; using InABox.Mobile; using Xamarin.Forms; namespace PRS.Mobile { public class DigitalFormIntegerEntry : MobileCard, IDigitalFormField { private readonly MobileEntry _entry; private DFLayoutIntegerField _definition; public DFLayoutIntegerField Definition { get => _definition; set { _definition = value; Initialize(value ?? new DFLayoutIntegerField()); } } private int _value; public int Value { get => _value; set { _value = value; UpdateUI(); } } private void UpdateUI() { _entry.Text = _value.ToString(); } public bool IsEmpty => Value.Equals(0); private bool _readOnly; public bool ReadOnly { get => _readOnly; set { _readOnly = value; UpdateStatus(); } } public void Deserialize(string serialized) { if (int.TryParse(serialized, out int d)) Value = d; } public string Serialize() => Value.ToString(CultureInfo.InvariantCulture); public event DigitalFormViewChangedHandler ValueChanged; public DigitalFormIntegerEntry() { MinimumHeightRequest = 45; Padding = 5; _entry = new MobileEntry() { Placeholder = "Enter value", BackgroundColor = Color.Transparent, Keyboard = Keyboard.Numeric }; _entry.TextChanged += (o,e) => { if (!string.IsNullOrWhiteSpace(e.NewTextValue)) { if (int.TryParse(e.NewTextValue, out int i)) { _value = i; ValueChanged?.Invoke(this, new DigitalFormViewChangedArgs(Definition,Value)); } else _entry.Text = e.OldTextValue; } }; Content = _entry; } private void Initialize(DFLayoutIntegerField definition) { UpdateStatus(); } private void UpdateStatus() { IsEnabled = !_readOnly || Definition.Properties.Secure; var colors = DigitalFormUtils.GetColors(!IsEnabled, Definition.Properties.Required, false); BackgroundColor = colors.Background; BorderColor = colors.Border; _entry.TextColor = colors.Foreground; } } }