DigitalFormHost.xaml.cs 8.4 KB

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