App.xaml.cs 7.8 KB

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