Settings.xaml.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. using System;
  2. using System.Linq;
  3. using System.Threading;
  4. using System.Threading.Tasks;
  5. using System.Timers;
  6. using InABox.Clients;
  7. using InABox.Configuration;
  8. using InABox.Core;
  9. using InABox.Mobile;
  10. using Syncfusion.SfPdfViewer.XForms;
  11. using Xamarin.Essentials;
  12. using Xamarin.Forms;
  13. using XF.Material.Forms.UI.Dialogs;
  14. using Timer = System.Threading.Timer;
  15. namespace comal.timesheets
  16. {
  17. class SettingsBinding : ConnectionSettings
  18. {
  19. public String DeviceID { get; set; }
  20. public String AppVersion { get; set; }
  21. public void Load(ConnectionSettings settings)
  22. {
  23. URL = settings.URL;
  24. Port = settings.Port;
  25. UserID = settings.UserID;
  26. Password = settings.Password;
  27. }
  28. public void Unload(ConnectionSettings settings)
  29. {
  30. settings.URL = URL;
  31. settings.Port = Port;
  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. //if (Application.Current.Properties.ContainsKey("IsSharedDevice"))
  112. //{
  113. // if (Application.Current.Properties["IsSharedDevice"].Equals("True"))
  114. // {
  115. // sharedDeviceRb.IsChecked = true;
  116. // }
  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. //if (!string.IsNullOrWhiteSpace(Password.Value.ToString()))
  173. //{
  174. // Password.Value = Password.Value.ToString().Trim();
  175. // s = Password.Value.ToString();
  176. // showPassword.Text = s;
  177. //}
  178. //else
  179. // s = "";
  180. }
  181. async void BackgroundLoadProducts()
  182. {
  183. await Task.Run(() =>
  184. {
  185. if (!GlobalVariables.ProductsLoaded/* && ClientFactory.IsAllowed<CanViewStockLocations>()*/)
  186. {
  187. GlobalVariables.ProductsLoaded = false;
  188. ProductsLoader productsLoader = new ProductsLoader();
  189. GlobalVariables.ProductShells = productsLoader.ProductShells;
  190. GlobalVariables.ProductsLoaded = true;
  191. }
  192. });
  193. }
  194. private void sharedDeviceRb_Changed(object sender, EventArgs e)
  195. {
  196. //string isSharedDevice = "IsSharedDevice";
  197. //if ((sender as CheckBox).IsChecked)
  198. //{
  199. // if (!Application.Current.Properties.ContainsKey(isSharedDevice))
  200. // {
  201. // Application.Current.Properties.Add(isSharedDevice, "True");
  202. // URL.Text = "http://mobile.com-al.com.au";
  203. // UserID.Text = "";
  204. // Password.Value = "";
  205. // }
  206. // else
  207. // {
  208. // Application.Current.Properties[isSharedDevice] = "True";
  209. // URL.Text = "http://mobile.com-al.com.au";
  210. // UserID.Text = "";
  211. // Password.Value = "";
  212. // }
  213. //}
  214. //else
  215. //{
  216. // if (Application.Current.Properties.ContainsKey(isSharedDevice))
  217. // {
  218. // Application.Current.Properties[isSharedDevice] = "False";
  219. // }
  220. //}
  221. }
  222. private void ShowPW_Tapped(object sender, EventArgs e)
  223. {
  224. //passwordOne.Width = new GridLength(0, GridUnitType.Absolute);
  225. //passwordTwo.Width = new GridLength(1, GridUnitType.Star);
  226. //Task.Run(() =>
  227. //{
  228. // Thread.Sleep(2500);
  229. // ShowMaskedText();
  230. //});
  231. }
  232. private void ShowMaskedText()
  233. {
  234. //Device.BeginInvokeOnMainThread(() =>
  235. //{
  236. // passwordTwo.Width = new GridLength(0, GridUnitType.Absolute);
  237. // passwordOne.Width = new GridLength(1, GridUnitType.Star);
  238. //});
  239. }
  240. private async void ClearStorageBtn_Clicked(object sender, EventArgs e)
  241. {
  242. string chosenOption = await DisplayActionSheet("Confirm Clear? This will also log you out", "Cancel", null, "Yes", "No");
  243. switch (chosenOption)
  244. {
  245. case "Cancel":
  246. return;
  247. default:
  248. return;
  249. case "No":
  250. return;
  251. case "Yes":
  252. RemoveDataFromSecureStorage();
  253. ClearConnectionSettings();
  254. ValidateAppData();
  255. break;
  256. }
  257. }
  258. private void RemoveDataFromSecureStorage()
  259. {
  260. try
  261. {
  262. SecureStorage.Remove(GlobalVariables.CacheURLString);
  263. SecureStorage.Remove(GlobalVariables.CacheURLString);
  264. SecureStorage.Remove(GlobalVariables.CacheURLString);
  265. SecureStorage.Remove(GlobalVariables.CacheURLString);
  266. }
  267. catch
  268. { }
  269. }
  270. private void ClearConnectionSettings()
  271. {
  272. try
  273. {
  274. new LocalConfiguration<ConnectionSettings>().Delete();
  275. MobileUtils.LoadDemoSettings(App.Settings);
  276. binding.Load(App.Settings);
  277. }
  278. catch { }
  279. }
  280. //private void DocsURL_Changed(object sender, EventArgs e)
  281. //{
  282. // if (!string.IsNullOrWhiteSpace(docsURL.Text))
  283. // {
  284. // if (!App.Current.Properties.ContainsKey("DocsURL"))
  285. // {
  286. // App.Current.Properties.Add("DocsURL", docsURL.Text);
  287. // }
  288. // else if (App.Current.Properties.ContainsKey("DocsURL"))
  289. // {
  290. // App.Current.Properties["DocsURL"] = docsURL.Text;
  291. // }
  292. // }
  293. //}
  294. //private void ClearDocsBtn_Clicked(object sender, EventArgs e)
  295. //{
  296. // if (App.Current.Properties.ContainsKey("DocsURL"))
  297. // {
  298. // App.Current.Properties.Remove("DocsURL");
  299. // DisplayAlert("Success", "", "OK");
  300. // }
  301. //}
  302. }
  303. }