App.xaml.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading.Tasks;
  4. using Comal.Classes;
  5. using InABox.Configuration;
  6. using InABox.Core;
  7. using InABox.Mobile;
  8. using Xamarin.Forms;
  9. using XF.Material.Forms;
  10. using XF.Material.Forms.UI;
  11. using System.Threading;
  12. using System.Linq;
  13. using InABox.Clients;
  14. using InABox.Rpc;
  15. using Xamarin.Essentials;
  16. using XF.Material.Forms.Resources;
  17. //[assembly: XamlCompilation(XamlCompilationOptions.Compile)]
  18. namespace comal.timesheets
  19. {
  20. public enum TransportStatus
  21. {
  22. BadConfiguration,
  23. NoConnection,
  24. OK,
  25. None
  26. }
  27. public partial class App : Application
  28. {
  29. public static string LaunchParameters { get; set; }
  30. public static IRpcClientTransport Transport { get; set; }
  31. public static TransportStatus ConnectTransport(string[] urls)
  32. {
  33. if (Transport?.IsConnected() == true)
  34. Transport.Disconnect();
  35. Transport = null;
  36. if (!urls.Any())
  37. return TransportStatus.BadConfiguration;
  38. var transport = new RpcClientSocketTransport(urls);
  39. transport.Connect();
  40. if (!transport.IsConnected())
  41. return TransportStatus.NoConnection;
  42. Transport = transport;
  43. ClientFactory.SetClientType(typeof(RpcClient<>), InABox.Core.Platform.TimeBench, MobileUtils.AppVersion.InstalledVersionNumber + App.DeviceString, Transport);
  44. return TransportStatus.OK;
  45. }
  46. public static LocationServices GPS { get; } = new LocationServices() { ScanDelay = new TimeSpan(0, 1, 0) };
  47. public static Bluetooth Bluetooth { get; } = new Bluetooth() { ScanDelay = new TimeSpan(0, 1, 0) };
  48. public static DataModel Data { get; } = new DataModel();
  49. public static string DeviceString
  50. {
  51. get
  52. {
  53. var result = Device.RuntimePlatform.Substring(0, 1).ToUpper();
  54. if (Device.Idiom == TargetIdiom.Phone)
  55. result = result.ToLower();
  56. return result;
  57. }
  58. }
  59. public static double GetXamarinWidth()
  60. {
  61. var mainDisplayInfo = DeviceDisplay.MainDisplayInfo;
  62. var width = mainDisplayInfo.Width;
  63. return width / mainDisplayInfo.Density;
  64. }
  65. public static bool IsInForeground { get; set; } = false;
  66. // this is set by PINLoginPage if DatabaseSettings.UserID is blank
  67. // Effective, this will enable the "back" (-> logout) button on the Main page
  68. // if the userid is not set.
  69. public static bool IsSharedDevice { get; set; }
  70. public const string MessageOnStart = "OnStart";
  71. public const string MessageOnSleep = "OnSleep";
  72. public const string MessageOnResume = "OnResume";
  73. public App()
  74. {
  75. InitializeComponent();
  76. Material.Init(this);
  77. Material.Use("Material.Configuration");
  78. MobileUtils.Init();
  79. CoreUtils.RegisterClasses();
  80. ComalUtils.RegisterClasses();
  81. StartMonitoringGPS();
  82. StartMonitoringBlueTooth();
  83. // PINLoginPage is going to be the root navigation page.
  84. // If shown, it will rerun the validation process
  85. // So we should be able to get to this page from
  86. // 1. App Startup => Autologin
  87. // 2. SettingsPage.Save() => AutoLogin
  88. // 3. Main Window => Always manual login?
  89. MainPage = new MaterialNavigationPage(new PinLoginPage());
  90. }
  91. private void StartMonitoringGPS()
  92. {
  93. Task.Run(() =>
  94. {
  95. while (true)
  96. {
  97. if (App.IsInForeground)
  98. {
  99. try
  100. {
  101. GPS.GetLocation();
  102. }
  103. catch (Exception e)
  104. {
  105. // Need to Log this
  106. }
  107. }
  108. Thread.Sleep(TimeSpan.FromSeconds(30));
  109. }
  110. });
  111. }
  112. private void StartMonitoringBlueTooth()
  113. {
  114. Task.Run(() =>
  115. {
  116. while (true)
  117. {
  118. if (App.IsInForeground)
  119. {
  120. try
  121. {
  122. Bluetooth.ScanForDevices();
  123. }
  124. catch (Exception e)
  125. {
  126. // Need to Log this
  127. }
  128. }
  129. Thread.Sleep(TimeSpan.FromSeconds(30));
  130. }
  131. });
  132. }
  133. protected override void OnStart()
  134. {
  135. MessagingCenter.Send<App>(this, MessageOnStart);
  136. IsInForeground = true;
  137. }
  138. protected override void OnSleep()
  139. {
  140. MessagingCenter.Send<App>(this, MessageOnSleep);
  141. IsInForeground = false;
  142. }
  143. protected override void OnResume()
  144. {
  145. MessagingCenter.Send<App>(this, MessageOnResume);
  146. IsInForeground = true;
  147. }
  148. }
  149. }