DigitalFormMultiImage.cs 10.0 KB

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