DigitalFormHost.xaml.cs 9.0 KB

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