123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- using InABox.Core;
- using Xamarin.Forms;
- using Xamarin.Forms.Xaml;
- using InABox.Mobile;
- using XF.Material.Forms.UI.Dialogs;
- using Comal.Classes;
- namespace PRS.Mobile
- {
- public class DigitalFormsHostClosingArgs : EventArgs
- {
- public bool Changed { get; }
- public DigitalFormsHostClosingArgs(bool changed)
- {
- Changed = changed;
- }
- }
- public delegate void DigitalFormsHostClosingDelegate(object sender, DigitalFormsHostClosingArgs args);
- [XamlCompilation(XamlCompilationOptions.Compile)]
- public partial class DigitalFormHost
- {
- public event DigitalFormsHostClosingDelegate OnClosing;
- public IDigitalFormHostModel Model { get; private set; }
- QAFormViewer viewer;
- bool readOnly = false;
- public DigitalFormDocumentModel _documents;
- public DigitalFormHost(IDigitalFormHostModel model, Guid jobid = default(Guid))
- {
-
-
- InitializeComponent();
- ProgressVisible = true;
- Title = model.DigitalFormDataModel.Instance.Form.Description;
-
- Model = model;
-
- _saveButton.IsVisible = !Model.ReadOnly;
- //_saveProgress.IsEnabled = !Model.ReadOnly;
- viewer = new QAFormViewer(Model.DigitalFormDataModel, Model.DFLayout, jobid);
- LoadDocs();
- formViewerScroller.Content = viewer;
- }
- protected override void OnAppearing()
- {
- base.OnAppearing();
- ProgressVisible = false;
- }
- private void ExitBtn_Clicked(object sender, EventArgs e)
- {
-
- }
- protected override async Task<bool> OnBackButtonClicked()
- {
- Dispatcher.BeginInvokeOnMainThread(() =>
- {
- RetainedResults.IsFormRetained = false;
- OnClosing?.Invoke(this, new DigitalFormsHostClosingArgs(false));
- Navigation.PopAsync();
- });
- return false;
- }
- private void SaveBtn_Clicked(object sender, EventArgs e)
- {
- SaveOptions();
- }
- //private async void DocumentBtn_Clicked(object sender, EventArgs e)
- //{
- //DocumentList pdfList = new DocumentList() { ItemsSource = _documents};
- //Navigation.PushAsync(pdfList);
- //}
- private void LoadDocs()
- {
- var docflt = new Filter<DigitalFormDocument>(x => x.EntityLink.ID).IsEqualTo(Model.DigitalFormDataModel.Instance.Form.ID);
- _documents = new DigitalFormDocumentModel(App.Data,
- () => docflt
- );
- _documents.Refresh(false);
- _doclist.ItemsSource = _documents;
- _tabStrip.IsVisible = _documents.Any();
- }
- private async void SaveOptions()
- {
- try
- {
- string chosenOption = "";
- if (Model.DigitalFormLayout.Form.AppliesTo.Equals("Kanban") || Model.DigitalFormLayout.Form.AppliesTo.Equals("Job"))
- chosenOption = await DisplayActionSheet("Select Option", "Cancel", null, "Save Progress", "Complete Form", "Complete and Duplicate","Delete Form");
- else
- chosenOption = await DisplayActionSheet("Select Option", "Cancel", null, "Save Progress", "Complete Form","Delete Form");
-
- if (!string.IsNullOrEmpty(chosenOption))
- {
- if (!chosenOption.Equals("Cancel"))
- {
- bool saveForLater = false;
- if (chosenOption.Equals("Save Progress"))
- {
- saveForLater = true;
- ClearRetainedStatusAndResults();
- }
- else if (chosenOption.Equals("Complete Form"))
- {
- ClearRetainedStatusAndResults();
- }
- else if (chosenOption.Equals("Complete and Duplicate"))
- {
- RetainedResults.IsFormRetained = true;
- RetainedResults.RetainedFormName = Model.DigitalFormLayout.Description;
- RetainedResults.Results = new Dictionary<string, string>();
- }
- bool bDeleting = chosenOption.Equals("Delete Form");
-
- using (await MaterialDialog.Instance.LoadingDialogAsync(message: bDeleting ? "Deleting Form" : "Saving Form"))
- {
-
- viewer.SaveData(saveForLater,bDeleting);
-
- if (!bDeleting)
- {
- if (viewer.isRequiredEmpty)
- {
- Device.BeginInvokeOnMainThread(async () =>
- {
- await DisplayAlert("Alert", "Please fill in compulsory field \"" + viewer.isRequiredMessage
- + "\" in order to submit form. (Compulsory fields are highlighted in orange)."
- , "OK");
- });
- return;
- }
- if (viewer.errors.Count > 0)
- {
- string message = "";
- int count = 1;
- foreach (string s in viewer.errors)
- {
- if (s.Contains("same key"))
- {
- string[] arrary = s.Split("Key");
- 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) + ")"
- + System.Environment.NewLine;
- }
- else
- {
- message = message + count + ". " + s + System.Environment.NewLine;
- count++;
- }
- }
- Device.BeginInvokeOnMainThread(() =>
- {
- DisplayAlert("Form saved but errors may be present", message, "OK");
- });
- InABox.Mobile.MobileLogging.Log(message);
- }
- }
-
- }
- if (chosenOption.Equals("Complete and Duplicate"))
- {
- viewer.DuplicateInstance();
- viewer.LoadData();
- }
- else
- {
- OnClosing?.Invoke(this, new DigitalFormsHostClosingArgs(true));
- Navigation.PopAsync();
- }
-
- }
- }
- }
- catch (Exception ex)
- {
- DisplayAlert("Alert", "Unable to update form! Issues: " + Environment.NewLine + ex.Message, "OK");
- InABox.Mobile.MobileLogging.Log(ex);
- }
- }
- private void ClearRetainedStatusAndResults()
- {
- try
- {
- RetainedResults.IsFormRetained = false;
- if (RetainedResults.Results != null)
- RetainedResults.Results.Clear();
- }
- catch
- {
- return;
- }
- }
- void _tabStrip_OnSelectionChanged(System.Object sender, System.EventArgs e)
- {
- _tabView.SelectedItem = _tabView.Items[_tabStrip.SelectedItem.Index];
- }
- }
- }
|