| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Xamarin.Forms;
- using Xamarin.Forms.Xaml;
- namespace comal.timesheets
- {
- public delegate void OnEmployeesSaved(List<EmployeeShell> employees);
- public delegate void OnEmployeeSaved(EmployeeShell employee);
- [XamlCompilation(XamlCompilationOptions.Compile)]
- public partial class EmployeeSelectionList : ContentPage
- {
- bool multiSelect = false;
- List<EmployeeShell> employees = new List<EmployeeShell>();
- List<EmployeeShell> selectedList = new List<EmployeeShell>();
- string currentFilter = "";
- public event OnEmployeesSaved OnEmployeesSaved;
- public event OnEmployeeSaved OnEmployeeSaved;
- public EmployeeSelectionList(bool multiselect = false)
- {
- InitializeComponent();
- NavigationPage.SetHasBackButton(this, false);
- multiSelect = multiselect;
- List<string> options = new List<string>();
- options.Add("All");
- employees.Clear();
- foreach (var emp in GlobalVariables.EmployeeShells)
- {
- employees.Add(emp);
- if (!options.Contains(emp.TeamName))
- options.Add(emp.TeamName);
- }
- filterOptionsControl.Options = options;
- filterOptionsControl.CreateRadioButtonsAndSetDefault(options[0]);
- currentFilter = options[0];
- filterOptionsControl.OnFilterOptionChanged += FilterOptionsControl_OnFilterOptionChanged;
- employeeListview.ItemsSource = employees;
- }
- private void FilterOptionsControl_OnFilterOptionChanged(string filterOption)
- {
- if (filterOption == currentFilter)
- return;
- currentFilter = filterOption;
- if (filterOption == "All")
- {
- employeeListview.ItemsSource = null;
- employeeListview.ItemsSource = employees;
- }
- else
- {
- employeeListview.ItemsSource = null;
- employeeListview.ItemsSource = employees.Where(x => x.TeamName == filterOption);
- }
- }
- private void Exit(object sender, EventArgs e)
- {
- Navigation.PopAsync();
- }
- private void Save_Clicked(object sender, EventArgs e)
- {
- OnEmployeesSaved?.Invoke(selectedList);
- Navigation.PopAsync();
- }
- private void SearchEdt_Changed(object sender, EventArgs e)
- {
- RunSearch();
- }
- private void RunSearch()
- {
- if (currentFilter != "All")
- {
- var list = employees.Where(x => x.TeamName.Equals(currentFilter));
- List<EmployeeShell> searchList = new List<EmployeeShell>();
- foreach (var item in list)
- {
- searchList.Add(item);
- }
- if (string.IsNullOrWhiteSpace(searchEnt.Text))
- {
- employeeListview.ItemsSource = null;
- employeeListview.ItemsSource = searchList;
- }
- else
- {
- employeeListview.ItemsSource = null;
- employeeListview.ItemsSource = searchList.Where(x => x.Name.Contains(searchEnt.Text.ToUpper()) || x.Name.Contains(searchEnt.Text.ToLower())
- || x.Name.Contains(UpperCaseFirst(searchEnt.Text)));
- }
- }
- else
- {
- if (string.IsNullOrWhiteSpace(searchEnt.Text))
- {
- employeeListview.ItemsSource = null;
- employeeListview.ItemsSource = employees;
- }
- else
- {
- employeeListview.ItemsSource = null;
- employeeListview.ItemsSource = employees.Where(x => x.Name.Contains(searchEnt.Text.ToUpper()) || x.Name.Contains(searchEnt.Text.ToLower())
- || x.Name.Contains(UpperCaseFirst(searchEnt.Text)));
- }
- }
- }
- private void ViewCell_Tapped(object sender, EventArgs e)
- {
- EmployeeShell shell = employeeListview.SelectedItem as EmployeeShell;
- if (multiSelect)
- {
- MultiSelectEmployee(shell);
- }
- else
- {
- OnEmployeeSaved?.Invoke(shell);
- Navigation.PopAsync();
- }
- }
- public void MultiSelectEmployee(EmployeeShell shell)
- {
- if (shell.Color == Color.Default)
- {
- saveBtn.IsVisible = true;
- selectedList.Add(shell);
- int index = employees.FindIndex(x => x.ID == shell.ID);
- employees.RemoveAt(index);
- shell.Color = Color.FromHex("#a2006d");
- employees.Insert(index, shell);
- }
- else if (shell.Color == Color.FromHex("#a2006d"))
- {
- int index = selectedList.FindIndex(x => x.ID == shell.ID);
- selectedList.RemoveAt(index);
- if (selectedList.Count == 0)
- saveBtn.IsVisible = false;
- int index2 = employees.FindIndex(x => x.ID == shell.ID);
- employees.RemoveAt(index2);
- shell.Color = Color.Default;
- employees.Insert(index2, shell);
- }
- RunSearch();
- }
- static String UpperCaseFirst(string s)
- {
- char[] a = s.ToCharArray();
- a[0] = char.ToUpper(a[0]);
- return new string(a);
- }
- }
- }
|