using InABox.Clients; using InABox.Configuration; using InABox.Core; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Xamarin.Forms; using Xamarin.Forms.Xaml; using XF.Material.Forms.UI.Dialogs; namespace comal.timesheets { public delegate void PasswordResetEvent(); [XamlCompilation(XamlCompilationOptions.Compile)] public partial class PasswordResetPage : ContentPage { public event PasswordResetEvent OnPasswordReset; private User user; public PasswordResetPage(string userid) { InitializeComponent(); Task.Run(() => { ClientFactory.SetBypass(); CoreTable table = new Client().Query(new Filter(x => x.UserID).IsEqualTo(userid)); if (table.Rows.Any()) { user = table.Rows.FirstOrDefault().ToObject(); } else { Device.BeginInvokeOnMainThread(() => { DisplayAlert("Error", "User not found", "OK"); }); } }); } private async void SaveNewPassword_Clicked(object sender, EventArgs e) { bool bOK = CheckPasswordValidity(); if (user.ID != Guid.Empty && bOK) { using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Saving")) { user.Password = newPasswordEnt1.Text; new Client().Save(user, "Password updated by User from Timebench"); ClientFactory.UnsetBypass(); App.Settings.Password = user.Password; new LocalConfiguration().Save(App.Settings); } DisplayAlert("Success", "Password changed", "OK"); Navigation.PopAsync(); OnPasswordReset?.Invoke(); } } private bool CheckPasswordValidity() { if (string.IsNullOrWhiteSpace(newPasswordEnt1.Text) || string.IsNullOrWhiteSpace(newPasswordEnt2.Text)) { DisplayAlert("Error", "Password entries may not be blank", "OK"); return false; } if (newPasswordEnt1.Text.Length < 5 || newPasswordEnt2.Text.Length < 5) { DisplayAlert("Error", "Passwords must be at least 5 characters", "OK"); return false; } if (newPasswordEnt1.Text != newPasswordEnt2.Text) { DisplayAlert("Error", "Passwords do not match", "OK"); return false; } return true; } } }