DigitalFormMultiImage.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using InABox.Core;
  6. using InABox.Mobile;
  7. using Xamarin.Forms;
  8. namespace PRS.Mobile
  9. {
  10. public class DigitalFormMultiImage : Grid, IDigitalFormField<DFLayoutMultiImage, DFLayoutMultiImageProperties, List<Guid>?, DFLayoutEmbeddedMediaValues>
  11. {
  12. private readonly CollectionView _images;
  13. private readonly MobileCard _frame;
  14. private readonly MobileButton _camera;
  15. private readonly MobileButton _library;
  16. private DFLayoutMultiImage? _definition;
  17. public DFLayoutMultiImage? Definition
  18. {
  19. get => _definition;
  20. set
  21. {
  22. _definition = value;
  23. Initialize(value ?? new DFLayoutMultiImage());
  24. }
  25. }
  26. private DFLayoutEmbeddedMediaValues _value = new DFLayoutEmbeddedMediaValues();
  27. public List<Guid>? Value
  28. {
  29. get => _value.Values.Select(x=>x.ID).ToList();
  30. set
  31. {
  32. _value.Clear();
  33. if (value != null)
  34. {
  35. foreach (var val in value)
  36. _value.Add(new DFLayoutEmbeddedMediaValue() { ID = val });
  37. }
  38. DoUpdateUI();
  39. }
  40. }
  41. public bool IsEmpty => Value?.Any() != true;
  42. private void DoUpdateUI()
  43. {
  44. _images.ItemsSource = null;
  45. _images.ItemsSource = _value.ToArray();
  46. }
  47. private bool _readOnly;
  48. public bool ReadOnly
  49. {
  50. get => _readOnly;
  51. set
  52. {
  53. _readOnly = value;
  54. UpdateStatus();
  55. }
  56. }
  57. public void Deserialize(DFLoadStorageEntry entry)
  58. {
  59. _value = Definition?.Properties.DeserializeValue(entry) ?? new DFLayoutEmbeddedMediaValues();
  60. foreach (var val in _value)
  61. {
  62. if ((val.Thumbnail?.Any() != true) && (val.Data?.Any() == true))
  63. val.Thumbnail = MobileUtils.ImageTools.CreateThumbnail(val.Data, 256, 256);
  64. }
  65. DoUpdateUI();
  66. }
  67. public void Serialize(DFSaveStorageEntry entry)
  68. {
  69. foreach (var val in _value)
  70. {
  71. if ((val.Data?.Any() == true) && (val.ID == Guid.Empty))
  72. val.ID = DigitalFormDocumentFactory.SaveDocument(val.Data);
  73. }
  74. Definition?.Properties.SerializeValue(entry,_value);
  75. }
  76. public event DigitalFormViewChangedHandler ValueChanged;
  77. public DigitalFormMultiImage()
  78. {
  79. _value = new DFLayoutEmbeddedMediaValues();
  80. _value.PropertyChanged += (sender, args) => DoUpdateUI();
  81. HeightRequest = 200;
  82. ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Star });
  83. ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Auto });
  84. RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto });
  85. RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto });
  86. RowDefinitions.Add(new RowDefinition() { Height = GridLength.Star });
  87. _images = new CollectionView()
  88. {
  89. ItemsLayout = new LinearItemsLayout(ItemsLayoutOrientation.Horizontal) { ItemSpacing = 10 },
  90. ItemSizingStrategy = ItemSizingStrategy.MeasureAllItems,
  91. // IndicatorView = new IndicatorView() { IndicatorColor = Color.Red, SelectedIndicatorColor = Color.Green, HorizontalOptions = LayoutOptions.Center},
  92. // PeekAreaInsets = new Thickness(100),
  93. ItemTemplate = new DataTemplate(() =>
  94. {
  95. var image = new Image()
  96. {
  97. Aspect = Aspect.AspectFit,
  98. VerticalOptions = LayoutOptions.Fill,
  99. HorizontalOptions = LayoutOptions.FillAndExpand,
  100. WidthRequest = 200,
  101. Margin = new Thickness(10),
  102. };
  103. image.GestureRecognizers.Add(new TapGestureRecognizer(ImageTapped));
  104. image.SetBinding(Image.SourceProperty, new Binding("Thumbnail",BindingMode.Default,new ByteArrayToImageSourceConverter()));
  105. var frame = new MobileCard()
  106. {
  107. BorderColor = Color.White,
  108. BackgroundColor = Color.White,
  109. CornerRadius = 5,
  110. Content = image
  111. };
  112. return frame;
  113. }),
  114. // Loop = false,
  115. // HorizontalScrollBarVisibility = ScrollBarVisibility.Default
  116. };
  117. _images.SelectionChanged += (sender, args) =>
  118. {
  119. };
  120. _frame = new MobileCard()
  121. {
  122. Content = _images,
  123. Padding = 10
  124. };
  125. _frame.SetValue(Grid.RowProperty,0);
  126. _frame.SetValue(Grid.ColumnProperty,0);
  127. _frame.SetValue(Grid.RowSpanProperty,3);
  128. Children.Add(_frame);
  129. _camera = new MobileButton()
  130. {
  131. Image = ImageSource.FromFile("camera"),
  132. Orientation = StackOrientation.Horizontal,
  133. WidthRequest = 40,
  134. HeightRequest = 40,
  135. CornerRadius = 5
  136. };
  137. _camera.Clicked += async (sender, args) =>
  138. {
  139. var doc = await MobileDocument.From<MobileDocumentCameraSource>(PhotoUtils.CreateCameraOptions());
  140. if (doc != null)
  141. {
  142. DFLayoutEmbeddedMediaValue val = new DFLayoutEmbeddedMediaValue()
  143. {
  144. Data = doc.Data,
  145. Thumbnail = MobileUtils.ImageTools.CreateThumbnail(doc.Data, 256, 256)
  146. };
  147. _value.Add(val);
  148. Device.BeginInvokeOnMainThread(DoUpdateUI);
  149. }
  150. };
  151. _camera.SetValue(Grid.RowProperty,0);
  152. _camera.SetValue(Grid.ColumnProperty,1);
  153. Children.Add(_camera);
  154. _library = new MobileButton()
  155. {
  156. Image = ImageSource.FromFile("photolibrary"),
  157. Orientation = StackOrientation.Horizontal,
  158. WidthRequest = 40,
  159. HeightRequest = 40,
  160. CornerRadius = 5
  161. };
  162. _library.Clicked += async (sender, args) =>
  163. {
  164. var doc = await MobileDocument.From<MobileDocumentPhotoLibrarySource>(PhotoUtils.CreatePhotoLibraryOptions());
  165. if (doc != null)
  166. {
  167. DFLayoutEmbeddedMediaValue val = new DFLayoutEmbeddedMediaValue()
  168. {
  169. Data = doc.Data,
  170. Thumbnail = MobileUtils.ImageTools.CreateThumbnail(doc.Data, 256, 256)
  171. };
  172. _value.Add(val);
  173. Device.BeginInvokeOnMainThread(DoUpdateUI);
  174. }
  175. };
  176. _library.SetValue(Grid.RowProperty,1);
  177. _library.SetValue(Grid.ColumnProperty,1);
  178. Children.Add(_library);
  179. }
  180. private void ImageTapped(View arg1, object arg2)
  181. {
  182. if ((arg1 as Image)?.BindingContext is DFLayoutEmbeddedMediaValue value)
  183. {
  184. if (value.Data?.Any() == true)
  185. Navigation.PushAsync(
  186. new ImageViewerPage(
  187. value.Data,
  188. () =>
  189. {
  190. _value.Remove(value);
  191. DoUpdateUI();
  192. }
  193. )
  194. );
  195. else
  196. {
  197. if (value.ID != Guid.Empty)
  198. {
  199. DigitalFormDocumentFactory.LoadDocument(
  200. value.ID,
  201. data =>
  202. {
  203. Device.BeginInvokeOnMainThread(() =>
  204. {
  205. value.Data = data;
  206. Navigation.PushAsync(
  207. new ImageViewerPage(
  208. data,
  209. () =>
  210. {
  211. _value.Remove(value);
  212. DoUpdateUI();
  213. }
  214. )
  215. );
  216. });
  217. }
  218. );
  219. }
  220. }
  221. }
  222. }
  223. private void Initialize(DFLayoutMultiImage value)
  224. {
  225. UpdateStatus();
  226. }
  227. protected bool DisableLibrary => Definition.Properties.DisableLibrary;
  228. protected bool Secure => Definition.Properties.Secure;
  229. protected bool Required => Definition.Properties.Required;
  230. private void UpdateStatus()
  231. {
  232. bool enabled = !_readOnly && !Secure;
  233. _camera.IsEnabled = enabled;
  234. _library.IsEnabled = enabled && !DisableLibrary;
  235. var colors = DigitalFormUtils.GetColors(!enabled, Required, false);
  236. _frame.BackgroundColor = colors.Background;
  237. _frame.BorderColor = colors.Border;
  238. colors = DigitalFormUtils.GetColors(!enabled, Required, true);
  239. _camera.BackgroundColor = colors.Background;
  240. _camera.BorderColor = colors.Border;
  241. _library.BackgroundColor = colors.Background;
  242. _library.BorderColor = colors.Border;
  243. }
  244. }
  245. }