DigitalFormSignature.cs 7.0 KB

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