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

@ -19,7 +19,7 @@
</ContentPage.Resources>
<ContentPage.ToolbarItems>
<ToolbarItem Order="Primary" IconImageSource="plus.png" Command="{Binding AddAccount}"/>
<ToolbarItem Order="Primary" IconImageSource="plus.png" Command="{Binding EditAccount}"/>
</ContentPage.ToolbarItems>
<ScrollView>
@ -57,17 +57,21 @@
</ui:GroupStackLayout.GroupHeaderTemplate>
<ui:GroupStackLayout.ItemTemplate>
<DataTemplate x:DataType="m:Account">
<StackLayout Orientation="Horizontal" Padding="20, 0, 10, 0" Spacing="10">
<Grid Padding="20, 0, 10, 0" ColumnSpacing="10" ColumnDefinitions="Auto, *, Auto, Auto">
<Grid.GestureRecognizers>
<TapGestureRecognizer Command="{Binding EditAccount, Source={x:Reference accountPage}}"
CommandParameter="{Binding .}"/>
</Grid.GestureRecognizers>
<ui:TintImage Source="{Binding Icon, Converter={StaticResource iconConverter}}"
WidthRequest="26" HeightRequest="20" VerticalOptions="Center"/>
<Label Text="{Binding Name}" TextColor="{DynamicResource TextColor}"
<Label Grid.Column="1" Text="{Binding Name}" TextColor="{DynamicResource TextColor}"
HorizontalOptions="FillAndExpand" VerticalOptions="Center"
FontSize="Default" FontAttributes="Bold"/>
<Label Text="{Binding Balance, Converter={StaticResource money2Converter}}"
<Label Grid.Column="2" Text="{Binding Balance, Converter={StaticResource money2Converter}}"
TextColor="{DynamicResource SecondaryTextColor}"
VerticalOptions="Center"/>
<ui:TintImage Source="right.png" HeightRequest="20"/>
</StackLayout>
<ui:TintImage Grid.Column="3" Source="right.png" HeightRequest="20"/>
</Grid>
</DataTemplate>
</ui:GroupStackLayout.ItemTemplate>
</ui:GroupStackLayout>

View File

