MultiSignaturePad.xaml.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. using Comal.Classes;
  2. using InABox.Clients;
  3. using InABox.Core;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using Xamarin.Forms;
  10. using Xamarin.Forms.Xaml;
  11. using XF.Material.Forms.UI.Dialogs;
  12. namespace PRS.Mobile
  13. {
  14. [XamlCompilation(XamlCompilationOptions.Compile)]
  15. public partial class MultiSignaturePad
  16. {
  17. public MultiSignatureSavedEvent OnMultiSignatureSaved;
  18. string Result = "";
  19. List<string> originalnames = new List<string>();
  20. List<string> newnames = new List<string>();
  21. List<string> tempCacheNames = new List<string>();
  22. List<string> addedNames = new List<string>();
  23. double currentheight = 0;
  24. Dictionary<string, string> namesSignatures = new Dictionary<string, string>();
  25. Dictionary<string, string> originalNamesSignatures = new Dictionary<string, string>();
  26. public MultiSignaturePad(string incomingData = "")
  27. {
  28. InitializeComponent();
  29. AddNames(incomingData);
  30. AddPads(3);
  31. }
  32. void PopulateNames_Clicked(object sender, EventArgs e)
  33. {
  34. var list = new EmployeeMultiSelectionPage(
  35. (employees) => List_OnEmployeesSaved(employees.ToList())
  36. );
  37. Navigation.PushAsync(list);
  38. }
  39. private async void List_OnEmployeesSaved(List<EmployeeShell> employees)
  40. {
  41. using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Loading"))
  42. {
  43. foreach (string s in addedNames)
  44. {
  45. var emp = employees.Find(x => x.Name == s);
  46. if (emp != null)
  47. employees.Remove(emp);
  48. }
  49. if (stackLayout.Children.Count < 4)
  50. {
  51. int extraSignaturesNeeded = 0;
  52. if (employees.Count > 3)
  53. extraSignaturesNeeded = employees.Count - 3;
  54. AddPads(extraSignaturesNeeded);
  55. }
  56. else
  57. AddPads(employees.Count);
  58. int count = 0;
  59. foreach (SignaturePad pad in stackLayout.Children)
  60. {
  61. if (string.IsNullOrWhiteSpace(pad.Name))
  62. {
  63. try
  64. {
  65. pad.PopulateName(employees[count].Name);
  66. LookupSignature(employees[count].ID, pad);
  67. addedNames.Add(employees[count].Name);
  68. count++;
  69. }
  70. catch { }
  71. }
  72. }
  73. }
  74. }
  75. private void LookupSignature(Guid employeeID, SignaturePad pad)
  76. {
  77. try
  78. {
  79. CoreTable table = new Client<Employee>().Query(new Filter<Employee>(x => x.ID).IsEqualTo(employeeID),
  80. new Columns<Employee>(x => x.Signature));
  81. if (table.Rows.Any())
  82. {
  83. byte[] data = table.Rows.First().Get<byte[]>("Signature");
  84. if (data != null)
  85. pad.PopulateSignature(data);
  86. }
  87. }
  88. catch { }
  89. }
  90. void AddNames(string incomingData)
  91. {
  92. if (!string.IsNullOrWhiteSpace(incomingData))
  93. {
  94. try
  95. {
  96. Dictionary<string, string> incoming = Serialization.Deserialize<Dictionary<String, string>>(incomingData);
  97. originalNamesSignatures = Serialization.Deserialize<Dictionary<String, string>>(incomingData);
  98. foreach (string s in incoming.Keys)
  99. {
  100. originalnames.Add(s);
  101. }
  102. if (originalnames.Count > 0)
  103. {
  104. int index = fixedStackLayout.Children.Count - 1;
  105. ScrollView scrollView = new ScrollView { Orientation = ScrollOrientation.Horizontal, HeightRequest = 50 };
  106. StackLayout newStackLayout = new StackLayout { Orientation = StackOrientation.Horizontal };
  107. scrollView.Content = newStackLayout;
  108. fixedStackLayout.Children.Insert
  109. (index, new Label
  110. {
  111. Text = "Previously signed: ",
  112. HorizontalOptions = LayoutOptions.Center,
  113. FontAttributes = FontAttributes.Bold,
  114. VerticalOptions = LayoutOptions.Center,
  115. FontSize = 20
  116. }
  117. );
  118. fixedStackLayout.Children.Insert
  119. (
  120. index + 1, scrollView
  121. );
  122. int count = 1;
  123. foreach (string s in originalnames)
  124. {
  125. newStackLayout.Children.Add
  126. (
  127. new Frame
  128. {
  129. BorderColor = Color.FromHex("#873260"),
  130. CornerRadius = 5,
  131. HasShadow = false,
  132. Padding = 0,
  133. Content = new Label
  134. {
  135. Text = count + ". " + s,
  136. HorizontalOptions = LayoutOptions.Center,
  137. VerticalOptions = LayoutOptions.Center,
  138. FontSize = 16,
  139. Margin = new Thickness(6, 0, 6, 0)
  140. }
  141. }
  142. );
  143. count++;
  144. }
  145. }
  146. }
  147. catch { }
  148. }
  149. }
  150. void AddPads(int count)
  151. {
  152. for (int i = 0; i < count; i++)
  153. {
  154. AddPad();
  155. }
  156. }
  157. void AddPad()
  158. {
  159. SignaturePad pad = new SignaturePad();
  160. pad.HeightRequest = 200;
  161. pad.Margin = new Thickness(1);
  162. pad.OnNameAdded += (s) =>
  163. {
  164. if (tempCacheNames.Contains(s) || originalnames.Contains(s))
  165. {
  166. DisplayAlert("Error", "Duplicate name detected for " + s + ". Please try again", "OK");
  167. pad.ClearName();
  168. }
  169. else
  170. tempCacheNames.Add(s);
  171. };
  172. pad.OnNameRemoved += (s) =>
  173. {
  174. if (tempCacheNames.Contains(s))
  175. tempCacheNames.Remove(s);
  176. if (addedNames.Contains(s))
  177. addedNames.Remove(s);
  178. };
  179. stackLayout.Children.Add(pad);
  180. }
  181. void Scrolldown_Tapped(object sender, EventArgs e)
  182. {
  183. AddPad();
  184. double height = 600;
  185. foreach (SignaturePad signaturePad in stackLayout.Children)
  186. {
  187. height = height + 200;
  188. }
  189. currentheight = height;
  190. scrollView.ScrollToAsync(0, height, true);
  191. }
  192. void AddSignature_Clicked(object sender, EventArgs e)
  193. {
  194. AddPad();
  195. double height = 600;
  196. foreach (SignaturePad signaturePad in stackLayout.Children)
  197. {
  198. height = height + 200;
  199. }
  200. currentheight = height;
  201. scrollView.ScrollToAsync(0, height, true);
  202. }
  203. void Save_Clicked(object sender, EventArgs e)
  204. {
  205. namesSignatures.Clear();
  206. newnames.Clear();
  207. foreach (SignaturePad pad in stackLayout.Children)
  208. {
  209. if (!string.IsNullOrWhiteSpace(pad.Name))
  210. {
  211. if (originalnames.Contains(pad.Name))
  212. {
  213. DisplayAlert("Error", "Duplicate names entered for " + pad.Name + " - please check and try again", "OK");
  214. return;
  215. }
  216. if (newnames.Contains(pad.Name))
  217. {
  218. DisplayAlert("Error", "Duplicate names entered for " + pad.Name + " - please check and try again", "OK");
  219. return;
  220. }
  221. else
  222. newnames.Add(pad.Name);
  223. }
  224. }
  225. foreach (SignaturePad pad in stackLayout.Children)
  226. {
  227. if (pad.StrokeAdded)
  228. {
  229. if (string.IsNullOrWhiteSpace(pad.Name))
  230. {
  231. DisplayAlert("Error", "Please fill in a name for all pads that have signatures", "OK");
  232. return;
  233. }
  234. }
  235. if (!string.IsNullOrWhiteSpace(pad.Name))
  236. {
  237. if (!pad.StrokeAdded)
  238. {
  239. DisplayAlert("Error", "Please add a signature for all names", "OK");
  240. return;
  241. }
  242. }
  243. }
  244. foreach (SignaturePad pad in stackLayout.Children)
  245. {
  246. if (!string.IsNullOrWhiteSpace(pad.Name))
  247. {
  248. pad.SaveSignature(); //saves image to base64
  249. if (!string.IsNullOrWhiteSpace(pad.Data))
  250. if (!namesSignatures.ContainsKey(pad.Name) && !originalNamesSignatures.ContainsKey(pad.Name))
  251. namesSignatures.Add(pad.Name, pad.Data);
  252. }
  253. }
  254. if (namesSignatures.Count > 0)
  255. {
  256. if (originalNamesSignatures.Count > 0)
  257. {
  258. foreach (var v in namesSignatures)
  259. {
  260. originalNamesSignatures.Add(v.Key, v.Value);
  261. }
  262. Result = Serialization.Serialize(originalNamesSignatures);
  263. }
  264. else
  265. {
  266. Result = Serialization.Serialize(namesSignatures);
  267. }
  268. OnMultiSignatureSaved?.Invoke(Result);
  269. DisplayAlert("Success", namesSignatures.Count + " signatures added to form. Form still needs to be saved", "OK");
  270. }
  271. Navigation.PopAsync();
  272. }
  273. }
  274. public delegate void MultiSignatureSavedEvent(string result);
  275. }