App.xaml.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. DBSettings = new DatabaseSettings();
  126. Settings = new LocalConfiguration<ConnectionSettings>().Load();
  127. DBSettings = new LocalConfiguration<DatabaseSettings>().Load();
  128. if (!string.IsNullOrWhiteSpace(Settings.UserID) && string.IsNullOrWhiteSpace(DBSettings.UserID))
  129. DBSettings.UserID = Settings.UserID;
  130. if (!string.IsNullOrWhiteSpace(Settings.Password) && string.IsNullOrWhiteSpace(DBSettings.Password))
  131. DBSettings.Password = Settings.Password;
  132. if (string.IsNullOrWhiteSpace(DBSettings.UserID)
  133. && string.IsNullOrWhiteSpace(DBSettings.Password)
  134. && DBUrslBlank())
  135. ApplyDemoSettings();
  136. if (DBUrslBlank())
  137. ApplyDemoURLs();
  138. }
  139. catch { }
  140. }
  141. private void ApplyDemoSettings()
  142. {
  143. DBSettings.UserID = "GUEST";
  144. DBSettings.Password = "guest";
  145. ApplyDemoURLs();
  146. }
  147. private void ApplyDemoURLs()
  148. {
  149. DBSettings.URLs[0] = "remote.com-al.com.au:8000";
  150. }
  151. private bool DBUrslBlank()
  152. {
  153. if (DBSettings.URLs.Count() == 0 || string.IsNullOrWhiteSpace(DBSettings.URLs[0]))
  154. return true;
  155. return false;
  156. }
  157. private void InitAndRegister()
  158. {
  159. try
  160. {
  161. Material.Init(this);
  162. Material.Use("Material.Configuration");
  163. MobileUtils.Init();
  164. CoreUtils.RegisterClasses();
  165. ComalUtils.RegisterClasses();
  166. FindDeviceInfo();
  167. }
  168. catch { }
  169. }
  170. private void RunTimers()
  171. {
  172. try
  173. {
  174. GPS.GetLocation();
  175. }
  176. catch
  177. {
  178. }
  179. try
  180. {
  181. Bluetooth.ScanForDevices();
  182. }
  183. catch { }
  184. try
  185. {
  186. Device.StartTimer(new TimeSpan(0, 0, 30), () =>
  187. {
  188. if (App.IsInForeground)
  189. {
  190. GPS.GetLocation();
  191. }
  192. return true;
  193. });
  194. Device.StartTimer(new TimeSpan(0, 0, 30), () =>
  195. {
  196. if (App.IsInForeground)
  197. {
  198. Bluetooth.ScanForDevices();
  199. }
  200. return true;
  201. });
  202. }
  203. catch { }
  204. }
  205. private async void TryLoadFromSecureCache()
  206. {
  207. try
  208. {
  209. Settings.URL = await SecureStorage.GetAsync(GlobalVariables.CacheURLString);
  210. Settings.Port = int.Parse(await SecureStorage.GetAsync(GlobalVariables.CachePortString));
  211. Settings.UserID = await SecureStorage.GetAsync(GlobalVariables.CacheUserIDString);
  212. Settings.Password = await SecureStorage.GetAsync(GlobalVariables.CachePasswordString);
  213. }
  214. catch { }
  215. }
  216. private void FindDeviceInfo()
  217. {
  218. try
  219. {
  220. var idiom = DeviceInfo.Idiom;
  221. if (Device.RuntimePlatform.Equals(Device.iOS))
  222. {
  223. if (idiom.Equals(DeviceIdiom.Phone))
  224. {
  225. DeviceString = "i";
  226. }
  227. else if (idiom.Equals(DeviceIdiom.Tablet))
  228. {
  229. DeviceString = "I";
  230. }
  231. }
  232. else if (Device.RuntimePlatform.Equals(Device.Android))
  233. {
  234. if (idiom.Equals(DeviceIdiom.Phone))
  235. {
  236. DeviceString = "a";
  237. }
  238. else if (idiom.Equals(DeviceIdiom.Tablet))
  239. {
  240. DeviceString = "A";
  241. }
  242. }
  243. GlobalVariables.DeviceString = DeviceString;
  244. }
  245. catch { }
  246. }
  247. protected override void OnStart()
  248. {
  249. MessagingCenter.Send<App>(this, MessageOnStart);
  250. IsInForeground = true;
  251. }
  252. protected override void OnSleep()
  253. {
  254. MessagingCenter.Send<App>(this, MessageOnSleep);
  255. IsInForeground = false;
  256. }
  257. protected override void OnResume()
  258. {
  259. MessagingCenter.Send<App>(this, MessageOnResume);
  260. IsInForeground = true;
  261. }
  262. }
  263. public class ConnectionSettings : ILocalConfigurationSettings
  264. {
  265. public string URL { get; set; }
  266. public int Port { get; set; }
  267. public SerializerProtocol Protocol { get; set; }
  268. public string UserID { get; set; }
  269. public string Password { get; set; }
  270. }
  271. }