using System; using System.Collections.Generic; using System.IO; using System.Linq; using InABox.Core; using InABox.Mobile; using Xamarin.Forms; namespace PRS.Mobile { public class DigitalFormMultiImage : Grid, IDigitalFormField, DFLayoutEmbeddedMediaValues> { private readonly CollectionView _images; private readonly MobileCard _frame; private readonly MobileButton _camera; private readonly MobileButton _library; private DFLayoutMultiImage? _definition; public DFLayoutMultiImage? Definition { get => _definition; set { _definition = value; Initialize(value ?? new DFLayoutMultiImage()); } } private DFLayoutEmbeddedMediaValues _value = new DFLayoutEmbeddedMediaValues(); public List Value { get => _value.Values.Select(x=>x.ID).ToList(); set { _value.Clear(); foreach (var val in value) _value.Add(new DFLayoutEmbeddedMediaValue() { ID = val }); 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 DFLayoutEmbeddedMediaValues(); foreach (var val in _value) { if ((val.Thumbnail?.Any() != true) && (val.Data?.Any() == true)) val.Thumbnail = MobileUtils.ImageTools.CreateThumbnail(val.Data, 256, 256); } DoUpdateUI(); } public void Serialize(DFSaveStorageEntry entry) { foreach (var val in _value) { if ((val.Data?.Any() == true) && (val.ID == Guid.Empty)) val.ID = DigitalFormDocumentFactory.SaveDocument(val.Data); } Definition?.Properties.SerializeValue(entry,_value); } public event DigitalFormViewChangedHandler ValueChanged; public DigitalFormMultiImage() { _value = new DFLayoutEmbeddedMediaValues(); _value.PropertyChanged += (sender, args) => DoUpdateUI(); 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 }); _images = new CollectionView() { ItemsLayout = new LinearItemsLayout(ItemsLayoutOrientation.Horizontal) { ItemSpacing = 10 }, ItemSizingStrategy = ItemSizingStrategy.MeasureAllItems, // IndicatorView = new IndicatorView() { IndicatorColor = Color.Red, SelectedIndicatorColor = Color.Green, HorizontalOptions = LayoutOptions.Center}, // PeekAreaInsets = new Thickness(100), ItemTemplate = new DataTemplate(() => { var image = new Image() { Aspect = Aspect.AspectFit, VerticalOptions = LayoutOptions.Fill, HorizontalOptions = LayoutOptions.FillAndExpand, WidthRequest = 200, Margin = new Thickness(10), }; image.GestureRecognizers.Add(new TapGestureRecognizer(ImageTapped)); image.SetBinding(Image.SourceProperty, new Binding("Thumbnail",BindingMode.Default,new ByteArrayToImageSourceConverter())); var frame = new MobileCard() { BorderColor = Color.White, BackgroundColor = Color.White, CornerRadius = 5, Content = image }; return frame; }), // Loop = false, // HorizontalScrollBarVisibility = ScrollBarVisibility.Default }; _images.SelectionChanged += (sender, args) => { }; _frame = new MobileCard() { Content = _images, Padding = 10 }; _frame.SetValue(Grid.RowProperty,0); _frame.SetValue(Grid.ColumnProperty,0); _frame.SetValue(Grid.RowSpanProperty,3); Children.Add(_frame); _camera = new MobileButton() { Image = ImageSource.FromFile("camera"), Orientation = StackOrientation.Horizontal, WidthRequest = 40, HeightRequest = 40, CornerRadius = 5 }; _camera.Clicked += async (sender, args) => { var doc = await MobileDocument.From(PhotoUtils.CreateCameraOptions()); if (doc != null) { DFLayoutEmbeddedMediaValue val = new DFLayoutEmbeddedMediaValue() { Data = doc.Data, Thumbnail = MobileUtils.ImageTools.CreateThumbnail(doc.Data, 256, 256) }; _value.Add(val); Device.BeginInvokeOnMainThread(DoUpdateUI); } }; _camera.SetValue(Grid.RowProperty,0); _camera.SetValue(Grid.ColumnProperty,1); Children.Add(_camera); _library = new MobileButton() { Image = ImageSource.FromFile("photolibrary"), Orientation = StackOrientation.Horizontal, WidthRequest = 40, HeightRequest = 40, CornerRadius = 5 }; _library.Clicked += async (sender, args) => { var doc = await MobileDocument.From(PhotoUtils.CreatePhotoLibraryOptions()); if (doc != null) { DFLayoutEmbeddedMediaValue val = new DFLayoutEmbeddedMediaValue() { Data = doc.Data, Thumbnail = MobileUtils.ImageTools.CreateThumbnail(doc.Data, 256, 256) }; _value.Add(val); Device.BeginInvokeOnMainThread(DoUpdateUI); } }; _library.SetValue(Grid.RowProperty,1); _library.SetValue(Grid.ColumnProperty,1); Children.Add(_library); } private void ImageTapped(View arg1, object arg2) { if ((arg1 as Image)?.BindingContext is DFLayoutEmbeddedMediaValue value) { if (value.Data?.Any() == true) Navigation.PushAsync( new ImageViewerPage( value.Data, () => { _value.Remove(value); DoUpdateUI(); } ) ); else { if (value.ID != Guid.Empty) { DigitalFormDocumentFactory.LoadDocument( value.ID, data => { Device.BeginInvokeOnMainThread(() => { value.Data = data; Navigation.PushAsync( new ImageViewerPage( data, () => { _value.Remove(value); DoUpdateUI(); } ) ); }); } ); } } } } private void Initialize(DFLayoutMultiImage value) { UpdateStatus(); } protected bool DisableLibrary => Definition.Properties.DisableLibrary; protected bool Secure => Definition.Properties.Secure; protected bool Required => Definition.Properties.Required; private void UpdateStatus() { bool enabled = !_readOnly && !Secure; _camera.IsEnabled = enabled; _library.IsEnabled = enabled && !DisableLibrary; var colors = DigitalFormUtils.GetColors(!enabled, Required, false); _frame.BackgroundColor = colors.Background; _frame.BorderColor = colors.Border; colors = DigitalFormUtils.GetColors(!enabled, Required, true); _camera.BackgroundColor = colors.Background; _camera.BorderColor = colors.Border; _library.BackgroundColor = colors.Background; _library.BorderColor = colors.Border; } } }