App.xaml.cs 9.2 KB

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