App.xaml.cs 8.4 KB

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