using InABox.Clients; using InABox.Configuration; using InABox.Mobile; using System; using System.Collections.Generic; using System.IO; using Xamarin.Essentials; using Xamarin.Forms; using Xamarin.Forms.Xaml; using XF.Material.Forms.UI.Dialogs; using Comal.Classes; using Email = Xamarin.Essentials.Email; using InABox.Core; using System.Linq; using System.Threading; using System.Threading.Tasks; using InABox.Rpc; namespace PRS.Mobile { [XamlCompilation(XamlCompilationOptions.Compile)] public partial class SettingsPage { private int count; private DatabaseSettings _settings; public SettingsPage() { _settings = new LocalConfiguration().Load(); InitializeComponent(); ProgressVisible = true; } private void ExitBtn_Clicked(object sender, EventArgs e) { Navigation.PopAsync(); } private async void Save_OnClicked(object sender, EventArgs args) { var urls = stringList.SaveItems(); var user = userIDEnt.Text?.Trim() ?? ""; var pass = passwordEnt.Text?.Trim() ?? ""; if (!urls.SequenceEqual(_settings.URLs) || (!String.Equals(user, _settings.UserID) || !String.Equals(pass, _settings.Password))) { _settings.URLs = urls; _settings.UserID = user; _settings.Password = pass; new LocalConfiguration().Save(_settings); DoClearCaches(); } ClientFactory.InvalidateUser(); if (App.Current.Properties.ContainsKey("SessionID")) App.Current.Properties.Remove("SessionID"); App.Data.Reset(); TransportStatus connection = TransportStatus.None; using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Connecting")) { await Task.Run(() => { return connection = App.ConnectTransport(_settings.URLs); }); } if (connection != TransportStatus.OK) { await DisplayAlert("Connection Error", $"Unable to establish a connection!\n\nERR: {connection}", "OK"); return; } //Navigation.PopToRootAsync(true); Navigation.PushAsync(new PinLoginPage()); } #region Populate Screen private void Populate() { if (_settings.URLs?.Any() ?? false) stringList.LoadList(_settings.URLs); else stringList.LoadList(new String[] { }); userIDEnt.Text = _settings.UserID; passwordEnt.Text = _settings.Password; deviceIDEnt.Text = MobileUtils.GetDeviceID(); Task.Run(() => { var text = InABox.Mobile.MobileLogging.ReadLog().Split('\n'); Device.BeginInvokeOnMainThread(() => { _log.ItemsSource = text; ProgressVisible = false; }); }); } protected override async void OnAppearing() { base.OnAppearing(); Populate(); } #endregion private async void ResetButton_OnClicked(object sender, MobileButtonClickEventArgs args) { var confirm = await MaterialDialog.Instance.ConfirmAsync("Are your sure you wish to reset your settings?", "Confirm Reset", "OK", "Cancel"); if (confirm == true) { _settings.URLs = new String[] { "demo.prsdigital.com.au:8033", "demo2.prsdigital.com.au:8033", }; _settings.UserID = "GUEST"; _settings.Password = "guest"; new LocalConfiguration().Save(_settings); DoClearCaches(); Device.BeginInvokeOnMainThread(Populate); } } private async void EmailLogs_OnClicked(object sender, MobileButtonClickEventArgs args) { try { var message = new EmailMessage { Subject = $"Mobile Error Logs from {App.Data.Me?.Name ?? "Unknown Employee"} ({Device.RuntimePlatform } - v{MobileUtils.AppVersion.InstalledVersionNumber})", Body = String.Join('\n',_log.ItemsSource as String[]), To = new List { "support@prsdigital.com.au" } }; await Email.ComposeAsync(message); } catch (Exception e) { DisplayAlert("Error", "Unable to Send Email!", "OK"); } } private async void ClearCaches_OnClicked(object sender, MobileButtonClickEventArgs args) { var confirm = await MaterialDialog.Instance.ConfirmAsync("Are your sure you wish to clear your caches?", "Clear Caches", "OK", "Cancel"); if (confirm == true) DoClearCaches(); } private void DoClearCaches() { ProgressVisible = true; var path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData); var files = Directory.GetFiles(path); Task.Run(() => { foreach (var file in files) File.Delete(file); }).Wait(); ProgressVisible = false; } } }