From 51ac42b9fc6c2c26ef5bc1ccb37f8831333f9e7c Mon Sep 17 00:00:00 2001 From: gaoyuan Date: Fri, 11 Mar 2022 22:25:31 +0800 Subject: [PATCH] share db --- Billing.Shared/App.cs | 37 ++++++++++++++++++-- Billing.Shared/MainShell.xaml | 9 +++-- Billing.Shared/SplashPage.xaml.cs | 4 +-- Billing.Shared/Store/StoreHelper.cs | 32 ++++++++++++++--- Billing.Shared/UI/BillingPage.cs | 13 ++++++- Billing.Shared/Views/AddAccountPage.xaml.cs | 2 +- Billing.Shared/Views/AddBillPage.xaml.cs | 2 +- Billing.Shared/Views/AddCategoryPage.xaml.cs | 2 +- Billing.Shared/Views/BillPage.xaml.cs | 2 +- Billing.Shared/Views/SettingPage.xaml | 4 +++ Billing.Shared/Views/SettingPage.xaml.cs | 18 ++++++++++ Billing/Billing.iOS/AppDelegate.cs | 5 +++ Billing/Billing.iOS/Billing.iOS.csproj | 8 +++++ Billing/Billing.iOS/Info.plist | 15 ++++++++ 14 files changed, 134 insertions(+), 19 deletions(-) diff --git a/Billing.Shared/App.cs b/Billing.Shared/App.cs index 9282a5d..9cddec6 100644 --- a/Billing.Shared/App.cs +++ b/Billing.Shared/App.cs @@ -1,9 +1,12 @@ -using System.Collections.Generic; +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; @@ -27,7 +30,7 @@ namespace Billing InitResources(); MainPage = new MainShell(); - Shell.Current.GoToAsync("//Splash"); + //Shell.Current.GoToAsync("//Splash"); } protected override void OnStart() @@ -36,7 +39,7 @@ namespace Billing Helper.Debug($"cache folder: {StoreHelper.CacheFolder}"); } - internal static async Task InitilalizeData() + internal static async Task InitializeData() { await Task.WhenAll( Task.Run(async () => accounts = await StoreHelper.GetAccountsAsync()), @@ -79,5 +82,33 @@ namespace Billing // 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; + } } } \ No newline at end of file diff --git a/Billing.Shared/MainShell.xaml b/Billing.Shared/MainShell.xaml index 44b1d24..0db2bbd 100644 --- a/Billing.Shared/MainShell.xaml +++ b/Billing.Shared/MainShell.xaml @@ -10,15 +10,14 @@ TitleColor="{DynamicResource PrimaryColor}" Shell.NavBarHasShadow="True"> + + + + - - - - - \ No newline at end of file diff --git a/Billing.Shared/SplashPage.xaml.cs b/Billing.Shared/SplashPage.xaml.cs index 5b3c88b..1533529 100644 --- a/Billing.Shared/SplashPage.xaml.cs +++ b/Billing.Shared/SplashPage.xaml.cs @@ -10,9 +10,9 @@ namespace Billing InitializeComponent(); } - public override async void OnLoaded() + protected override async void OnLoaded() { - await App.InitilalizeData(); + await App.InitializeData(); await Shell.Current.GoToAsync("//Bills"); } diff --git a/Billing.Shared/Store/StoreHelper.cs b/Billing.Shared/Store/StoreHelper.cs index e799346..2ad5562 100644 --- a/Billing.Shared/Store/StoreHelper.cs +++ b/Billing.Shared/Store/StoreHelper.cs @@ -14,11 +14,32 @@ namespace Billing.Store public static readonly string PersonalFolder = Environment.GetFolderPath(Environment.SpecialFolder.Personal); public static readonly string CacheFolder = FileSystem.CacheDirectory; + public static string DatabasePath => Path.Combine(PersonalFolder, dbfile); + #region Sqlite3 private const string dbfile = "data.db3"; private static SQLiteAsyncConnection database; #endregion + public static async Task ReloadDatabase(string file) + { + var path = DatabasePath; + if (string.Equals(file, path, StringComparison.OrdinalIgnoreCase)) + { + return false; + } + if (database != null) + { + await database.CloseAsync(); + } + File.Copy(file, path, true); + database = new SQLiteAsyncConnection(path, + SQLiteOpenFlags.ReadWrite | + SQLiteOpenFlags.Create | + SQLiteOpenFlags.SharedCache); + return true; + } + private static readonly AsyncLazy Instance = new(async () => { var instance = new StoreHelper(); @@ -114,10 +135,13 @@ namespace Billing.Store private StoreHelper() { - database = new SQLiteAsyncConnection(Path.Combine(PersonalFolder, dbfile), - SQLiteOpenFlags.ReadWrite | - SQLiteOpenFlags.Create | - SQLiteOpenFlags.SharedCache); + if (database == null) + { + database = new SQLiteAsyncConnection(DatabasePath, + SQLiteOpenFlags.ReadWrite | + SQLiteOpenFlags.Create | + SQLiteOpenFlags.SharedCache); + } } public Task> GetListAsync(string query, params object[] args) where T : new() diff --git a/Billing.Shared/UI/BillingPage.cs b/Billing.Shared/UI/BillingPage.cs index fd3178c..429be05 100644 --- a/Billing.Shared/UI/BillingPage.cs +++ b/Billing.Shared/UI/BillingPage.cs @@ -7,6 +7,7 @@ namespace Billing.UI public abstract class BillingPage : ContentPage { public event EventHandler Loaded; + public event EventHandler Refreshed; private bool loaded; @@ -16,11 +17,16 @@ namespace Billing.UI Shell.SetTabBarIsVisible(this, false); } - public virtual void OnLoaded() + protected virtual void OnLoaded() { Loaded?.Invoke(this, EventArgs.Empty); } + protected virtual void OnRefresh() + { + Refreshed?.Invoke(this, EventArgs.Empty); + } + public void TriggerLoad() { if (!loaded) @@ -29,5 +35,10 @@ namespace Billing.UI OnLoaded(); } } + + public void TriggerRefresh() + { + OnRefresh(); + } } } \ No newline at end of file diff --git a/Billing.Shared/Views/AddAccountPage.xaml.cs b/Billing.Shared/Views/AddAccountPage.xaml.cs index 4c6e7fb..c6743b6 100644 --- a/Billing.Shared/Views/AddAccountPage.xaml.cs +++ b/Billing.Shared/Views/AddAccountPage.xaml.cs @@ -73,7 +73,7 @@ namespace Billing.Views InitializeComponent(); } - public override void OnLoaded() + protected override void OnLoaded() { editorName.SetFocus(); } diff --git a/Billing.Shared/Views/AddBillPage.xaml.cs b/Billing.Shared/Views/AddBillPage.xaml.cs index f744115..0bd0ddb 100644 --- a/Billing.Shared/Views/AddBillPage.xaml.cs +++ b/Billing.Shared/Views/AddBillPage.xaml.cs @@ -116,7 +116,7 @@ namespace Billing.Views } } - public override void OnLoaded() + protected override void OnLoaded() { editorAmount.SetFocus(); } diff --git a/Billing.Shared/Views/AddCategoryPage.xaml.cs b/Billing.Shared/Views/AddCategoryPage.xaml.cs index c4d7751..c881cec 100644 --- a/Billing.Shared/Views/AddCategoryPage.xaml.cs +++ b/Billing.Shared/Views/AddCategoryPage.xaml.cs @@ -78,7 +78,7 @@ namespace Billing.Views InitializeComponent(); } - public override void OnLoaded() + protected override void OnLoaded() { editorName.SetFocus(); } diff --git a/Billing.Shared/Views/BillPage.xaml.cs b/Billing.Shared/Views/BillPage.xaml.cs index f3dbdba..ea07142 100644 --- a/Billing.Shared/Views/BillPage.xaml.cs +++ b/Billing.Shared/Views/BillPage.xaml.cs @@ -55,7 +55,7 @@ namespace Billing.Views InitializeComponent(); } - public override void OnLoaded() + protected override void OnLoaded() { billingDate.SetDateTime(DateTime.Today); } diff --git a/Billing.Shared/Views/SettingPage.xaml b/Billing.Shared/Views/SettingPage.xaml index a86151f..6fb6511 100644 --- a/Billing.Shared/Views/SettingPage.xaml +++ b/Billing.Shared/Views/SettingPage.xaml @@ -11,6 +11,10 @@ BindingContext="{x:Reference settingPage}" Shell.TabBarIsVisible="True"> + + + + SetValue(PrimaryColorProperty, value); } + public Command ShareCommand { get; } public Command CategoryCommand { get; } public Command ColorPickerCommand { get; } public SettingPage() { + ShareCommand = new Command(OnShareCommand); CategoryCommand = new Command(OnCategoryCommand); ColorPickerCommand = new Command(OnColorPickerCommand); InitializeComponent(); @@ -48,6 +51,21 @@ namespace Billing.Views Light.Instance.RefreshColor(Color.FromHex(color)); } + private async void OnShareCommand() + { + if (Tap.IsBusy) + { + return; + } + using (Tap.Start()) + { + await Share.RequestAsync(new ShareFileRequest + { + File = new ShareFile(StoreHelper.DatabasePath) + }); + } + } + private async void OnCategoryCommand() { if (Tap.IsBusy) diff --git a/Billing/Billing.iOS/AppDelegate.cs b/Billing/Billing.iOS/AppDelegate.cs index c0f57ad..76aab35 100644 --- a/Billing/Billing.iOS/AppDelegate.cs +++ b/Billing/Billing.iOS/AppDelegate.cs @@ -23,5 +23,10 @@ namespace Billing.iOS return base.FinishedLaunching(app, options); } + + public override bool OpenUrl(UIApplication app, NSUrl url, NSDictionary options) + { + return App.OpenUrl(url); + } } } \ No newline at end of file diff --git a/Billing/Billing.iOS/Billing.iOS.csproj b/Billing/Billing.iOS/Billing.iOS.csproj index 4097576..97d5b5f 100644 --- a/Billing/Billing.iOS/Billing.iOS.csproj +++ b/Billing/Billing.iOS/Billing.iOS.csproj @@ -477,6 +477,14 @@ + + + true + {D4EA6D3A-17E6-4300-80A7-2ED19B738C6B} + Billing.ShareExtension + + + \ No newline at end of file diff --git a/Billing/Billing.iOS/Info.plist b/Billing/Billing.iOS/Info.plist index 59d4c76..2d44104 100644 --- a/Billing/Billing.iOS/Info.plist +++ b/Billing/Billing.iOS/Info.plist @@ -41,6 +41,21 @@ OpenSans-Regular.ttf OpenSans-SemiBold.ttf + CFBundleDocumentTypes + + + CFBundleTypeName + Master SQLite file + LSItemContentTypes + + public.data + + CFBundleTypeIconFiles + + Assets.xcassets/AppIcon.appiconset/Icon180 + + + CFBundleVersion 10 CFBundleShortVersionString