App.xaml.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. Settings.URL = "https://remote.com-al.com.au";
  48. Settings.Port = 8005;
  49. Settings.UserID = "NICK";
  50. Settings.Password = "nick";
  51. if (String.IsNullOrWhiteSpace(Settings.URL) || Settings.URL == "http://demo.prsdigital.com.au")
  52. {
  53. TryLoadFromSecureCache();
  54. if (String.IsNullOrWhiteSpace(Settings.URL))
  55. MobileUtils.LoadDemoSettings(Settings);
  56. }
  57. else
  58. MobileUtils.SaveToSecureStorage();
  59. if (!string.IsNullOrWhiteSpace(GlobalVariables.LoadFromLinkString))
  60. {
  61. MobileUtils.LoadFromLink();
  62. }
  63. ClientFactory.SetClientType(typeof(JsonClient<>), "TimeBench", MobileUtils.AppVersion.InstalledVersionNumber + deviceString, new object[] { Settings.URL, Settings.Port, true });
  64. GlobalVariables.InternalOnAppearing = true;
  65. GlobalVariables.ChangeUser = false;
  66. GPS.GetLocation();
  67. Bluetooth.ScanForDevices();
  68. Device.StartTimer(new TimeSpan(0, 0, 30), () =>
  69. {
  70. if (App.IsInForeground)
  71. {
  72. GPS.GetLocation();
  73. }
  74. return true;
  75. });
  76. Device.StartTimer(new TimeSpan(0, 0, 30), () =>
  77. {
  78. if (App.IsInForeground)
  79. {
  80. Bluetooth.ScanForDevices();
  81. }
  82. return true;
  83. });
  84. MainPage = new MaterialNavigationPage(new comal.timesheets.PINLoginPage());
  85. }
  86. catch
  87. {
  88. MainPage = new MaterialNavigationPage(new comal.timesheets.PINLoginPage());
  89. }
  90. }
  91. private async void TryLoadFromSecureCache()
  92. {
  93. try
  94. {
  95. Settings.URL = await SecureStorage.GetAsync(GlobalVariables.CacheURLString);
  96. Settings.Port = int.Parse(await SecureStorage.GetAsync(GlobalVariables.CachePortString));
  97. Settings.UserID = await SecureStorage.GetAsync(GlobalVariables.CacheUserIDString);
  98. Settings.Password = await SecureStorage.GetAsync(GlobalVariables.CachePasswordString);
  99. }
  100. catch { }
  101. }
  102. private void FindDeviceInfo()
  103. {
  104. var idiom = DeviceInfo.Idiom;
  105. if (Device.RuntimePlatform.Equals(Device.iOS))
  106. {
  107. if (idiom.Equals(DeviceIdiom.Phone))
  108. {
  109. deviceString = "i";
  110. }
  111. else if (idiom.Equals(DeviceIdiom.Tablet))
  112. {
  113. deviceString = "I";
  114. }
  115. }
  116. else if (Device.RuntimePlatform.Equals(Device.Android))
  117. {
  118. if (idiom.Equals(DeviceIdiom.Phone))
  119. {
  120. deviceString = "a";
  121. }
  122. else if (idiom.Equals(DeviceIdiom.Tablet))
  123. {
  124. deviceString = "A";
  125. }
  126. }
  127. GlobalVariables.DeviceString = deviceString;
  128. }
  129. protected override void OnStart()
  130. {
  131. MessagingCenter.Send<App>(this, MessageOnStart);
  132. IsInForeground = true;
  133. }
  134. protected override void OnSleep()
  135. {
  136. MessagingCenter.Send<App>(this, MessageOnSleep);
  137. IsInForeground = false;
  138. }
  139. protected override void OnResume()
  140. {
  141. MessagingCenter.Send<App>(this, MessageOnResume);
  142. IsInForeground = true;
  143. }
  144. }
  145. }