DigitalFormIntegerEntry.cs 2.8 KB

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