budget mockup, account settings & transactions filter

This commit is contained in:
2024-02-18 00:08:17 +01:00
parent b2da8436e4
commit 44279796c4
18 changed files with 367 additions and 32 deletions

View File

@@ -1,3 +1,4 @@
import 'dart:async';
import 'dart:io';
import 'package:csv/csv.dart';
@@ -49,6 +50,11 @@ class AccountBloc extends Bloc<AccountEvent, AccountState> {
on<AccountLoad>(_onAccountLoad);
// on<AccountExportJSON>(_onAccountImportJSON);
// on<AccountExportCSV>(_onAccountImportJSON);
on<AccountAdd>(_onAccountAdd);
on<AccountRemove>(_onAcountRemove);
on<AccountEditLabel>(_onAccountEditLabel);
on<AccountEditSaving>(_onAccountEditSaving);
on<AccountEditColor>(_onAccountEditColor);
_metadataRepository
.getAccountsStream()
@@ -103,7 +109,7 @@ class AccountBloc extends Bloc<AccountEvent, AccountState> {
String accountLabel = line[3];
if (accounts[accountLabel] == null) {
accounts[accountLabel] = Account(label: accountLabel);
accounts[accountLabel] = Account(label: accountLabel, color: 'FF74feff');
}
return Transaction(
@@ -134,4 +140,72 @@ class AccountBloc extends Bloc<AccountEvent, AccountState> {
state.copyWith(event.subAccounts)
);
}
_onAccountAdd(AccountAdd event, Emitter<AccountState> emit) async {
String uuid = const Uuid().v8();
Account account = Account(
label: 'Account $uuid',
color: 'FF74feff',
saving: false
);
emit(
state.copyWith(await _saveAccount(account))
);
}
_onAcountRemove(AccountRemove event, Emitter<AccountState> emit) async {
Account accountToRemove = event.account;
List<Account> accounts = state.accounts;
List<Transaction> transactions = _transactionsRepository.getTransactions();
if (transactions.any((transaction) => transaction.account == accountToRemove.label)) {
emit(AccountRemoveFail());
emit(AccountState(accounts: accounts));
} else {
accounts.removeWhere((account) => account.label == accountToRemove.label);
emit(AccountRemoveSucess());
emit(
state.copyWith(await _metadataRepository.saveAccounts(accounts))
);
}
}
_onAccountEditLabel(AccountEditLabel event, Emitter<AccountState> emit) async {
Account account = event.account;
account.label = event.label;
emit(
state.copyWith(await _saveAccount(account))
);
}
_onAccountEditSaving(AccountEditSaving event, Emitter<AccountState> emit) async {
Account account = event.account;
account.saving = event.saving;
emit(
state.copyWith(await _saveAccount(account))
);
}
_onAccountEditColor(AccountEditColor event, Emitter<AccountState> emit) async {
Account account = event.account;
account.color = event.color;
emit(
state.copyWith(await _saveAccount(account))
);
}
Future<List<Account>> _saveAccount(Account accountToSave) async {
List<Account> accounts = _metadataRepository.getAccounts();
try {
Account accountFound = accounts.firstWhere((account) => account.label == accountToSave.label);
accountFound.color = accountToSave.color;
accountFound.saving = accountToSave.saving;
} catch (e) {
accounts.add(accountToSave);
}
await _metadataRepository.saveAccounts(accounts);
return accounts;
}
}