Settings.xaml.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. using System.Timers;
  8. using InABox.Clients;
  9. using InABox.Configuration;
  10. using InABox.Core;
  11. using InABox.Mobile;
  12. using Syncfusion.SfPdfViewer.XForms;
  13. using Xamarin.Essentials;
  14. using Xamarin.Forms;
  15. using XF.Material.Forms.UI.Dialogs;
  16. using Timer = System.Threading.Timer;
  17. namespace comal.timesheets
  18. {
  19. class SettingsBinding : ConnectionSettings
  20. {
  21. public String DeviceID { get; set; }
  22. public String AppVersion { get; set; }
  23. public void Load(ConnectionSettings settings)
  24. {
  25. URL = settings.URL;
  26. UserID = settings.UserID;
  27. Password = settings.Password;
  28. }
  29. public void Unload(ConnectionSettings settings)
  30. {
  31. settings.URL = URL;
  32. settings.UserID = UserID;
  33. settings.Password = Password;
  34. }
  35. }
  36. public partial class Settings : ContentPage
  37. {
  38. SettingsBinding binding = new SettingsBinding();
  39. int count = 0;
  40. string s = "";
  41. public bool SettingsChanged { get; private set; }
  42. public Settings()
  43. {
  44. InitializeComponent();
  45. binding.Load(App.Settings);
  46. binding.DeviceID = MobileUtils.GetDeviceID();
  47. binding.AppVersion = MobileUtils.AppVersion.InstalledVersionNumber;
  48. this.BindingContext = binding;
  49. NavigationPage.SetHasBackButton(this, false);
  50. }
  51. private async void ExitBtn_Clicked(object sender, EventArgs e)
  52. {
  53. using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Working"))
  54. {
  55. //Syncfusion Masked editor still needs access before popping the page
  56. Thread.Sleep(1000);
  57. Navigation.PopAsync();
  58. }
  59. }
  60. private void SaveBtn_Clicked(object sender, EventArgs e)
  61. {
  62. ValidateAppData();
  63. }
  64. private async void ValidateAppData()
  65. {
  66. try
  67. {
  68. using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Validating"))
  69. {
  70. ClientFactory.InvalidateUser();
  71. binding.Unload(App.Settings);
  72. new LocalConfiguration<ConnectionSettings>().Delete();
  73. new LocalConfiguration<ConnectionSettings>().Save(App.Settings);
  74. App.Settings = new LocalConfiguration<ConnectionSettings>().Load();
  75. ClientFactory.SetClientType(typeof(JsonClient<>), "TimeBench", MobileUtils.AppVersion.InstalledVersionNumber + GlobalVariables.DeviceString, new object[] { App.Settings.URL, App.Settings.Port, true });
  76. GlobalVariables.ChangeUser = true;
  77. GlobalVariables.InternalOnAppearing = true;
  78. if (App.Current.Properties.ContainsKey("SessionID"))
  79. App.Current.Properties.Remove("SessionID");
  80. //Syncfusion Masked editor still needs access before popping the page
  81. Thread.Sleep(1000);
  82. Navigation.PopToRootAsync(true);
  83. PINLoginPage pin = new PINLoginPage();
  84. Navigation.PushAsync(pin, true);
  85. }
  86. return;
  87. }
  88. catch { }
  89. }
  90. protected override async void OnAppearing()
  91. {
  92. bool isLatest = true;
  93. try
  94. {
  95. isLatest = await MobileUtils.AppVersion.IsUsingLatestVersion();
  96. }
  97. catch (Exception eLatest)
  98. {
  99. }
  100. if (isLatest)
  101. {
  102. updateVersionBtn.IsEnabled = false;
  103. updateVersionBtn.Text = "App is up to date";
  104. }
  105. else if (!isLatest)
  106. {
  107. string latestVersionNumber = await MobileUtils.AppVersion.GetLatestVersionNumber();
  108. updateVersionBtn.IsEnabled = true;
  109. updateVersionBtn.Text = "Update App Version (" + latestVersionNumber + ")";
  110. }
  111. const string errorFilename = "Fatal.log";
  112. var libraryPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
  113. var errorFilePath = Path.Combine(libraryPath, errorFilename);
  114. if (File.Exists(errorFilePath))
  115. {
  116. sendErrorsBtn.IsEnabled = true;
  117. }
  118. base.OnAppearing();
  119. }
  120. private async void ChangePasswordBtn_Clicked(object sender, EventArgs e)
  121. {
  122. if (count == 3)
  123. {
  124. DisplayAlert("Alert", "You have attempted incorrectly " + count + " times and cannot try again. Please contact your system administrator.", "OK");
  125. return;
  126. }
  127. CoreTable table = new Client<User>().Query(new Filter<User>(x => x.UserID).IsEqualTo(ClientFactory.UserID),
  128. new Columns<User>(x => x.Password));
  129. if (table.Rows.Any())
  130. {
  131. string p = table.Rows.FirstOrDefault().Values[0].ToString();
  132. string userInput = await DisplayPromptAsync("Alert", "Enter Current Password", "OK", "Cancel");
  133. if (!string.IsNullOrWhiteSpace(userInput) && userInput != "Cancel")
  134. {
  135. if (p != userInput)
  136. {
  137. count++;
  138. DisplayAlert("Alert", "Password is incorrect. You have attempted " + count + " times out of 3.", "OK");
  139. return;
  140. }
  141. else if (p == userInput)
  142. {
  143. PasswordResetPage page = new PasswordResetPage(ClientFactory.UserID);
  144. page.OnPasswordReset += (() =>
  145. {
  146. Navigation.PopToRootAsync();
  147. PINLoginPage pinpage = new PINLoginPage();
  148. Navigation.PushAsync(pinpage);
  149. });
  150. Navigation.PushAsync(page);
  151. }
  152. }
  153. }
  154. }
  155. private async void UpdateVersionBtn_Clicked(object sender, EventArgs e)
  156. {
  157. Dispatcher.BeginInvokeOnMainThread(() => { MobileUtils.AppVersion.OpenAppInStore(); });
  158. }
  159. private void UserID_Changed(object sender, EventArgs e)
  160. {
  161. if (!string.IsNullOrWhiteSpace(UserID.Text))
  162. {
  163. UserID.Text = UserID.Text.Trim();
  164. }
  165. }
  166. private void UserPassword_Changed(object sender, EventArgs e)
  167. {
  168. if (!string.IsNullOrWhiteSpace(UserID.Text))
  169. {
  170. Password.Text = Password.Text.Trim();
  171. }
  172. }
  173. private async void ClearStorageBtn_Clicked(object sender, EventArgs e)
  174. {
  175. string chosenOption = await DisplayActionSheet("Confirm Clear? This will also log you out", "Cancel", null, "Yes", "No");
  176. switch (chosenOption)
  177. {
  178. case "Cancel":
  179. return;
  180. default:
  181. return;
  182. case "No":
  183. return;
  184. case "Yes":
  185. RemoveDataFromSecureStorage();
  186. ClearConnectionSettings();
  187. ValidateAppData();
  188. break;
  189. }
  190. }
  191. private void RemoveDataFromSecureStorage()
  192. {
  193. try
  194. {
  195. SecureStorage.Remove(GlobalVariables.CacheURLString);
  196. SecureStorage.Remove(GlobalVariables.CacheURLString);
  197. SecureStorage.Remove(GlobalVariables.CacheURLString);
  198. SecureStorage.Remove(GlobalVariables.CacheURLString);
  199. }
  200. catch
  201. { }
  202. }
  203. private void ClearConnectionSettings()
  204. {
  205. try
  206. {
  207. new LocalConfiguration<ConnectionSettings>().Delete();
  208. MobileUtils.LoadDemoSettings(App.DBSettings);
  209. binding.Load(App.Settings);
  210. }
  211. catch { }
  212. }
  213. //private void DocsURL_Changed(object sender, EventArgs e)
  214. //{
  215. // if (!string.IsNullOrWhiteSpace(docsURL.Text))
  216. // {
  217. // if (!App.Current.Properties.ContainsKey("DocsURL"))
  218. // {
  219. // App.Current.Properties.Add("DocsURL", docsURL.Text);
  220. // }
  221. // else if (App.Current.Properties.ContainsKey("DocsURL"))
  222. // {
  223. // App.Current.Properties["DocsURL"] = docsURL.Text;
  224. // }
  225. // }
  226. //}
  227. //private void ClearDocsBtn_Clicked(object sender, EventArgs e)
  228. //{
  229. // if (App.Current.Properties.ContainsKey("DocsURL"))
  230. // {
  231. // App.Current.Properties.Remove("DocsURL");
  232. // DisplayAlert("Success", "", "OK");
  233. // }
  234. //}
  235. private async void SendErrorsBtn_Clicked(object sender, EventArgs e)
  236. {
  237. try
  238. {
  239. const string errorFilename = "Fatal.log";
  240. string libraryPath = "";
  241. if (Device.RuntimePlatform.Equals(Device.Android))
  242. libraryPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
  243. else if (Device.RuntimePlatform.Equals(Device.iOS))
  244. libraryPath = Environment.GetFolderPath(Environment.SpecialFolder.Resources);
  245. var errorFilePath = Path.Combine(libraryPath, errorFilename);
  246. if (!File.Exists(errorFilePath))
  247. {
  248. return;
  249. }
  250. var errorText = File.ReadAllText(errorFilePath);
  251. var message = new EmailMessage
  252. {
  253. Subject = "Error Logs from " + GlobalVariables.EmpName,
  254. Body = errorText,
  255. To = new List<string> { "support@prsdigital.com.au" }
  256. };
  257. await Email.ComposeAsync(message);
  258. File.Delete(errorFilePath);
  259. }
  260. catch { }
  261. }
  262. }
  263. }