DigitalFormStringPopup.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. using System;
  2. using InABox.Core;
  3. using InABox.Mobile;
  4. namespace PRS.Mobile
  5. {
  6. public class DigitalFormStringPopup : MobileButton, IDigitalFormField<DFLayoutStringField, DFLayoutStringFieldProperties, string>
  7. {
  8. private DFLayoutStringField _definition;
  9. public DFLayoutStringField Definition
  10. {
  11. get => _definition;
  12. set
  13. {
  14. _definition = value;
  15. Initialize(value ?? new DFLayoutStringField());
  16. }
  17. }
  18. private string _value;
  19. public string Value
  20. {
  21. get => _value;
  22. set
  23. {
  24. _value = value;
  25. UpdateButtonText(value);
  26. }
  27. }
  28. public bool IsEmpty => String.IsNullOrWhiteSpace(Value);
  29. private bool _readOnly;
  30. public bool ReadOnly
  31. {
  32. get => _readOnly;
  33. set
  34. {
  35. _readOnly = value;
  36. UpdateStatus();
  37. }
  38. }
  39. public void Deserialize(string serialized) => Value = serialized;
  40. public string Serialize() => Value;
  41. public event DigitalFormViewChangedHandler ValueChanged;
  42. public DigitalFormStringPopup()
  43. {
  44. Clicked += (s, e) =>
  45. {
  46. PopupEditor edt = new PopupEditor(Value);
  47. edt.OnPopupEdtSaved += (text) =>
  48. {
  49. Value = text;
  50. ValueChanged?.Invoke(this, new DigitalFormViewChangedArgs(Definition,Value));
  51. };
  52. Navigation.PushAsync(edt);
  53. };
  54. }
  55. private void Initialize(DFLayoutStringField definition)
  56. {
  57. }
  58. private void UpdateButtonText(string value)
  59. {
  60. Text = String.IsNullOrWhiteSpace(value)
  61. ? "Edit"
  62. : value.Length > 25
  63. ? value.Substring(0, 25) + "..."
  64. : value;
  65. }
  66. private void UpdateStatus()
  67. {
  68. IsEnabled = !_readOnly || Definition.Properties.Secure;
  69. var colors = DigitalFormUtils.GetColors(!IsEnabled, Definition.Properties.Required, true);
  70. BackgroundColor = colors.Background;
  71. BorderColor = colors.Border;
  72. TextColor = colors.Foreground;
  73. }
  74. }
  75. }