public LoginWindow(MainWindow mainWindow) { InitializeComponent(); _mainWindow = mainWindow; VoteButton.Clicked += VoteButton_Clicked; Loaded += (s, ea) => { //Focus on first name field in the login form FirstNameTextBox.FocusOnMe(); }; }
private void VoteButton_Clicked(object sender, EventArgs e) { try { //Check if all fields valid, then check if pesel is not blocked, then check if pesel didn't already vote if (ValidateFields()) { //Disable login form SetLoginFormEnable(false); //Download blocked pesels from server and check if it contains provided pesel _peselWorker = new BackgroundWorker(); _peselWorker.DoWork += (s, ea) => { try { List <string> errorList = new List <string>(); string providedPesel = ""; //Show loading overlay this.Dispatcher.Invoke(() => { _mainWindow.LoadingLabel.Foreground = ColorHelper.HexToColorBrush("#1c9ad2"); _mainWindow.LoadingLabel.Content = "Checking your data..."; _mainWindow.LoadingContainer.Visibility = Visibility.Visible; providedPesel = PeselTextBox.Text; }); using (System.Net.WebClient client = new System.Net.WebClient()) { string jsonData = client.DownloadString(new Uri("http://webtask.future-processing.com:8069/blocked")); BlockedRoot root = JsonConvert.DeserializeObject <BlockedRoot>(jsonData); List <string> blockedPesels = root.Disallowed.Person.Select(obj => obj.Pesel).ToList(); if (blockedPesels.Contains(providedPesel)) { //Blocked pesels list contains provided pesel, invoke event to report disallowed try to database errorList.Add("You have lost your voting privileges."); Dispatcher.Invoke(() => { LoginDisallowed?.Invoke(this, new LoginEventArgs() { FirstName = FirstNameTextBox.Text, LastName = LastNameTextBox.Text, Pesel = PeselTextBox.Text }); }); } else { //Check if pesel already voted if (DBHelper.CheckPeselAlreadyVoted(providedPesel)) { errorList.Add("You have already voted."); } } ea.Result = errorList; } } catch (Exception) { MessageBox.Show( "An error occurred while checking your data, please contact person responsible for managing the system", "Error occured", MessageBoxButton.OK); _mainWindow.Logout(); } }; //If pesel is blocked or already voted, lock program for 10 seconds _peselWorker.RunWorkerCompleted += (s, ea) => { List <string> errors = (List <string>)ea.Result; if (errors.Count > 0) { //Show warning overlay _mainWindow.LoadingLabel.Foreground = ColorHelper.HexToColorBrush("#d11b1b"); _mainWindow.LoadingLabel.Content = errors.First(); _mainWindow.LoadingAdditionalLabel.Content = "Returning to login page in 10 seconds"; _mainWindow.LoadingAdditionalLabel.Visibility = Visibility.Visible; //Count 10 seconds int counter = 9; Timer timer = new Timer(); timer.Interval = 1000; timer.Elapsed += (s2, ea2) => { Dispatcher.Invoke(() => { _mainWindow.LoadingAdditionalLabel.Content = $"Returning to login page in {counter} seconds"; if (counter == 0) { //10 seconds elapsed, stop timer, hide warning overlay, clear and unlock login form timer.Stop(); _mainWindow.LoadingContainer.Visibility = Visibility.Collapsed; _mainWindow.LoadingAdditionalLabel.Visibility = Visibility.Collapsed; ClearLoginForm(); SetLoginFormEnable(true); FirstNameTextBox.FocusOnMe(); } }); counter--; }; timer.Start(); } else { //Login successfull LoginSuccessful?.Invoke(this, new LoginEventArgs() { FirstName = FirstNameTextBox.Text, LastName = LastNameTextBox.Text, Pesel = PeselTextBox.Text }); _mainWindow.LoadingContainer.Visibility = Visibility.Collapsed; } }; _peselWorker.RunWorkerAsync(); } } catch (Exception) { MessageBox.Show( "An error occurred while checking your data, please contact person responsible for managing the system", "Error occured", MessageBoxButton.OK); _mainWindow.Logout(); } }