using System; using System.Threading.Tasks; using Comal.Classes; using InABox.Clients; using InABox.Configuration; using InABox.Core; using InABox.Mobile; using Xamarin.Forms; using Xamarin.Forms.Xaml; using XF.Material.Forms; using XF.Material.Forms.Resources; using XF.Material.Forms.Resources.Typography; using XF.Material.Forms.UI; using Xamarin.Essentials; using SkiaSharp; using System.Threading; using System.Linq; using System.Collections.Generic; //[assembly: XamlCompilation(XamlCompilationOptions.Compile)] namespace comal.timesheets { public partial class App : Application { public static string DeviceString = ""; public static bool IsUserLoggedIn => ClientFactory.UserGuid != Guid.Empty; public static void LogoutUser() { ClientFactory.InvalidateUser(); } 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 bool IsInForeground { get; set; } = false; public const string MessageOnStart = "OnStart"; public const string MessageOnSleep = "OnSleep"; public const string MessageOnResume = "OnResume"; public static ConnectionSettings Settings = null; public static DatabaseSettings DBSettings = null; public App() { InitializeComponent(); LoadAll(); } private void LoadAll(bool reload = false) { try { InitAndRegister(); LoadSettings(); SaveSettings(); SetupClient(); FinishSetup(); LaunchLoginPage(); } catch { LaunchLoginPage(); } } private void LaunchLoginPage() { MainPage = new MaterialNavigationPage(new PINLoginPage()); } private void FinishSetup() { GlobalVariables.InternalOnAppearing = true; GlobalVariables.ChangeUser = false; RunTimers(); } private void SetupClient() { if (CheckLoadFromLink()) return; string result = ""; while (string.IsNullOrWhiteSpace(result)) result = TryPing(); TrySetClientType(result); } private void TrySetClientType(string result) { try { ClientFactory.SetClientType(typeof(JsonClient<>), "TimeBench", MobileUtils.AppVersion.InstalledVersionNumber + App.DeviceString, result, true); } catch (Exception ex) { var log = new MobileLogging(LogType.BackgroundProcess, "Client Factory SetClientType", ex.Message + ex.StackTrace, this.GetType().Name); TrySetClientType(result); } } private string TryPing() { try { return JsonClient.Ping(App.DBSettings.URLs, out DatabaseInfo info); } catch (Exception ex) { var log = new MobileLogging(LogType.BackgroundProcess, "JsonClient.Ping()", ex.Message + ex.StackTrace, this.GetType().Name); } return ""; } private bool CheckLoadFromLink() { if (!string.IsNullOrWhiteSpace(GlobalVariables.LoadFromLinkString)) { MobileUtils.LoadFromLink(); return true; } return false; } private void SaveSettings() { try { new LocalConfiguration().Save(DBSettings); MobileUtils.SaveToSecureStorage(); } catch { } } private void LoadSettings() { try { DBSettings = new DatabaseSettings(); Settings = new LocalConfiguration().Load(); DBSettings = new LocalConfiguration().Load(); if (!string.IsNullOrWhiteSpace(Settings.UserID) && string.IsNullOrWhiteSpace(DBSettings.UserID)) DBSettings.UserID = Settings.UserID; if (!string.IsNullOrWhiteSpace(Settings.Password) && string.IsNullOrWhiteSpace(DBSettings.Password)) DBSettings.Password = Settings.Password; if (string.IsNullOrWhiteSpace(DBSettings.UserID) && string.IsNullOrWhiteSpace(DBSettings.Password) && DBUrslBlank()) ApplyDemoSettings(); if (DBUrslBlank()) ApplyDemoURLs(); } catch { } } private void ApplyDemoSettings() { DBSettings.UserID = "GUEST"; DBSettings.Password = "guest"; ApplyDemoURLs(); } private void ApplyDemoURLs() { DBSettings.URLs[0] = "remote.com-al.com.au:8000"; } private bool DBUrslBlank() { if (DBSettings.URLs.Count() == 0 || string.IsNullOrWhiteSpace(DBSettings.URLs[0])) return true; return false; } private void InitAndRegister() { try { Material.Init(this); Material.Use("Material.Configuration"); MobileUtils.Init(); CoreUtils.RegisterClasses(); ComalUtils.RegisterClasses(); FindDeviceInfo(); } catch { } } private void RunTimers() { try { GPS.GetLocation(); } catch { } try { Bluetooth.ScanForDevices(); } catch { } try { Device.StartTimer(new TimeSpan(0, 0, 30), () => { if (App.IsInForeground) { GPS.GetLocation(); } return true; }); Device.StartTimer(new TimeSpan(0, 0, 30), () => { if (App.IsInForeground) { Bluetooth.ScanForDevices(); } return true; }); } catch { } } private async void TryLoadFromSecureCache() { try { Settings.URL = await SecureStorage.GetAsync(GlobalVariables.CacheURLString); Settings.Port = int.Parse(await SecureStorage.GetAsync(GlobalVariables.CachePortString)); Settings.UserID = await SecureStorage.GetAsync(GlobalVariables.CacheUserIDString); Settings.Password = await SecureStorage.GetAsync(GlobalVariables.CachePasswordString); } catch { } } private void FindDeviceInfo() { try { var idiom = DeviceInfo.Idiom; if (Device.RuntimePlatform.Equals(Device.iOS)) { if (idiom.Equals(DeviceIdiom.Phone)) { DeviceString = "i"; } else if (idiom.Equals(DeviceIdiom.Tablet)) { DeviceString = "I"; } } else if (Device.RuntimePlatform.Equals(Device.Android)) { if (idiom.Equals(DeviceIdiom.Phone)) { DeviceString = "a"; } else if (idiom.Equals(DeviceIdiom.Tablet)) { DeviceString = "A"; } } GlobalVariables.DeviceString = DeviceString; } catch { } } 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; } } public class ConnectionSettings : ILocalConfigurationSettings { public string URL { get; set; } public int Port { get; set; } public SerializerProtocol Protocol { get; set; } public string UserID { get; set; } public string Password { get; set; } } }