123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- using System;
- using System.Globalization;
- using InABox.Core;
- using InABox.Mobile;
- using Xamarin.CommunityToolkit.UI.Views.Options;
- using Xamarin.Forms;
- namespace PRS.Mobile
- {
- public class DigitalFormDoubleEntry : MobileCard, IDigitalFormField<DFLayoutDoubleField, DFLayoutDoubleFieldProperties, double>
- {
- private MobileEntry _entry;
-
- private DFLayoutDoubleField _definition;
- public DFLayoutDoubleField Definition
- {
- get => _definition;
- set
- {
- _definition = value;
- Initialize(value ?? new DFLayoutDoubleField());
- }
- }
-
- private double _value;
- public double Value
- {
- get => _value;
- set
- {
- _value = value;
- UpdateUI();
- }
- }
- private void UpdateUI()
- {
- _entry.Text = Definition.Properties.FormatValue(_value);
- }
- public bool IsEmpty => Math.Abs(Value).Equals(0F);
-
- public void Deserialize(string serialized)
- {
- if (double.TryParse(serialized, out double d))
- Value = d;
- }
- public string Serialize()
- => Value.ToString(CultureInfo.InvariantCulture);
-
- private bool _readOnly;
- public bool ReadOnly
- {
- get => _readOnly;
- set
- {
- _readOnly = value;
- UpdateStatus();
- }
- }
- public event DigitalFormViewChangedHandler ValueChanged;
- public DigitalFormDoubleEntry()
- {
- MinimumHeightRequest = 45;
- Padding = 5;
-
- _entry = new MobileEntry()
- {
- Placeholder = "Enter value",
- BackgroundColor = Color.Transparent,
- Keyboard = Keyboard.Numeric
- };
- _entry.TextChanged += (o,e) =>
- {
- if (double.TryParse(e.NewTextValue, out double d))
- {
- _value = d;
- ValueChanged?.Invoke(this,new DigitalFormViewChangedArgs(Definition,Value));
- }
- else
- _entry.Text = e.OldTextValue;
- };
- Content = _entry;
- }
- private void Initialize(DFLayoutDoubleField 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;
- }
-
- }
- }
|