DigitalFormHost.xaml.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. using InABox.Core;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using PRSClasses;
  8. using Xamarin.Forms;
  9. using Xamarin.Forms.Xaml;
  10. using comal.timesheets.QAForms;
  11. using InABox.Clients;
  12. using Comal.Classes;
  13. using XF.Material.Forms.UI.Dialogs;
  14. namespace comal.timesheets
  15. {
  16. public class DigitalFormsHostClosingArgs : EventArgs
  17. {
  18. public bool Changed { get; }
  19. public DigitalFormsHostClosingArgs(bool changed)
  20. {
  21. Changed = changed;
  22. }
  23. }
  24. public delegate void DigitalFormsHostClosingDelegate(object sender, DigitalFormsHostClosingArgs args);
  25. [XamlCompilation(XamlCompilationOptions.Compile)]
  26. public partial class DigitalFormHost : ContentPage
  27. {
  28. public event DigitalFormsHostClosingDelegate OnClosing;
  29. public IDigitalFormHostModel Model { get; private set; }
  30. QAFormViewer viewer;
  31. bool readOnly = false;
  32. Dictionary<String, Guid> fileNameDocIDs = new Dictionary<string, Guid>();
  33. public DigitalFormHost(IDigitalFormHostModel model, Guid jobid = default(Guid))
  34. {
  35. InitializeComponent();
  36. NavigationPage.SetHasBackButton(this, false);
  37. Model = model;
  38. //titleLbl.Text = Model.DigitalFormDataModel.Instance.Form.Description;
  39. Model.OnDigitalFormHostModelBeforeSave += () =>
  40. {
  41. Model.SetPropertyValues(viewer);
  42. };
  43. Model.OnDigitalFormHostModelSaved += async (responseRequest) =>
  44. {
  45. DisplayAlert("Success", "Form saved " + Model.DigitalFormDataModel.Instance.Form.Description, "OK");
  46. if (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. saveBtn.IsEnabled = !Model.ReadOnly;
  58. saveProgressBtn.IsEnabled = !Model.ReadOnly;
  59. viewer = new QAFormViewer(Model.DigitalFormDataModel, Model.DFLayout, Model.NewForm, Model.ReadOnly, jobid);
  60. LoadDocs();
  61. formViewerScroller.Content = viewer;
  62. }
  63. private void ExitBtn_Clicked(object sender, EventArgs e)
  64. {
  65. RetainedResults.IsFormRetained = false;
  66. OnClosing?.Invoke(this, new DigitalFormsHostClosingArgs(false));
  67. Navigation.PopAsync();
  68. }
  69. private async void SaveProgressBtn_Clicked(object sender, EventArgs e)
  70. {
  71. if (!readOnly)
  72. {
  73. using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Saving Progress"))
  74. {
  75. viewer.SaveData(true);
  76. }
  77. }
  78. Navigation.PopAsync();
  79. }
  80. private void SaveBtn_Clicked(object sender, EventArgs e)
  81. {
  82. SaveOptions();
  83. }
  84. private async void DocumentBtn_Clicked(object sender, EventArgs e)
  85. {
  86. if (fileNameDocIDs.Count > 1)
  87. {
  88. PDFList pdfList = new PDFList(fileNameDocIDs, true);
  89. Navigation.PushAsync(pdfList);
  90. }
  91. else if (fileNameDocIDs.Count == 1)
  92. {
  93. PDFViewer pDFViewer = new PDFViewer(fileNameDocIDs.Values.First(), true);
  94. Device.BeginInvokeOnMainThread(() =>
  95. {
  96. Navigation.PushAsync(pDFViewer);
  97. });
  98. }
  99. }
  100. private void LoadDocs()
  101. {
  102. try
  103. {
  104. Task.Run(() =>
  105. {
  106. CoreTable table = new Client<DigitalFormDocument>().Query
  107. (
  108. new Filter<DigitalFormDocument>(x => x.EntityLink.ID).IsEqualTo(Model.DigitalFormLayout.Form.ID),
  109. new Columns<DigitalFormDocument>(x => x.DocumentLink.ID, x => x.DocumentLink.FileName)
  110. );
  111. if (table.Rows.Any())
  112. {
  113. foreach (CoreRow row in table.Rows)
  114. {
  115. List<object> list = row.Values;
  116. if (list[0] == null) list[0] = Guid.Empty;
  117. if (list[1] == null) list[1] = "";
  118. fileNameDocIDs.Add(list[1].ToString(), Guid.Parse(list[0].ToString()));
  119. }
  120. Device.BeginInvokeOnMainThread(() =>
  121. {
  122. documentBtn.IsVisible = true;
  123. documentBtn.Text = "View Attached Document(s) (" + table.Rows.Count + ")";
  124. });
  125. }
  126. });
  127. }
  128. catch { }
  129. }
  130. private async void SaveOptions()
  131. {
  132. try
  133. {
  134. string chosenOption = "";
  135. if (Model.DigitalFormLayout.Form.AppliesTo.Equals("Kanban"))
  136. chosenOption = await DisplayActionSheet("Select Option", "Cancel", null, "Save Progress", "Complete Form", "Complete and Duplicate");
  137. else if (Model.DigitalFormLayout.Form.AppliesTo.Equals("Kanban") || Model.DigitalFormLayout.Form.AppliesTo.Equals("Job"))
  138. chosenOption = await DisplayActionSheet("Select Option", "Cancel", null, "Save Progress", "Complete Form", "Complete and Duplicate");
  139. else if (Model.DigitalFormLayout.Form.AppliesTo.Equals("Product"))
  140. chosenOption = await DisplayActionSheet("Select Option", "Cancel", null, "Save Progress", "Complete Form");
  141. else
  142. chosenOption = await DisplayActionSheet("Select Option", "Cancel", null, "Save Progress", "Complete Form");
  143. if (!string.IsNullOrEmpty(chosenOption))
  144. {
  145. if (!chosenOption.Equals("Cancel"))
  146. {
  147. bool saveForLater = false;
  148. if (chosenOption.Equals("Save Progress"))
  149. {
  150. saveForLater = true;
  151. ClearRetainedStatusAndResults();
  152. }
  153. else if (chosenOption.Equals("Complete Form"))
  154. {
  155. ClearRetainedStatusAndResults();
  156. }
  157. else if (chosenOption.Equals("Complete and Duplicate"))
  158. {
  159. RetainedResults.IsFormRetained = true;
  160. RetainedResults.RetainedFormName = Model.DigitalFormLayout.Description;
  161. RetainedResults.Results = new Dictionary<string, string>();
  162. }
  163. using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Saving"))
  164. {
  165. viewer.SaveData(saveForLater);
  166. if (viewer.isRequiredEmpty)
  167. {
  168. Device.BeginInvokeOnMainThread(async () =>
  169. {
  170. await DisplayAlert("Alert", "Please fill in compulsory field \"" + viewer.isRequiredMessage
  171. + "\" in order to submit form. (Compulsory fields are highlighted in orange)."
  172. , "OK");
  173. });
  174. return;
  175. }
  176. if (viewer.errors.Count > 0)
  177. {
  178. string message = "";
  179. int count = 1;
  180. foreach (string s in viewer.errors)
  181. {
  182. if (s.Contains("same key"))
  183. {
  184. string[] arrary = s.Split("Key");
  185. 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) + ")"
  186. + System.Environment.NewLine;
  187. }
  188. else
  189. {
  190. message = message + count + ". " + s + System.Environment.NewLine;
  191. count++;
  192. }
  193. }
  194. Device.BeginInvokeOnMainThread(() =>
  195. {
  196. DisplayAlert("Form saved but errors may be present", message, "OK");
  197. });
  198. var log = new MobileLogging(LogType.Save, typeof(DigitalForm).ToString(), message, this.GetType().Name);
  199. }
  200. }
  201. OnClosing?.Invoke(this, new DigitalFormsHostClosingArgs(true));
  202. Navigation.PopAsync();
  203. }
  204. }
  205. }
  206. catch (Exception ex)
  207. {
  208. DisplayAlert("Alert", "Unable to save. Issues: " + Environment.NewLine + ex.Message, "OK");
  209. var log = new MobileLogging(LogType.Save, "Digital Forms Save", ex.Message, this.GetType().Name);
  210. }
  211. }
  212. private void ClearRetainedStatusAndResults()
  213. {
  214. try
  215. {
  216. RetainedResults.IsFormRetained = false;
  217. if (RetainedResults.Results != null)
  218. RetainedResults.Results.Clear();
  219. }
  220. catch
  221. {
  222. return;
  223. }
  224. }
  225. }
  226. }