DigitalFormDateEntry.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using System;
  2. using System.Globalization;
  3. using System.Text.RegularExpressions;
  4. using InABox.Core;
  5. using InABox.Mobile;
  6. using Xamarin.Forms;
  7. namespace PRS.Mobile
  8. {
  9. public class DigitalFormDateEntry : MobileDateButton, IDigitalFormField<DFLayoutDateField, DFLayoutDateFieldProperties, DateTime>
  10. {
  11. private DFLayoutDateField _definition;
  12. public DFLayoutDateField Definition
  13. {
  14. get => _definition;
  15. set
  16. {
  17. _definition = value;
  18. Initialize(value ?? new DFLayoutDateField());
  19. }
  20. }
  21. public DateTime Value
  22. {
  23. get => Date;
  24. set => Date = value;
  25. }
  26. public bool IsEmpty => Value.IsEmpty();
  27. public void Deserialize(string serialized)
  28. {
  29. if (DateTime.TryParseExact(serialized, "dd-MM-yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None,
  30. out DateTime d))
  31. Value = d;
  32. }
  33. public string Serialize() =>
  34. Value.ToString("dd-MM-yyyy");
  35. private bool _readOnly;
  36. public bool ReadOnly
  37. {
  38. get => _readOnly;
  39. set
  40. {
  41. _readOnly = value;
  42. UpdateStatus();
  43. }
  44. }
  45. public event DigitalFormViewChangedHandler ValueChanged;
  46. public DigitalFormDateEntry()
  47. {
  48. HeightRequest = 40;
  49. Changed += (sender, args) =>
  50. {
  51. ValueChanged?.Invoke(this,new DigitalFormViewChangedArgs(Definition,Value));
  52. };
  53. }
  54. private void Initialize(DFLayoutDateField definition)
  55. {
  56. UpdateStatus();
  57. }
  58. private void UpdateStatus()
  59. {
  60. IsEnabled = !_readOnly || Definition.Properties.Secure;
  61. var colors = DigitalFormUtils.GetColors(!IsEnabled, Definition.Properties.Required, true);
  62. BackgroundColor = colors.Background;
  63. BorderColor = colors.Border;
  64. TextColor = colors.Foreground;
  65. }
  66. }
  67. }