DigitalFormDateEntry.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. using System;
  2. using InABox.Core;
  3. using InABox.Mobile;
  4. namespace PRS.Mobile
  5. {
  6. public class DigitalFormDateEntry : MobileDateButton, IDigitalFormField<DFLayoutDateField, DFLayoutDateFieldProperties, DateTime, DateTime?>
  7. {
  8. private DateTime _value = DateTime.MinValue;
  9. private DFLayoutDateField? _definition;
  10. public DFLayoutDateField? Definition
  11. {
  12. get => _definition;
  13. set
  14. {
  15. _definition = value;
  16. Initialize(value ?? new DFLayoutDateField());
  17. }
  18. }
  19. public DateTime Value
  20. {
  21. get => _value;
  22. set
  23. {
  24. _value = value;
  25. UpdateUI();
  26. }
  27. }
  28. private void UpdateUI()
  29. {
  30. Date = _value;
  31. }
  32. protected override void DoChanged()
  33. {
  34. _value = Date;
  35. base.DoChanged();
  36. }
  37. public bool IsEmpty => Value.IsEmpty();
  38. public void Deserialize(DFLoadStorageEntry entry)
  39. => Value = Definition?.Properties.DeserializeValue(entry) ?? DateTime.MinValue;
  40. public void Serialize(DFSaveStorageEntry entry)
  41. => entry.SetValue(Value);
  42. private bool _readOnly;
  43. public bool ReadOnly
  44. {
  45. get => _readOnly;
  46. set
  47. {
  48. _readOnly = value;
  49. UpdateStatus();
  50. }
  51. }
  52. public event DigitalFormViewChangedHandler? ValueChanged;
  53. public DigitalFormDateEntry()
  54. {
  55. HeightRequest = 40;
  56. Changed += (sender, args) =>
  57. {
  58. ValueChanged?.Invoke(this,new DigitalFormViewChangedArgs(Definition,Value));
  59. };
  60. }
  61. private void Initialize(DFLayoutDateField definition)
  62. {
  63. UpdateStatus();
  64. }
  65. private void UpdateStatus()
  66. {
  67. IsEnabled = !_readOnly && Definition?.Properties.Secure == false;
  68. var colors = DigitalFormUtils.GetColors(!IsEnabled, Definition?.Properties.Required ?? false, true);
  69. BackgroundColor = colors.Background;
  70. BorderColor = colors.Border;
  71. TextColor = colors.Foreground;
  72. }
  73. }
  74. }