App.xaml.cs 7.7 KB

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