SettingsPage.xaml.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. }
  81. catch { }
  82. }
  83. protected override async void OnAppearing()
  84. {
  85. bool isLatest = true;
  86. try
  87. {
  88. isLatest = await MobileUtils.AppVersion.IsUsingLatestVersion();
  89. }
  90. catch (Exception eLatest)
  91. {
  92. }
  93. if (isLatest)
  94. {
  95. updateVersionBtn.IsEnabled = false;
  96. updateVersionBtn.Text = "App is up to date";
  97. }
  98. else if (!isLatest)
  99. {
  100. string latestVersionNumber = await MobileUtils.AppVersion.GetLatestVersionNumber();
  101. updateVersionBtn.IsEnabled = true;
  102. updateVersionBtn.Text = "Update App Version (" + latestVersionNumber + ")";
  103. }
  104. const string errorFilename = "Fatal.log";
  105. var libraryPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
  106. var errorFilePath = Path.Combine(libraryPath, errorFilename);
  107. if (File.Exists(errorFilePath))
  108. {
  109. sendErrorsBtn.IsEnabled = true;
  110. }
  111. base.OnAppearing();
  112. }
  113. #endregion
  114. #region Buttons
  115. private async void SendErrorsBtn_Clicked(object sender, EventArgs e)
  116. {
  117. try
  118. {
  119. const string errorFilename = "Fatal.log";
  120. string libraryPath = "";
  121. if (Device.RuntimePlatform.Equals(Device.Android))
  122. libraryPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
  123. else if (Device.RuntimePlatform.Equals(Device.iOS))
  124. libraryPath = Environment.GetFolderPath(Environment.SpecialFolder.Resources);
  125. var errorFilePath = Path.Combine(libraryPath, errorFilename);
  126. if (!File.Exists(errorFilePath))
  127. {
  128. return;
  129. }
  130. var errorText = File.ReadAllText(errorFilePath);
  131. var message = new EmailMessage
  132. {
  133. Subject = "Error Logs from " + GlobalVariables.EmpName,
  134. Body = errorText,
  135. To = new List<string> { "support@prsdigital.com.au" }
  136. };
  137. await Email.ComposeAsync(message);
  138. File.Delete(errorFilePath);
  139. }
  140. catch { }
  141. }
  142. private void UpdateVersionBtn_Clicked(Object sender, EventArgs e)
  143. {
  144. Dispatcher.BeginInvokeOnMainThread(() => { MobileUtils.AppVersion.OpenAppInStore(); });
  145. }
  146. private async void ChangePasswordBtn_Clicked(object sender, EventArgs e)
  147. {
  148. if (count == 3)
  149. {
  150. DisplayAlert("Alert", "You have attempted incorrectly " + count + " times and cannot try again. Please contact your system administrator.", "OK");
  151. return;
  152. }
  153. CoreTable table = new Client<User>().Query(new Filter<User>(x => x.UserID).IsEqualTo(ClientFactory.UserID),
  154. new Columns<User>(x => x.Password));
  155. if (table.Rows.Any())
  156. {
  157. string p = table.Rows.FirstOrDefault().Values[0].ToString();
  158. string userInput = await DisplayPromptAsync("Alert", "Enter Current Password", "OK", "Cancel");
  159. if (!string.IsNullOrWhiteSpace(userInput) && userInput != "Cancel")
  160. {
  161. if (p != userInput)
  162. {
  163. count++;
  164. DisplayAlert("Alert", "Password is incorrect. You have attempted " + count + " times out of 3.", "OK");
  165. return;
  166. }
  167. else if (p == userInput)
  168. {
  169. PasswordResetPage page = new PasswordResetPage(ClientFactory.UserID);
  170. page.OnPasswordReset += (() =>
  171. {
  172. Navigation.PopToRootAsync();
  173. PINLoginPage pinpage = new PINLoginPage();
  174. Navigation.PushAsync(pinpage);
  175. });
  176. Navigation.PushAsync(page);
  177. }
  178. }
  179. }
  180. }
  181. #endregion
  182. }
  183. }