App.xaml.cs 5.3 KB

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