using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Threading; using System.Threading.Tasks; using System.Timers; using InABox.Clients; using InABox.Configuration; using InABox.Core; using InABox.Mobile; using Syncfusion.SfPdfViewer.XForms; using Xamarin.Essentials; using Xamarin.Forms; using XF.Material.Forms.UI.Dialogs; using Timer = System.Threading.Timer; namespace comal.timesheets { class SettingsBinding : ConnectionSettings { public String DeviceID { get; set; } public String AppVersion { get; set; } public void Load(ConnectionSettings settings) { URL = settings.URL; Port = settings.Port; UserID = settings.UserID; Password = settings.Password; } public void Unload(ConnectionSettings settings) { settings.URL = URL; settings.Port = Port; settings.UserID = UserID; settings.Password = Password; } } public partial class Settings : ContentPage { SettingsBinding binding = new SettingsBinding(); int count = 0; string s = ""; public bool SettingsChanged { get; private set; } public Settings() { InitializeComponent(); binding.Load(App.Settings); binding.DeviceID = MobileUtils.GetDeviceID(); binding.AppVersion = MobileUtils.AppVersion.InstalledVersionNumber; this.BindingContext = binding; NavigationPage.SetHasBackButton(this, false); } private async void ExitBtn_Clicked(object sender, EventArgs e) { using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Working")) { //Syncfusion Masked editor still needs access before popping the page Thread.Sleep(1000); Navigation.PopAsync(); } } private void SaveBtn_Clicked(object sender, EventArgs e) { ValidateAppData(); } private async void ValidateAppData() { try { using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Validating")) { ClientFactory.InvalidateUser(); binding.Unload(App.Settings); new LocalConfiguration().Delete(); new LocalConfiguration().Save(App.Settings); App.Settings = new LocalConfiguration().Load(); ClientFactory.SetClientType(typeof(JsonClient<>), "TimeBench", MobileUtils.AppVersion.InstalledVersionNumber + GlobalVariables.DeviceString, new object[] { App.Settings.URL, App.Settings.Port, true }); GlobalVariables.ChangeUser = true; GlobalVariables.InternalOnAppearing = true; if (App.Current.Properties.ContainsKey("SessionID")) App.Current.Properties.Remove("SessionID"); //Syncfusion Masked editor still needs access before popping the page Thread.Sleep(1000); Navigation.PopToRootAsync(true); PINLoginPage pin = new PINLoginPage(); Navigation.PushAsync(pin, true); } return; } catch { } } protected override async void OnAppearing() { bool isLatest = true; try { isLatest = await MobileUtils.AppVersion.IsUsingLatestVersion(); } catch (Exception eLatest) { } if (isLatest) { updateVersionBtn.IsEnabled = false; updateVersionBtn.Text = "App is up to date"; } else if (!isLatest) { string latestVersionNumber = await MobileUtils.AppVersion.GetLatestVersionNumber(); updateVersionBtn.IsEnabled = true; updateVersionBtn.Text = "Update App Version (" + latestVersionNumber + ")"; } const string errorFilename = "Fatal.log"; var libraryPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal); var errorFilePath = Path.Combine(libraryPath, errorFilename); if (File.Exists(errorFilePath)) { sendErrorsBtn.IsEnabled = true; } //if (Application.Current.Properties.ContainsKey("IsSharedDevice")) //{ // if (Application.Current.Properties["IsSharedDevice"].Equals("True")) // { // sharedDeviceRb.IsChecked = true; // } //} base.OnAppearing(); } private async void ChangePasswordBtn_Clicked(object sender, EventArgs e) { if (count == 3) { DisplayAlert("Alert", "You have attempted incorrectly " + count + " times and cannot try again. Please contact your system administrator.", "OK"); return; } CoreTable table = new Client().Query(new Filter(x => x.UserID).IsEqualTo(ClientFactory.UserID), new Columns(x => x.Password)); if (table.Rows.Any()) { string p = table.Rows.FirstOrDefault().Values[0].ToString(); string userInput = await DisplayPromptAsync("Alert", "Enter Current Password", "OK", "Cancel"); if (!string.IsNullOrWhiteSpace(userInput) && userInput != "Cancel") { if (p != userInput) { count++; DisplayAlert("Alert", "Password is incorrect. You have attempted " + count + " times out of 3.", "OK"); return; } else if (p == userInput) { PasswordResetPage page = new PasswordResetPage(ClientFactory.UserID); page.OnPasswordReset += (() => { Navigation.PopToRootAsync(); PINLoginPage pinpage = new PINLoginPage(); Navigation.PushAsync(pinpage); }); Navigation.PushAsync(page); } } } } private async void UpdateVersionBtn_Clicked(object sender, EventArgs e) { Dispatcher.BeginInvokeOnMainThread(() => { MobileUtils.AppVersion.OpenAppInStore(); }); } private void UserID_Changed(object sender, EventArgs e) { if (!string.IsNullOrWhiteSpace(UserID.Text)) { UserID.Text = UserID.Text.Trim(); } } private void UserPassword_Changed(object sender, EventArgs e) { if (!string.IsNullOrWhiteSpace(UserID.Text)) { Password.Text = Password.Text.Trim(); } //if (!string.IsNullOrWhiteSpace(Password.Value.ToString())) //{ // Password.Value = Password.Value.ToString().Trim(); // s = Password.Value.ToString(); // showPassword.Text = s; //} //else // s = ""; } async void BackgroundLoadProducts() { await Task.Run(() => { if (!GlobalVariables.ProductsLoaded/* && ClientFactory.IsAllowed()*/) { GlobalVariables.ProductsLoaded = false; ProductsLoader productsLoader = new ProductsLoader(); GlobalVariables.ProductShells = productsLoader.ProductShells; GlobalVariables.ProductsLoaded = true; } }); } private void sharedDeviceRb_Changed(object sender, EventArgs e) { //string isSharedDevice = "IsSharedDevice"; //if ((sender as CheckBox).IsChecked) //{ // if (!Application.Current.Properties.ContainsKey(isSharedDevice)) // { // Application.Current.Properties.Add(isSharedDevice, "True"); // URL.Text = "http://mobile.com-al.com.au"; // UserID.Text = ""; // Password.Value = ""; // } // else // { // Application.Current.Properties[isSharedDevice] = "True"; // URL.Text = "http://mobile.com-al.com.au"; // UserID.Text = ""; // Password.Value = ""; // } //} //else //{ // if (Application.Current.Properties.ContainsKey(isSharedDevice)) // { // Application.Current.Properties[isSharedDevice] = "False"; // } //} } private void ShowPW_Tapped(object sender, EventArgs e) { //passwordOne.Width = new GridLength(0, GridUnitType.Absolute); //passwordTwo.Width = new GridLength(1, GridUnitType.Star); //Task.Run(() => //{ // Thread.Sleep(2500); // ShowMaskedText(); //}); } private void ShowMaskedText() { //Device.BeginInvokeOnMainThread(() => //{ // passwordTwo.Width = new GridLength(0, GridUnitType.Absolute); // passwordOne.Width = new GridLength(1, GridUnitType.Star); //}); } private async void ClearStorageBtn_Clicked(object sender, EventArgs e) { string chosenOption = await DisplayActionSheet("Confirm Clear? This will also log you out", "Cancel", null, "Yes", "No"); switch (chosenOption) { case "Cancel": return; default: return; case "No": return; case "Yes": RemoveDataFromSecureStorage(); ClearConnectionSettings(); ValidateAppData(); break; } } private void RemoveDataFromSecureStorage() { try { SecureStorage.Remove(GlobalVariables.CacheURLString); SecureStorage.Remove(GlobalVariables.CacheURLString); SecureStorage.Remove(GlobalVariables.CacheURLString); SecureStorage.Remove(GlobalVariables.CacheURLString); } catch { } } private void ClearConnectionSettings() { try { new LocalConfiguration().Delete(); MobileUtils.LoadDemoSettings(App.Settings); binding.Load(App.Settings); } catch { } } //private void DocsURL_Changed(object sender, EventArgs e) //{ // if (!string.IsNullOrWhiteSpace(docsURL.Text)) // { // if (!App.Current.Properties.ContainsKey("DocsURL")) // { // App.Current.Properties.Add("DocsURL", docsURL.Text); // } // else if (App.Current.Properties.ContainsKey("DocsURL")) // { // App.Current.Properties["DocsURL"] = docsURL.Text; // } // } //} //private void ClearDocsBtn_Clicked(object sender, EventArgs e) //{ // if (App.Current.Properties.ContainsKey("DocsURL")) // { // App.Current.Properties.Remove("DocsURL"); // DisplayAlert("Success", "", "OK"); // } //} private async void SendErrorsBtn_Clicked(object sender, EventArgs e) { try { const string errorFilename = "Fatal.log"; string libraryPath = ""; if (Device.RuntimePlatform.Equals(Device.Android)) libraryPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal); else if (Device.RuntimePlatform.Equals(Device.iOS)) libraryPath = Environment.GetFolderPath(Environment.SpecialFolder.Resources); var errorFilePath = Path.Combine(libraryPath, errorFilename); if (!File.Exists(errorFilePath)) { return; } var errorText = File.ReadAllText(errorFilePath); var message = new EmailMessage { Subject = "Error Logs from " + GlobalVariables.EmpName, Body = errorText, To = new List { "nick@prsdigital.com.au" } }; await Email.ComposeAsync(message); File.Delete(errorFilePath); } catch { } } } }