JobDocsFilterPage.xaml.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. using Comal.Classes;
  2. using InABox.Clients;
  3. using InABox.Core;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using Xamarin.Forms;
  10. using Xamarin.Forms.Xaml;
  11. namespace comal.timesheets
  12. {
  13. public delegate void JobDocFiltersPicked(Dictionary<JobDocFilterType, string> filters);
  14. [XamlCompilation(XamlCompilationOptions.Compile)]
  15. public partial class JobDocsFilterPage : ContentPage
  16. {
  17. Guid JobID = Guid.Empty;
  18. List<string> Disciplines = new List<string>();
  19. List<string> Types = new List<string>();
  20. List<string> Categories = new List<string>();
  21. List<string> Areas = new List<string>();
  22. Dictionary<JobDocFilterType, string> Filters = new Dictionary<JobDocFilterType, string>();
  23. public event JobDocFiltersPicked OnJobDocFiltersPicked;
  24. public JobDocsFilterPage(Guid jobid, Dictionary<JobDocFilterType, string> filters)
  25. {
  26. InitializeComponent();
  27. NavigationPage.SetHasBackButton(this, false);
  28. JobID = jobid;
  29. Filters = filters;
  30. LoadFilters();
  31. }
  32. private void CancelBtn_Clicked(object sender, EventArgs e)
  33. {
  34. Navigation.PopAsync();
  35. }
  36. private void SaveBtn_Clicked(object sender, EventArgs e)
  37. {
  38. Navigation.PopAsync();
  39. OnJobDocFiltersPicked?.Invoke(CreateFilters());
  40. }
  41. private Dictionary<JobDocFilterType, string> CreateFilters()
  42. {
  43. Dictionary<JobDocFilterType, string> filters = new Dictionary<JobDocFilterType, string>();
  44. foreach (var child in filterGrid.Children)
  45. {
  46. if (child is CustomPickeriOS)
  47. {
  48. var picker = child as CustomPickeriOS;
  49. if (!string.IsNullOrWhiteSpace(picker.SelectedItem))
  50. {
  51. filters.Add(picker.Type, picker.SelectedItem);
  52. }
  53. }
  54. }
  55. return filters;
  56. }
  57. private void LoadFilters()
  58. {
  59. LoadTags();
  60. LoadAreas();
  61. PopulateLists();
  62. }
  63. private void LoadTags()
  64. {
  65. CoreTable table = new Client<JobDocumentSetTag>().Query();
  66. foreach (CoreRow row in table.Rows)
  67. {
  68. var tag = row.ToObject<JobDocumentSetTag>();
  69. if (tag.Job.ID == Guid.Empty || tag.Job.ID == JobID)
  70. CategoriseTag(tag);
  71. }
  72. }
  73. private void LoadAreas()
  74. {
  75. CoreTable table = new Client<JobITP>().Query(
  76. new Filter<JobITP>(x => x.Job.ID).IsEqualTo(JobID),
  77. new Columns<JobITP>(x => x.Description));
  78. foreach (CoreRow row in table.Rows)
  79. Areas.Add(row.Get<JobITP, string>(x => x.Description));
  80. }
  81. private void PopulateLists()
  82. {
  83. disciplinePicker.AddItems(Disciplines, CheckNotBlank(JobDocFilterType.Discipline));
  84. typePicker.AddItems(Types, CheckNotBlank(JobDocFilterType.Type));
  85. categoryPicker.AddItems(Categories, CheckNotBlank(JobDocFilterType.Category));
  86. areaPicker.AddItems(Areas, CheckNotBlank(JobDocFilterType.Area));
  87. }
  88. private string CheckNotBlank(JobDocFilterType type)
  89. {
  90. return Filters.ContainsKey(type) ? Filters[type] : "";
  91. }
  92. private void CategoriseTag(JobDocumentSetTag tag)
  93. {
  94. if (tag.Type == JobDocumentSetTagType.Discipline)
  95. Disciplines.Add(tag.Description);
  96. else if (tag.Type == JobDocumentSetTagType.Type)
  97. Types.Add(tag.Description);
  98. else if (tag.Type == JobDocumentSetTagType.Category)
  99. Categories.Add(tag.Description);
  100. }
  101. }
  102. }