Settings.xaml.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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. Port = settings.Port;
  27. UserID = settings.UserID;
  28. Password = settings.Password;
  29. }
  30. public void Unload(ConnectionSettings settings)
  31. {
  32. settings.URL = URL;
  33. settings.Port = Port;
  34. settings.UserID = UserID;
  35. settings.Password = Password;
  36. }
  37. }
  38. public partial class Settings : ContentPage
  39. {
  40. SettingsBinding binding = new SettingsBinding();
  41. int count = 0;
  42. string s = "";
  43. public bool SettingsChanged { get; private set; }
  44. public Settings()
  45. {
  46. InitializeComponent();
  47. binding.Load(App.Settings);
  48. binding.DeviceID = MobileUtils.GetDeviceID();
  49. binding.AppVersion = MobileUtils.AppVersion.InstalledVersionNumber;
  50. this.BindingContext = binding;
  51. NavigationPage.SetHasBackButton(this, false);
  52. }
  53. private async void ExitBtn_Clicked(object sender, EventArgs e)
  54. {
  55. using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Working"))
  56. {
  57. //Syncfusion Masked editor still needs access before popping the page
  58. Thread.Sleep(1000);
  59. Navigation.PopAsync();
  60. }
  61. }
  62. private void SaveBtn_Clicked(object sender, EventArgs e)
  63. {
  64. ValidateAppData();
  65. }
  66. private async void ValidateAppData()
  67. {
  68. try
  69. {
  70. using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Validating"))
  71. {
  72. ClientFactory.InvalidateUser();
  73. binding.Unload(App.Settings);
  74. new LocalConfiguration<ConnectionSettings>().Delete();
  75. new LocalConfiguration<ConnectionSettings>().Save(App.Settings);
  76. App.Settings = new LocalConfiguration<ConnectionSettings>().Load();
  77. ClientFactory.SetClientType(typeof(JsonClient<>), "TimeBench", MobileUtils.AppVersion.InstalledVersionNumber + GlobalVariables.DeviceString, new object[] { App.Settings.URL, App.Settings.Port, true });
  78. GlobalVariables.ChangeUser = true;
  79. GlobalVariables.InternalOnAppearing = true;
  80. if (App.Current.Properties.ContainsKey("SessionID"))
  81. App.Current.Properties.Remove("SessionID");
  82. //Syncfusion Masked editor still needs access before popping the page
  83. Thread.Sleep(1000);
  84. Navigation.PopToRootAsync(true);
  85. PINLoginPage pin = new PINLoginPage();
  86. Navigation.PushAsync(pin, true);
  87. }
  88. return;
  89. }
  90. catch { }
  91. }
  92. protected override async void OnAppearing()
  93. {
  94. bool isLatest = true;
  95. try
  96. {
  97. isLatest = await MobileUtils.AppVersion.IsUsingLatestVersion();
  98. }
  99. catch (Exception eLatest)
  100. {
  101. }
  102. if (isLatest)
  103. {
  104. updateVersionBtn.IsEnabled = false;
  105. updateVersionBtn.Text = "App is up to date";
  106. }
  107. else if (!isLatest)
  108. {
  109. string latestVersionNumber = await MobileUtils.AppVersion.GetLatestVersionNumber();
  110. updateVersionBtn.IsEnabled = true;
  111. updateVersionBtn.Text = "Update App Version (" + latestVersionNumber + ")";
  112. }
  113. const string errorFilename = "Fatal.log";
  114. var libraryPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
  115. var errorFilePath = Path.Combine(libraryPath, errorFilename);
  116. if (File.Exists(errorFilePath))
  117. {
  118. sendErrorsBtn.IsEnabled = true;
  119. }
  120. //if (Application.Current.Properties.ContainsKey("IsSharedDevice"))
  121. //{
  122. // if (Application.Current.Properties["IsSharedDevice"].Equals("True"))
  123. // {
  124. // sharedDeviceRb.IsChecked = true;
  125. // }
  126. //}
  127. base.OnAppearing();
  128. }
  129. private async void ChangePasswordBtn_Clicked(object sender, EventArgs e)
  130. {
  131. if (count == 3)
  132. {
  133. DisplayAlert("Alert", "You have attempted incorrectly " + count + " times and cannot try again. Please contact your system administrator.", "OK");
  134. return;
  135. }
  136. CoreTable table = new Client<User>().Query(new Filter<User>(x => x.UserID).IsEqualTo(ClientFactory.UserID),
  137. new Columns<User>(x => x.Password));
  138. if (table.Rows.Any())
  139. {
  140. string p = table.Rows.FirstOrDefault().Values[0].ToString();
  141. string userInput = await DisplayPromptAsync("Alert", "Enter Current Password", "OK", "Cancel");
  142. if (!string.IsNullOrWhiteSpace(userInput) && userInput != "Cancel")
  143. {
  144. if (p != userInput)
  145. {
  146. count++;
  147. DisplayAlert("Alert", "Password is incorrect. You have attempted " + count + " times out of 3.", "OK");
  148. return;
  149. }
  150. else if (p == userInput)
  151. {
  152. PasswordResetPage page = new PasswordResetPage(ClientFactory.UserID);
  153. page.OnPasswordReset += (() =>
  154. {
  155. Navigation.PopToRootAsync();
  156. PINLoginPage pinpage = new PINLoginPage();
  157. Navigation.PushAsync(pinpage);
  158. });
  159. Navigation.PushAsync(page);
  160. }
  161. }
  162. }
  163. }
  164. private async void UpdateVersionBtn_Clicked(object sender, EventArgs e)
  165. {
  166. Dispatcher.BeginInvokeOnMainThread(() => { MobileUtils.AppVersion.OpenAppInStore(); });
  167. }
  168. private void UserID_Changed(object sender, EventArgs e)
  169. {
  170. if (!string.IsNullOrWhiteSpace(UserID.Text))
  171. {
  172. UserID.Text = UserID.Text.Trim();
  173. }
  174. }
  175. private void UserPassword_Changed(object sender, EventArgs e)
  176. {
  177. if (!string.IsNullOrWhiteSpace(UserID.Text))
  178. {
  179. Password.Text = Password.Text.Trim();
  180. }
  181. //if (!string.IsNullOrWhiteSpace(Password.Value.ToString()))
  182. //{
  183. // Password.Value = Password.Value.ToString().Trim();
  184. // s = Password.Value.ToString();
  185. // showPassword.Text = s;
  186. //}
  187. //else
  188. // s = "";
  189. }
  190. async void BackgroundLoadProducts()
  191. {
  192. await Task.Run(() =>
  193. {
  194. if (!GlobalVariables.ProductsLoaded/* && ClientFactory.IsAllowed<CanViewStockLocations>()*/)
  195. {
  196. GlobalVariables.ProductsLoaded = false;
  197. ProductsLoader productsLoader = new ProductsLoader();
  198. GlobalVariables.ProductShells = productsLoader.ProductShells;
  199. GlobalVariables.ProductsLoaded = true;
  200. }
  201. });
  202. }
  203. private void sharedDeviceRb_Changed(object sender, EventArgs e)
  204. {
  205. //string isSharedDevice = "IsSharedDevice";
  206. //if ((sender as CheckBox).IsChecked)
  207. //{
  208. // if (!Application.Current.Properties.ContainsKey(isSharedDevice))
  209. // {
  210. // Application.Current.Properties.Add(isSharedDevice, "True");
  211. // URL.Text = "http://mobile.com-al.com.au";
  212. // UserID.Text = "";
  213. // Password.Value = "";
  214. // }
  215. // else
  216. // {
  217. // Application.Current.Properties[isSharedDevice] = "True";
  218. // URL.Text = "http://mobile.com-al.com.au";
  219. // UserID.Text = "";
  220. // Password.Value = "";
  221. // }
  222. //}
  223. //else
  224. //{
  225. // if (Application.Current.Properties.ContainsKey(isSharedDevice))
  226. // {
  227. // Application.Current.Properties[isSharedDevice] = "False";
  228. // }
  229. //}
  230. }
  231. private void ShowPW_Tapped(object sender, EventArgs e)
  232. {
  233. //passwordOne.Width = new GridLength(0, GridUnitType.Absolute);
  234. //passwordTwo.Width = new GridLength(1, GridUnitType.Star);
  235. //Task.Run(() =>
  236. //{
  237. // Thread.Sleep(2500);
  238. // ShowMaskedText();
  239. //});
  240. }
  241. private void ShowMaskedText()
  242. {
  243. //Device.BeginInvokeOnMainThread(() =>
  244. //{
  245. // passwordTwo.Width = new GridLength(0, GridUnitType.Absolute);
  246. // passwordOne.Width = new GridLength(1, GridUnitType.Star);
  247. //});
  248. }
  249. private async void ClearStorageBtn_Clicked(object sender, EventArgs e)
  250. {
  251. string chosenOption = await DisplayActionSheet("Confirm Clear? This will also log you out", "Cancel", null, "Yes", "No");
  252. switch (chosenOption)
  253. {
  254. case "Cancel":
  255. return;
  256. default:
  257. return;
  258. case "No":
  259. return;
  260. case "Yes":
  261. RemoveDataFromSecureStorage();
  262. ClearConnectionSettings();
  263. ValidateAppData();
  264. break;
  265. }
  266. }
  267. private void RemoveDataFromSecureStorage()
  268. {
  269. try
  270. {
  271. SecureStorage.Remove(GlobalVariables.CacheURLString);
  272. SecureStorage.Remove(GlobalVariables.CacheURLString);
  273. SecureStorage.Remove(GlobalVariables.CacheURLString);
  274. SecureStorage.Remove(GlobalVariables.CacheURLString);
  275. }
  276. catch
  277. { }
  278. }
  279. private void ClearConnectionSettings()
  280. {
  281. try
  282. {
  283. new LocalConfiguration<ConnectionSettings>().Delete();
  284. MobileUtils.LoadDemoSettings(App.Settings);
  285. binding.Load(App.Settings);
  286. }
  287. catch { }
  288. }
  289. //private void DocsURL_Changed(object sender, EventArgs e)
  290. //{
  291. // if (!string.IsNullOrWhiteSpace(docsURL.Text))
  292. // {
  293. // if (!App.Current.Properties.ContainsKey("DocsURL"))
  294. // {
  295. // App.Current.Properties.Add("DocsURL", docsURL.Text);
  296. // }
  297. // else if (App.Current.Properties.ContainsKey("DocsURL"))
  298. // {
  299. // App.Current.Properties["DocsURL"] = docsURL.Text;
  300. // }
  301. // }
  302. //}
  303. //private void ClearDocsBtn_Clicked(object sender, EventArgs e)
  304. //{
  305. // if (App.Current.Properties.ContainsKey("DocsURL"))
  306. // {
  307. // App.Current.Properties.Remove("DocsURL");
  308. // DisplayAlert("Success", "", "OK");
  309. // }
  310. //}
  311. private async void SendErrorsBtn_Clicked(object sender, EventArgs e)
  312. {
  313. try
  314. {
  315. const string errorFilename = "Fatal.log";
  316. string libraryPath = "";
  317. if (Device.RuntimePlatform.Equals(Device.Android))
  318. libraryPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
  319. else if (Device.RuntimePlatform.Equals(Device.iOS))
  320. libraryPath = Environment.GetFolderPath(Environment.SpecialFolder.Resources);
  321. var errorFilePath = Path.Combine(libraryPath, errorFilename);
  322. if (!File.Exists(errorFilePath))
  323. {
  324. return;
  325. }
  326. var errorText = File.ReadAllText(errorFilePath);
  327. var message = new EmailMessage
  328. {
  329. Subject = "Error Logs from " + GlobalVariables.EmpName,
  330. Body = errorText,
  331. To = new List<string> { "support@prsdigital.com.au" }
  332. };
  333. await Email.ComposeAsync(message);
  334. File.Delete(errorFilePath);
  335. }
  336. catch { }
  337. }
  338. }
  339. }