DigitalFormIntegerEntry.cs 2.8 KB

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