@ -19,14 +19,14 @@ namespace Billing.Views
public decimal Liability => (decimal)GetValue(LiabilityProperty);
public List<AccountGrouping> Accounts => (List<AccountGrouping>)GetValue(AccountsProperty);
public Command AddAccount { get; }
public Command EditAccount { get; }
private readonly List<AccountGrouping> accounts = new();
private bool initialized;
public AccountPage()
{
AddAccount = new Command(OnAddAccount);
EditAccount = new Command(OnEditAccount);
SetValue(AccountsProperty, accounts);
InitializeComponent();
@ -40,9 +40,17 @@ namespace Billing.Views
accounts.Clear();
foreach (var account in App.Accounts)
{
account.Balance = account.Initial + App.Bills.Where(b => b.WalletId == account.Id).Sum(b => b.Amount);
AddToAccountGroup(account);
}
}
else
{
foreach (var account in App.Accounts)
{
account.Balance = account.Initial + App.Bills.Where(b => b.WalletId == account.Id).Sum(b => b.Amount);
}
}
groupLayout.Refresh(accounts);
}
@ -72,7 +80,7 @@ namespace Billing.Views
}
}
private async void OnAddAccount()
private async void OnEditAccount(object o)
{
if (Tap.IsBusy)
{
@ -80,7 +88,7 @@ namespace Billing.Views
}
using (Tap.Start())
{
var page = new AddAccountPage();
var page = new AddAccountPage(o as Account);
page.AccountChecked += AccountChecked;
await Navigation.PushAsync(page);
}
@ -88,8 +96,11 @@ namespace Billing.Views
private void AccountChecked(object sender, AccountEventArgs e)
{
App.Accounts.Add(e.Account);
AddToAccountGroup(e.Account);
if (e.Account.Id < 0)
{
App.Accounts.Add(e.Account);
AddToAccountGroup(e.Account);
}
groupLayout.Refresh(accounts);
Task.Run(App.WriteAccounts);

View File

@ -7,7 +7,6 @@
x:Class="Billing.Views.AddAccountPage"
x:Name="addAccountPage"
x:DataType="v:AddAccountPage"
Title="{r:Text AddAccount}"
BindingContext="{x:Reference addAccountPage}">
<ContentPage.Resources>

View File

@ -49,29 +49,27 @@ namespace Billing.Views
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)
public AddAccountPage(Account account = null)
{
CheckAccount = new Command(OnCheckAccount);
SelectIcon = new Command(OnSelectIcon);
SelectCategory = new Command(OnSelectCategory);
Title = Resource.EditAccount;
this.account = account;
AccountName = account.Name;
AccountIcon = account.Icon;
Category = account.Category;
Initial = account.Initial.ToString();
Memo = account.Memo;
if (account == null)
{
AccountIcon = BaseModel.ICON_DEFAULT;
Category = AccountCategory.Cash;
}
else
{
AccountName = account.Name;
AccountIcon = account.Icon;
Category = account.Category;
Initial = account.Initial.ToString();
Memo = account.Memo;
}
InitializeComponent();
}
@ -83,13 +81,26 @@ namespace Billing.Views
}
using (Tap.Start())
{
if (string.IsNullOrWhiteSpace(AccountName))
{
await this.ShowMessage(Resource.AccountRequired);
return;
}
await Navigation.PopAsync();
_ = decimal.TryParse(Initial, out decimal initial);
if (account != null)
{
account.Name = AccountName;
account.Icon = AccountIcon;
account.Category = Category;
account.Initial = initial;
account.Memo = Memo;
}
AccountChecked?.Invoke(this, new AccountEventArgs
{
Account = new Account
Account = account ?? new Account
{
Id = account?.Id ?? -1,
Id = -1,
Name = AccountName,
Icon = AccountIcon,
Category = Category,

View File

@ -16,27 +16,32 @@
<ContentPage.Content>
<TableView Intent="Settings" HasUnevenRows="True">
<TableSection Title=" ">
<ui:OptionEditorCell Height="120" Icon="pencil.png" FontSize="20" Keyboard="Text"
Title="{r:Text Account}"
Text="{Binding AccountName, Mode=TwoWay}"
<ui:OptionEditorCell Height="120" Icon="yuan.png" FontSize="20" Keyboard="Numeric"
Text="{Binding Amount, Mode=TwoWay}"
Placeholder="0.00"/>
</TableSection>
<TableSection>
<TableSection.Title>
<OnPlatform x:TypeArguments="x:String" Android=" "/>
</TableSection.Title>
<ui:OptionEntryCell Height="44" Icon="pencil.png"
Title="{r:Text Name}"
Text="{Binding Name, Mode=TwoWay}"
Placeholder="{r:Text NamePlaceholder}"/>
<ui:OptionSelectCell Height="44" Icon="project.png"
Title="{r:Text Category}"
Detail="{Binding Category, Converter={StaticResource categoryConverter}}"
Detail="{Binding CategoryName}"
Command="{Binding SelectCategory}"/>
<ui:OptionSelectCell Height="44" Icon="project.png"
<ui:OptionSelectCell Height="44" Icon="wallet.png"
Title="{r:Text Account}"
Detail="{Binding Category, Converter={StaticResource categoryConverter}}"
Command="{Binding SelectCategory}"/>
<ui:OptionSelectCell Height="44" Icon="project.png"
Detail="{Binding WalletName}"
Command="{Binding SelectWallet}"/>
<ui:OptionEntryCell Height="44" Icon="online.png"
Title="{r:Text Store}"
Text="{Binding Store, Mode=TwoWay}"/>
<ui:OptionSelectCell Height="44" Icon="bars.png"
Title="{r:Text CreatedTime}"
Detail="{Binding Category, Converter={StaticResource categoryConverter}}"
Command="{Binding SelectCategory}"/>
Detail="{Binding CreatedTime}"/>
</TableSection>
<TableSection>
<TableSection.Title>
@ -44,7 +49,7 @@
</TableSection.Title>
<ui:OptionEditorCell Height="120" Icon="note.png" Keyboard="Plain"
Title="{r:Text Memo}"
Text="{Binding Memo, Mode=TwoWay}"
Text="{Binding Note, Mode=TwoWay}"
Placeholder="{r:Text MemoPlaceholder}"/>
</TableSection>
</TableView>

View File

@ -1,4 +1,6 @@
using System;
using System.Globalization;
using System.Linq;
using Billing.Languages;
using Billing.Models;
using Billing.UI;
@ -8,28 +10,158 @@ namespace Billing.Views
{
public partial class AddBillPage : BillingPage
{
private static readonly BindableProperty AmountProperty = BindableProperty.Create(nameof(Amount), typeof(string), typeof(AddBillPage));
private static readonly BindableProperty NameProperty = BindableProperty.Create(nameof(Name), typeof(string), typeof(AddBillPage));
private static readonly BindableProperty CategoryNameProperty = BindableProperty.Create(nameof(CategoryName), typeof(string), typeof(AddBillPage));
private static readonly BindableProperty WalletNameProperty = BindableProperty.Create(nameof(WalletName), typeof(string), typeof(AddBillPage));
private static readonly BindableProperty StoreProperty = BindableProperty.Create(nameof(Store), typeof(string), typeof(AddBillPage));
private static readonly BindableProperty CreatedTimeProperty = BindableProperty.Create(nameof(CreatedTime), typeof(DateTime), typeof(AddBillPage));
private static readonly BindableProperty NoteProperty = BindableProperty.Create(nameof(Note), typeof(string), typeof(AddBillPage));
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 CreatedTime
{
get => (DateTime)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 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 OnCheckBill()
private void Initial()
{
if (bill != null)
{
Amount = 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;
CreatedTime = bill.CreateTime;
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);
CreatedTime = createDate.Date.Add(DateTime.Now.TimeOfDay);
}
}
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();
if (bill != null)
{
bill.Amount = amount;
bill.Name = Name;
bill.CategoryId = categoryId;
bill.WalletId = walletId;
bill.CreateTime = CreatedTime;
bill.Store = Store;
bill.Note = Note;
}
BillChecked?.Invoke(this, bill ?? new Bill
{
Id = -1,
Amount = amount,
Name = Name,
CategoryId = categoryId,
WalletId = walletId,
CreateTime = CreatedTime,
Store = Store,
Note = Note
});
}
}
private void OnSelectCategory()
{
}
private void OnSelectWallet()
{
}

View File

@ -30,6 +30,10 @@
</Grid>
</Shell.TitleView>
<ContentPage.ToolbarItems>
<ToolbarItem Order="Primary" IconImageSource="plus.png" Command="{Binding EditBilling}"/>
</ContentPage.ToolbarItems>
<Grid RowDefinitions="Auto, Auto, *">
<ui:BillingDate x:Name="billingDate" SelectedDate="{Binding SelectedDate}" DateSelected="OnDateSelected"/>
<Grid Grid.Row="1" Padding="8" ColumnSpacing="8" ColumnDefinitions="Auto, *, Auto"
@ -39,7 +43,7 @@
VerticalOptions="Center"/>
<StackLayout Grid.Column="2" Orientation="Horizontal" Spacing="6">
<StackLayout.GestureRecognizers>
<TapGestureRecognizer Command="{Binding AddBilling}"/>
<TapGestureRecognizer Command="{Binding EditBilling}"/>
</StackLayout.GestureRecognizers>
<Label Text="{r:Text TapToMemo}" TextColor="{DynamicResource PrimaryColor}"
VerticalOptions="Center"/>
@ -48,11 +52,16 @@
</Grid>
<!-- bill list -->
<ScrollView Grid.Row="2">
<ui:GroupStackLayout x:Name="billsLayout" ItemsSource="{Binding Bills}" Margin="0, 10, 0, 0">
<ui:GroupStackLayout x:Name="billsLayout" ItemsSource="{Binding Bills}"
Margin="0, 10, 0, 0" RowHeight="50">
<ui:GroupStackLayout.ItemTemplate>
<DataTemplate x:DataType="v:UIBill">
<Grid Padding="20, 0, 10, 0" ColumnSpacing="10"
ColumnDefinitions="Auto, *, Auto" RowDefinitions="Auto, Auto">
<Grid.GestureRecognizers>
<TapGestureRecognizer Command="{Binding EditBilling, Source={x:Reference billPage}}"
CommandParameter="{Binding .}"/>
</Grid.GestureRecognizers>
<ui:TintImage Source="{Binding Icon, Converter={StaticResource iconConverter}}"
WidthRequest="26" HeightRequest="20" VerticalOptions="Center"/>
<Label Grid.Column="1" Text="{Binding Name}" TextColor="{DynamicResource TextColor}"
@ -62,7 +71,7 @@
TextColor="{Binding Amount, Converter={StaticResource colorConverter}}"
VerticalOptions="Center"/>
<Label Grid.Row="1" Grid.Column="1" Text="{Binding ., Converter={StaticResource billConverter}}"
FontSize="Small"
FontSize="10"
TextColor="{DynamicResource SecondaryTextColor}"/>
</Grid>
</DataTemplate>

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