EmployeeSelectionList.xaml.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Xamarin.Forms;
  7. using Xamarin.Forms.Xaml;
  8. namespace comal.timesheets
  9. {
  10. public delegate void OnEmployeesSaved(List<EmployeeShell> employees);
  11. public delegate void OnEmployeeSaved(EmployeeShell employee);
  12. [XamlCompilation(XamlCompilationOptions.Compile)]
  13. public partial class EmployeeSelectionList : ContentPage
  14. {
  15. bool multiSelect = false;
  16. List<EmployeeShell> employees = new List<EmployeeShell>();
  17. List<EmployeeShell> selectedList = new List<EmployeeShell>();
  18. string currentFilter = "";
  19. public event OnEmployeesSaved OnEmployeesSaved;
  20. public event OnEmployeeSaved OnEmployeeSaved;
  21. public EmployeeSelectionList(bool multiselect = false)
  22. {
  23. InitializeComponent();
  24. NavigationPage.SetHasBackButton(this, false);
  25. multiSelect = multiselect;
  26. List<string> options = new List<string>();
  27. options.Add("All");
  28. employees.Clear();
  29. foreach (var emp in GlobalVariables.EmployeeShells)
  30. {
  31. employees.Add(emp);
  32. if (!options.Contains(emp.TeamName))
  33. options.Add(emp.TeamName);
  34. }
  35. filterOptionsControl.Options = options;
  36. filterOptionsControl.CreateRadioButtonsAndSetDefault(options[0]);
  37. currentFilter = options[0];
  38. filterOptionsControl.OnFilterOptionChanged += FilterOptionsControl_OnFilterOptionChanged;
  39. employeeListview.ItemsSource = employees;
  40. }
  41. private void FilterOptionsControl_OnFilterOptionChanged(string filterOption)
  42. {
  43. if (filterOption == currentFilter)
  44. return;
  45. currentFilter = filterOption;
  46. if (filterOption == "All")
  47. {
  48. employeeListview.ItemsSource = null;
  49. employeeListview.ItemsSource = employees;
  50. }
  51. else
  52. {
  53. employeeListview.ItemsSource = null;
  54. employeeListview.ItemsSource = employees.Where(x => x.TeamName == filterOption);
  55. }
  56. }
  57. private void Exit(object sender, EventArgs e)
  58. {
  59. Navigation.PopAsync();
  60. }
  61. private void Save_Clicked(object sender, EventArgs e)
  62. {
  63. OnEmployeesSaved?.Invoke(selectedList);
  64. Navigation.PopAsync();
  65. }
  66. private void SearchEdt_Changed(object sender, EventArgs e)
  67. {
  68. RunSearch();
  69. }
  70. private void RunSearch()
  71. {
  72. if (currentFilter != "All")
  73. {
  74. var list = employees.Where(x => x.TeamName.Equals(currentFilter));
  75. List<EmployeeShell> searchList = new List<EmployeeShell>();
  76. foreach (var item in list)
  77. {
  78. searchList.Add(item);
  79. }
  80. if (string.IsNullOrWhiteSpace(searchEnt.Text))
  81. {
  82. employeeListview.ItemsSource = null;
  83. employeeListview.ItemsSource = searchList;
  84. }
  85. else
  86. {
  87. employeeListview.ItemsSource = null;
  88. employeeListview.ItemsSource = searchList.Where(x => x.Name.Contains(searchEnt.Text.ToUpper()) || x.Name.Contains(searchEnt.Text.ToLower())
  89. || x.Name.Contains(UpperCaseFirst(searchEnt.Text)));
  90. }
  91. }
  92. else
  93. {
  94. if (string.IsNullOrWhiteSpace(searchEnt.Text))
  95. {
  96. employeeListview.ItemsSource = null;
  97. employeeListview.ItemsSource = employees;
  98. }
  99. else
  100. {
  101. employeeListview.ItemsSource = null;
  102. employeeListview.ItemsSource = employees.Where(x => x.Name.Contains(searchEnt.Text.ToUpper()) || x.Name.Contains(searchEnt.Text.ToLower())
  103. || x.Name.Contains(UpperCaseFirst(searchEnt.Text)));
  104. }
  105. }
  106. }
  107. private void ViewCell_Tapped(object sender, EventArgs e)
  108. {
  109. EmployeeShell shell = employeeListview.SelectedItem as EmployeeShell;
  110. if (multiSelect)
  111. {
  112. MultiSelectEmployee(shell);
  113. }
  114. else
  115. {
  116. OnEmployeeSaved?.Invoke(shell);
  117. Navigation.PopAsync();
  118. }
  119. }
  120. public void MultiSelectEmployee(EmployeeShell shell)
  121. {
  122. if (shell.Color == Color.Default)
  123. {
  124. saveBtn.IsVisible = true;
  125. selectedList.Add(shell);
  126. int index = employees.FindIndex(x => x.ID == shell.ID);
  127. employees.RemoveAt(index);
  128. shell.Color = Color.FromHex("#a2006d");
  129. employees.Insert(index, shell);
  130. }
  131. else if (shell.Color == Color.FromHex("#a2006d"))
  132. {
  133. int index = selectedList.FindIndex(x => x.ID == shell.ID);
  134. selectedList.RemoveAt(index);
  135. if (selectedList.Count == 0)
  136. saveBtn.IsVisible = false;
  137. int index2 = employees.FindIndex(x => x.ID == shell.ID);
  138. employees.RemoveAt(index2);
  139. shell.Color = Color.Default;
  140. employees.Insert(index2, shell);
  141. }
  142. RunSearch();
  143. }
  144. static String UpperCaseFirst(string s)
  145. {
  146. char[] a = s.ToCharArray();
  147. a[0] = char.ToUpper(a[0]);
  148. return new string(a);
  149. }
  150. }
  151. }