2022-03-11 22:25:31 +08:00

114 lines
3.3 KiB
C#

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<Bill> Bills => bills ??= new List<Bill>();
public static List<Account> Accounts => accounts ??= new List<Account>();
public static List<Category> Categories => categories ??= new List<Category>();
private static List<Bill> bills;
private static List<Account> accounts;
private static List<Category> categories;
public App()
{
CurrentCulture = new PlatformCulture();
InitResources();
MainPage = new MainShell();
//Shell.Current.GoToAsync("//Splash");
}
protected override void OnStart()
{
Helper.Debug($"personal folder: {StoreHelper.PersonalFolder}");
Helper.Debug($"cache folder: {StoreHelper.CacheFolder}");
}
internal static async Task InitializeData()
{
await Task.WhenAll(
Task.Run(async () => accounts = await StoreHelper.GetAccountsAsync()),
Task.Run(async () => categories = await StoreHelper.GetCategoriesAsync()),
Task.Run(async () => bills = await StoreHelper.GetBillsAsync()));
}
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 bool OpenUrl(Uri uri)
{
if (uri == null)
{
return true;
}
var absolute = uri.AbsolutePath;
var url = System.Net.WebUtility.UrlDecode(absolute);
if (File.Exists(url))
{
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;
}
}
}