using System; using System.Linq; using InABox.Core; using InABox.Mobile; using Xamarin.Forms; namespace PRS.Mobile { public class DigitalFormOptionComboBox : Grid, IDigitalFormField { private readonly MobileCard _labelframe; private readonly Label _label; private readonly MobileCard _menuframe; private readonly MobileMenuButton _menu; private DFLayoutOptionField _definition; public DFLayoutOptionField Definition { get => _definition; set { _definition = value; Initialize(value ?? new DFLayoutOptionField()); } } private String _value; public String Value { get => _value; set { _value = value; _label.Text = String.IsNullOrWhiteSpace(value) ? "Select Value" : value; } } public bool IsEmpty => String.IsNullOrWhiteSpace(Value); private bool _readOnly; public bool ReadOnly { get => _readOnly; set { _readOnly = value; UpdateStatus(); } } public void Deserialize(string serialized) { Value = serialized; } public string Serialize() => Value; public event DigitalFormViewChangedHandler ValueChanged; public DigitalFormOptionComboBox() { HeightRequest = 40; ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Star }); ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Auto }); RowDefinitions.Add(new RowDefinition() { Height = GridLength.Star }); _labelframe = new MobileCard() { Padding = 5 }; Children.Add(_labelframe); _label = new Label { Text = "Select Value", VerticalTextAlignment = TextAlignment.Center, FontSize = Device.GetNamedSize(NamedSize.Small,new Label()), Margin = 5 }; _labelframe.Content = _label; _menuframe = new MobileCard() { Padding = 0 }; _menuframe.SetValue(Grid.ColumnProperty,1); Children.Add(_menuframe); _menu = new MobileMenuButton { WidthRequest = 35, Image = ImageSource.FromFile("lines") }; _menuframe.Content = _menu; } private void Initialize(DFLayoutOptionField definition) { _menu.Items.Clear(); foreach (var option in definition.Properties.Options.Replace(",","\n").Split('\n')) { var item = new MobileMenuItem() { Text = option.Trim() }; item.Clicked += (sender, args) => { Value = item.Text; ValueChanged?.Invoke(this, new DigitalFormViewChangedArgs(Definition,Value)); }; _menu.Items.Add(item); } if (_menu.Items.Any()) _menu.Items.Add(new MobileMenuSeparator()); var clear = new MobileMenuItem() { Text = "Clear Value" }; clear.Clicked += (sender, args) => { Value = ""; ValueChanged?.Invoke(this, new DigitalFormViewChangedArgs(Definition,Value)); }; _menu.Items.Add(clear); UpdateStatus(); } private void UpdateStatus() { IsEnabled = !_readOnly || Definition.Properties.Secure; var labelcolors = DigitalFormUtils.GetColors(!IsEnabled, Definition.Properties.Required, false); _labelframe.BackgroundColor = labelcolors.Background; _labelframe.BorderColor = labelcolors.Border; _label.TextColor = labelcolors.Foreground; var menucolors = DigitalFormUtils.GetColors(!IsEnabled, Definition.Properties.Required, true); _menuframe.BackgroundColor = menucolors.Background; _menuframe.BorderColor = menucolors.Border; } } }