using System.Collections.Generic; using System.Linq; using InABox.Core; using InABox.Mobile; using Xamarin.Forms; namespace PRS.Mobile { public class DigitalFormMultiSignature : Grid, IDigitalFormField, Dictionary?> { private readonly CollectionView _images; private readonly MobileCard _frame; private readonly MobileButton _add; private readonly MobileButton _select; private readonly MobileButton _clear; private DFLayoutMultiSignaturePad? _definition; public DFLayoutMultiSignaturePad? Definition { get => _definition; set { _definition = value; Initialize(value ?? new DFLayoutMultiSignaturePad()); } } private Dictionary _value = new Dictionary(); public Dictionary Value { get => _value; set { _value = value; DoUpdateUI(); } } public bool IsEmpty => Value.Any() != true; private void DoUpdateUI() { _images.ItemsSource = null; _images.ItemsSource = _value.ToArray(); } private bool _readOnly; public bool ReadOnly { get => _readOnly; set { _readOnly = value; UpdateStatus(); } } public void Deserialize(DFLoadStorageEntry entry) { _value = Definition?.Properties.DeserializeValue(entry) ?? new Dictionary(); DoUpdateUI(); } public void Serialize(DFSaveStorageEntry entry) { Definition?.Properties.SerializeValue(entry,_value); } public event DigitalFormViewChangedHandler? ValueChanged; public DigitalFormMultiSignature() { HeightRequest = 200; ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Star }); ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Auto }); RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto }); RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto }); RowDefinitions.Add(new RowDefinition() { Height = GridLength.Star }); RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto }); _images = new CollectionView() { ItemsLayout = new LinearItemsLayout(ItemsLayoutOrientation.Horizontal) { ItemSpacing = 10 }, ItemSizingStrategy = ItemSizingStrategy.MeasureAllItems, ItemTemplate = new DataTemplate(() => { var image = new Image() { Aspect = Aspect.AspectFit, VerticalOptions = LayoutOptions.Fill, HorizontalOptions = LayoutOptions.FillAndExpand, WidthRequest = 200, Margin = new Thickness(10), }; image.SetBinding(Image.SourceProperty, new Binding("Value",BindingMode.Default,new ByteArrayToImageSourceConverter())); image.SetValue(Grid.RowProperty,0); image.SetValue(Grid.ColumnProperty,0); image.SetValue(Grid.ColumnSpanProperty,2); var label = new Label() { HorizontalOptions = LayoutOptions.Fill, HorizontalTextAlignment = TextAlignment.Center }; label.SetBinding(Image.SourceProperty, new Binding("Key")); label.SetValue(Grid.RowProperty,1); label.SetValue(Grid.ColumnProperty,0); var button = new MobileMenuButton() { Image = ImageSource.FromFile("cross") }; button.Clicked += (sender, args) => { /* DoDelete functionality here*/ }; button.SetValue(Grid.RowProperty,1); button.SetValue(Grid.ColumnProperty,1); var grid = new Grid(); grid.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Star }); grid.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto }); grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Star }); grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Auto }); grid.Children.Add(image); grid.Children.Add(label); grid.Children.Add(button); var frame = new MobileCard() { BorderColor = Color.White, BackgroundColor = Color.White, CornerRadius = 5, Content = grid }; return frame; }), }; _frame = new MobileCard() { Content = _images, Padding = 10 }; _frame.SetValue(Grid.RowProperty,0); _frame.SetValue(Grid.ColumnProperty,0); _frame.SetValue(Grid.RowSpanProperty,4); Children.Add(_frame); _add = new MobileButton() { Image = ImageSource.FromFile("plus"), Orientation = StackOrientation.Horizontal, WidthRequest = 40, HeightRequest = 40, CornerRadius = 5 }; _add.Clicked += (sender, args) => { }; _add.SetValue(Grid.RowProperty,0); _add.SetValue(Grid.ColumnProperty,1); Children.Add(_add); _select = new MobileButton() { Image = ImageSource.FromFile("photolibrary"), Orientation = StackOrientation.Horizontal, WidthRequest = 40, HeightRequest = 40, CornerRadius = 5 }; _select.Clicked += (sender, args) => { }; _select.SetValue(Grid.RowProperty,1); _select.SetValue(Grid.ColumnProperty,1); Children.Add(_select); _clear = new MobileButton() { Image = ImageSource.FromFile("cross"), Orientation = StackOrientation.Horizontal, WidthRequest = 40, HeightRequest = 40, CornerRadius = 5 }; _clear.Clicked += (sender, args) => { }; _clear.SetValue(Grid.RowProperty,3); _clear.SetValue(Grid.ColumnProperty,1); Children.Add(_clear); } private void Initialize(DFLayoutMultiSignaturePad value) { UpdateStatus(); } protected bool Secure => Definition?.Properties.Secure ?? false; protected bool Required => Definition?.Properties.Required ?? false; private void UpdateStatus() { bool enabled = !_readOnly && !Secure; _add.IsEnabled = enabled; _clear.IsEnabled = enabled; var colors = DigitalFormUtils.GetColors(!enabled, Required, false); _frame.BackgroundColor = colors.Background; _frame.BorderColor = colors.Border; colors = DigitalFormUtils.GetColors(!enabled, Required, true); _add.BackgroundColor = colors.Background; _add.BorderColor = colors.Border; _clear.BackgroundColor = colors.Background; _clear.BorderColor = colors.Border; } } }