Billing/Billing.Shared/Views/AddAccountPage.xaml.cs
2022-02-26 17:29:12 +08:00

121 lines
4.0 KiB
C#

using Billing.Models;
using Billing.UI;
using System;
using Xamarin.Forms;
namespace Billing.Views
{
public partial class AddAccountPage : BillingPage
{
private static readonly BindableProperty AccountNameProperty = BindableProperty.Create(nameof(AccountName), typeof(string), typeof(AddAccountPage));
private static readonly BindableProperty AccountIconProperty = BindableProperty.Create(nameof(AccountIcon), typeof(string), typeof(AddAccountPage));
private static readonly BindableProperty CategoryProperty = BindableProperty.Create(nameof(Category), typeof(AccountCategory), typeof(AddAccountPage));
private static readonly BindableProperty BalanceProperty = BindableProperty.Create(nameof(Balance), typeof(decimal), typeof(AddAccountPage));
private static readonly BindableProperty MemoProperty = BindableProperty.Create(nameof(Memo), typeof(string), typeof(AddAccountPage));
public string AccountName
{
get => (string)GetValue(AccountNameProperty);
set => SetValue(AccountNameProperty, value);
}
public string AccountIcon
{
get => (string)GetValue(AccountIconProperty);
set => SetValue(AccountIconProperty, value);
}
public AccountCategory Category
{
get => (AccountCategory)GetValue(CategoryProperty);
set => SetValue(CategoryProperty, value);
}
public decimal Balance
{
get => (decimal)GetValue(BalanceProperty);
set => SetValue(BalanceProperty, value);
}
public string Memo
{
get => (string)GetValue(MemoProperty);
set => SetValue(MemoProperty, value);
}
private readonly Account account;
public Command CheckAccount { get; }
public Command SelectIcon { get; }
public Command SelectCategory { get; }
public event EventHandler<AccountEventArgs> AccountChecked;
public AddAccountPage()
{
CheckAccount = new Command(OnCheckAccount);
SelectIcon = new Command(OnSelectIcon);
SelectCategory = new Command(OnSelectCategory);
AccountIcon = BaseModel.ICON_DEFAULT;
Category = AccountCategory.Cash;
InitializeComponent();
}
public AddAccountPage(Account account)
{
this.account = account;
AccountName = account.Name;
AccountIcon = account.Icon;
Category = account.Category;
Balance = account.Balance;
Memo = account.Memo;
CheckAccount = new Command(OnCheckAccount);
InitializeComponent();
}
private void Balance_Unfocused(object sender, FocusEventArgs e)
{
if (sender is OptionEntry entry && decimal.TryParse(entry.Text, out decimal d))
{
var converter = (MoneyConverter)Resources["moneyConverter"];
entry.Text = converter.Convert(d, null, null, null)?.ToString();
//Balance = d;
}
}
private async void OnCheckAccount()
{
if (Tap.IsBusy)
{
return;
}
using (Tap.Start())
{
await Navigation.PopAsync();
AccountChecked?.Invoke(this, new AccountEventArgs
{
Account = new Account
{
Id = account?.Id ?? -1,
Name = AccountName,
Icon = AccountIcon,
Category = Category,
Balance = Balance,
Memo = Memo
}
});
}
}
private void OnSelectIcon()
{
}
private void OnSelectCategory()
{
}
}
public class AccountEventArgs : EventArgs
{
public Account Account { get; set; }
}
}