using System; using System.Globalization; using System.Linq; using Billing.Languages; using Billing.Models; using Billing.Store; using Billing.UI; using Xamarin.Forms; namespace Billing.Views { public partial class AddBillPage : BillingPage { 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 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)); private static readonly BindableProperty NoteProperty = Helper.Create<string, AddBillPage>(nameof(Note)); public string Amount { get => (string)GetValue(AmountProperty); set => SetValue(AmountProperty, value); } public string Name { get => (string)GetValue(NameProperty); set => SetValue(NameProperty, value); } public Category Category => (Category)GetValue(CategoryProperty); public Account Wallet => (Account)GetValue(WalletProperty); public string Store { get => (string)GetValue(StoreProperty); set => SetValue(StoreProperty, value); } public DateTime CreatedDate { get => (DateTime)GetValue(CreatedDateProperty); set => SetValue(CreatedDateProperty, value); } public TimeSpan CreatedTime { get => (TimeSpan)GetValue(CreatedTimeProperty); set => SetValue(CreatedTimeProperty, value); } public string Note { get => (string)GetValue(NoteProperty); set => SetValue(NoteProperty, value); } public Command CheckBill { get; } public Command SelectCategory { get; } public Command SelectWallet { get; } public event EventHandler<Bill> BillChecked; private readonly Bill bill; private readonly DateTime createDate; private bool categoryChanged; public AddBillPage(DateTime date) { createDate = date; CheckBill = new Command(OnCheckBill); SelectCategory = new Command(OnSelectCategory); SelectWallet = new Command(OnSelectWallet); InitializeComponent(); Title = Resource.AddBill; Initial(); } public AddBillPage(Bill bill) { this.bill = bill; CheckBill = new Command(OnCheckBill); SelectCategory = new Command(OnSelectCategory); SelectWallet = new Command(OnSelectWallet); InitializeComponent(); Title = Resource.EditBill; Initial(); } private void Initial() { if (bill != null) { Amount = Math.Abs(bill.Amount).ToString(CultureInfo.InvariantCulture); Name = bill.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; Note = bill.Note; } else { SetValue(WalletProperty, App.Accounts.FirstOrDefault() ?? Account.Empty); SetValue(CategoryProperty, App.Categories.FirstOrDefault() ?? Category.Empty); CreatedDate = createDate.Date; CreatedTime = DateTime.Now.TimeOfDay; } } protected override void OnLoaded() { editorAmount.SetFocus(); } private async void OnCheckBill() { if (Tap.IsBusy) { return; } using (Tap.Start()) { if (!decimal.TryParse(Amount, out decimal amount)) { return; } if (amount == 0) { await this.ShowMessage(Resource.AmountRequired); return; } var category = Category; var wallet = Wallet; amount = Math.Abs(amount); if (category.Type == CategoryType.Spending) { amount *= -1; } await Navigation.PopAsync(); var name = Name; if (string.IsNullOrWhiteSpace(name)) { name = category.Name; } if (bill != null) { bill.Amount = amount; bill.Name = name; bill.CategoryId = category.Id; bill.WalletId = wallet.Id; bill.CreateTime = CreatedDate.Date.Add(CreatedTime); bill.Store = Store; bill.Note = Note; } BillChecked?.Invoke(this, bill ?? new Bill { Amount = amount, Name = name, CategoryId = category.Id, WalletId = wallet.Id, CreateTime = CreatedDate.Date.Add(CreatedTime), Store = Store, Note = Note }); category.LastAccountId = wallet.Id; category.LastUsed = DateTime.Now; await StoreHelper.SaveCategoryItemAsync(category); } } private async void OnSelectCategory() { if (Tap.IsBusy) { return; } using (Tap.Start()) { var page = new CategorySelectPage(categoryChanged ? Category.Id : -1); page.CategoryTapped += CategorySelectPage_Tapped; await Navigation.PushAsync(page); } } private void CategorySelectPage_Tapped(object sender, UICategory e) { 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(WalletProperty, wallet); } } } private async void OnSelectWallet() { if (Tap.IsBusy) { return; } using (Tap.Start()) { var page = new ItemSelectPage<Account>(App.Accounts); page.ItemTapped += Wallet_ItemTapped; await Navigation.PushAsync(page); } } private void Wallet_ItemTapped(object sender, Account account) { SetValue(WalletProperty, account); } } }