App.xaml.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. using Java.Util;
  18. using System.Linq;
  19. using System.Collections.Generic;
  20. //[assembly: XamlCompilation(XamlCompilationOptions.Compile)]
  21. namespace comal.timesheets
  22. {
  23. public partial class App : Application
  24. {
  25. string deviceString = "";
  26. public static bool IsUserLoggedIn => ClientFactory.UserGuid != Guid.Empty;
  27. public static void LogoutUser()
  28. {
  29. ClientFactory.InvalidateUser();
  30. }
  31. public static LocationServices GPS { get; } = new LocationServices() { ScanDelay = new TimeSpan(0, 1, 0) };
  32. public static Bluetooth Bluetooth { get; } = new Bluetooth() { ScanDelay = new TimeSpan(0, 1, 0) };
  33. public static DataModel Data { get; } = new DataModel();
  34. public static bool IsInForeground { get; set; } = false;
  35. public const string MessageOnStart = "OnStart";
  36. public const string MessageOnSleep = "OnSleep";
  37. public const string MessageOnResume = "OnResume";
  38. public static ConnectionSettings Settings = null;
  39. public static DatabaseSettings DBSettings = null;
  40. public App()
  41. {
  42. LoadAll();
  43. }
  44. private void LoadAll(bool reload = false)
  45. {
  46. try
  47. {
  48. Material.Init(this);
  49. InitializeComponent();
  50. Material.Use("Material.Configuration");
  51. MobileUtils.Init();
  52. CoreUtils.RegisterClasses();
  53. ComalUtils.RegisterClasses();
  54. FindDeviceInfo();
  55. Settings = new LocalConfiguration<ConnectionSettings>().Load();
  56. DBSettings = new LocalConfiguration<DatabaseSettings>().Load();
  57. if (DBSettings.URLs.Count() == 0 || string.IsNullOrWhiteSpace(DBSettings.URLs[0]))
  58. {
  59. if (string.IsNullOrWhiteSpace(Settings.URL))
  60. TryLoadFromSecureCache();
  61. if (string.IsNullOrWhiteSpace(Settings.URL))
  62. MobileUtils.LoadDemoSettings(DBSettings);
  63. else
  64. {
  65. DBSettings.URLs[0] = Settings.URL + ":" + Settings.Port;
  66. if (Settings.URL.ToUpper().Contains("COM-AL"))
  67. {
  68. List<string> list = new List<string>();
  69. list.Add(DBSettings.URLs[0]);
  70. list.Add("http://remote2.com-al.com.au:" + Settings.Port);
  71. list.Add("http://remote3.com-al.com.au:" + Settings.Port);
  72. DBSettings.URLs = list.ToArray();
  73. }
  74. DBSettings.UserID = Settings.UserID;
  75. DBSettings.Password = Settings.Password;
  76. }
  77. }
  78. if (DBSettings.URLs.Count() > 0)
  79. {
  80. new LocalConfiguration<DatabaseSettings>().Save(DBSettings);
  81. MobileUtils.SaveToSecureStorage();
  82. }
  83. //if (!string.IsNullOrWhiteSpace(GlobalVariables.LoadFromLinkString))
  84. //{
  85. // MobileUtils.LoadFromLink();
  86. //}
  87. var result = JsonClient<User>.Ping(DBSettings.URLs, out DatabaseInfo info);
  88. ClientFactory.SetClientType(typeof(JsonClient<>), "TimeBench", MobileUtils.AppVersion.InstalledVersionNumber + deviceString, result, true);
  89. GlobalVariables.InternalOnAppearing = true;
  90. GlobalVariables.ChangeUser = false;
  91. if (!reload)
  92. RunTimers();
  93. MainPage = new MaterialNavigationPage(new PINLoginPage());
  94. }
  95. catch
  96. {
  97. Thread.Sleep(1000);
  98. LoadAll(true);
  99. }
  100. }
  101. private void RunTimers()
  102. {
  103. Task.Run(() =>
  104. {
  105. GPS.GetLocation();
  106. Bluetooth.ScanForDevices();
  107. Device.StartTimer(new TimeSpan(0, 0, 30), () =>
  108. {
  109. if (App.IsInForeground)
  110. {
  111. GPS.GetLocation();
  112. }
  113. return true;
  114. });
  115. Device.StartTimer(new TimeSpan(0, 0, 30), () =>
  116. {
  117. if (App.IsInForeground)
  118. {
  119. Bluetooth.ScanForDevices();
  120. }
  121. return true;
  122. });
  123. });
  124. }
  125. private async void TryLoadFromSecureCache()
  126. {
  127. try
  128. {
  129. Settings.URL = await SecureStorage.GetAsync(GlobalVariables.CacheURLString);
  130. Settings.Port = int.Parse(await SecureStorage.GetAsync(GlobalVariables.CachePortString));
  131. Settings.UserID = await SecureStorage.GetAsync(GlobalVariables.CacheUserIDString);
  132. Settings.Password = await SecureStorage.GetAsync(GlobalVariables.CachePasswordString);
  133. }
  134. catch { }
  135. }
  136. private void FindDeviceInfo()
  137. {
  138. try
  139. {
  140. var idiom = DeviceInfo.Idiom;
  141. if (Device.RuntimePlatform.Equals(Device.iOS))
  142. {
  143. if (idiom.Equals(DeviceIdiom.Phone))
  144. {
  145. deviceString = "i";
  146. }
  147. else if (idiom.Equals(DeviceIdiom.Tablet))
  148. {
  149. deviceString = "I";
  150. }
  151. }
  152. else if (Device.RuntimePlatform.Equals(Device.Android))
  153. {
  154. if (idiom.Equals(DeviceIdiom.Phone))
  155. {
  156. deviceString = "a";
  157. }
  158. else if (idiom.Equals(DeviceIdiom.Tablet))
  159. {
  160. deviceString = "A";
  161. }
  162. }
  163. GlobalVariables.DeviceString = deviceString;
  164. }
  165. catch { }
  166. }
  167. protected override void OnStart()
  168. {
  169. MessagingCenter.Send<App>(this, MessageOnStart);
  170. IsInForeground = true;
  171. }
  172. protected override void OnSleep()
  173. {
  174. MessagingCenter.Send<App>(this, MessageOnSleep);
  175. IsInForeground = false;
  176. }
  177. protected override void OnResume()
  178. {
  179. MessagingCenter.Send<App>(this, MessageOnResume);
  180. IsInForeground = true;
  181. }
  182. }
  183. public class ConnectionSettings : ILocalConfigurationSettings
  184. {
  185. public string URL { get; set; }
  186. public int Port { get; set; }
  187. public SerializerProtocol Protocol { get; set; }
  188. public string UserID { get; set; }
  189. public string Password { get; set; }
  190. }
  191. }