DigitalFormTimeEntry.cs 2.4 KB

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