App.xaml.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. using System;
  2. using System.Threading.Tasks;
  3. using Comal.Classes;
  4. using InABox.Clients;
  5. using InABox.Configuration;
  6. using InABox.Core;
  7. using InABox.Mobile;
  8. using Xamarin.Forms;
  9. using Xamarin.Forms.Xaml;
  10. using XF.Material.Forms;
  11. using XF.Material.Forms.Resources;
  12. using XF.Material.Forms.Resources.Typography;
  13. using XF.Material.Forms.UI;
  14. using Xamarin.Essentials;
  15. using SkiaSharp;
  16. using System.Threading;
  17. //[assembly: XamlCompilation(XamlCompilationOptions.Compile)]
  18. namespace comal.timesheets
  19. {
  20. public partial class App : Application
  21. {
  22. string deviceString = "";
  23. public static bool IsUserLoggedIn => ClientFactory.UserGuid != Guid.Empty;
  24. public static void LogoutUser()
  25. {
  26. ClientFactory.InvalidateUser();
  27. }
  28. public static LocationServices GPS { get; } = new LocationServices() { ScanDelay = new TimeSpan(0, 1, 0) };
  29. public static Bluetooth Bluetooth { get; } = new Bluetooth() { ScanDelay = new TimeSpan(0, 1, 0) };
  30. public static DataModel Data { get; } = new DataModel();
  31. public static bool IsInForeground { get; set; } = false;
  32. public const string MessageOnStart = "OnStart";
  33. public const string MessageOnSleep = "OnSleep";
  34. public const string MessageOnResume = "OnResume";
  35. public static ConnectionSettings Settings = null;
  36. public App()
  37. {
  38. LoadAll();
  39. }
  40. private void LoadAll(bool reload = false)
  41. {
  42. try
  43. {
  44. Material.Init(this);
  45. InitializeComponent();
  46. Material.Use("Material.Configuration");
  47. MobileUtils.Init();
  48. CoreUtils.RegisterClasses();
  49. ComalUtils.RegisterClasses();
  50. FindDeviceInfo();
  51. Settings = new LocalConfiguration<ConnectionSettings>().Load();
  52. if (string.IsNullOrWhiteSpace(Settings.URL) || Settings.URL == "http://demo.prsdigital.com.au")
  53. {
  54. TryLoadFromSecureCache();
  55. if (String.IsNullOrWhiteSpace(Settings.URL))
  56. MobileUtils.LoadDemoSettings(Settings);
  57. }
  58. else
  59. MobileUtils.SaveToSecureStorage();
  60. if (!string.IsNullOrWhiteSpace(GlobalVariables.LoadFromLinkString))
  61. {
  62. MobileUtils.LoadFromLink();
  63. }
  64. ClientFactory.SetClientType(typeof(JsonClient<>), "TimeBench", MobileUtils.AppVersion.InstalledVersionNumber + deviceString, new object[] { Settings.URL, Settings.Port, true });
  65. GlobalVariables.InternalOnAppearing = true;
  66. GlobalVariables.ChangeUser = false;
  67. if (!reload)
  68. RunTimers();
  69. MainPage = new MaterialNavigationPage(new PINLoginPage());
  70. }
  71. catch
  72. {
  73. Thread.Sleep(1000);
  74. LoadAll(true);
  75. }
  76. }
  77. private void RunTimers()
  78. {
  79. Task.Run(() =>
  80. {
  81. GPS.GetLocation();
  82. Bluetooth.ScanForDevices();
  83. Device.StartTimer(new TimeSpan(0, 0, 30), () =>
  84. {
  85. if (App.IsInForeground)
  86. {
  87. GPS.GetLocation();
  88. }
  89. return true;
  90. });
  91. Device.StartTimer(new TimeSpan(0, 0, 30), () =>
  92. {
  93. if (App.IsInForeground)
  94. {
  95. Bluetooth.ScanForDevices();
  96. }
  97. return true;
  98. });
  99. });
  100. }
  101. private async void TryLoadFromSecureCache()
  102. {
  103. try
  104. {
  105. Settings.URL = await SecureStorage.GetAsync(GlobalVariables.CacheURLString);
  106. Settings.Port = int.Parse(await SecureStorage.GetAsync(GlobalVariables.CachePortString));
  107. Settings.UserID = await SecureStorage.GetAsync(GlobalVariables.CacheUserIDString);
  108. Settings.Password = await SecureStorage.GetAsync(GlobalVariables.CachePasswordString);
  109. }
  110. catch { }
  111. }
  112. private void FindDeviceInfo()
  113. {
  114. try
  115. {
  116. var idiom = DeviceInfo.Idiom;
  117. if (Device.RuntimePlatform.Equals(Device.iOS))
  118. {
  119. if (idiom.Equals(DeviceIdiom.Phone))
  120. {
  121. deviceString = "i";
  122. }
  123. else if (idiom.Equals(DeviceIdiom.Tablet))
  124. {
  125. deviceString = "I";
  126. }
  127. }
  128. else if (Device.RuntimePlatform.Equals(Device.Android))
  129. {
  130. if (idiom.Equals(DeviceIdiom.Phone))
  131. {
  132. deviceString = "a";
  133. }
  134. else if (idiom.Equals(DeviceIdiom.Tablet))
  135. {
  136. deviceString = "A";
  137. }
  138. }
  139. GlobalVariables.DeviceString = deviceString;
  140. }
  141. catch { }
  142. }
  143. protected override void OnStart()
  144. {
  145. MessagingCenter.Send<App>(this, MessageOnStart);
  146. IsInForeground = true;
  147. }
  148. protected override void OnSleep()
  149. {
  150. MessagingCenter.Send<App>(this, MessageOnSleep);
  151. IsInForeground = false;
  152. }
  153. protected override void OnResume()
  154. {
  155. MessagingCenter.Send<App>(this, MessageOnResume);
  156. IsInForeground = true;
  157. }
  158. }
  159. }