SettingsPage.xaml.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. namespace comal.timesheets
  16. {
  17. [XamlCompilation(XamlCompilationOptions.Compile)]
  18. public partial class SettingsPage : ContentPage
  19. {
  20. private int count;
  21. public SettingsPage()
  22. {
  23. InitializeComponent();
  24. NavigationPage.SetHasBackButton(this, false);
  25. Populate();
  26. }
  27. private void ExitBtn_Clicked(object sender, EventArgs e)
  28. {
  29. Navigation.PopAsync();
  30. }
  31. private void SaveBtn_Clicked(object sender, EventArgs e)
  32. {
  33. ValidateData();
  34. }
  35. private async void ValidateData()
  36. {
  37. try
  38. {
  39. App.DBSettings.URLs = stringList.SaveItems();
  40. App.DBSettings.UserID = userIDEnt.Text.Trim();
  41. App.DBSettings.Password = passwordEnt.Text.Trim();
  42. }
  43. catch { }
  44. try
  45. {
  46. using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Validating"))
  47. {
  48. ClientFactory.InvalidateUser();
  49. new LocalConfiguration<DatabaseSettings>().Delete();
  50. new LocalConfiguration<DatabaseSettings>().Save(App.DBSettings);
  51. App.DBSettings = new LocalConfiguration<DatabaseSettings>().Load();
  52. if (App.Current.Properties.ContainsKey("SessionID"))
  53. App.Current.Properties.Remove("SessionID");
  54. var result = JsonClient<User>.Ping(App.DBSettings.URLs, out DatabaseInfo info);
  55. ClientFactory.SetClientType(typeof(JsonClient<>), "TimeBench", MobileUtils.AppVersion.InstalledVersionNumber + GlobalVariables.DeviceString, result, true);
  56. GlobalVariables.ChangeUser = true;
  57. GlobalVariables.InternalOnAppearing = true;
  58. Navigation.PopToRootAsync(true);
  59. PINLoginPage pin = new PINLoginPage();
  60. Navigation.PushAsync(pin, true);
  61. }
  62. return;
  63. }
  64. catch (Exception ex)
  65. {
  66. DisplayAlert("Error", ex.Message, "OK");
  67. }
  68. Navigation.PopAsync();
  69. }
  70. #region Populate Screen
  71. private void Populate()
  72. {
  73. try
  74. {
  75. stringList.LoadList(App.DBSettings.URLs);
  76. userIDEnt.Text = App.DBSettings.UserID;
  77. passwordEnt.Text = App.DBSettings.Password;
  78. deviceIDEnt.Text = MobileUtils.GetDeviceID();
  79. appVersionEnt.Text = MobileUtils.AppVersion.InstalledVersionNumber;
  80. if (GlobalVariables.EmpID == Guid.Parse("40f6ccd9-5272-4b1a-99bf-de7542205aac") || GlobalVariables.EmpID == Guid.Parse("8fa878db-6bc9-421d-ac4d-ac3ec0a2cd19"))
  81. {
  82. websocketLogBtn.IsVisible = true;
  83. }
  84. }
  85. catch { }
  86. }
  87. protected override async void OnAppearing()
  88. {
  89. bool isLatest = true;
  90. try
  91. {
  92. isLatest = await MobileUtils.AppVersion.IsUsingLatestVersion();
  93. }
  94. catch (Exception eLatest)
  95. {
  96. }
  97. if (isLatest)
  98. {
  99. updateVersionBtn.IsEnabled = false;
  100. updateVersionBtn.Text = "App is up to date";
  101. }
  102. else if (!isLatest)
  103. {
  104. try
  105. {
  106. string latestVersionNumber = await MobileUtils.AppVersion.GetLatestVersionNumber();
  107. updateVersionBtn.IsEnabled = true;
  108. updateVersionBtn.Text = "Update App Version (" + latestVersionNumber + ")";
  109. }
  110. catch { }
  111. }
  112. const string errorFilename = "Fatal.log";
  113. var libraryPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
  114. var errorFilePath = Path.Combine(libraryPath, errorFilename);
  115. if (File.Exists(errorFilePath))
  116. {
  117. sendErrorsBtn.IsEnabled = true;
  118. }
  119. base.OnAppearing();
  120. }
  121. #endregion
  122. #region Buttons
  123. private async void SendErrorsBtn_Clicked(object sender, EventArgs e)
  124. {
  125. try
  126. {
  127. const string errorFilename = "Fatal.log";
  128. string libraryPath = "";
  129. if (Device.RuntimePlatform.Equals(Device.Android))
  130. libraryPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
  131. else if (Device.RuntimePlatform.Equals(Device.iOS))
  132. libraryPath = Environment.GetFolderPath(Environment.SpecialFolder.Resources);
  133. var errorFilePath = Path.Combine(libraryPath, errorFilename);
  134. if (!File.Exists(errorFilePath))
  135. {
  136. return;
  137. }
  138. var errorText = File.ReadAllText(errorFilePath);
  139. var message = new EmailMessage
  140. {
  141. Subject = "Error Logs from " + GlobalVariables.EmpName,
  142. Body = errorText,
  143. To = new List<string> { "support@prsdigital.com.au" }
  144. };
  145. await Email.ComposeAsync(message);
  146. File.Delete(errorFilePath);
  147. }
  148. catch { }
  149. }
  150. private void UpdateVersionBtn_Clicked(Object sender, EventArgs e)
  151. {
  152. Dispatcher.BeginInvokeOnMainThread(() => { MobileUtils.AppVersion.OpenAppInStore(); });
  153. }
  154. private async void ChangePasswordBtn_Clicked(object sender, EventArgs e)
  155. {
  156. if (count == 3)
  157. {
  158. DisplayAlert("Alert", "You have attempted incorrectly " + count + " times and cannot try again. Please contact your system administrator.", "OK");
  159. return;
  160. }
  161. CoreTable table = new Client<User>().Query(new Filter<User>(x => x.UserID).IsEqualTo(ClientFactory.UserID),
  162. new Columns<User>(x => x.Password));
  163. if (table.Rows.Any())
  164. {
  165. string p = table.Rows.FirstOrDefault().Values[0].ToString();
  166. string userInput = await DisplayPromptAsync("Alert", "Enter Current Password", "OK", "Cancel");
  167. if (!string.IsNullOrWhiteSpace(userInput) && userInput != "Cancel")
  168. {
  169. if (p != userInput)
  170. {
  171. count++;
  172. DisplayAlert("Alert", "Password is incorrect. You have attempted " + count + " times out of 3.", "OK");
  173. return;
  174. }
  175. else if (p == userInput)
  176. {
  177. PasswordResetPage page = new PasswordResetPage(ClientFactory.UserID);
  178. page.OnPasswordReset += (() =>
  179. {
  180. Navigation.PopToRootAsync();
  181. PINLoginPage pinpage = new PINLoginPage();
  182. Navigation.PushAsync(pinpage);
  183. });
  184. Navigation.PushAsync(page);
  185. }
  186. }
  187. }
  188. }
  189. private async void WebsocketLogBtn_Clicked(object sender, EventArgs e)
  190. {
  191. const string errorFilename = "Websocket.log";
  192. string libraryPath = "";
  193. if (Device.RuntimePlatform.Equals(Device.Android))
  194. libraryPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
  195. else if (Device.RuntimePlatform.Equals(Device.iOS))
  196. libraryPath = Environment.GetFolderPath(Environment.SpecialFolder.Resources);
  197. var errorFilePath = Path.Combine(libraryPath, errorFilename);
  198. if (!File.Exists(errorFilePath))
  199. {
  200. return;
  201. }
  202. var errorText = File.ReadAllText(errorFilePath);
  203. var page = new TextViewer(errorText);
  204. Navigation.PushAsync(page);
  205. }
  206. #endregion
  207. }
  208. }