DigitalFormOptionComboBox.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. using System;
  2. using System.Linq;
  3. using InABox.Core;
  4. using InABox.Mobile;
  5. using Xamarin.Forms;
  6. namespace PRS.Mobile
  7. {
  8. public class DigitalFormOptionComboBox : Grid, IDigitalFormField<DFLayoutOptionField, DFLayoutOptionFieldProperties, string, string?>
  9. {
  10. private readonly MobileCard _labelframe;
  11. private readonly Label _label;
  12. private readonly MobileCard _menuframe;
  13. private readonly MobileMenuButton _menu;
  14. private DFLayoutOptionField? _definition;
  15. public DFLayoutOptionField? Definition
  16. {
  17. get => _definition;
  18. set
  19. {
  20. _definition = value;
  21. Initialize(value ?? new DFLayoutOptionField());
  22. }
  23. }
  24. private String _value = "";
  25. public String Value
  26. {
  27. get => _value;
  28. set
  29. {
  30. _value = value;
  31. UpdateUI();
  32. }
  33. }
  34. private void UpdateUI()
  35. {
  36. _label.Text = String.IsNullOrWhiteSpace(_value) ? "Select Value" : _value;
  37. }
  38. public bool IsEmpty => String.IsNullOrWhiteSpace(Value);
  39. private bool _readOnly;
  40. public bool ReadOnly
  41. {
  42. get => _readOnly;
  43. set
  44. {
  45. _readOnly = value;
  46. UpdateStatus();
  47. }
  48. }
  49. public void Deserialize(DFLoadStorageEntry entry)
  50. => Value = Definition?.Properties.DeserializeValue(entry) ?? string.Empty;
  51. public void Serialize(DFSaveStorageEntry entry)
  52. => Definition?.Properties.SerializeValue(entry, _value);
  53. public event DigitalFormViewChangedHandler? ValueChanged;
  54. public DigitalFormOptionComboBox()
  55. {
  56. HeightRequest = 40;
  57. ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Star });
  58. ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Auto });
  59. RowDefinitions.Add(new RowDefinition() { Height = GridLength.Star });
  60. _labelframe = new MobileCard()
  61. {
  62. Padding = 5
  63. };
  64. Children.Add(_labelframe);
  65. _label = new Label
  66. {
  67. Text = "Select Value",
  68. VerticalTextAlignment = TextAlignment.Center,
  69. FontSize = Device.GetNamedSize(NamedSize.Small,new Label()),
  70. Margin = 5
  71. };
  72. _labelframe.Content = _label;
  73. _menuframe = new MobileCard() { Padding = 0 };
  74. _menuframe.SetValue(Grid.ColumnProperty,1);
  75. Children.Add(_menuframe);
  76. _menu = new MobileMenuButton
  77. {
  78. WidthRequest = 35,
  79. Image = ImageSource.FromFile("lines")
  80. };
  81. _menuframe.Content = _menu;
  82. }
  83. private void Initialize(DFLayoutOptionField definition)
  84. {
  85. _menu.Items.Clear();
  86. foreach (var option in definition.Properties.Options.Replace(",","\n").Split('\n'))
  87. {
  88. var item = new MobileMenuItem() { Text = option.Trim() };
  89. item.Clicked += (sender, args) =>
  90. {
  91. Value = item.Text;
  92. ValueChanged?.Invoke(this, new DigitalFormViewChangedArgs(Definition,Value));
  93. };
  94. _menu.Items.Add(item);
  95. }
  96. if (_menu.Items.Any())
  97. _menu.Items.Add(new MobileMenuSeparator());
  98. var clear = new MobileMenuItem() { Text = "Clear Value" };
  99. clear.Clicked += (sender, args) =>
  100. {
  101. Value = "";
  102. ValueChanged?.Invoke(this, new DigitalFormViewChangedArgs(Definition,Value));
  103. };
  104. _menu.Items.Add(clear);
  105. UpdateStatus();
  106. }
  107. private void UpdateStatus()
  108. {
  109. IsEnabled = !_readOnly && Definition?.Properties.Secure == false;
  110. var labelcolors = DigitalFormUtils.GetColors(!IsEnabled, Definition?.Properties.Required ?? false, false);
  111. _labelframe.BackgroundColor = labelcolors.Background;
  112. _labelframe.BorderColor = labelcolors.Border;
  113. _label.TextColor = labelcolors.Foreground;
  114. var menucolors = DigitalFormUtils.GetColors(!IsEnabled, Definition?.Properties.Required ?? false, true);
  115. _menuframe.BackgroundColor = menucolors.Background;
  116. _menuframe.BorderColor = menucolors.Border;
  117. }
  118. }
  119. }