123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234 |
- 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 Java.Util;
- using System.Linq;
- using System.Collections.Generic;
- //[assembly: XamlCompilation(XamlCompilationOptions.Compile)]
- namespace comal.timesheets
- {
- public partial class App : Application
- {
- 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()
- {
- LoadAll();
- }
- private void LoadAll(bool reload = false)
- {
- try
- {
- Material.Init(this);
- InitializeComponent();
- Material.Use("Material.Configuration");
- MobileUtils.Init();
- CoreUtils.RegisterClasses();
- ComalUtils.RegisterClasses();
- FindDeviceInfo();
- Settings = new LocalConfiguration<ConnectionSettings>().Load();
- DBSettings = new LocalConfiguration<DatabaseSettings>().Load();
- if (DBSettings.URLs.Count() == 0 || string.IsNullOrWhiteSpace(DBSettings.URLs[0]))
- {
- if (string.IsNullOrWhiteSpace(Settings.URL))
- TryLoadFromSecureCache();
- if (string.IsNullOrWhiteSpace(Settings.URL))
- MobileUtils.LoadDemoSettings(DBSettings);
- else
- {
- if (Settings.URL.ToUpper().Contains("COM-AL"))
- {
- List<string> list = new List<string>();
- list.Add("http://remote.com-al.com.au:)" + Settings.Port);
- list.Add("http://remote2.com-al.com.au:" + Settings.Port);
- list.Add("http://remote3.com-al.com.au:" + Settings.Port);
- DBSettings.URLs = list.ToArray();
- }
- DBSettings.UserID = Settings.UserID;
- DBSettings.Password = Settings.Password;
- }
- }
-
- if (DBSettings.URLs.Count() > 0)
- {
- if (DBSettings.URLs[0].ToUpper().Contains("COM-AL"))
- {
- List<string> list = new List<string>();
- list.Add("http://remote.com-al.com.au:)" + Settings.Port);
- list.Add("http://remote2.com-al.com.au:" + Settings.Port);
- list.Add("http://remote3.com-al.com.au:" + Settings.Port);
- DBSettings.URLs = list.ToArray();
- }
- new LocalConfiguration<DatabaseSettings>().Save(DBSettings);
- MobileUtils.SaveToSecureStorage();
- }
- //if (!string.IsNullOrWhiteSpace(GlobalVariables.LoadFromLinkString))
- //{
- // MobileUtils.LoadFromLink();
- //}
- var result = JsonClient<User>.Ping(DBSettings.URLs, out DatabaseInfo info);
- ClientFactory.SetClientType(typeof(JsonClient<>), "TimeBench", MobileUtils.AppVersion.InstalledVersionNumber + deviceString, result, true);
- GlobalVariables.InternalOnAppearing = true;
- GlobalVariables.ChangeUser = false;
- if (!reload)
- RunTimers();
- MainPage = new MaterialNavigationPage(new PINLoginPage());
- }
- catch
- {
- Thread.Sleep(1000);
- LoadAll(true);
- }
- }
- private void RunTimers()
- {
- Task.Run(() =>
- {
- GPS.GetLocation();
- Bluetooth.ScanForDevices();
- 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;
- });
- });
- }
- 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<App>(this, MessageOnStart);
- IsInForeground = true;
- }
- protected override void OnSleep()
- {
- MessagingCenter.Send<App>(this, MessageOnSleep);
- IsInForeground = false;
- }
- protected override void OnResume()
- {
- MessagingCenter.Send<App>(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; }
- }
- }
|