SettingsPage.xaml.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. using InABox.Clients;
  2. using InABox.Configuration;
  3. using InABox.Mobile;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. using Xamarin.Essentials;
  8. using Xamarin.Forms;
  9. using Xamarin.Forms.Xaml;
  10. using XF.Material.Forms.UI.Dialogs;
  11. using Comal.Classes;
  12. using Email = Xamarin.Essentials.Email;
  13. using InABox.Core;
  14. using System.Linq;
  15. using System.Threading;
  16. using System.Threading.Tasks;
  17. using InABox.Rpc;
  18. namespace PRS.Mobile
  19. {
  20. [XamlCompilation(XamlCompilationOptions.Compile)]
  21. public partial class SettingsPage
  22. {
  23. private int count;
  24. private DatabaseSettings _settings;
  25. public SettingsPage()
  26. {
  27. _settings = new LocalConfiguration<DatabaseSettings>().Load();
  28. InitializeComponent();
  29. ProgressVisible = true;
  30. }
  31. private void ExitBtn_Clicked(object sender, EventArgs e)
  32. {
  33. Navigation.PopAsync();
  34. }
  35. private async void Save_OnClicked(object sender, EventArgs args)
  36. {
  37. var urls = stringList.SaveItems();
  38. var user = userIDEnt.Text?.Trim() ?? "";
  39. var pass = passwordEnt.Text?.Trim() ?? "";
  40. if (!urls.SequenceEqual(_settings.URLs) ||
  41. (!String.Equals(user, _settings.UserID) || !String.Equals(pass, _settings.Password)))
  42. {
  43. _settings.URLs = urls;
  44. _settings.UserID = user;
  45. _settings.Password = pass;
  46. new LocalConfiguration<DatabaseSettings>().Save(_settings);
  47. DoClearCaches();
  48. }
  49. ClientFactory.InvalidateUser();
  50. if (App.Current.Properties.ContainsKey("SessionID"))
  51. App.Current.Properties.Remove("SessionID");
  52. App.Data.Reset();
  53. TransportStatus connection = TransportStatus.None;
  54. using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Connecting"))
  55. {
  56. await Task.Run(() =>
  57. {
  58. return connection = App.ConnectTransport(_settings.URLs);
  59. });
  60. }
  61. if (connection != TransportStatus.OK)
  62. {
  63. await DisplayAlert("Connection Error", $"Unable to establish a connection!\n\nERR: {connection}", "OK");
  64. return;
  65. }
  66. //Navigation.PopToRootAsync(true);
  67. Navigation.PushAsync(new PinLoginPage());
  68. }
  69. #region Populate Screen
  70. private void Populate()
  71. {
  72. if (_settings.URLs?.Any() ?? false)
  73. stringList.LoadList(_settings.URLs);
  74. else
  75. stringList.LoadList(new String[] { });
  76. userIDEnt.Text = _settings.UserID;
  77. passwordEnt.Text = _settings.Password;
  78. deviceIDEnt.Text = MobileUtils.GetDeviceID();
  79. Task.Run(() =>
  80. {
  81. var text = InABox.Mobile.MobileLogging.ReadLog().Split('\n');
  82. Device.BeginInvokeOnMainThread(() =>
  83. {
  84. _log.ItemsSource = text;
  85. ProgressVisible = false;
  86. });
  87. });
  88. }
  89. protected override async void OnAppearing()
  90. {
  91. base.OnAppearing();
  92. Populate();
  93. }
  94. #endregion
  95. private async void ResetButton_OnClicked(object sender, MobileButtonClickEventArgs args)
  96. {
  97. var confirm = await MaterialDialog.Instance.ConfirmAsync("Are your sure you wish to reset your settings?",
  98. "Confirm Reset", "OK", "Cancel");
  99. if (confirm == true)
  100. {
  101. _settings.URLs = new String[]
  102. {
  103. "demo.prsdigital.com.au:8033",
  104. "demo2.prsdigital.com.au:8033",
  105. };
  106. _settings.UserID = "GUEST";
  107. _settings.Password = "guest";
  108. new LocalConfiguration<DatabaseSettings>().Save(_settings);
  109. DoClearCaches();
  110. Device.BeginInvokeOnMainThread(Populate);
  111. }
  112. }
  113. private async void EmailLogs_OnClicked(object sender, MobileButtonClickEventArgs args)
  114. {
  115. try
  116. {
  117. var message = new EmailMessage
  118. {
  119. Subject = $"Mobile Error Logs from {App.Data.Me?.Name ?? "Unknown Employee"} ({Device.RuntimePlatform } - v{MobileUtils.AppVersion.InstalledVersionNumber})",
  120. Body = String.Join('\n',_log.ItemsSource as String[]),
  121. To = new List<string> { "support@prsdigital.com.au" }
  122. };
  123. await Email.ComposeAsync(message);
  124. }
  125. catch (Exception e)
  126. {
  127. DisplayAlert("Error", "Unable to Send Email!", "OK");
  128. }
  129. }
  130. private async void ClearCaches_OnClicked(object sender, MobileButtonClickEventArgs args)
  131. {
  132. var confirm = await MaterialDialog.Instance.ConfirmAsync("Are your sure you wish to clear your caches?",
  133. "Clear Caches", "OK", "Cancel");
  134. if (confirm == true)
  135. DoClearCaches();
  136. }
  137. private void DoClearCaches()
  138. {
  139. ProgressVisible = true;
  140. var path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
  141. var files = Directory.GetFiles(path);
  142. Task.Run(() =>
  143. {
  144. foreach (var file in files)
  145. File.Delete(file);
  146. }).Wait();
  147. ProgressVisible = false;
  148. }
  149. }
  150. }