DigitalFormStringEntry.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. using InABox.Core;
  2. using InABox.Mobile;
  3. using Xamarin.Forms;
  4. namespace PRS.Mobile
  5. {
  6. public class DigitalFormStringEntry : MobileCard, IDigitalFormField<DFLayoutStringField, DFLayoutStringFieldProperties, string, string?>
  7. {
  8. private string _value = "";
  9. private readonly MobileEntry _entry;
  10. private DFLayoutStringField? _definition;
  11. public DFLayoutStringField? Definition
  12. {
  13. get => _definition;
  14. set
  15. {
  16. _definition = value;
  17. Initialize(value);
  18. }
  19. }
  20. public string Value
  21. {
  22. get => _value;
  23. set
  24. {
  25. _value = value;
  26. UpdateUI();
  27. }
  28. }
  29. private void UpdateUI()
  30. {
  31. _entry.Text = _value;
  32. }
  33. public bool IsEmpty => string.IsNullOrWhiteSpace(Value);
  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) ?? string.Empty;
  46. public void Serialize(DFSaveStorageEntry entry)
  47. => entry.SetValue(Value);
  48. public event DigitalFormViewChangedHandler? ValueChanged;
  49. public DigitalFormStringEntry()
  50. {
  51. HeightRequest = 40;
  52. Padding = new Thickness(5,0);
  53. _entry = new MobileEntry()
  54. {
  55. Placeholder = "Enter answer",
  56. BackgroundColor = Color.Transparent
  57. };
  58. _entry.TextChanged += (o,e) =>
  59. {
  60. Value = _entry.Text;
  61. ValueChanged?.Invoke(this, new DigitalFormViewChangedArgs(Definition,Value));
  62. };
  63. Content = _entry;
  64. }
  65. private void Initialize(DFLayoutStringField? definition)
  66. {
  67. UpdateStatus();
  68. }
  69. private void UpdateStatus()
  70. {
  71. IsEnabled = !_readOnly && Definition?.Properties.Secure == false;
  72. var colors = DigitalFormUtils.GetColors(!IsEnabled, Definition?.Properties.Required ?? false, false);
  73. BackgroundColor = colors.Background;
  74. BorderColor = colors.Border;
  75. _entry.TextColor = colors.Foreground;
  76. }
  77. }
  78. }