complete account page
This commit is contained in:
@ -1,5 +1,6 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Billing.Models;
|
||||
using Billing.UI;
|
||||
using Xamarin.Forms;
|
||||
@ -20,17 +21,57 @@ namespace Billing.Views
|
||||
|
||||
public Command AddAccount { get; }
|
||||
|
||||
private readonly List<AccountGrouping> accounts;
|
||||
private readonly List<AccountGrouping> accounts = new();
|
||||
private bool initialized;
|
||||
|
||||
public AccountPage()
|
||||
{
|
||||
AddAccount = new Command(OnAddAccount);
|
||||
accounts = new List<AccountGrouping>();
|
||||
SetValue(AccountsProperty, accounts);
|
||||
|
||||
InitializeComponent();
|
||||
}
|
||||
|
||||
protected override void OnAppearing()
|
||||
{
|
||||
if (!initialized)
|
||||
{
|
||||
initialized = true;
|
||||
accounts.Clear();
|
||||
foreach (var account in App.Accounts)
|
||||
{
|
||||
AddToAccountGroup(account);
|
||||
}
|
||||
}
|
||||
groupLayout.Refresh(accounts);
|
||||
}
|
||||
|
||||
private void AddToAccountGroup(Account account)
|
||||
{
|
||||
int maxId;
|
||||
if (accounts.Count > 0)
|
||||
{
|
||||
maxId = accounts.Max(g => g.Max(a => a.Id));
|
||||
}
|
||||
else
|
||||
{
|
||||
maxId = -1;
|
||||
}
|
||||
account.Id = maxId + 1;
|
||||
|
||||
var group = accounts.FirstOrDefault(g => g.Key == account.Category);
|
||||
if (group == null)
|
||||
{
|
||||
group = new AccountGrouping(account.Category, account.Balance) { account };
|
||||
accounts.Add(group);
|
||||
}
|
||||
else
|
||||
{
|
||||
group.Add(account);
|
||||
group.Balance += account.Balance;
|
||||
}
|
||||
}
|
||||
|
||||
private async void OnAddAccount()
|
||||
{
|
||||
if (Tap.IsBusy)
|
||||
@ -47,28 +88,23 @@ namespace Billing.Views
|
||||
|
||||
private void AccountChecked(object sender, AccountEventArgs e)
|
||||
{
|
||||
Helper.Debug(e.Account.ToString());
|
||||
var group = accounts.FirstOrDefault(g => g.Key == e.Account.Category);
|
||||
if (group == null)
|
||||
{
|
||||
group = new AccountGrouping(e.Account.Category)
|
||||
{
|
||||
e.Account
|
||||
};
|
||||
accounts.Add(group);
|
||||
}
|
||||
else
|
||||
{
|
||||
group.Add(e.Account);
|
||||
}
|
||||
App.Accounts.Add(e.Account);
|
||||
AddToAccountGroup(e.Account);
|
||||
groupLayout.Refresh(accounts);
|
||||
|
||||
Task.Run(App.WriteAccounts);
|
||||
}
|
||||
}
|
||||
|
||||
public class AccountGrouping : List<Account>, IGrouping<AccountCategory, Account>
|
||||
{
|
||||
public AccountGrouping(AccountCategory key) : base() => Key = key;
|
||||
public AccountCategory Key { get; }
|
||||
public decimal Balance { get; set; }
|
||||
|
||||
public AccountGrouping(AccountCategory key, decimal balance) : base()
|
||||
{
|
||||
Key = key;
|
||||
Balance = balance;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user