DigitalFormHost.xaml.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. ProgressVisible = true;
  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. _saveButton.IsVisible = !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. protected override void OnAppearing()
  60. {
  61. base.OnAppearing();
  62. ProgressVisible = false;
  63. }
  64. private void ExitBtn_Clicked(object sender, EventArgs e)
  65. {
  66. RetainedResults.IsFormRetained = false;
  67. OnClosing?.Invoke(this, new DigitalFormsHostClosingArgs(false));
  68. Navigation.PopAsync();
  69. }
  70. private async void SaveProgressBtn_Clicked(object sender, EventArgs e)
  71. {
  72. if (!readOnly)
  73. {
  74. using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Saving Progress"))
  75. {
  76. viewer.SaveData(true);
  77. }
  78. }
  79. Navigation.PopAsync();
  80. }
  81. private void SaveBtn_Clicked(object sender, EventArgs e)
  82. {
  83. SaveOptions();
  84. }
  85. private async void DocumentBtn_Clicked(object sender, EventArgs e)
  86. {
  87. PDFList pdfList = new PDFList() { Documents = _documents, AllowUpload = true };
  88. Navigation.PushAsync(pdfList);
  89. }
  90. private void LoadDocs()
  91. {
  92. App.Data.DigitalForms.Refresh(false);
  93. _documents = App.Data.DigitalForms.Documents.Where(x => x.EntityID == Model.DigitalFormLayout.Form.ID).ToList();
  94. documentBtn.IsVisible = true;
  95. documentBtn.Text = "View Attached Document(s) (" + _documents.Count + ")";
  96. }
  97. private async void SaveOptions()
  98. {
  99. try
  100. {
  101. string chosenOption = "";
  102. if (Model.DigitalFormLayout.Form.AppliesTo.Equals("Kanban"))
  103. chosenOption = await DisplayActionSheet("Select Option", "Cancel", null, "Save Progress", "Complete Form", "Complete and Duplicate");
  104. else if (Model.DigitalFormLayout.Form.AppliesTo.Equals("Kanban") || Model.DigitalFormLayout.Form.AppliesTo.Equals("Job"))
  105. chosenOption = await DisplayActionSheet("Select Option", "Cancel", null, "Save Progress", "Complete Form", "Complete and Duplicate");
  106. else if (Model.DigitalFormLayout.Form.AppliesTo.Equals("Product"))
  107. chosenOption = await DisplayActionSheet("Select Option", "Cancel", null, "Save Progress", "Complete Form");
  108. else
  109. chosenOption = await DisplayActionSheet("Select Option", "Cancel", null, "Save Progress", "Complete Form");
  110. if (!string.IsNullOrEmpty(chosenOption))
  111. {
  112. if (!chosenOption.Equals("Cancel"))
  113. {
  114. bool saveForLater = false;
  115. if (chosenOption.Equals("Save Progress"))
  116. {
  117. saveForLater = true;
  118. ClearRetainedStatusAndResults();
  119. }
  120. else if (chosenOption.Equals("Complete Form"))
  121. {
  122. ClearRetainedStatusAndResults();
  123. }
  124. else if (chosenOption.Equals("Complete and Duplicate"))
  125. {
  126. RetainedResults.IsFormRetained = true;
  127. RetainedResults.RetainedFormName = Model.DigitalFormLayout.Description;
  128. RetainedResults.Results = new Dictionary<string, string>();
  129. }
  130. using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Saving"))
  131. {
  132. viewer.SaveData(saveForLater);
  133. if (viewer.isRequiredEmpty)
  134. {
  135. Device.BeginInvokeOnMainThread(async () =>
  136. {
  137. await DisplayAlert("Alert", "Please fill in compulsory field \"" + viewer.isRequiredMessage
  138. + "\" in order to submit form. (Compulsory fields are highlighted in orange)."
  139. , "OK");
  140. });
  141. return;
  142. }
  143. if (viewer.errors.Count > 0)
  144. {
  145. string message = "";
  146. int count = 1;
  147. foreach (string s in viewer.errors)
  148. {
  149. if (s.Contains("same key"))
  150. {
  151. string[] arrary = s.Split("Key");
  152. 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) + ")"
  153. + System.Environment.NewLine;
  154. }
  155. else
  156. {
  157. message = message + count + ". " + s + System.Environment.NewLine;
  158. count++;
  159. }
  160. }
  161. Device.BeginInvokeOnMainThread(() =>
  162. {
  163. DisplayAlert("Form saved but errors may be present", message, "OK");
  164. });
  165. InABox.Mobile.MobileLogging.Log(message);
  166. }
  167. }
  168. OnClosing?.Invoke(this, new DigitalFormsHostClosingArgs(true));
  169. Navigation.PopAsync();
  170. }
  171. }
  172. }
  173. catch (Exception ex)
  174. {
  175. DisplayAlert("Alert", "Unable to save. Issues: " + Environment.NewLine + ex.Message, "OK");
  176. InABox.Mobile.MobileLogging.Log(ex);
  177. }
  178. }
  179. private void ClearRetainedStatusAndResults()
  180. {
  181. try
  182. {
  183. RetainedResults.IsFormRetained = false;
  184. if (RetainedResults.Results != null)
  185. RetainedResults.Results.Clear();
  186. }
  187. catch
  188. {
  189. return;
  190. }
  191. }
  192. }
  193. }