DigitalFormHost.xaml.cs 8.7 KB

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