using System; using System.Globalization; using System.Linq; using Billing.Languages; using Billing.Models; using Billing.UI; using Xamarin.Forms; namespace Billing.Views { public partial class AddBillPage : BillingPage { private static readonly BindableProperty AmountProperty = Helper.Create(nameof(Amount)); private static readonly BindableProperty NameProperty = Helper.Create(nameof(Name)); private static readonly BindableProperty CategoryNameProperty = Helper.Create(nameof(CategoryName)); private static readonly BindableProperty WalletNameProperty = Helper.Create(nameof(WalletName)); private static readonly BindableProperty StoreProperty = Helper.Create(nameof(Store)); private static readonly BindableProperty CreatedDateProperty = Helper.Create(nameof(CreatedDate)); private static readonly BindableProperty CreatedTimeProperty = Helper.Create(nameof(CreatedTime)); private static readonly BindableProperty NoteProperty = Helper.Create(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 string CategoryName => (string)GetValue(CategoryNameProperty); public string WalletName => (string)GetValue(WalletNameProperty); 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 BillChecked; private readonly Bill bill; private readonly DateTime createDate; private int walletId; private int categoryId; 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; 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); Store = bill.Store; CreatedDate = bill.CreateTime.Date; CreatedTime = bill.CreateTime.TimeOfDay; Note = bill.Note; } 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); CreatedDate = createDate.Date; CreatedTime = DateTime.Now.TimeOfDay; } } public 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; } amount = Math.Abs(amount); var category = App.Categories.FirstOrDefault(c => c.Id == categoryId); 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 = categoryId; bill.WalletId = walletId; bill.CreateTime = CreatedDate.Date.Add(CreatedTime); bill.Store = Store; bill.Note = Note; } BillChecked?.Invoke(this, bill ?? new Bill { Id = -1, Amount = amount, Name = name, CategoryId = categoryId, WalletId = walletId, CreateTime = CreatedDate.Date.Add(CreatedTime), Store = Store, Note = Note }); } } private async void OnSelectCategory() { if (Tap.IsBusy) { return; } using (Tap.Start()) { var page = new CategorySelectPage(categoryId); page.CategoryTapped += CategorySelectPage_Tapped; await Navigation.PushAsync(page); } } private void CategorySelectPage_Tapped(object sender, UICategory e) { categoryId = e.Category.Id; SetValue(CategoryNameProperty, e.Name); } private async void OnSelectWallet() { if (Tap.IsBusy) { return; } using (Tap.Start()) { var source = App.Accounts.Select(a => new SelectItem { Value = a.Id, Name = a.Name, Icon = a.Icon }); var page = new ItemSelectPage>(source); page.ItemTapped += Wallet_ItemTapped; await Navigation.PushAsync(page); } } private void Wallet_ItemTapped(object sender, SelectItem account) { walletId = account.Value; SetValue(WalletNameProperty, account.Name); } } }