App.xaml.cs 9.1 KB

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