first step

This commit is contained in:
gaoyuan
2022-03-02 22:29:13 +08:00
parent 51f8e4f2e8
commit 9929eee056
17 changed files with 343 additions and 60 deletions

View File

@ -1,8 +1,10 @@
using Billing.Languages;
using Billing.Models;
using Billing.UI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Xamarin.Forms;
namespace Billing.Views
@ -23,11 +25,11 @@ namespace Billing.Views
set => SetValue(BillsProperty, value);
}
public Command AddBilling { get; }
public Command EditBilling { get; }
public BillPage()
{
AddBilling = new Command(OnAddBilling);
EditBilling = new Command(OnEditBilling);
InitializeComponent();
@ -42,14 +44,28 @@ namespace Billing.Views
b.CreateTime.Year == e.Date.Year &&
b.CreateTime.Month == e.Date.Month &&
b.CreateTime.Day == e.Date.Day);
Bills = new List<UIBill>(bills.Select(b => new UIBill(b)
Bills = new List<UIBill>(bills.Select(b => WrapBill(b)));
}
private UIBill WrapBill(Bill b)
{
return 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 UpdateBill(UIBill bill)
{
bill.Icon = App.Categories.FirstOrDefault(c => c.Id == bill.Bill.CategoryId)?.Icon ?? BaseModel.ICON_DEFAULT;
bill.Name = bill.Bill.Name;
bill.DateCreation = bill.Bill.CreateTime;
bill.Amount = bill.Bill.Amount;
bill.Wallet = App.Accounts.FirstOrDefault(a => a.Id == bill.Bill.WalletId)?.Name;
}
private void OnTitleDateLongPressed(object sender, EventArgs e)
@ -57,7 +73,7 @@ namespace Billing.Views
billingDate.SetDateTime(DateTime.Now);
}
private async void OnAddBilling()
private async void OnEditBilling(object o)
{
if (Tap.IsBusy)
{
@ -65,10 +81,55 @@ namespace Billing.Views
}
using (Tap.Start())
{
var page = new AddBillPage(SelectedDate);
AddBillPage page;
if (o is UIBill bill)
{
page = new AddBillPage(bill.Bill);
}
else
{
if (App.Accounts.Count == 0)
{
await this.ShowMessage(Resource.NeedAccount);
await Shell.Current.GoToAsync("//Accounts");
return;
}
page = new AddBillPage(SelectedDate);
}
page.BillChecked += OnBillChecked;
await Navigation.PushAsync(page);
}
}
private void OnBillChecked(object sender, Bill e)
{
if (e.Id < 0)
{
int maxId;
if (App.Bills.Count > 0)
{
maxId = App.Bills.Max(b => b.Id);
}
else
{
maxId = -1;
}
e.Id = maxId + 1;
App.Bills.Add(e);
Bills.Add(WrapBill(e));
billsLayout.Refresh(Bills);
}
else
{
var bill = Bills.FirstOrDefault(b => b.Bill == e);
if (bill != null)
{
UpdateBill(bill);
}
}
Task.Run(App.WriteBills);
}
}
public class UIBill : BindableObject