DigitalFormMultiSignature.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using InABox.Core;
  4. using InABox.Mobile;
  5. using Xamarin.Forms;
  6. namespace PRS.Mobile
  7. {
  8. public class DigitalFormMultiSignature : Grid, IDigitalFormField<DFLayoutMultiSignaturePad, DFLayoutMultiSignaturePadProperties, Dictionary<string, byte[]>, Dictionary<string, byte[]>?>
  9. {
  10. private readonly CollectionView _images;
  11. private readonly MobileCard _frame;
  12. private readonly MobileButton _add;
  13. private readonly MobileButton _select;
  14. private readonly MobileButton _clear;
  15. private DFLayoutMultiSignaturePad? _definition;
  16. public DFLayoutMultiSignaturePad? Definition
  17. {
  18. get => _definition;
  19. set
  20. {
  21. _definition = value;
  22. Initialize(value ?? new DFLayoutMultiSignaturePad());
  23. }
  24. }
  25. private Dictionary<string, byte[]> _value = new Dictionary<string, byte[]>();
  26. public Dictionary<string, byte[]> Value
  27. {
  28. get => _value;
  29. set
  30. {
  31. _value = value;
  32. DoUpdateUI();
  33. }
  34. }
  35. public bool IsEmpty => Value.Any() != true;
  36. private void DoUpdateUI()
  37. {
  38. _images.ItemsSource = null;
  39. _images.ItemsSource = _value.ToArray();
  40. }
  41. private bool _readOnly;
  42. public bool ReadOnly
  43. {
  44. get => _readOnly;
  45. set
  46. {
  47. _readOnly = value;
  48. UpdateStatus();
  49. }
  50. }
  51. public void Deserialize(DFLoadStorageEntry entry)
  52. {
  53. _value = Definition?.Properties.DeserializeValue(entry) ?? new Dictionary<string, byte[]>();
  54. DoUpdateUI();
  55. }
  56. public void Serialize(DFSaveStorageEntry entry)
  57. {
  58. Definition?.Properties.SerializeValue(entry,_value);
  59. }
  60. public event DigitalFormViewChangedHandler? ValueChanged;
  61. public DigitalFormMultiSignature()
  62. {
  63. HeightRequest = 200;
  64. ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Star });
  65. ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Auto });
  66. RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto });
  67. RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto });
  68. RowDefinitions.Add(new RowDefinition() { Height = GridLength.Star });
  69. RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto });
  70. _images = new CollectionView()
  71. {
  72. ItemsLayout = new LinearItemsLayout(ItemsLayoutOrientation.Horizontal) { ItemSpacing = 10 },
  73. ItemSizingStrategy = ItemSizingStrategy.MeasureAllItems,
  74. ItemTemplate = new DataTemplate(() =>
  75. {
  76. var image = new Image()
  77. {
  78. Aspect = Aspect.AspectFit,
  79. VerticalOptions = LayoutOptions.Fill,
  80. HorizontalOptions = LayoutOptions.FillAndExpand,
  81. WidthRequest = 200,
  82. Margin = new Thickness(10),
  83. };
  84. image.SetBinding(Image.SourceProperty, new Binding("Value",BindingMode.Default,new ByteArrayToImageSourceConverter()));
  85. image.SetValue(Grid.RowProperty,0);
  86. image.SetValue(Grid.ColumnProperty,0);
  87. image.SetValue(Grid.ColumnSpanProperty,2);
  88. var label = new Label()
  89. {
  90. HorizontalOptions = LayoutOptions.Fill,
  91. HorizontalTextAlignment = TextAlignment.Center
  92. };
  93. label.SetBinding(Image.SourceProperty, new Binding("Key"));
  94. label.SetValue(Grid.RowProperty,1);
  95. label.SetValue(Grid.ColumnProperty,0);
  96. var button = new MobileMenuButton()
  97. {
  98. Image = ImageSource.FromFile("cross")
  99. };
  100. button.Clicked += (sender, args) =>
  101. {
  102. /* DoDelete functionality here*/
  103. };
  104. button.SetValue(Grid.RowProperty,1);
  105. button.SetValue(Grid.ColumnProperty,1);
  106. var grid = new Grid();
  107. grid.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Star });
  108. grid.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto });
  109. grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Star });
  110. grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Auto });
  111. grid.Children.Add(image);
  112. grid.Children.Add(label);
  113. grid.Children.Add(button);
  114. var frame = new MobileCard()
  115. {
  116. BorderColor = Color.White,
  117. BackgroundColor = Color.White,
  118. CornerRadius = 5,
  119. Content = grid
  120. };
  121. return frame;
  122. }),
  123. };
  124. _frame = new MobileCard()
  125. {
  126. Content = _images,
  127. Padding = 10
  128. };
  129. _frame.SetValue(Grid.RowProperty,0);
  130. _frame.SetValue(Grid.ColumnProperty,0);
  131. _frame.SetValue(Grid.RowSpanProperty,4);
  132. Children.Add(_frame);
  133. _add = new MobileButton()
  134. {
  135. Image = ImageSource.FromFile("plus"),
  136. Orientation = StackOrientation.Horizontal,
  137. WidthRequest = 40,
  138. HeightRequest = 40,
  139. CornerRadius = 5
  140. };
  141. _add.Clicked += (sender, args) =>
  142. {
  143. };
  144. _add.SetValue(Grid.RowProperty,0);
  145. _add.SetValue(Grid.ColumnProperty,1);
  146. Children.Add(_add);
  147. _select = new MobileButton()
  148. {
  149. Image = ImageSource.FromFile("photolibrary"),
  150. Orientation = StackOrientation.Horizontal,
  151. WidthRequest = 40,
  152. HeightRequest = 40,
  153. CornerRadius = 5
  154. };
  155. _select.Clicked += (sender, args) =>
  156. {
  157. };
  158. _select.SetValue(Grid.RowProperty,1);
  159. _select.SetValue(Grid.ColumnProperty,1);
  160. Children.Add(_select);
  161. _clear = new MobileButton()
  162. {
  163. Image = ImageSource.FromFile("cross"),
  164. Orientation = StackOrientation.Horizontal,
  165. WidthRequest = 40,
  166. HeightRequest = 40,
  167. CornerRadius = 5
  168. };
  169. _clear.Clicked += (sender, args) =>
  170. {
  171. };
  172. _clear.SetValue(Grid.RowProperty,3);
  173. _clear.SetValue(Grid.ColumnProperty,1);
  174. Children.Add(_clear);
  175. }
  176. private void Initialize(DFLayoutMultiSignaturePad value)
  177. {
  178. UpdateStatus();
  179. }
  180. protected bool Secure => Definition?.Properties.Secure ?? false;
  181. protected bool Required => Definition?.Properties.Required ?? false;
  182. private void UpdateStatus()
  183. {
  184. bool enabled = !_readOnly && !Secure;
  185. _add.IsEnabled = enabled;
  186. _clear.IsEnabled = enabled;
  187. var colors = DigitalFormUtils.GetColors(!enabled, Required, false);
  188. _frame.BackgroundColor = colors.Background;
  189. _frame.BorderColor = colors.Border;
  190. colors = DigitalFormUtils.GetColors(!enabled, Required, true);
  191. _add.BackgroundColor = colors.Background;
  192. _add.BorderColor = colors.Border;
  193. _clear.BackgroundColor = colors.Background;
  194. _clear.BorderColor = colors.Border;
  195. }
  196. }
  197. }