budget-view, dashboard-view displayed correct, transactions-form finalized
This commit is contained in:
@@ -1,8 +1,13 @@
|
||||
using System.Threading.Tasks;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Avalonia;
|
||||
using Avalonia.Controls.ApplicationLifetimes;
|
||||
using Avalonia.Styling;
|
||||
using Clario.Data;
|
||||
using Clario.Models;
|
||||
using Clario.Models.GeneralModels;
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
using CommunityToolkit.Mvvm.Input;
|
||||
@@ -12,11 +17,19 @@ namespace Clario.ViewModels;
|
||||
|
||||
public partial class MainViewModel : ViewModelBase
|
||||
{
|
||||
public DashboardViewModel _dashboardViewModel;
|
||||
private DashboardViewModel _dashboardViewModel;
|
||||
public TransactionsViewModel _transactionsViewModel;
|
||||
public AccountsViewModel _accountsViewModel;
|
||||
public BudgetViewModel _budgetViewModel;
|
||||
private AccountsViewModel _accountsViewModel;
|
||||
private BudgetViewModel _budgetViewModel;
|
||||
[ObservableProperty] private TransactionFormViewModel _transactionFormViewModel;
|
||||
[ObservableProperty] public Profile? _profile;
|
||||
private List<Transaction> _transactions = new();
|
||||
private List<Category> _categories = new();
|
||||
private List<Budget> _budgets = new();
|
||||
private List<Account> _accounts = new();
|
||||
|
||||
[ObservableProperty] private bool _isTransactionFormVisible;
|
||||
|
||||
|
||||
[ObservableProperty] [NotifyPropertyChangedFor(nameof(isOnDashboard), nameof(isOnTransactions), nameof(isOnAccounts), nameof(isOnBudget))]
|
||||
private ViewModelBase? _currentView;
|
||||
@@ -25,24 +38,162 @@ public partial class MainViewModel : ViewModelBase
|
||||
|
||||
public MainViewModel()
|
||||
{
|
||||
_dashboardViewModel = new DashboardViewModel() { parentViewModel = this };
|
||||
_transactionsViewModel = new TransactionsViewModel() { parentViewModel = this };
|
||||
_accountsViewModel = new AccountsViewModel() { parentViewModel = this };
|
||||
_budgetViewModel = new BudgetViewModel() { parentViewModel = this };
|
||||
CurrentView = _dashboardViewModel;
|
||||
// CurrentView = _transactionsViewModel;
|
||||
IsDarkTheme = ThemeService.IsDarkTheme;
|
||||
|
||||
Console.WriteLine("main vm loaded");
|
||||
CurrentView = new LoadingViewModel();
|
||||
_ = InitializeApp();
|
||||
}
|
||||
|
||||
|
||||
private async Task InitializeApp()
|
||||
{
|
||||
Profile = await DataRepo.General.FetchProfileInfo();
|
||||
_ = await DataRepo.General.FetchCategories();
|
||||
_ = await DataRepo.General.FetchAccounts();
|
||||
ThemeService.SwitchToTheme(Profile?.Theme ?? "system");
|
||||
try
|
||||
{
|
||||
var profilesTask = DataRepo.General.FetchProfileInfo();
|
||||
var categoriesTask = DataRepo.General.FetchCategories();
|
||||
var accountsTask = DataRepo.General.FetchAccounts();
|
||||
var transactionsTask = DataRepo.General.FetchTransactions();
|
||||
var budgetsTask = DataRepo.General.FetchBudgets();
|
||||
|
||||
await Task.WhenAll(profilesTask, categoriesTask, accountsTask, transactionsTask, budgetsTask);
|
||||
|
||||
Profile = profilesTask.Result;
|
||||
_categories = categoriesTask.Result;
|
||||
_accounts = accountsTask.Result;
|
||||
_transactions = transactionsTask.Result;
|
||||
_budgets = budgetsTask.Result;
|
||||
|
||||
Console.WriteLine("fetched all data");
|
||||
|
||||
_dashboardViewModel = new DashboardViewModel()
|
||||
{
|
||||
parentViewModel = this,
|
||||
Transactions = _transactions,
|
||||
Categories = _categories,
|
||||
Accounts = _accounts,
|
||||
Budgets = _budgets
|
||||
};
|
||||
_dashboardViewModel.initialize();
|
||||
CurrentView = _dashboardViewModel;
|
||||
|
||||
Console.WriteLine("initialized DashboardViewModel");
|
||||
_transactionsViewModel = new TransactionsViewModel()
|
||||
{
|
||||
parentViewModel = this,
|
||||
AllTransactions = _transactions.OrderByDescending(x => x.Date).ToList(),
|
||||
Categories = new ObservableCollection<Category>(_categories.OrderBy(x => x.CreatedAt)),
|
||||
Accounts = new ObservableCollection<Account>(_accounts.OrderBy(x => x.CreatedAt))
|
||||
};
|
||||
await _transactionsViewModel.Initialize();
|
||||
|
||||
Console.WriteLine("initialized TransactionsViewModel");
|
||||
_accountsViewModel = new AccountsViewModel()
|
||||
{
|
||||
parentViewModel = this,
|
||||
Accounts = _accounts,
|
||||
Transactions = _transactions
|
||||
};
|
||||
await _accountsViewModel.Initialize();
|
||||
|
||||
Console.WriteLine("initialized AccountsViewModel");
|
||||
_budgetViewModel = new BudgetViewModel()
|
||||
{
|
||||
parentViewModel = this,
|
||||
Profile = Profile,
|
||||
Budgets = _budgets,
|
||||
Categories = _categories,
|
||||
Transactions = _transactions
|
||||
};
|
||||
await _budgetViewModel.Initialize();
|
||||
Console.WriteLine("initialized BudgetViewModel");
|
||||
TransactionFormViewModel = new TransactionFormViewModel()
|
||||
{
|
||||
parentViewModel = this
|
||||
};
|
||||
Console.WriteLine("initialized TransactionFormViewModel");
|
||||
|
||||
IsDarkTheme = ThemeService.IsDarkTheme;
|
||||
|
||||
ThemeService.SwitchToTheme(Profile?.Theme ?? "system");
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine(e);
|
||||
}
|
||||
}
|
||||
|
||||
[RelayCommand]
|
||||
public void OpenAddTransaction()
|
||||
{
|
||||
if (IsTransactionFormVisible) return;
|
||||
TransactionFormViewModel.SetupForAdd(
|
||||
new ObservableCollection<Category>(_categories),
|
||||
new ObservableCollection<Account>(_accounts)
|
||||
);
|
||||
TransactionFormViewModel.OnSaved = () =>
|
||||
{
|
||||
if (TransactionFormViewModel.ResultTransaction is not null)
|
||||
{
|
||||
var previousItem = _transactionsViewModel.AllTransactions.First(x => x.Date < TransactionFormViewModel.ResultTransaction.Date);
|
||||
var index = _transactionsViewModel.AllTransactions.IndexOf(previousItem);
|
||||
if (index != -1)
|
||||
_transactionsViewModel.AllTransactions.Insert(index, TransactionFormViewModel.ResultTransaction);
|
||||
_transactionsViewModel.LoadPageCommand.Execute(1);
|
||||
}
|
||||
|
||||
CloseTransactionForm();
|
||||
};
|
||||
TransactionFormViewModel.OnCancelled = () => CloseTransactionForm();
|
||||
TransactionFormViewModel.OnDeleted = () =>
|
||||
{
|
||||
if (TransactionFormViewModel.ResultTransaction is { } resultTransaction)
|
||||
{
|
||||
_transactionsViewModel.AllTransactions.Remove(resultTransaction);
|
||||
_transactionsViewModel.LoadPageCommand.Execute(1);
|
||||
}
|
||||
|
||||
CloseTransactionForm();
|
||||
};
|
||||
IsTransactionFormVisible = true;
|
||||
}
|
||||
|
||||
[RelayCommand]
|
||||
public void OpenEditTransaction(Transaction transaction)
|
||||
{
|
||||
TransactionFormViewModel.SetupForEdit(
|
||||
transaction,
|
||||
new ObservableCollection<Category>(_categories),
|
||||
new ObservableCollection<Account>(_accounts)
|
||||
);
|
||||
TransactionFormViewModel.OnSaved = () =>
|
||||
{
|
||||
if (TransactionFormViewModel.ResultTransaction is { } resultTransaction)
|
||||
{
|
||||
var index = _transactionsViewModel.AllTransactions.FindIndex(x => x.Id == transaction.Id);
|
||||
if (index != -1)
|
||||
_transactionsViewModel.AllTransactions[index] = resultTransaction;
|
||||
_transactionsViewModel.LoadPageCommand.Execute(1);
|
||||
}
|
||||
|
||||
CloseTransactionForm();
|
||||
};
|
||||
TransactionFormViewModel.OnCancelled = CloseTransactionForm;
|
||||
TransactionFormViewModel.OnDeleted = () =>
|
||||
{
|
||||
if (TransactionFormViewModel.ResultTransaction is { } resultTransaction)
|
||||
{
|
||||
_transactionsViewModel.AllTransactions.Remove(resultTransaction);
|
||||
_transactionsViewModel.LoadPageCommand.Execute(1);
|
||||
}
|
||||
|
||||
CloseTransactionForm();
|
||||
};
|
||||
IsTransactionFormVisible = true;
|
||||
}
|
||||
|
||||
|
||||
private void CloseTransactionForm()
|
||||
{
|
||||
IsTransactionFormVisible = false;
|
||||
}
|
||||
|
||||
[RelayCommand]
|
||||
|
||||
Reference in New Issue
Block a user