AssignmentForms.xaml.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Linq.Expressions;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using comal.timesheets.QAForms;
  8. using Comal.Classes;
  9. using InABox.Clients;
  10. using InABox.Core;
  11. using Xamarin.Forms;
  12. using Xamarin.Forms.Xaml;
  13. using XF.Material.Forms.UI;
  14. using XF.Material.Forms.UI.Dialogs;
  15. namespace comal.timesheets
  16. {
  17. [XamlCompilation(XamlCompilationOptions.Compile)]
  18. public partial class AssignmentForms : ContentView, IAssignmentPage
  19. {
  20. List<Log> logs = new List<Log>();
  21. public AssignmentEditDataModel DataModel => BindingContext as AssignmentEditDataModel;
  22. public AssignmentForms()
  23. {
  24. InitializeComponent();
  25. }
  26. public void Load()
  27. {
  28. NoForms.IsVisible = !DataModel.Forms.Any();
  29. Forms.IsVisible = DataModel.Forms.Any();
  30. Forms.ItemsSource = DataModel.Forms.ToArray();
  31. }
  32. private async void MaterialCard_OnClicked(object sender, EventArgs e)
  33. {
  34. logs.Add(new Log("Clicked", QAForms.MobileLogType.START));
  35. using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Loading"))
  36. {
  37. var instance = (sender as MaterialCard).BindingContext as AssignmentFormInstance;
  38. var form = new Client<AssignmentForm>()
  39. .Query(new Filter<AssignmentForm>(x => x.ID).IsEqualTo(instance.ID))
  40. .Rows
  41. .Select(x => x.ToObject<AssignmentForm>())
  42. .FirstOrDefault();
  43. var model = new DigitalFormHostModel<Assignment, AssignmentLink, AssignmentForm>();
  44. logs.Add(new Log("Load Items", QAForms.MobileLogType.START));
  45. model.LoadItems(DataModel.Item.Entity, form, null);
  46. logs.Add(new Log("Load Items", QAForms.MobileLogType.END));
  47. var host = new DigitalFormHost(model);
  48. host.OnClosing += (o, args) =>
  49. {
  50. if (args.Changed)
  51. {
  52. instance.Completed = !model.DigitalFormDataModel.Instance.FormCompleted.IsEmpty();
  53. }
  54. };
  55. Navigation.PushAsync(host);
  56. }
  57. logs.Add(new Log("Clicked", QAForms.MobileLogType.END));
  58. var log = Log.ProduceLog(logs);
  59. }
  60. private void AddForm_OnClicked(object sender, EventArgs e)
  61. {
  62. GenericSelectionPage page = new GenericSelectionPage
  63. (
  64. "Select Type",
  65. new SelectionViewModel<EmployeeDigitalForm>
  66. (
  67. new Filter<EmployeeDigitalForm>(x => x.Form.AppliesTo).IsEqualTo(typeof(Assignment).EntityName().Split('.').Last())
  68. .And(x=>x.Employee.ID).IsEqualTo(DataModel.EmployeeID)
  69. .And(x=>x.Form.Active).IsEqualTo(true)
  70. .And(x=>x.Form.Secure).IsEqualTo(false),
  71. new Expression<Func<EmployeeDigitalForm, object>>[] { x => x.Form.Code, x => x.Form.Description },
  72. new Expression<Func<EmployeeDigitalForm, object>>[] { x => x.Form.ID },
  73. new SortOrder<EmployeeDigitalForm>(x => x.Form.Code)
  74. ));
  75. page.OnItemSelected += (row) =>
  76. {
  77. var form = new AssignmentForm();
  78. form.Parent.ID = DataModel.Item.ID;
  79. form.Form.ID = row.Get<EmployeeDigitalForm, Guid>(x => x.Form.ID);
  80. form.Form.Code = row.Get<EmployeeDigitalForm, String>(x => x.Form.Code);
  81. form.Form.Description = row.Get<EmployeeDigitalForm, String>(x => x.Form.Description);
  82. new Client<AssignmentForm>().Save(form,"Created on Mobile Device",
  83. (o, e) =>
  84. {
  85. Dispatcher.BeginInvokeOnMainThread(
  86. () => {
  87. DataModel.Forms.Add(
  88. new AssignmentFormInstance()
  89. {
  90. ID = o.ID,
  91. Description = o.Form.Description,
  92. Completed = false
  93. }
  94. );
  95. Forms.ItemsSource = DataModel.Forms.ToArray();
  96. }
  97. );
  98. }
  99. );
  100. };
  101. Navigation.PushAsync(page);
  102. }
  103. }
  104. }