DigitalFormSignature.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6. using InABox.Core;
  7. using InABox.Mobile;
  8. using Syncfusion.XForms.SignaturePad;
  9. using Xamarin.Forms;
  10. namespace PRS.Mobile
  11. {
  12. public class DigitalFormSignature : MobileCard, IDigitalFormField<DFLayoutSignaturePad, DFLayoutSignaturePadProperties, byte[]>
  13. {
  14. private readonly Grid _grid;
  15. private readonly SfSignaturePad _pad;
  16. private readonly Image _image;
  17. private readonly MobileButton _save;
  18. private readonly MobileButton _apply;
  19. private readonly MobileButton _clear;
  20. private DFLayoutSignaturePad _definition;
  21. public DFLayoutSignaturePad Definition
  22. {
  23. get => _definition;
  24. set
  25. {
  26. _definition = value;
  27. Initialize(value ?? new DFLayoutSignaturePad());
  28. }
  29. }
  30. private byte[] _value;
  31. public byte[] Value
  32. {
  33. get => _value;
  34. set
  35. {
  36. _value = value;
  37. UpdateValue();
  38. }
  39. }
  40. public bool IsEmpty => Value?.Any() != true;
  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(string serialized)
  52. {
  53. if (!String.IsNullOrWhiteSpace(serialized) && serialized.IsBase64String())
  54. Value = Convert.FromBase64String(serialized);
  55. else
  56. Value = null;
  57. }
  58. public string Serialize()
  59. {
  60. if (_value == null)
  61. {
  62. _pad.Save();
  63. _value = GetSignature(_pad.ImageSource);
  64. }
  65. return Convert.ToBase64String(_value ?? new byte[] { });
  66. }
  67. public event DigitalFormViewChangedHandler ValueChanged;
  68. public DigitalFormSignature()
  69. {
  70. Padding = 5;
  71. _grid = new Grid();
  72. _grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Star });
  73. _grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Star });
  74. _grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Star });
  75. _grid.RowDefinitions.Add(new RowDefinition() { Height= GridLength.Star });
  76. _grid.RowDefinitions.Add(new RowDefinition() { Height= GridLength.Auto });
  77. _save = AddButton("Save New", SaveSignature, 0, false);
  78. _apply = AddButton("Apply Saved", ApplySignature, 1, App.Data.Me.Signature?.Any() == true);
  79. _clear = AddButton("Clear", ClearSignature, 2, false);
  80. _pad = new SfSignaturePad()
  81. {
  82. MinimumStrokeWidth = 0.2,
  83. MaximumStrokeWidth = 17,
  84. BackgroundColor = Color.Transparent
  85. };
  86. _pad.StrokeCompleted += (sender, args) =>
  87. {
  88. _save.IsEnabled = true;
  89. _clear.IsEnabled = true;
  90. };
  91. _pad.SetValue(Grid.ColumnProperty,0);
  92. _pad.SetValue(Grid.ColumnSpanProperty,3);
  93. _pad.SetValue(Grid.RowProperty,0);
  94. _grid.Children.Add(_pad);
  95. _image = new Image
  96. {
  97. IsVisible = false
  98. };
  99. _image.SetValue(Grid.ColumnProperty,0);
  100. _image.SetValue(Grid.ColumnSpanProperty,3);
  101. _image.SetValue(Grid.RowProperty,0);
  102. _grid.Children.Add(_image);
  103. Content = _grid;
  104. }
  105. private MobileButton AddButton(String text, Action clicked, int column, bool enabled)
  106. {
  107. var result = new MobileButton() { Text = text };
  108. result.SetValue(Grid.RowProperty,1);
  109. result.SetValue(Grid.ColumnProperty,column);
  110. result.Clicked += (sender, args) => clicked();
  111. result.IsEnabled = enabled;
  112. _grid.Children.Add(result);
  113. return result;
  114. }
  115. private void SaveSignature()
  116. {
  117. _pad.Save();
  118. _value = GetSignature(_pad.ImageSource);
  119. App.Data.Me.Signature = _value;
  120. App.Data.Me.Save("Updated Signature");
  121. _save.IsEnabled = false;
  122. _apply.IsEnabled = App.Data.Me.Signature?.Any() == true;
  123. UpdateValue();
  124. }
  125. private void ApplySignature()
  126. {
  127. _value = App.Data.Me.Signature;
  128. _save.IsEnabled = false;
  129. UpdateValue();
  130. }
  131. private void ClearSignature()
  132. {
  133. _pad.Clear();
  134. _pad.Save();
  135. _value = null;
  136. _save.IsEnabled = false;
  137. _clear.IsEnabled = false;
  138. UpdateValue();
  139. }
  140. private byte[] GetSignature(ImageSource source)
  141. {
  142. try
  143. {
  144. StreamImageSource streamImageSource = (StreamImageSource)source;
  145. CancellationToken cancellationToken = CancellationToken.None;
  146. Task<Stream> task = streamImageSource.Stream(cancellationToken);
  147. Stream stream = task.Result;
  148. byte[] bytes = new byte[stream.Length];
  149. _ = stream.Read(bytes, 0, bytes.Length);
  150. return bytes;
  151. }
  152. catch (Exception e)
  153. {
  154. InABox.Mobile.MobileLogging.Log(e,"GetSignature");
  155. return new byte[] { };
  156. }
  157. }
  158. private void Initialize(DFLayoutSignaturePad definition)
  159. {
  160. UpdateStatus();
  161. }
  162. private void UpdateStatus()
  163. {
  164. bool enabled = !_readOnly && !Definition.Properties.Secure;
  165. _pad.IsEnabled = enabled;
  166. _save.IsEnabled = enabled && (Value?.Any() == true);
  167. _apply.IsEnabled = enabled && (App.Data.Me.Signature?.Any() == true);
  168. _clear.IsEnabled = enabled && (Value?.Any() == true);
  169. var colors = DigitalFormUtils.GetColors(!enabled || _value?.Any() == true, Definition.Properties.Required, false);
  170. BackgroundColor = colors.Background;
  171. BorderColor = colors.Border;
  172. }
  173. private void UpdateValue()
  174. {
  175. if (_value == null)
  176. {
  177. _pad.Clear();
  178. _pad.IsVisible = true;
  179. _image.IsVisible = false;
  180. }
  181. else
  182. {
  183. _pad.IsVisible = false;
  184. _image.Source = ImageSource.FromStream(() => new MemoryStream(_value));
  185. _image.IsVisible = true;
  186. }
  187. UpdateStatus();
  188. }
  189. }
  190. }