SettingsPage.xaml.cs 9.5 KB

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