App.xaml.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. public static 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. InitializeComponent();
  42. LoadAll();
  43. }
  44. private void LoadAll(bool reload = false)
  45. {
  46. try
  47. {
  48. InitAndRegister();
  49. LoadSettings();
  50. SaveSettings();
  51. SetupClient();
  52. FinishSetup();
  53. LaunchLoginPage();
  54. }
  55. catch
  56. {
  57. LaunchLoginPage();
  58. }
  59. }
  60. private void LaunchLoginPage()
  61. {
  62. MainPage = new MaterialNavigationPage(new PINLoginPage());
  63. }
  64. private void FinishSetup()
  65. {
  66. GlobalVariables.InternalOnAppearing = true;
  67. GlobalVariables.ChangeUser = false;
  68. RunTimers();
  69. }
  70. private void SetupClient()
  71. {
  72. if (CheckLoadFromLink())
  73. return;
  74. string result = "";
  75. while (string.IsNullOrWhiteSpace(result))
  76. result = TryPing();
  77. TrySetClientType(result);
  78. }
  79. private void TrySetClientType(string result)
  80. {
  81. try
  82. {
  83. ClientFactory.SetClientType(typeof(JsonClient<>), "TimeBench", MobileUtils.AppVersion.InstalledVersionNumber + App.DeviceString, result, true);
  84. }
  85. catch (Exception ex)
  86. {
  87. var log = new MobileLogging(LogType.BackgroundProcess, "Client Factory SetClientType", ex.Message + ex.StackTrace, this.GetType().Name);
  88. TrySetClientType(result);
  89. }
  90. }
  91. private string TryPing()
  92. {
  93. try
  94. {
  95. return JsonClient<User>.Ping(App.DBSettings.URLs, out DatabaseInfo info);
  96. }
  97. catch (Exception ex)
  98. {
  99. var log = new MobileLogging(LogType.BackgroundProcess, "JsonClient.Ping()", ex.Message + ex.StackTrace, this.GetType().Name);
  100. }
  101. return "";
  102. }
  103. private bool CheckLoadFromLink()
  104. {
  105. if (!string.IsNullOrWhiteSpace(GlobalVariables.LoadFromLinkString))
  106. {
  107. MobileUtils.LoadFromLink();
  108. return true;
  109. }
  110. return false;
  111. }
  112. private void SaveSettings()
  113. {
  114. try
  115. {
  116. new LocalConfiguration<DatabaseSettings>().Save(DBSettings);
  117. MobileUtils.SaveToSecureStorage();
  118. }
  119. catch { }
  120. }
  121. private void LoadSettings()
  122. {
  123. try
  124. {
  125. Settings = new LocalConfiguration<ConnectionSettings>().Load();
  126. DBSettings = new LocalConfiguration<DatabaseSettings>().Load();
  127. if (!string.IsNullOrWhiteSpace(Settings.UserID) && string.IsNullOrWhiteSpace(DBSettings.UserID))
  128. DBSettings.UserID = Settings.UserID;
  129. if (!string.IsNullOrWhiteSpace(Settings.Password) && string.IsNullOrWhiteSpace(DBSettings.Password))
  130. DBSettings.Password = Settings.Password;
  131. }
  132. catch { }
  133. }
  134. private void InitAndRegister()
  135. {
  136. try
  137. {
  138. Material.Init(this);
  139. Material.Use("Material.Configuration");
  140. MobileUtils.Init();
  141. CoreUtils.RegisterClasses();
  142. ComalUtils.RegisterClasses();
  143. FindDeviceInfo();
  144. }
  145. catch { }
  146. }
  147. private void RunTimers()
  148. {
  149. try
  150. {
  151. GPS.GetLocation();
  152. }
  153. catch
  154. {
  155. }
  156. try
  157. {
  158. Bluetooth.ScanForDevices();
  159. }
  160. catch { }
  161. try
  162. {
  163. Device.StartTimer(new TimeSpan(0, 0, 30), () =>
  164. {
  165. if (App.IsInForeground)
  166. {
  167. GPS.GetLocation();
  168. }
  169. return true;
  170. });
  171. Device.StartTimer(new TimeSpan(0, 0, 30), () =>
  172. {
  173. if (App.IsInForeground)
  174. {
  175. Bluetooth.ScanForDevices();
  176. }
  177. return true;
  178. });
  179. }
  180. catch { }
  181. }
  182. private async void TryLoadFromSecureCache()
  183. {
  184. try
  185. {
  186. Settings.URL = await SecureStorage.GetAsync(GlobalVariables.CacheURLString);
  187. Settings.Port = int.Parse(await SecureStorage.GetAsync(GlobalVariables.CachePortString));
  188. Settings.UserID = await SecureStorage.GetAsync(GlobalVariables.CacheUserIDString);
  189. Settings.Password = await SecureStorage.GetAsync(GlobalVariables.CachePasswordString);
  190. }
  191. catch { }
  192. }
  193. private void FindDeviceInfo()
  194. {
  195. try
  196. {
  197. var idiom = DeviceInfo.Idiom;
  198. if (Device.RuntimePlatform.Equals(Device.iOS))
  199. {
  200. if (idiom.Equals(DeviceIdiom.Phone))
  201. {
  202. DeviceString = "i";
  203. }
  204. else if (idiom.Equals(DeviceIdiom.Tablet))
  205. {
  206. DeviceString = "I";
  207. }
  208. }
  209. else if (Device.RuntimePlatform.Equals(Device.Android))
  210. {
  211. if (idiom.Equals(DeviceIdiom.Phone))
  212. {
  213. DeviceString = "a";
  214. }
  215. else if (idiom.Equals(DeviceIdiom.Tablet))
  216. {
  217. DeviceString = "A";
  218. }
  219. }
  220. GlobalVariables.DeviceString = DeviceString;
  221. }
  222. catch { }
  223. }
  224. protected override void OnStart()
  225. {
  226. MessagingCenter.Send<App>(this, MessageOnStart);
  227. IsInForeground = true;
  228. }
  229. protected override void OnSleep()
  230. {
  231. MessagingCenter.Send<App>(this, MessageOnSleep);
  232. IsInForeground = false;
  233. }
  234. protected override void OnResume()
  235. {
  236. MessagingCenter.Send<App>(this, MessageOnResume);
  237. IsInForeground = true;
  238. }
  239. }
  240. public class ConnectionSettings : ILocalConfigurationSettings
  241. {
  242. public string URL { get; set; }
  243. public int Port { get; set; }
  244. public SerializerProtocol Protocol { get; set; }
  245. public string UserID { get; set; }
  246. public string Password { get; set; }
  247. }
  248. }