AssignmentForms.xaml.cs 4.1 KB

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