using System; using System.Collections.Generic; using System.IO; using System.Threading.Tasks; using Billing.Languages; using Billing.Models; using Billing.Store; using Billing.Themes; using Billing.UI; using Xamarin.Essentials; using Xamarin.Forms; namespace Billing { public class App : Application { public static AppTheme CurrentTheme { get; private set; } public static PlatformCulture CurrentCulture { get; private set; } public static List Bills => bills ??= new List(); public static List Accounts => accounts ??= new List(); public static List Categories => categories ??= new List(); private static List bills; private static List accounts; private static List categories; private string initialUrl; public App(string url = null) { initialUrl = url; CurrentCulture = new PlatformCulture(); InitResources(); MainPage = new MainShell(); } protected override void OnStart() { Helper.Debug($"personal folder: {StoreHelper.PersonalFolder}"); Helper.Debug($"cache folder: {StoreHelper.CacheFolder}"); if (initialUrl != null) { var url = initialUrl; initialUrl = null; _ = OpenUrl(url); } } public static async Task InitializeData() { var instance = await StoreHelper.Instance; await Task.WhenAll( Task.Run(async () => accounts = await instance.GetListAsync()), Task.Run(async () => categories = await instance.GetListAsync()), Task.Run(async () => bills = await instance.GetListAsync())); } protected override void OnResume() { SetTheme(AppInfo.RequestedTheme); } private void InitResources() { var theme = AppInfo.RequestedTheme; SetTheme(theme, true); } private void SetTheme(AppTheme theme, bool force = false) { if (force || theme != CurrentTheme) { CurrentTheme = theme; } else { return; } Helper.Debug($"application theme: {theme}"); BaseTheme instance; if (theme == AppTheme.Dark) { instance = Dark.Instance; } else { instance = Light.Instance; } // TODO: status bar Resources = instance; } public static async Task OpenUrl(string url) { if (string.IsNullOrEmpty(url)) { return false; } if (File.Exists(url)) { var status = await Permissions.CheckStatusAsync(); if (status != PermissionStatus.Disabled && status != PermissionStatus.Granted) { status = await Permissions.RequestAsync(); } if (status != PermissionStatus.Granted) { return false; } _ = Task.Run(async () => { var result = await StoreHelper.ReloadDatabase(url); if (result) { await InitializeData(); var current = Shell.Current.CurrentPage; if (current is BillingPage page) { MainThread.BeginInvokeOnMainThread(() => page.TriggerRefresh()); } } }); } return true; } } }