DigitalFormTimeEntry.cs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. using System;
  2. using System.Globalization;
  3. using InABox.Core;
  4. using InABox.Mobile;
  5. namespace PRS.Mobile
  6. {
  7. public class DigitalFormTimeEntry : MobileTimeButton, IDigitalFormField<DFLayoutTimeField, DFLayoutTimeFieldProperties, TimeSpan>
  8. {
  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 => Time;
  22. set => Time = value;
  23. }
  24. public bool IsEmpty => Value.Equals(TimeSpan.Zero);
  25. private bool _readOnly;
  26. public bool ReadOnly
  27. {
  28. get => _readOnly;
  29. set
  30. {
  31. _readOnly = value;
  32. UpdateStatus();
  33. }
  34. }
  35. public void Deserialize(string serialized)
  36. {
  37. if (TimeSpan.TryParseExact(serialized, "c", CultureInfo.InvariantCulture, TimeSpanStyles.None,
  38. out TimeSpan t))
  39. Value = t;
  40. }
  41. public string Serialize() => Value.ToString("c");
  42. public event DigitalFormViewChangedHandler ValueChanged;
  43. public DigitalFormTimeEntry()
  44. {
  45. HeightRequest = 40;
  46. Changed += (sender, args) =>
  47. {
  48. ValueChanged?.Invoke(this, new DigitalFormViewChangedArgs(Definition,Value));
  49. };
  50. }
  51. private void Initialize(DFLayoutTimeField definition)
  52. {
  53. UpdateStatus();
  54. }
  55. private void UpdateStatus()
  56. {
  57. IsEnabled = !_readOnly || Definition.Properties.Secure;
  58. var colors = DigitalFormUtils.GetColors(!IsEnabled, Definition.Properties.Required, true);
  59. BackgroundColor = colors.Background;
  60. BorderColor = colors.Border;
  61. TextColor = colors.Foreground;
  62. }
  63. }
  64. }