DigitalFormStringPopup.cs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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, string?>
  7. {
  8. private string _value = "";
  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 => _value;
  22. set
  23. {
  24. _value = value;
  25. UpdateUI();
  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(DFLoadStorageEntry entry)
  40. => Value = Definition?.Properties.DeserializeValue(entry) ?? string.Empty;
  41. public void Serialize(DFSaveStorageEntry entry)
  42. => entry.SetValue(Value);
  43. public event DigitalFormViewChangedHandler? ValueChanged;
  44. public DigitalFormStringPopup()
  45. {
  46. Clicked += (s, e) =>
  47. {
  48. PopupEditor edt = new PopupEditor(Value);
  49. edt.OnPopupEdtSaved += (text) =>
  50. {
  51. Value = text;
  52. ValueChanged?.Invoke(this, new DigitalFormViewChangedArgs(Definition,Value));
  53. };
  54. Navigation.PushAsync(edt);
  55. };
  56. }
  57. private void Initialize(DFLayoutStringField definition)
  58. {
  59. }
  60. private void UpdateUI()
  61. {
  62. Text = String.IsNullOrWhiteSpace(_value)
  63. ? "Edit"
  64. : _value.Length > 25
  65. ? _value.Substring(0, 25) + "..."
  66. : _value;
  67. }
  68. private void UpdateStatus()
  69. {
  70. IsEnabled = !_readOnly && Definition?.Properties.Secure == false;
  71. var colors = DigitalFormUtils.GetColors(!IsEnabled, Definition?.Properties.Required ?? false, true);
  72. BackgroundColor = colors.Background;
  73. BorderColor = colors.Border;
  74. TextColor = colors.Foreground;
  75. }
  76. }
  77. }