DigitalFormHost.xaml.cs 9.5 KB

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