using Billing.Models; using Billing.UI; using System; using System.Collections.Generic; using System.Linq; using Xamarin.Forms; namespace Billing.Views { public partial class BillPage : BillingPage { private static readonly BindableProperty SelectedDateProperty = BindableProperty.Create(nameof(SelectedDate), typeof(DateTime), typeof(BillPage)); private static readonly BindableProperty BillsProperty = BindableProperty.Create(nameof(Bills), typeof(List), typeof(BillPage)); public DateTime SelectedDate { get => (DateTime)GetValue(SelectedDateProperty); set => SetValue(SelectedDateProperty, value); } public List Bills { get => (List)GetValue(BillsProperty); set => SetValue(BillsProperty, value); } public Command AddBilling { get; } public BillPage() { AddBilling = new Command(OnAddBilling); InitializeComponent(); billingDate.SetDateTime(DateTime.Now); } private void OnDateSelected(object sender, DateEventArgs e) { SelectedDate = e.Date; var bills = App.Bills.Where(b => b.CreateTime.Year == e.Date.Year && b.CreateTime.Month == e.Date.Month && b.CreateTime.Day == e.Date.Day); Bills = new List(bills.Select(b => new UIBill(b) { Icon = App.Categories.FirstOrDefault(c => c.Id == b.CategoryId)?.Icon ?? BaseModel.ICON_DEFAULT, Name = b.Name, DateCreation = b.CreateTime, Amount = b.Amount, Wallet = App.Accounts.FirstOrDefault(a => a.Id == b.WalletId)?.Name })); } private void OnTitleDateLongPressed(object sender, EventArgs e) { billingDate.SetDateTime(DateTime.Now); } private async void OnAddBilling() { if (Tap.IsBusy) { return; } using (Tap.Start()) { var page = new AddBillPage(SelectedDate); await Navigation.PushAsync(page); } } } public class UIBill : BindableObject { public static readonly BindableProperty IconProperty = BindableProperty.Create(nameof(Icon), typeof(string), typeof(UIBill)); public static readonly BindableProperty NameProperty = BindableProperty.Create(nameof(Name), typeof(string), typeof(UIBill)); public static readonly BindableProperty DateCreationProperty = BindableProperty.Create(nameof(DateCreation), typeof(DateTime), typeof(UIBill)); public static readonly BindableProperty AmountProperty = BindableProperty.Create(nameof(Amount), typeof(decimal), typeof(UIBill)); public static readonly BindableProperty WalletProperty = BindableProperty.Create(nameof(Wallet), typeof(string), typeof(UIBill)); public string Icon { get => (string)GetValue(IconProperty); set => SetValue(IconProperty, value); } public string Name { get => (string)GetValue(NameProperty); set => SetValue(NameProperty, value); } public DateTime DateCreation { get => (DateTime)GetValue(DateCreationProperty); set => SetValue(DateCreationProperty, value); } public decimal Amount { get => (decimal)GetValue(AmountProperty); set => SetValue(AmountProperty, value); } public string Wallet { get => (string)GetValue(WalletProperty); set => SetValue(WalletProperty, value); } public Bill Bill { get; } public UIBill(Bill bill) { Bill = bill; } } }