App.xaml.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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. using InABox.Clients;
  20. //[assembly: XamlCompilation(XamlCompilationOptions.Compile)]
  21. namespace comal.timesheets
  22. {
  23. public partial class App : Application
  24. {
  25. public static 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. InitializeComponent();
  43. LoadAll();
  44. }
  45. private void LoadAll(bool reload = false)
  46. {
  47. try
  48. {
  49. InitAndRegister();
  50. LoadSettings();
  51. SaveSettings();
  52. SetupClient();
  53. FinishSetup();
  54. LaunchLoginPage();
  55. }
  56. catch
  57. {
  58. LaunchLoginPage();
  59. }
  60. }
  61. private void LaunchLoginPage()
  62. {
  63. MainPage = new MaterialNavigationPage(new PINLoginPage());
  64. }
  65. private void FinishSetup()
  66. {
  67. GlobalVariables.InternalOnAppearing = true;
  68. GlobalVariables.ChangeUser = false;
  69. RunTimers();
  70. }
  71. private void SetupClient()
  72. {
  73. if (CheckLoadFromLink())
  74. return;
  75. string result = RestClient<User>.Ping(App.DBSettings.URLs, out DatabaseInfo info);
  76. TrySetClientType(result);
  77. }
  78. private void TrySetClientType(string result)
  79. {
  80. try
  81. {
  82. ClientFactory.SetClientType(typeof(RestClient<>), InABox.Core.Platform.Mobile, MobileUtils.AppVersion.InstalledVersionNumber + App.DeviceString, result, true);
  83. }
  84. catch (Exception ex)
  85. {
  86. var log = new MobileLogging(LogType.BackgroundProcess, "Client Factory SetClientType", ex.Message + ex.StackTrace, this.GetType().Name);
  87. TrySetClientType(result);
  88. }
  89. }
  90. private string TryPing()
  91. {
  92. try
  93. {
  94. return RestClient<User>.Ping(App.DBSettings.URLs, out DatabaseInfo info);
  95. }
  96. catch (Exception ex)
  97. {
  98. var log = new MobileLogging(LogType.BackgroundProcess, "JsonClient.Ping()", ex.Message + ex.StackTrace, this.GetType().Name);
  99. }
  100. return "";
  101. }
  102. private bool CheckLoadFromLink()
  103. {
  104. if (!string.IsNullOrWhiteSpace(GlobalVariables.LoadFromLinkString))
  105. {
  106. MobileUtils.LoadFromLink();
  107. return true;
  108. }
  109. return false;
  110. }
  111. private void SaveSettings()
  112. {
  113. try
  114. {
  115. new LocalConfiguration<DatabaseSettings>().Save(DBSettings);
  116. MobileUtils.SaveToSecureStorage();
  117. }
  118. catch { }
  119. }
  120. private void LoadSettings()
  121. {
  122. try
  123. {
  124. DBSettings = new DatabaseSettings();
  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. //ApplyTestSettings();
  132. if (string.IsNullOrWhiteSpace(DBSettings.UserID)
  133. && string.IsNullOrWhiteSpace(DBSettings.Password)
  134. && DBUrlsBlank())
  135. ApplyDemoSettings();
  136. if (DBUrlsBlank())
  137. ApplyDemoURLS();
  138. }
  139. catch { }
  140. }
  141. private void ApplyTestSettings()
  142. {
  143. DBSettings.UserID = "TAN";
  144. DBSettings.Password = "nictan";
  145. DBSettings.URLs[0] = "remote.com-al.com.au:8000";
  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 void ApplyDemoSettings()
  154. {
  155. DBSettings.UserID = "GUEST";
  156. DBSettings.Password = "guest";
  157. ApplyDemoURLS();
  158. }
  159. private bool DBUrlsBlank()
  160. {
  161. if (DBSettings.URLs.Count() == 0 || string.IsNullOrWhiteSpace(DBSettings.URLs[0]))
  162. return true;
  163. else return false;
  164. }
  165. private void InitAndRegister()
  166. {
  167. try
  168. {
  169. Material.Init(this);
  170. Material.Use("Material.Configuration");
  171. MobileUtils.Init();
  172. CoreUtils.RegisterClasses();
  173. ComalUtils.RegisterClasses();
  174. FindDeviceInfo();
  175. }
  176. catch { }
  177. }
  178. private void RunTimers()
  179. {
  180. try
  181. {
  182. GPS.GetLocation();
  183. }
  184. catch
  185. {
  186. }
  187. try
  188. {
  189. Bluetooth.ScanForDevices();
  190. }
  191. catch { }
  192. try
  193. {
  194. Device.StartTimer(new TimeSpan(0, 0, 30), () =>
  195. {
  196. if (App.IsInForeground)
  197. {
  198. GPS.GetLocation();
  199. }
  200. return true;
  201. });
  202. Device.StartTimer(new TimeSpan(0, 0, 30), () =>
  203. {
  204. if (App.IsInForeground)
  205. {
  206. Bluetooth.ScanForDevices();
  207. }
  208. return true;
  209. });
  210. }
  211. catch { }
  212. }
  213. private async void TryLoadFromSecureCache()
  214. {
  215. try
  216. {
  217. Settings.URL = await SecureStorage.GetAsync(GlobalVariables.CacheURLString);
  218. Settings.Port = int.Parse(await SecureStorage.GetAsync(GlobalVariables.CachePortString));
  219. Settings.UserID = await SecureStorage.GetAsync(GlobalVariables.CacheUserIDString);
  220. Settings.Password = await SecureStorage.GetAsync(GlobalVariables.CachePasswordString);
  221. }
  222. catch { }
  223. }
  224. private void FindDeviceInfo()
  225. {
  226. try
  227. {
  228. var idiom = DeviceInfo.Idiom;
  229. if (Device.RuntimePlatform.Equals(Device.iOS))
  230. {
  231. if (idiom.Equals(DeviceIdiom.Phone))
  232. {
  233. DeviceString = "i";
  234. }
  235. else if (idiom.Equals(DeviceIdiom.Tablet))
  236. {
  237. DeviceString = "I";
  238. }
  239. }
  240. else if (Device.RuntimePlatform.Equals(Device.Android))
  241. {
  242. if (idiom.Equals(DeviceIdiom.Phone))
  243. {
  244. DeviceString = "a";
  245. }
  246. else if (idiom.Equals(DeviceIdiom.Tablet))
  247. {
  248. DeviceString = "A";
  249. }
  250. }
  251. GlobalVariables.DeviceString = DeviceString;
  252. }
  253. catch { }
  254. }
  255. protected override void OnStart()
  256. {
  257. MessagingCenter.Send<App>(this, MessageOnStart);
  258. IsInForeground = true;
  259. }
  260. protected override void OnSleep()
  261. {
  262. MessagingCenter.Send<App>(this, MessageOnSleep);
  263. IsInForeground = false;
  264. }
  265. protected override void OnResume()
  266. {
  267. MessagingCenter.Send<App>(this, MessageOnResume);
  268. IsInForeground = true;
  269. }
  270. }
  271. public class ConnectionSettings : ILocalConfigurationSettings
  272. {
  273. public string URL { get; set; }
  274. public int Port { get; set; }
  275. public SerializerProtocol Protocol { get; set; }
  276. public string UserID { get; set; }
  277. public string Password { get; set; }
  278. }
  279. }