DigitalFormHost.xaml.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. using InABox.Core;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using PRSClasses;
  8. using Xamarin.Forms;
  9. using Xamarin.Forms.Xaml;
  10. using comal.timesheets.QAForms;
  11. using InABox.Clients;
  12. using Comal.Classes;
  13. using XF.Material.Forms.UI.Dialogs;
  14. namespace comal.timesheets
  15. {
  16. [XamlCompilation(XamlCompilationOptions.Compile)]
  17. public partial class DigitalFormHost : ContentPage
  18. {
  19. IDigitalFormHostModel model;
  20. QAFormViewer viewer;
  21. bool readOnly = false;
  22. Dictionary<String, Guid> fileNameDocIDs = new Dictionary<string, Guid>();
  23. public DigitalFormHost(IDigitalFormHostModel DFHostModel, Guid jobid = default(Guid))
  24. {
  25. InitializeComponent();
  26. NavigationPage.SetHasBackButton(this, false);
  27. model = DFHostModel;
  28. titleLbl.Text = model.DigitalFormLayout.Description;
  29. model.OnDigitalFormHostModelBeforeSave += () =>
  30. {
  31. model.SetPropertyValues(viewer);
  32. };
  33. model.OnDigitalFormHostModelSaved += () =>
  34. {
  35. DisplayAlert("Success", "Form completed: " + model.DigitalFormLayout.Description, "OK");
  36. };
  37. completeBtn.IsEnabled = !model.ReadOnly;
  38. saveProgressBtn.IsEnabled = !model.ReadOnly;
  39. viewer = new QAFormViewer(model.DigitalFormDataModel, model.DFLayout, model.NewForm, model.ReadOnly, jobid);
  40. LoadDocs();
  41. formViewerScroller.Content = viewer;
  42. }
  43. private void ExitBtn_Clicked(object sender, EventArgs e)
  44. {
  45. RetainedResults.IsFormRetained = false;
  46. Navigation.PopAsync();
  47. }
  48. private async void SaveProgressBtn_Clicked(object sender, EventArgs e)
  49. {
  50. if (!readOnly)
  51. {
  52. using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Saving Progress"))
  53. {
  54. viewer.SaveData(true);
  55. }
  56. }
  57. Navigation.PopAsync();
  58. }
  59. private void CompleteBtn_Clicked(object sender, EventArgs e)
  60. {
  61. SaveOptions();
  62. }
  63. private async void DocumentBtn_Clicked(object sender, EventArgs e)
  64. {
  65. if (fileNameDocIDs.Count > 1)
  66. {
  67. PDFList pdfList = new PDFList(fileNameDocIDs, true);
  68. Navigation.PushAsync(pdfList);
  69. }
  70. else if (fileNameDocIDs.Count == 1)
  71. {
  72. PDFViewer pDFViewer = new PDFViewer(fileNameDocIDs.Values.First(), true);
  73. Device.BeginInvokeOnMainThread(() =>
  74. {
  75. Navigation.PushAsync(pDFViewer);
  76. });
  77. }
  78. }
  79. private void LoadDocs()
  80. {
  81. try
  82. {
  83. Task.Run(() =>
  84. {
  85. CoreTable table = new Client<DigitalFormDocument>().Query
  86. (
  87. new Filter<DigitalFormDocument>(x => x.EntityLink.ID).IsEqualTo(model.DigitalFormLayout.Form.ID),
  88. new Columns<DigitalFormDocument>(x => x.DocumentLink.ID, x => x.DocumentLink.FileName)
  89. );
  90. if (table.Rows.Any())
  91. {
  92. foreach (CoreRow row in table.Rows)
  93. {
  94. List<object> list = row.Values;
  95. if (list[0] == null) list[0] = Guid.Empty;
  96. if (list[1] == null) list[1] = "";
  97. fileNameDocIDs.Add(list[1].ToString(), Guid.Parse(list[0].ToString()));
  98. }
  99. Device.BeginInvokeOnMainThread(() =>
  100. {
  101. documentBtn.IsVisible = true;
  102. documentBtn.Text = "View Attached Document(s) (" + table.Rows.Count + ")";
  103. });
  104. }
  105. });
  106. }
  107. catch { }
  108. }
  109. private async void SaveOptions()
  110. {
  111. try
  112. {
  113. string chosenOption = "";
  114. if (model.DigitalFormLayout.Form.AppliesTo.Equals("Kanban"))
  115. {
  116. chosenOption = await DisplayActionSheet("Select Save Type", "Cancel", null, "Complete", "Complete and Duplicate");
  117. }
  118. else if (model.DigitalFormLayout.Form.AppliesTo.Equals("Kanban") || model.DigitalFormLayout.Form.AppliesTo.Equals("Job"))
  119. {
  120. chosenOption = await DisplayActionSheet("Select Save Type", "Cancel", null, "Complete", "Complete and Duplicate");
  121. }
  122. else if (model.DigitalFormLayout.Form.AppliesTo.Equals("Product"))
  123. {
  124. chosenOption = await DisplayActionSheet("Save", "Cancel", null, "Complete Product Form", "Cancel");
  125. }
  126. else
  127. {
  128. chosenOption = await DisplayActionSheet("Save", "Cancel", null, "Complete", "Cancel");
  129. }
  130. if (!string.IsNullOrEmpty(chosenOption))
  131. {
  132. if (chosenOption != "Cancel")
  133. {
  134. bool saveForLater = false;
  135. if (chosenOption.Equals("Save for later"))
  136. {
  137. saveForLater = true;
  138. ClearRetainedStatusAndResults();
  139. }
  140. else if (chosenOption.Equals("Complete"))
  141. {
  142. ClearRetainedStatusAndResults();
  143. }
  144. else if (chosenOption.Equals("Complete and Duplicate"))
  145. {
  146. RetainedResults.IsFormRetained = true;
  147. RetainedResults.RetainedFormName = model.DigitalFormLayout.Description;
  148. RetainedResults.Results = new Dictionary<string, string>();
  149. }
  150. using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Saving"))
  151. {
  152. viewer.SaveData(saveForLater);
  153. if (viewer.isRequiredEmpty)
  154. {
  155. Device.BeginInvokeOnMainThread(async () =>
  156. {
  157. await DisplayAlert("Alert", "Please fill in compulsory field \"" + viewer.isRequiredMessage
  158. + "\" in order to submit form. (Compulsory fields are highlighted in orange)."
  159. , "OK");
  160. });
  161. return;
  162. }
  163. if (viewer.errors.Count > 0)
  164. {
  165. string message = "";
  166. int count = 1;
  167. foreach (string s in viewer.errors)
  168. {
  169. if (s.Contains("same key"))
  170. {
  171. string[] arrary = s.Split("Key");
  172. message = message + count + ". " + "Please check with the person that designs your forms - a duplicate variable property is present (" + (arrary[arrary.Length - 1]).Remove(0, 1) + ")"
  173. + System.Environment.NewLine;
  174. }
  175. else
  176. {
  177. message = message + count + ". " + s + System.Environment.NewLine;
  178. count++;
  179. }
  180. }
  181. Device.BeginInvokeOnMainThread(() =>
  182. {
  183. DisplayAlert("Form saved but errors may be present", message, "OK");
  184. });
  185. }
  186. }
  187. Navigation.PopAsync();
  188. }
  189. }
  190. }
  191. catch (Exception ex)
  192. {
  193. DisplayAlert("Alert", "Unable to save. Issues: " + Environment.NewLine + ex.Message, "OK");
  194. }
  195. }
  196. private void ClearRetainedStatusAndResults()
  197. {
  198. try
  199. {
  200. RetainedResults.IsFormRetained = false;
  201. RetainedResults.Results.Clear();
  202. }
  203. catch
  204. {
  205. return;
  206. }
  207. }
  208. }
  209. }