EmployeeSelectionPage.xaml.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using InABox.Core;
  7. using Comal.Classes;
  8. using InABox.Clients;
  9. using Xamarin.Forms;
  10. using Xamarin.Forms.Xaml;
  11. namespace comal.timesheets.CustomControls
  12. {
  13. [XamlCompilation(XamlCompilationOptions.Compile)]
  14. public partial class EmployeeSelectionPage : ContentPage
  15. {
  16. public event ItemSelectedEvent OnItemSelected;
  17. public EmployeeShell employee { get; set; }
  18. string currentButton = "";
  19. public List<EmployeeGroupDisplayList> employeeGroupDisplayLists { get; set; }
  20. bool Multiselect = false;
  21. List<EmployeeShell> selectedList = new List<EmployeeShell>();
  22. public EmployeeSelectionPage(bool multiselect = false)
  23. {
  24. InitializeComponent();
  25. NavigationPage.SetHasBackButton(this, false);
  26. employeeGroupDisplayLists = new List<EmployeeGroupDisplayList>();
  27. foreach (EmployeeShell employee in GlobalVariables.EmployeeShells)
  28. {
  29. char c = employee.Name.FirstOrDefault();
  30. var group = employeeGroupDisplayLists.Find(x => x.Heading.Equals(c.ToString()));
  31. if (group == null)
  32. {
  33. EmployeeGroupDisplayList newGroupList = new EmployeeGroupDisplayList() { Heading = c.ToString() };
  34. newGroupList.Add(employee);
  35. employeeGroupDisplayLists.Add(newGroupList);
  36. }
  37. else
  38. {
  39. employeeGroupDisplayLists.Remove(group);
  40. group.Add(employee);
  41. employeeGroupDisplayLists.Add(group);
  42. }
  43. }
  44. currentButton = "All Staff";
  45. employeeListView.ItemsSource = employeeGroupDisplayLists;
  46. searchEdt.IsEnabled = true;
  47. List<string> options = new List<string>();
  48. foreach (string s in GlobalVariables.TeamNames)
  49. {
  50. options.Add(s);
  51. }
  52. filterOptionsControl.Options = options;
  53. filterOptionsControl.CreateRadioButtonsAndSetDefault("All Staff");
  54. filterOptionsControl.OnFilterOptionChanged += FilterOptionsControl_OnFilterOptionChanged;
  55. }
  56. private void Exit(object sender, EventArgs e)
  57. {
  58. Navigation.PopAsync();
  59. }
  60. private void FilterOptionsControl_OnFilterOptionChanged(string filterOption)
  61. {
  62. if (filterOption == filterOptionsControl.CurrentOption)
  63. return;
  64. if (filterOption.Equals("All Staff"))
  65. {
  66. employeeListView.IsGroupingEnabled = true;
  67. employeeListView.ItemsSource = employeeGroupDisplayLists;
  68. }
  69. else
  70. {
  71. employeeListView.IsGroupingEnabled = false;
  72. employeeListView.ItemsSource = GlobalVariables.TeamEmployeeShells.Where(x => x.TeamName == filterOption);
  73. }
  74. currentButton = filterOption;
  75. filterOptionsControl.CurrentOption = filterOption;
  76. }
  77. private void SearchEdt_Changed(object sender, EventArgs e)
  78. {
  79. if (string.IsNullOrWhiteSpace(searchEdt.Text))
  80. {
  81. if (currentButton.Equals("All Staff"))
  82. {
  83. employeeListView.IsGroupingEnabled = true;
  84. employeeListView.ItemsSource = employeeGroupDisplayLists;
  85. }
  86. else
  87. employeeListView.ItemsSource = GlobalVariables.TeamEmployeeShells.Where(x => x.TeamName == currentButton);
  88. }
  89. else
  90. {
  91. employeeListView.IsGroupingEnabled = false;
  92. employeeListView.ItemsSource = GlobalVariables.EmployeeShells.Where
  93. (
  94. x => x.Name.Contains(searchEdt.Text) || x.Name.Contains(UpperCaseFirst(searchEdt.Text))
  95. || x.Name.Contains(searchEdt.Text.ToUpper()) || x.Name.Contains(searchEdt.Text.ToLower())
  96. );
  97. }
  98. }
  99. static String UpperCaseFirst(string s)
  100. {
  101. char[] a = s.ToCharArray();
  102. a[0] = char.ToUpper(a[0]);
  103. return new string(a);
  104. }
  105. private void Employee_Clicked(object sender, EventArgs e)
  106. {
  107. employee = employeeListView.SelectedItem as EmployeeShell;
  108. OnItemSelected?.Invoke();
  109. Navigation.PopAsync();
  110. }
  111. }
  112. public class EmployeeGroupDisplayList : List<EmployeeShell>
  113. {
  114. public string Heading { get; set; }
  115. public List<EmployeeShell> employeeShells;
  116. }
  117. public delegate void ItemSelectedEvent();
  118. }