MultiSignaturePad.xaml.cs 11 KB

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