DigitalFormDoubleEntry.cs 2.8 KB

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