App.xaml.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. if (Settings.URL.ToUpper().Contains("COM-AL"))
  66. {
  67. List<string> list = new List<string>();
  68. list.Add("http://remote.com-al.com.au:)" + Settings.Port);
  69. list.Add("http://remote2.com-al.com.au:" + Settings.Port);
  70. list.Add("http://remote3.com-al.com.au:" + Settings.Port);
  71. DBSettings.URLs = list.ToArray();
  72. }
  73. DBSettings.UserID = Settings.UserID;
  74. DBSettings.Password = Settings.Password;
  75. }
  76. }
  77. if (DBSettings.URLs.Count() > 0)
  78. {
  79. if (DBSettings.URLs[0].ToUpper().Contains("COM-AL"))
  80. {
  81. List<string> list = new List<string>();
  82. list.Add("http://remote.com-al.com.au:)" + Settings.Port);
  83. list.Add("http://remote2.com-al.com.au:" + Settings.Port);
  84. list.Add("http://remote3.com-al.com.au:" + Settings.Port);
  85. DBSettings.URLs = list.ToArray();
  86. }
  87. new LocalConfiguration<DatabaseSettings>().Save(DBSettings);
  88. MobileUtils.SaveToSecureStorage();
  89. }
  90. //if (!string.IsNullOrWhiteSpace(GlobalVariables.LoadFromLinkString))
  91. //{
  92. // MobileUtils.LoadFromLink();
  93. //}
  94. var result = JsonClient<User>.Ping(DBSettings.URLs, out DatabaseInfo info);
  95. ClientFactory.SetClientType(typeof(JsonClient<>), "TimeBench", MobileUtils.AppVersion.InstalledVersionNumber + deviceString, result, true);
  96. GlobalVariables.InternalOnAppearing = true;
  97. GlobalVariables.ChangeUser = false;
  98. if (!reload)
  99. RunTimers();
  100. MainPage = new MaterialNavigationPage(new PINLoginPage());
  101. }
  102. catch
  103. {
  104. Thread.Sleep(1000);
  105. LoadAll(true);
  106. }
  107. }
  108. private void RunTimers()
  109. {
  110. Task.Run(() =>
  111. {
  112. GPS.GetLocation();
  113. Bluetooth.ScanForDevices();
  114. Device.StartTimer(new TimeSpan(0, 0, 30), () =>
  115. {
  116. if (App.IsInForeground)
  117. {
  118. GPS.GetLocation();
  119. }
  120. return true;
  121. });
  122. Device.StartTimer(new TimeSpan(0, 0, 30), () =>
  123. {
  124. if (App.IsInForeground)
  125. {
  126. Bluetooth.ScanForDevices();
  127. }
  128. return true;
  129. });
  130. });
  131. }
  132. private async void TryLoadFromSecureCache()
  133. {
  134. try
  135. {
  136. Settings.URL = await SecureStorage.GetAsync(GlobalVariables.CacheURLString);
  137. Settings.Port = int.Parse(await SecureStorage.GetAsync(GlobalVariables.CachePortString));
  138. Settings.UserID = await SecureStorage.GetAsync(GlobalVariables.CacheUserIDString);
  139. Settings.Password = await SecureStorage.GetAsync(GlobalVariables.CachePasswordString);
  140. }
  141. catch { }
  142. }
  143. private void FindDeviceInfo()
  144. {
  145. try
  146. {
  147. var idiom = DeviceInfo.Idiom;
  148. if (Device.RuntimePlatform.Equals(Device.iOS))
  149. {
  150. if (idiom.Equals(DeviceIdiom.Phone))
  151. {
  152. deviceString = "i";
  153. }
  154. else if (idiom.Equals(DeviceIdiom.Tablet))
  155. {
  156. deviceString = "I";
  157. }
  158. }
  159. else if (Device.RuntimePlatform.Equals(Device.Android))
  160. {
  161. if (idiom.Equals(DeviceIdiom.Phone))
  162. {
  163. deviceString = "a";
  164. }
  165. else if (idiom.Equals(DeviceIdiom.Tablet))
  166. {
  167. deviceString = "A";
  168. }
  169. }
  170. GlobalVariables.DeviceString = deviceString;
  171. }
  172. catch { }
  173. }
  174. protected override void OnStart()
  175. {
  176. MessagingCenter.Send<App>(this, MessageOnStart);
  177. IsInForeground = true;
  178. }
  179. protected override void OnSleep()
  180. {
  181. MessagingCenter.Send<App>(this, MessageOnSleep);
  182. IsInForeground = false;
  183. }
  184. protected override void OnResume()
  185. {
  186. MessagingCenter.Send<App>(this, MessageOnResume);
  187. IsInForeground = true;
  188. }
  189. }
  190. public class ConnectionSettings : ILocalConfigurationSettings
  191. {
  192. public string URL { get; set; }
  193. public int Port { get; set; }
  194. public SerializerProtocol Protocol { get; set; }
  195. public string UserID { get; set; }
  196. public string Password { get; set; }
  197. }
  198. }