public MainViewModel() { EmployeeHandler = new EmployeeHandler(this); if (OpenDbConnection) { PersistencyService.OpenApiConnection(); } EmployeesInDb = new ObservableCollection <Employee>(); LoggedInEmployees = new List <Employee>(); DatabaseTable = new ObservableCollection <Employee>(); if (OpenDbConnection) { PersistencyService.GetData(EmployeesInDb); PersistencyService.GetDataLoggedIn(LoggedInEmployees); } LoginOrLogoutCommand = new RelayCommand(EmployeeHandler.LoginOrLogout); CompleteEmployeeListCommand = new RelayCommand(EmployeeHandler.CompleteEmployeeList); PersonalEntryListCommand = new RelayCommand(EmployeeHandler.PersonalEntryList); OwnDepartmentListCommand = new RelayCommand(EmployeeHandler.OwnDepartmentList); AddSickOrVacationdaysCommand = new RelayCommand(EmployeeHandler.ChangeVacationOrSickdays); AdminChangePersonalInfoCommand = new RelayCommand(EmployeeHandler.AdminChangePersonalInfo); AdminChangeSalaryInfoCommand = new RelayCommand(EmployeeHandler.AdminChangeSalaryInfo); SortByNameCommand = new RelayCommand(EmployeeHandler.SortByName); SortByEmployeeIdCommand = new RelayCommand(EmployeeHandler.SortByEmployeeId); ExportAsCsvCommand = new RelayCommand(() => ExportEmployeesToCsvFile()); }
/// <summary> /// Will log the matching employee in if he isn't already, and start the time management. /// If the matching employee is logged in, he will be logged out, and the total hours will be updated thereafter. /// </summary> public async void LoginOrLogout() { if (MainViewModel.OpenDbConnection) { PersistencyService.GetDataLoggedIn(_viewModel.LoggedInEmployees); } var employees = _viewModel.EmployeesInDb.ToList(); var matcingEmployee = employees.Find(e => e.EmployeeId.ToString() == _viewModel.LoginOrLogoutBox); var matchingLoggedInEmployee = _viewModel.LoggedInEmployees.Find(e => e.EmployeeId.ToString() == _viewModel.LoginOrLogoutBox); //If user IS NOT logged in, he will be logged in: if (matcingEmployee != null && matchingLoggedInEmployee == null) { matcingEmployee.LastLogin = DateTime.Now; LastLoggedIn = matcingEmployee; if (LastLoggedIn.TotalHours > new TimeSpan(24, 59, 59)) { LastLoggedIn.TotalHours = new TimeSpan(10, 35, 28); //Due to unknown unresponsiveness if the employees TotalHours-property is above 24 hours. } _viewModel.DatabaseTable.Clear(); _viewModel.DatabaseTable.Add(LastLoggedIn); if (!MainViewModel.OpenDbConnection) { return; } PersistencyService.PostDataLoggedIn(matcingEmployee); //Posted to logged in employees database. PersistencyService.PutData(matcingEmployee); //Updates logintime for the employee on the shown employee list. DanxMainPage.CloseCanvases(); DanxMainPage.MainScreenLoginCanvas.Visibility = Visibility.Visible; if (matcingEmployee.GetType() == typeof(AdminEmp)) { DanxMainPage.AdminToolsCanvas.Visibility = Visibility.Visible; } else { DanxMainPage.AdminToolsCanvas.Visibility = Visibility.Collapsed; } } //If user IS logged in, he will be logged out: else if (matchingLoggedInEmployee != null) { _employeeToLogout = matchingLoggedInEmployee; UpdateLogoutTime(); UpdateTotalHours(); _employeeToLogout = null; if (!MainViewModel.OpenDbConnection) { return; } PersistencyService.DeleteDataLoggedIn(matchingLoggedInEmployee); //Removing logged out employee from logged in table. DanxMainPage.CloseCanvases(); DanxMainPage.MainScreenCanvas.Visibility = Visibility.Visible; var hoursWorkedFormat = String.Format(_hoursWorked.Hours + ":" + _hoursWorked.Minutes + ":" + _hoursWorked.Seconds); //Goodbye message DanxMainPage.UiWelcomeMessage.Text = " Goodbye!\n You have worked for " + hoursWorkedFormat + " today."; await Task.Delay(8000); DanxMainPage.UiWelcomeMessage.Text = " Welcome!\nInsert worker-id in one of the boxes above."; } //If a wrong user id is entered: else { try { throw new ArgumentException("Please enter a valid employee id."); //Unittest purposes } catch (ArgumentException) { var errorMsg = new MessageDialog("Please enter a valid employee id.", "Error"); if (MainViewModel.OpenDbConnection) { errorMsg.ShowAsync(); //Unittest purposes } } } }