DigitalFormStringEntry.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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>
  7. {
  8. private readonly MobileEntry _entry;
  9. private DFLayoutStringField _definition;
  10. public DFLayoutStringField Definition
  11. {
  12. get => _definition;
  13. set
  14. {
  15. _definition = value;
  16. Initialize(value ?? new DFLayoutStringField());
  17. }
  18. }
  19. public string Value
  20. {
  21. get => _entry.Text;
  22. set => _entry.Text = value;
  23. }
  24. public bool IsEmpty => string.IsNullOrWhiteSpace(Value);
  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) => Value = serialized;
  36. public string Serialize() => Value;
  37. public event DigitalFormViewChangedHandler ValueChanged;
  38. public DigitalFormStringEntry()
  39. {
  40. HeightRequest = 40;
  41. Padding = new Thickness(5,0);
  42. _entry = new MobileEntry()
  43. {
  44. Placeholder = "Enter answer",
  45. BackgroundColor = Color.Transparent
  46. };
  47. _entry.TextChanged += (o,e) =>
  48. {
  49. Value = _entry.Text;
  50. ValueChanged?.Invoke(this, new DigitalFormViewChangedArgs(Definition,Value));
  51. };
  52. Content = _entry;
  53. }
  54. private void Initialize(DFLayoutStringField 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, false);
  62. BackgroundColor = colors.Background;
  63. BorderColor = colors.Border;
  64. _entry.TextColor = colors.Foreground;
  65. }
  66. }
  67. }