optimized and add diagnostic feature

This commit is contained in:
2022-03-15 15:17:02 +08:00
parent 77b4e54734
commit 5b209cc19c
45 changed files with 380 additions and 122 deletions

View File

@ -3,6 +3,7 @@ using System.Globalization;
using System.Linq;
using Billing.Languages;
using Billing.Models;
using Billing.Store;
using Billing.UI;
using Xamarin.Forms;
@ -12,8 +13,8 @@ namespace Billing.Views
{
private static readonly BindableProperty AmountProperty = Helper.Create<string, AddBillPage>(nameof(Amount));
private static readonly BindableProperty NameProperty = Helper.Create<string, AddBillPage>(nameof(Name));
private static readonly BindableProperty CategoryNameProperty = Helper.Create<string, AddBillPage>(nameof(CategoryName));
private static readonly BindableProperty WalletNameProperty = Helper.Create<string, AddBillPage>(nameof(WalletName));
private static readonly BindableProperty CategoryProperty = Helper.Create<Category, AddBillPage>(nameof(Category));
private static readonly BindableProperty WalletProperty = Helper.Create<Account, AddBillPage>(nameof(Wallet));
private static readonly BindableProperty StoreProperty = Helper.Create<string, AddBillPage>(nameof(Store));
private static readonly BindableProperty CreatedDateProperty = Helper.Create<DateTime, AddBillPage>(nameof(CreatedDate));
private static readonly BindableProperty CreatedTimeProperty = Helper.Create<TimeSpan, AddBillPage>(nameof(CreatedTime));
@ -29,8 +30,8 @@ namespace Billing.Views
get => (string)GetValue(NameProperty);
set => SetValue(NameProperty, value);
}
public string CategoryName => (string)GetValue(CategoryNameProperty);
public string WalletName => (string)GetValue(WalletNameProperty);
public Category Category => (Category)GetValue(CategoryProperty);
public Account Wallet => (Account)GetValue(WalletProperty);
public string Store
{
get => (string)GetValue(StoreProperty);
@ -61,8 +62,7 @@ namespace Billing.Views
private readonly Bill bill;
private readonly DateTime createDate;
private int walletId;
private int categoryId;
private bool categoryChanged;
public AddBillPage(DateTime date)
{
@ -94,10 +94,9 @@ namespace Billing.Views
{
Amount = Math.Abs(bill.Amount).ToString(CultureInfo.InvariantCulture);
Name = bill.Name;
walletId = bill.WalletId;
categoryId = bill.CategoryId;
SetValue(WalletNameProperty, App.Accounts.FirstOrDefault(a => a.Id == walletId)?.Name);
SetValue(CategoryNameProperty, App.Categories.FirstOrDefault(c => c.Id == categoryId)?.Name);
SetValue(WalletProperty, App.Accounts.FirstOrDefault(a => a.Id == bill.WalletId) ?? Account.Empty);
SetValue(CategoryProperty, App.Categories.FirstOrDefault(c => c.Id == bill.CategoryId) ?? Category.Empty);
categoryChanged = true;
Store = bill.Store;
CreatedDate = bill.CreateTime.Date;
CreatedTime = bill.CreateTime.TimeOfDay;
@ -105,12 +104,8 @@ namespace Billing.Views
}
else
{
var first = App.Accounts.First();
walletId = first.Id;
SetValue(WalletNameProperty, first.Name);
var firstCategory = App.Categories.First();
categoryId = firstCategory.Id;
SetValue(CategoryNameProperty, firstCategory.Name);
SetValue(WalletProperty, App.Accounts.FirstOrDefault() ?? Account.Empty);
SetValue(CategoryProperty, App.Categories.FirstOrDefault() ?? Category.Empty);
CreatedDate = createDate.Date;
CreatedTime = DateTime.Now.TimeOfDay;
}
@ -138,8 +133,9 @@ namespace Billing.Views
await this.ShowMessage(Resource.AmountRequired);
return;
}
var category = Category;
var wallet = Wallet;
amount = Math.Abs(amount);
var category = App.Categories.FirstOrDefault(c => c.Id == categoryId);
if (category.Type == CategoryType.Spending)
{
amount *= -1;
@ -154,8 +150,8 @@ namespace Billing.Views
{
bill.Amount = amount;
bill.Name = name;
bill.CategoryId = categoryId;
bill.WalletId = walletId;
bill.CategoryId = category.Id;
bill.WalletId = wallet.Id;
bill.CreateTime = CreatedDate.Date.Add(CreatedTime);
bill.Store = Store;
bill.Note = Note;
@ -164,15 +160,17 @@ namespace Billing.Views
{
Amount = amount,
Name = name,
CategoryId = categoryId,
WalletId = walletId,
CategoryId = category.Id,
WalletId = wallet.Id,
CreateTime = CreatedDate.Date.Add(CreatedTime),
Store = Store,
Note = Note
});
category.LastAccountId = walletId;
category.LastAccountId = wallet.Id;
category.LastUsed = DateTime.Now;
await StoreHelper.SaveCategoryItemAsync(category);
}
}
@ -184,7 +182,7 @@ namespace Billing.Views
}
using (Tap.Start())
{
var page = new CategorySelectPage(categoryId);
var page = new CategorySelectPage(categoryChanged ? Category.Id : -1);
page.CategoryTapped += CategorySelectPage_Tapped;
await Navigation.PushAsync(page);
}
@ -192,16 +190,16 @@ namespace Billing.Views
private void CategorySelectPage_Tapped(object sender, UICategory e)
{
categoryId = e.Category.Id;
SetValue(CategoryProperty, e.Category);
categoryChanged = true;
if (e.Category.LastAccountId != null)
{
var wallet = App.Accounts.FirstOrDefault(a => a.Id == e.Category.LastAccountId.Value);
if (wallet != null)
{
SetValue(WalletNameProperty, wallet.Name);
SetValue(WalletProperty, wallet);
}
}
SetValue(CategoryNameProperty, e.Name);
}
private async void OnSelectWallet()
@ -212,22 +210,15 @@ namespace Billing.Views
}
using (Tap.Start())
{
var source = App.Accounts.Select(a => new SelectItem<int>
{
Value = a.Id,
Name = a.Name,
Icon = a.Icon
});
var page = new ItemSelectPage<SelectItem<int>>(source);
var page = new ItemSelectPage<Account>(App.Accounts);
page.ItemTapped += Wallet_ItemTapped;
await Navigation.PushAsync(page);
}
}
private void Wallet_ItemTapped(object sender, SelectItem<int> account)
private void Wallet_ItemTapped(object sender, Account account)
{
walletId = account.Value;
SetValue(WalletNameProperty, account.Name);
SetValue(WalletProperty, account);
}
}
}