using System; using System.Collections.Generic; using System.Threading.Tasks; using Comal.Classes; using InABox.Configuration; using InABox.Core; using InABox.Mobile; using Xamarin.Forms; using XF.Material.Forms; using XF.Material.Forms.UI; using System.Threading; using System.Linq; using InABox.Clients; using InABox.Rpc; using Xamarin.Essentials; using XF.Material.Forms.Resources; //[assembly: XamlCompilation(XamlCompilationOptions.Compile)] namespace comal.timesheets { public enum TransportStatus { BadConfiguration, NoConnection, OK, None } public partial class App : Application { public static string LaunchParameters { get; set; } public static IRpcClientTransport Transport { get; set; } public static TransportStatus ConnectTransport(string[] urls) { if (Transport?.IsConnected() == true) Transport.Disconnect(); Transport = null; if (!urls.Any()) return TransportStatus.BadConfiguration; var transport = new RpcClientSocketTransport(urls); transport.Connect(); if (!transport.IsConnected()) return TransportStatus.NoConnection; Transport = transport; ClientFactory.SetClientType(typeof(RpcClient<>), InABox.Core.Platform.TimeBench, MobileUtils.AppVersion.InstalledVersionNumber + App.DeviceString, Transport); return TransportStatus.OK; } public static LocationServices GPS { get; } = new LocationServices() { ScanDelay = new TimeSpan(0, 1, 0) }; public static Bluetooth Bluetooth { get; } = new Bluetooth() { ScanDelay = new TimeSpan(0, 1, 0) }; public static DataModel Data { get; } = new DataModel(); public static string DeviceString { get { var result = Device.RuntimePlatform.Substring(0, 1).ToUpper(); if (Device.Idiom == TargetIdiom.Phone) result = result.ToLower(); return result; } } public static double GetXamarinWidth() { var mainDisplayInfo = DeviceDisplay.MainDisplayInfo; var width = mainDisplayInfo.Width; return width / mainDisplayInfo.Density; } public static bool IsInForeground { get; set; } = false; // this is set by PINLoginPage if DatabaseSettings.UserID is blank // Effective, this will enable the "back" (-> logout) button on the Main page // if the userid is not set. public static bool IsSharedDevice { get; set; } public const string MessageOnStart = "OnStart"; public const string MessageOnSleep = "OnSleep"; public const string MessageOnResume = "OnResume"; public App() { InitializeComponent(); Material.Init(this); Material.Use("Material.Configuration"); MobileUtils.Init(); CoreUtils.RegisterClasses(); ComalUtils.RegisterClasses(); StartMonitoringGPS(); StartMonitoringBlueTooth(); // PINLoginPage is going to be the root navigation page. // If shown, it will rerun the validation process // So we should be able to get to this page from // 1. App Startup => Autologin // 2. SettingsPage.Save() => AutoLogin // 3. Main Window => Always manual login? MainPage = new MaterialNavigationPage(new PinLoginPage()); } private void StartMonitoringGPS() { Task.Run(() => { while (true) { if (App.IsInForeground) { try { GPS.GetLocation(); } catch (Exception e) { // Need to Log this } } Thread.Sleep(TimeSpan.FromSeconds(30)); } }); } private void StartMonitoringBlueTooth() { Task.Run(() => { while (true) { if (App.IsInForeground) { try { Bluetooth.ScanForDevices(); } catch (Exception e) { // Need to Log this } } Thread.Sleep(TimeSpan.FromSeconds(30)); } }); } protected override void OnStart() { MessagingCenter.Send(this, MessageOnStart); IsInForeground = true; } protected override void OnSleep() { MessagingCenter.Send(this, MessageOnSleep); IsInForeground = false; } protected override void OnResume() { MessagingCenter.Send(this, MessageOnResume); IsInForeground = true; } } }