DigitalFormHost.xaml.cs 9.3 KB

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