DigitalFormHost.xaml.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Xamarin.Forms;
  5. using Xamarin.Forms.Xaml;
  6. using comal.timesheets.QAForms;
  7. using InABox.Mobile;
  8. using XF.Material.Forms.UI.Dialogs;
  9. namespace comal.timesheets
  10. {
  11. public class DigitalFormsHostClosingArgs : EventArgs
  12. {
  13. public bool Changed { get; }
  14. public DigitalFormsHostClosingArgs(bool changed)
  15. {
  16. Changed = changed;
  17. }
  18. }
  19. public delegate void DigitalFormsHostClosingDelegate(object sender, DigitalFormsHostClosingArgs args);
  20. [XamlCompilation(XamlCompilationOptions.Compile)]
  21. public partial class DigitalFormHost
  22. {
  23. public event DigitalFormsHostClosingDelegate OnClosing;
  24. public IDigitalFormHostModel Model { get; private set; }
  25. QAFormViewer viewer;
  26. bool readOnly = false;
  27. public IList<IDocumentShell> _documents = null;
  28. //Dictionary<String, Guid> fileNameDocIDs = new Dictionary<string, Guid>();
  29. public DigitalFormHost(IDigitalFormHostModel model, Guid jobid = default(Guid))
  30. {
  31. InitializeComponent();
  32. NavigationPage.SetHasBackButton(this, false);
  33. Model = model;
  34. //titleLbl.Text = Model.DigitalFormDataModel.Instance.Form.Description;
  35. Model.OnDigitalFormHostModelBeforeSave += () =>
  36. {
  37. Model.SetPropertyValues(viewer);
  38. };
  39. Model.OnDigitalFormHostModelSaved += async (responseRequest) =>
  40. {
  41. DisplayAlert("Success", "Form saved " + Model.DigitalFormDataModel.Instance.Form.Description, "OK");
  42. if (responseRequest == DigitalFormHostResponseRequest.CloseKanban)
  43. {
  44. string chosenOption = await DisplayActionSheet("Input Required", "All forms for this task are complete. Complete this task as well?", null, "Yes", "No");
  45. switch (chosenOption)
  46. {
  47. case "Yes":
  48. return DigitalFormHostUserResponse.Yes;
  49. }
  50. }
  51. return DigitalFormHostUserResponse.No;
  52. };
  53. _saveComplete.IsEnabled = !Model.ReadOnly;
  54. _saveProgress.IsEnabled = !Model.ReadOnly;
  55. viewer = new QAFormViewer(Model.DigitalFormDataModel, Model.DFLayout, Model.NewForm, Model.ReadOnly, jobid);
  56. LoadDocs();
  57. formViewerScroller.Content = viewer;
  58. }
  59. private void ExitBtn_Clicked(object sender, EventArgs e)
  60. {
  61. RetainedResults.IsFormRetained = false;
  62. OnClosing?.Invoke(this, new DigitalFormsHostClosingArgs(false));
  63. Navigation.PopAsync();
  64. }
  65. private async void SaveProgressBtn_Clicked(object sender, EventArgs e)
  66. {
  67. if (!readOnly)
  68. {
  69. using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Saving Progress"))
  70. {
  71. viewer.SaveData(true);
  72. }
  73. }
  74. Navigation.PopAsync();
  75. }
  76. private void SaveBtn_Clicked(object sender, EventArgs e)
  77. {
  78. SaveOptions();
  79. }
  80. private async void DocumentBtn_Clicked(object sender, EventArgs e)
  81. {
  82. PDFList pdfList = new PDFList() { Documents = _documents, AllowUpload = true };
  83. Navigation.PushAsync(pdfList);
  84. }
  85. private void LoadDocs()
  86. {
  87. App.Data.DigitalForms.Refresh(false);
  88. _documents = App.Data.DigitalForms.Documents.Where(x => x.EntityID == Model.DigitalFormLayout.Form.ID).ToList();
  89. documentBtn.IsVisible = true;
  90. documentBtn.Text = "View Attached Document(s) (" + _documents.Count + ")";
  91. }
  92. private async void SaveOptions()
  93. {
  94. try
  95. {
  96. string chosenOption = "";
  97. if (Model.DigitalFormLayout.Form.AppliesTo.Equals("Kanban"))
  98. chosenOption = await DisplayActionSheet("Select Option", "Cancel", null, "Save Progress", "Complete Form", "Complete and Duplicate");
  99. else if (Model.DigitalFormLayout.Form.AppliesTo.Equals("Kanban") || Model.DigitalFormLayout.Form.AppliesTo.Equals("Job"))
  100. chosenOption = await DisplayActionSheet("Select Option", "Cancel", null, "Save Progress", "Complete Form", "Complete and Duplicate");
  101. else if (Model.DigitalFormLayout.Form.AppliesTo.Equals("Product"))
  102. chosenOption = await DisplayActionSheet("Select Option", "Cancel", null, "Save Progress", "Complete Form");
  103. else
  104. chosenOption = await DisplayActionSheet("Select Option", "Cancel", null, "Save Progress", "Complete Form");
  105. if (!string.IsNullOrEmpty(chosenOption))
  106. {
  107. if (!chosenOption.Equals("Cancel"))
  108. {
  109. bool saveForLater = false;
  110. if (chosenOption.Equals("Save Progress"))
  111. {
  112. saveForLater = true;
  113. ClearRetainedStatusAndResults();
  114. }
  115. else if (chosenOption.Equals("Complete Form"))
  116. {
  117. ClearRetainedStatusAndResults();
  118. }
  119. else if (chosenOption.Equals("Complete and Duplicate"))
  120. {
  121. RetainedResults.IsFormRetained = true;
  122. RetainedResults.RetainedFormName = Model.DigitalFormLayout.Description;
  123. RetainedResults.Results = new Dictionary<string, string>();
  124. }
  125. using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Saving"))
  126. {
  127. viewer.SaveData(saveForLater);
  128. if (viewer.isRequiredEmpty)
  129. {
  130. Device.BeginInvokeOnMainThread(async () =>
  131. {
  132. await DisplayAlert("Alert", "Please fill in compulsory field \"" + viewer.isRequiredMessage
  133. + "\" in order to submit form. (Compulsory fields are highlighted in orange)."
  134. , "OK");
  135. });
  136. return;
  137. }
  138. if (viewer.errors.Count > 0)
  139. {
  140. string message = "";
  141. int count = 1;
  142. foreach (string s in viewer.errors)
  143. {
  144. if (s.Contains("same key"))
  145. {
  146. string[] arrary = s.Split("Key");
  147. 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) + ")"
  148. + System.Environment.NewLine;
  149. }
  150. else
  151. {
  152. message = message + count + ". " + s + System.Environment.NewLine;
  153. count++;
  154. }
  155. }
  156. Device.BeginInvokeOnMainThread(() =>
  157. {
  158. DisplayAlert("Form saved but errors may be present", message, "OK");
  159. });
  160. InABox.Mobile.MobileLogging.Log(message);
  161. }
  162. }
  163. OnClosing?.Invoke(this, new DigitalFormsHostClosingArgs(true));
  164. Navigation.PopAsync();
  165. }
  166. }
  167. }
  168. catch (Exception ex)
  169. {
  170. DisplayAlert("Alert", "Unable to save. Issues: " + Environment.NewLine + ex.Message, "OK");
  171. InABox.Mobile.MobileLogging.Log(ex);
  172. }
  173. }
  174. private void ClearRetainedStatusAndResults()
  175. {
  176. try
  177. {
  178. RetainedResults.IsFormRetained = false;
  179. if (RetainedResults.Results != null)
  180. RetainedResults.Results.Clear();
  181. }
  182. catch
  183. {
  184. return;
  185. }
  186. }
  187. }
  188. }