JobDocsFilterPage.xaml.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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(string discipline, string type, string category, string area);
  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(disciplinePicker.SelectedItem, typePicker.SelectedItem, categoryPicker.SelectedItem, areaPicker.SelectedItem);
  40. }
  41. private void LoadFilters()
  42. {
  43. LoadTags();
  44. LoadAreas();
  45. PopulateLists();
  46. }
  47. private void LoadTags()
  48. {
  49. CoreTable table = new Client<JobDocumentSetTag>().Query();
  50. foreach (CoreRow row in table.Rows)
  51. {
  52. var tag = row.ToObject<JobDocumentSetTag>();
  53. if (tag.Job.ID == Guid.Empty || tag.Job.ID == JobID)
  54. CategoriseTag(tag);
  55. }
  56. }
  57. private void LoadAreas()
  58. {
  59. CoreTable table = new Client<JobITP>().Query(
  60. new Filter<JobITP>(x => x.Job.ID).IsEqualTo(JobID),
  61. new Columns<JobITP>(x => x.Code));
  62. foreach (CoreRow row in table.Rows)
  63. Areas.Add(row.Get<JobITP, string>(x => x.Code));
  64. }
  65. private void PopulateLists()
  66. {
  67. disciplinePicker.AddItems(Disciplines, CheckNotBlank(JobDocFilterType.Discipline));
  68. typePicker.AddItems(Types, CheckNotBlank(JobDocFilterType.Type));
  69. categoryPicker.AddItems(Categories, CheckNotBlank(JobDocFilterType.Category));
  70. areaPicker.AddItems(Areas, CheckNotBlank(JobDocFilterType.Area));
  71. }
  72. private string CheckNotBlank(JobDocFilterType type)
  73. {
  74. return Filters.ContainsKey(type) ? Filters[type] : "";
  75. }
  76. private void CategoriseTag(JobDocumentSetTag tag)
  77. {
  78. if (tag.Type == JobDocumentSetTagType.Discipline)
  79. Disciplines.Add(tag.Description);
  80. else if (tag.Type == JobDocumentSetTagType.Type)
  81. Types.Add(tag.Description);
  82. else if (tag.Type == JobDocumentSetTagType.Category)
  83. Categories.Add(tag.Description);
  84. }
  85. }
  86. }