DigitalFormDoubleEntry.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. using System;
  2. using InABox.Core;
  3. using InABox.Mobile;
  4. using Xamarin.Forms;
  5. namespace PRS.Mobile
  6. {
  7. public class DigitalFormDoubleEntry : MobileCard, IDigitalFormField<DFLayoutDoubleField, DFLayoutDoubleFieldProperties, double, double?>
  8. {
  9. private readonly MobileEntry _entry;
  10. private DFLayoutDoubleField? _definition;
  11. public DFLayoutDoubleField? Definition
  12. {
  13. get => _definition;
  14. set
  15. {
  16. _definition = value;
  17. Initialize(value ?? new DFLayoutDoubleField());
  18. }
  19. }
  20. private double _value;
  21. public double Value
  22. {
  23. get => _value;
  24. set
  25. {
  26. _value = value;
  27. UpdateUI();
  28. }
  29. }
  30. private void UpdateUI()
  31. {
  32. _entry.Text = Definition?.Properties.FormatValue(_value);
  33. }
  34. public bool IsEmpty => Math.Abs(Value).Equals(0F);
  35. public void Deserialize(DFLoadStorageEntry entry)
  36. => Value = Definition?.Properties.DeserializeValue(entry) ?? 0.0;
  37. public void Serialize(DFSaveStorageEntry entry)
  38. => entry.SetValue(Value);
  39. private bool _readOnly;
  40. public bool ReadOnly
  41. {
  42. get => _readOnly;
  43. set
  44. {
  45. _readOnly = value;
  46. UpdateStatus();
  47. }
  48. }
  49. public event DigitalFormViewChangedHandler? ValueChanged;
  50. public DigitalFormDoubleEntry()
  51. {
  52. MinimumHeightRequest = 45;
  53. Padding = 5;
  54. _entry = new MobileEntry()
  55. {
  56. Placeholder = "Enter value",
  57. BackgroundColor = Color.Transparent,
  58. Keyboard = Keyboard.Numeric
  59. };
  60. _entry.TextChanged += (o,e) =>
  61. {
  62. if (double.TryParse(e.NewTextValue, out double d))
  63. {
  64. _value = d;
  65. ValueChanged?.Invoke(this,new DigitalFormViewChangedArgs(Definition,Value));
  66. }
  67. else
  68. _entry.Text = e.OldTextValue;
  69. };
  70. Content = _entry;
  71. }
  72. private void Initialize(DFLayoutDoubleField definition)
  73. {
  74. UpdateStatus();
  75. }
  76. private void UpdateStatus()
  77. {
  78. IsEnabled = !_readOnly && Definition?.Properties.Secure == false;
  79. var colors = DigitalFormUtils.GetColors(!IsEnabled, Definition?.Properties.Required ?? false, false);
  80. BackgroundColor = colors.Background;
  81. BorderColor = colors.Border;
  82. _entry.TextColor = colors.Foreground;
  83. }
  84. }
  85. }