123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263 |
- using System;
- using System.IO;
- using System.Linq;
- using System.Text.RegularExpressions;
- using System.Threading.Tasks;
- using InABox.Core;
- using InABox.Mobile;
- using Xamarin.CommunityToolkit.Converters;
- using Xamarin.Forms;
- namespace PRS.Mobile
- {
- public abstract class DigitalFormEmbeddedMedia<T, TProperties> : Grid, IDigitalFormField<T,TProperties, DFLayoutEmbeddedMediaValue>
- where T : DFLayoutField<TProperties>, new()
- where TProperties : DFLayoutFieldProperties<DFLayoutEmbeddedMediaValue>, new()
- {
- protected abstract bool IsVideo { get; }
- private readonly MobileCard _card;
- private readonly Image _image;
- private readonly MobileButton _cameraButton;
- private readonly MobileButton _libraryButton;
- private readonly MobileButton _clearButton;
- private T _definition;
- public T Definition
- {
- get => _definition;
- set
- {
- _definition = value;
- Initialize(value ?? new T());
- }
- }
-
- private DFLayoutEmbeddedMediaValue _value = new();
- public DFLayoutEmbeddedMediaValue Value
- {
- get => _value;
- set
- {
- _value = value;
- UpdateUI();
- }
- }
- protected abstract byte[] CreateThumbnail(byte[] data, float maxwidth = 200, float maxheight = 200);
- private void UpdateUI()
- {
- if ((_value?.Thumbnail?.Any() == false) && (_value?.Data?.Any() != true))
- _value.Thumbnail = CreateThumbnail(_value.Data);
- _image.Source = _value?.Thumbnail?.Any() == true
- ? ImageSource.FromStream(() => new MemoryStream(_value.Thumbnail))
- : null;
- }
- public bool IsEmpty => Value.Data?.Any() != true;
- private bool _readOnly;
-
- public bool ReadOnly
- {
- get => _readOnly;
- set
- {
- _readOnly = value;
- UpdateStatus();
- }
- }
-
- public void Deserialize(string serialized)
- {
- _value.Load(serialized);
- UpdateUI();
- }
- public string Serialize()
- {
- if (_value == null)
- return new DFLayoutEmbeddedMediaValue().ToString();
- if ((_value.Data?.Any() == true) && (_value.ID == Guid.Empty))
- _value.ID = DigitalFormDocumentFactory.SaveDocument(_value.Data);
- return _value?.ToString() ?? "";
- }
- public event DigitalFormViewChangedHandler ValueChanged;
- protected DigitalFormEmbeddedMedia()
- {
- HeightRequest = 250;
-
- ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
- ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Auto) });
-
- RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Auto) });
- RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Auto) });
- RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Star) });
- RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Auto) });
- _card = new MobileCard()
- {
- Padding = 5
- };
- SetRow(_card, 0);
- SetColumn(_card, 0);
- SetRowSpan(_card, 4);
- Children.Add(_card);
-
- _image = new Image()
- {
- Aspect = Aspect.AspectFit
- };
- _image.GestureRecognizers.Add(new TapGestureRecognizer() { Command = new Command(Image_Tapped) });
- _card.Content = _image;
-
- _cameraButton = new MobileButton
- {
- Image = "camera",
- ImageSize = new Size(25, 25),
- Orientation = StackOrientation.Vertical,
- CornerRadius = 5,
- WidthRequest = 40,
- HeightRequest = 40,
- };
- SetRow(_cameraButton, 0);
- SetColumn(_cameraButton, 1);
- _cameraButton.Clicked += CameraButton_Clicked;
- Children.Add(_cameraButton);
-
- _libraryButton = new MobileButton
- {
- Image = "gallery",
- ImageSize = new Size(25, 25),
- Orientation = StackOrientation.Vertical,
- CornerRadius = 5,
- WidthRequest = 40,
- HeightRequest = 40,
- };
- SetRow(_libraryButton, 1);
- SetColumn(_libraryButton, 1);
- _libraryButton.Clicked += LibraryButton_Clicked;
- Children.Add(_libraryButton);
-
- _clearButton = new MobileButton
- {
- Image = "cross",
- ImageSize = new Size(25, 25),
- Orientation = StackOrientation.Vertical,
- CornerRadius = 5,
- WidthRequest = 40,
- HeightRequest = 40,
- };
- SetRow(_clearButton, 3);
- SetColumn(_clearButton, 1);
- _clearButton.Clicked += ClearButton_Clicked;
- Children.Add(_clearButton);
- }
- private void Image_Tapped()
- {
- if (_value == null)
- return;
-
- if (_value.Data?.Any() == true)
- Navigation.PushAsync(new ImageViewerPage(_value.Data));
- else
- {
- if (_value.ID != Guid.Empty)
- {
- DigitalFormDocumentFactory.LoadDocument(
- _value.ID,
- data =>
- {
- Device.BeginInvokeOnMainThread(() =>
- {
- _value.Data = data;
- Navigation.PushAsync(new ImageViewerPage(data));
- });
- }
- );
- }
- }
- }
- private void ClearButton_Clicked(object sender, MobileButtonClickEventArgs args)
- {
- _value.ID = Guid.Empty;
- _value.Thumbnail = null;
- _value.Data = null;
- UpdateUI();
- }
- protected abstract Task<MobileDocument> CaptureMedia();
-
- protected abstract Task<MobileDocument> SelectMedia();
- private async void CameraButton_Clicked(object sender, MobileButtonClickEventArgs args)
- {
- var doc = await CaptureMedia();
- if (doc != null)
- {
- _value.ID = Guid.Empty;
- _value.Thumbnail = CreateThumbnail(doc.Data);
- _value.Data = doc.Data;
- }
- Device.BeginInvokeOnMainThread(UpdateUI);
- }
- private async void LibraryButton_Clicked(object sender, MobileButtonClickEventArgs args)
- {
- var doc = await SelectMedia();
- if (doc != null)
- {
- _value.ID = Guid.Empty;
- _value.Thumbnail = CreateThumbnail(doc.Data);
- _value.Data = doc.Data;
- }
- Device.BeginInvokeOnMainThread(UpdateUI);
- }
- private void Initialize(T definition)
- {
- UpdateStatus();
- }
- protected abstract bool DisableLibrary { get; }
- protected abstract bool Secure { get; }
- protected abstract bool Required { get; }
-
- private void UpdateStatus()
- {
- _cameraButton.Image = IsVideo ? ImageSource.FromFile("camcorder") : ImageSource.FromFile("camera");
- _libraryButton.Image = IsVideo ? ImageSource.FromFile("videolibrary") : ImageSource.FromFile("photolibrary");
-
- _libraryButton.IsEnabled = !DisableLibrary;
-
- bool enabled = !_readOnly && !Secure;
- _cameraButton.IsEnabled = enabled;
- _libraryButton.IsEnabled = enabled && !DisableLibrary;
-
- var colors = DigitalFormUtils.GetColors(!enabled, Required, false);
- _card.BackgroundColor = colors.Background;
- _card.BorderColor = colors.Border;
-
- colors = DigitalFormUtils.GetColors(!enabled, Required, true);
- _cameraButton.BackgroundColor = colors.Background;
- _cameraButton.BorderColor = colors.Border;
-
- _libraryButton.BackgroundColor = colors.Background;
- _libraryButton.BorderColor = colors.Border;
-
- _clearButton.BackgroundColor = colors.Background;
- _clearButton.BorderColor = colors.Border;
-
- }
-
- }
- }
|