123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267 |
- 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<DFLayoutMultiImage, DFLayoutMultiImageProperties, List<Guid>, 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<Guid> 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<MobileDocumentCameraSource>(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<MobileDocumentPhotoLibrarySource>(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;
- }
- }
- }
|