complete budget page
This commit is contained in:
@@ -2,7 +2,7 @@ import 'package:dynamic_color/dynamic_color.dart';
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||||
import 'package:flutter_localizations/flutter_localizations.dart';
|
import 'package:flutter_localizations/flutter_localizations.dart';
|
||||||
import 'package:tunas/clients/storage/storage_client.dart';
|
import 'package:tunas/clients/storage/json_storage_client.dart';
|
||||||
import 'package:tunas/pages/home/home_page.dart';
|
import 'package:tunas/pages/home/home_page.dart';
|
||||||
import 'package:tunas/repositories/json/json_repository.dart';
|
import 'package:tunas/repositories/json/json_repository.dart';
|
||||||
import 'package:tunas/repositories/metadata/metadata_repository.dart';
|
import 'package:tunas/repositories/metadata/metadata_repository.dart';
|
||||||
@@ -30,7 +30,7 @@ class AppView extends StatefulWidget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class _AppViewState extends State<AppView> {
|
class _AppViewState extends State<AppView> {
|
||||||
late final StorageClient _storageClient;
|
late final JsonStorageClient _storageClient;
|
||||||
late final JsonRepository _jsonRepository;
|
late final JsonRepository _jsonRepository;
|
||||||
late final TransactionsRepository _transactionsRepository;
|
late final TransactionsRepository _transactionsRepository;
|
||||||
late final MetadataRepository _metadataRepository;
|
late final MetadataRepository _metadataRepository;
|
||||||
@@ -39,7 +39,7 @@ class _AppViewState extends State<AppView> {
|
|||||||
void initState() {
|
void initState() {
|
||||||
super.initState();
|
super.initState();
|
||||||
|
|
||||||
_storageClient = StorageClient();
|
_storageClient = JsonStorageClient();
|
||||||
_jsonRepository = JsonRepository(storageClient: _storageClient);
|
_jsonRepository = JsonRepository(storageClient: _storageClient);
|
||||||
_transactionsRepository = TransactionsRepository(jsonRepository: _jsonRepository);
|
_transactionsRepository = TransactionsRepository(jsonRepository: _jsonRepository);
|
||||||
_metadataRepository = MetadataRepository(jsonRepository: _jsonRepository);
|
_metadataRepository = MetadataRepository(jsonRepository: _jsonRepository);
|
||||||
|
|||||||
@@ -2,7 +2,8 @@ import 'dart:io';
|
|||||||
|
|
||||||
import 'package:path_provider/path_provider.dart';
|
import 'package:path_provider/path_provider.dart';
|
||||||
|
|
||||||
class StorageClient {
|
class JsonStorageClient {
|
||||||
|
|
||||||
save(String filename, String data) async {
|
save(String filename, String data) async {
|
||||||
File file = await _getJson(filename);
|
File file = await _getJson(filename);
|
||||||
await file.writeAsString(data);
|
await file.writeAsString(data);
|
||||||
@@ -123,13 +123,13 @@ class AccountBloc extends Bloc<AccountEvent, AccountState> {
|
|||||||
})
|
})
|
||||||
.toList();
|
.toList();
|
||||||
|
|
||||||
await _metadataRepository.deleteMetadata();
|
_metadataRepository.deleteMetadata();
|
||||||
await _transactionsRepository.deleteTransactions();
|
_transactionsRepository.deleteTransactions();
|
||||||
|
|
||||||
await _metadataRepository.saveAccounts(accounts.values.toList());
|
_metadataRepository.saveAccounts(accounts.values.toList());
|
||||||
await _metadataRepository.saveBudgets([]);
|
_metadataRepository.saveBudgets([]);
|
||||||
await _metadataRepository.saveCategories(categoriesMap.values.toList());
|
_metadataRepository.saveCategories(categoriesMap.values.toList());
|
||||||
await _transactionsRepository.saveTransactions(transactions);
|
_transactionsRepository.saveTransactions(transactions);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -141,7 +141,7 @@ class AccountBloc extends Bloc<AccountEvent, AccountState> {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
_onAccountAdd(AccountAdd event, Emitter<AccountState> emit) async {
|
_onAccountAdd(AccountAdd event, Emitter<AccountState> emit) {
|
||||||
String uuid = const Uuid().v8();
|
String uuid = const Uuid().v8();
|
||||||
Account account = Account(
|
Account account = Account(
|
||||||
label: 'Account $uuid',
|
label: 'Account $uuid',
|
||||||
@@ -149,11 +149,11 @@ class AccountBloc extends Bloc<AccountEvent, AccountState> {
|
|||||||
saving: false
|
saving: false
|
||||||
);
|
);
|
||||||
emit(
|
emit(
|
||||||
state.copyWith(await _saveAccount(account))
|
state.copyWith(_saveAccount(account))
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
_onAcountRemove(AccountRemove event, Emitter<AccountState> emit) async {
|
_onAcountRemove(AccountRemove event, Emitter<AccountState> emit) {
|
||||||
Account accountToRemove = event.account;
|
Account accountToRemove = event.account;
|
||||||
List<Account> accounts = state.accounts;
|
List<Account> accounts = state.accounts;
|
||||||
List<Transaction> transactions = _transactionsRepository.getTransactions();
|
List<Transaction> transactions = _transactionsRepository.getTransactions();
|
||||||
@@ -165,33 +165,33 @@ class AccountBloc extends Bloc<AccountEvent, AccountState> {
|
|||||||
accounts.removeWhere((account) => account.label == accountToRemove.label);
|
accounts.removeWhere((account) => account.label == accountToRemove.label);
|
||||||
emit(AccountRemoveSucess());
|
emit(AccountRemoveSucess());
|
||||||
emit(
|
emit(
|
||||||
state.copyWith(await _metadataRepository.saveAccounts(accounts))
|
state.copyWith(_metadataRepository.saveAccounts(accounts))
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_onAccountEditLabel(AccountEditLabel event, Emitter<AccountState> emit) async {
|
_onAccountEditLabel(AccountEditLabel event, Emitter<AccountState> emit) {
|
||||||
Account account = event.account;
|
Account account = event.account;
|
||||||
// TODO check for existance, rename every transaction
|
// TODO check for existance, rename every transaction
|
||||||
}
|
}
|
||||||
|
|
||||||
_onAccountEditSaving(AccountEditSaving event, Emitter<AccountState> emit) async {
|
_onAccountEditSaving(AccountEditSaving event, Emitter<AccountState> emit) {
|
||||||
Account account = event.account;
|
Account account = event.account;
|
||||||
account.saving = event.saving;
|
account.saving = event.saving;
|
||||||
emit(
|
emit(
|
||||||
state.copyWith(await _saveAccount(account))
|
state.copyWith(_saveAccount(account))
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
_onAccountEditColor(AccountEditColor event, Emitter<AccountState> emit) async {
|
_onAccountEditColor(AccountEditColor event, Emitter<AccountState> emit) {
|
||||||
Account account = event.account;
|
Account account = event.account;
|
||||||
account.color = event.color;
|
account.color = event.color;
|
||||||
emit(
|
emit(
|
||||||
state.copyWith(await _saveAccount(account))
|
state.copyWith(_saveAccount(account))
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<List<Account>> _saveAccount(Account accountToSave) async {
|
List<Account> _saveAccount(Account accountToSave) {
|
||||||
List<Account> accounts = _metadataRepository.getAccounts();
|
List<Account> accounts = _metadataRepository.getAccounts();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -206,7 +206,7 @@ class AccountBloc extends Bloc<AccountEvent, AccountState> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
await _metadataRepository.saveAccounts(accounts);
|
_metadataRepository.saveAccounts(accounts);
|
||||||
return accounts;
|
return accounts;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,55 +4,66 @@ import 'package:equatable/equatable.dart';
|
|||||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||||
import 'package:tunas/repositories/metadata/models/budget.dart';
|
import 'package:tunas/repositories/metadata/models/budget.dart';
|
||||||
import 'package:tunas/repositories/metadata/metadata_repository.dart';
|
import 'package:tunas/repositories/metadata/metadata_repository.dart';
|
||||||
|
import 'package:tunas/repositories/transactions/transactions_repository.dart';
|
||||||
|
|
||||||
part 'budget_event.dart';
|
part 'budget_event.dart';
|
||||||
part 'budget_state.dart';
|
part 'budget_state.dart';
|
||||||
|
|
||||||
class BudgetBloc extends Bloc<BudgetEvent, BudgetState> {
|
class BudgetBloc extends Bloc<BudgetEvent, BudgetState> {
|
||||||
final MetadataRepository _metadataRepository;
|
final MetadataRepository _metadataRepository;
|
||||||
|
final TransactionsRepository _transactionsRepository;
|
||||||
|
Timer? setValueTimer;
|
||||||
|
|
||||||
BudgetBloc({required MetadataRepository metadataRepository}) : _metadataRepository = metadataRepository, super(const BudgetState()) {
|
BudgetBloc({required MetadataRepository metadataRepository, required TransactionsRepository transactionsRepository}) : _metadataRepository = metadataRepository, _transactionsRepository = transactionsRepository, super(const BudgetState()) {
|
||||||
on<BudgetsLoad>(_onBudgetsLoad);
|
on<BudgetsLoad>(_onBudgetsLoad);
|
||||||
on<BudgetAdd>(_onBudgetAdd);
|
on<BudgetAdd>(_onBudgetAdd);
|
||||||
on<BudgetRemove>(_onBudgetRemove);
|
on<BudgetRemove>(_onBudgetRemove);
|
||||||
on<BudgetSetValue>(_onBudgetSetValue);
|
on<BudgetSetValue>(_onBudgetSetValue);
|
||||||
|
on<BudgetSetLabel>(_onBudgetSetLabel);
|
||||||
on<BudgetCompareNext>(_onBudgetCompareNext);
|
on<BudgetCompareNext>(_onBudgetCompareNext);
|
||||||
on<BudgetComparePrevious>(_onBudgetComparePrevious);
|
on<BudgetComparePrevious>(_onBudgetComparePrevious);
|
||||||
|
on<BudgetSetCompare>(_onBudgetSetCompare);
|
||||||
|
on<BudgetSetInitial>(_onBudgetSetInitial);
|
||||||
|
|
||||||
_metadataRepository
|
_metadataRepository
|
||||||
.getBudgetsStream()
|
.getBudgetsStream()
|
||||||
.listen((budgets) => add(BudgetsLoad(budgets)));
|
.listen((budgets) => add(BudgetsLoad(budgets)));
|
||||||
|
|
||||||
|
_transactionsRepository
|
||||||
|
.getTransactionsStream()
|
||||||
|
.listen((transactions) => add(BudgetSetCompare()));
|
||||||
}
|
}
|
||||||
|
|
||||||
_onBudgetsLoad(
|
_onBudgetsLoad(
|
||||||
BudgetsLoad event, Emitter<BudgetState> emit
|
BudgetsLoad event, Emitter<BudgetState> emit
|
||||||
) {
|
) {
|
||||||
emit(state.copyWith(
|
emit(_computeState(event.budgets, null));
|
||||||
budgets: event.budgets,
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
FutureOr<void> _onBudgetAdd(BudgetAdd event, Emitter<BudgetState> emit) async {
|
FutureOr<void> _onBudgetAdd(BudgetAdd event, Emitter<BudgetState> emit) {
|
||||||
Budget budget = Budget(
|
Budget budget = Budget(
|
||||||
label: event.label,
|
label: event.label,
|
||||||
);
|
);
|
||||||
|
|
||||||
emit(_computeState(await _saveBudget(budget)));
|
List<Budget> budgets = _computeBudgets(budget);
|
||||||
|
_saveBudget(budgets);
|
||||||
|
emit(_computeState(budgets, null));
|
||||||
}
|
}
|
||||||
|
|
||||||
FutureOr<void> _onBudgetRemove(BudgetRemove event, Emitter<BudgetState> emit) async {
|
FutureOr<void> _onBudgetRemove(BudgetRemove event, Emitter<BudgetState> emit) {
|
||||||
List<Budget> budgets = _metadataRepository.getBudgets();
|
List<Budget> budgets = _metadataRepository.getBudgets();
|
||||||
Budget budgetToRemove = event.budget;
|
Budget budgetToRemove = event.budget;
|
||||||
|
|
||||||
budgets.removeWhere((budget) => budget.label == budgetToRemove.label);
|
budgets.removeWhere((budget) => budget.label == budgetToRemove.label);
|
||||||
emit(_computeState(await _metadataRepository.saveBudgets(budgets)));
|
emit(_computeState(_metadataRepository.saveBudgets(budgets), null));
|
||||||
}
|
}
|
||||||
|
|
||||||
FutureOr<void> _onBudgetSetValue(BudgetSetValue event, Emitter<BudgetState> emit) async {
|
void _onBudgetSetValue(BudgetSetValue event, Emitter<BudgetState> emit) {
|
||||||
Budget budgetToUpdate = event.budget;
|
Budget budgetToUpdate = event.budget;
|
||||||
|
double newValue = event.value;
|
||||||
|
|
||||||
if (state.remainingBudget - (event.value - budgetToUpdate.value) < 0) {
|
if (state.remainingBudget - (event.value - budgetToUpdate.value) < 0) {
|
||||||
return;
|
newValue = event.budget.value + state.remainingBudget;
|
||||||
}
|
}
|
||||||
|
|
||||||
// if (state.remainingBudget - event.value < 0 && state.remainingBudget < 10) {
|
// if (state.remainingBudget - event.value < 0 && state.remainingBudget < 10) {
|
||||||
@@ -60,18 +71,112 @@ class BudgetBloc extends Bloc<BudgetEvent, BudgetState> {
|
|||||||
// } else {
|
// } else {
|
||||||
// budgetToUpdate.value = event.value;
|
// budgetToUpdate.value = event.value;
|
||||||
// }
|
// }
|
||||||
budgetToUpdate.value = event.value;
|
budgetToUpdate.value = newValue;
|
||||||
|
List<Budget> budgets = _computeBudgets(budgetToUpdate);
|
||||||
|
emit(_computeState(budgets, null));
|
||||||
|
|
||||||
emit(_computeState(await _saveBudget(budgetToUpdate)));
|
setValueTimer?.cancel();
|
||||||
|
setValueTimer = Timer(
|
||||||
|
const Duration(milliseconds: 100),
|
||||||
|
() {
|
||||||
|
_saveBudget(budgets);
|
||||||
|
}
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
FutureOr<void> _onBudgetCompareNext(BudgetCompareNext event, Emitter<BudgetState> emit) {
|
void _onBudgetSetLabel(BudgetSetLabel event, Emitter<BudgetState> emit) {
|
||||||
|
Budget budgetToUpdate = event.budget;
|
||||||
|
|
||||||
|
budgetToUpdate.label = event.label;
|
||||||
|
List<Budget> budgets = _computeBudgets(budgetToUpdate);
|
||||||
|
_saveBudget(budgets);
|
||||||
|
emit(_computeState(budgets, null));
|
||||||
}
|
}
|
||||||
|
|
||||||
FutureOr<void> _onBudgetComparePrevious(BudgetComparePrevious event, Emitter<BudgetState> emit) {
|
void _onBudgetCompareNext(BudgetCompareNext event, Emitter<BudgetState> emit) {
|
||||||
|
num compareMonth = state.compareMonth;
|
||||||
|
num compareYear = state.compareYear;
|
||||||
|
if (state.compareMonth == 12) {
|
||||||
|
compareMonth = 1;
|
||||||
|
compareYear++;
|
||||||
|
} else {
|
||||||
|
compareMonth++;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (state.lastDate != null && state.lastDate!.isBefore(DateTime(compareYear.toInt(), compareMonth.toInt()))) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
final compareResult = _computeCompareBudget(state.budgets, compareYear, compareMonth);
|
||||||
|
emit(state.copyWith(
|
||||||
|
compareBudgets: compareResult.$1,
|
||||||
|
otherBudgets: compareResult.$2,
|
||||||
|
compareMonth: compareMonth,
|
||||||
|
compareYear: compareYear,
|
||||||
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<List<Budget>> _saveBudget(Budget budgetToSave) async {
|
void _onBudgetComparePrevious(BudgetComparePrevious event, Emitter<BudgetState> emit) {
|
||||||
|
num compareMonth = state.compareMonth;
|
||||||
|
num compareYear = state.compareYear;
|
||||||
|
if (state.compareMonth == 1) {
|
||||||
|
compareMonth = 12;
|
||||||
|
compareYear--;
|
||||||
|
} else {
|
||||||
|
compareMonth--;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (state.firstDate != null && state.firstDate!.isAfter(DateTime(compareYear.toInt(), compareMonth.toInt()))) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
final compareResult = _computeCompareBudget(state.budgets, compareYear, compareMonth);
|
||||||
|
emit(state.copyWith(
|
||||||
|
compareBudgets: compareResult.$1,
|
||||||
|
otherBudgets: compareResult.$2,
|
||||||
|
compareMonth: compareMonth,
|
||||||
|
compareYear: compareYear,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
_onBudgetSetCompare(BudgetSetCompare event, Emitter<BudgetState> emit) {
|
||||||
|
DateTime firstDate = DateTime.now();
|
||||||
|
DateTime lastDate = DateTime.fromMicrosecondsSinceEpoch(0);
|
||||||
|
|
||||||
|
for (var transaction in _transactionsRepository.getTransactions()) {
|
||||||
|
if (firstDate.compareTo(transaction.date) > 0) {
|
||||||
|
firstDate = transaction.date;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (lastDate.compareTo(transaction.date) < 0) {
|
||||||
|
lastDate = transaction.date;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
num compareMonth = state.compareMonth;
|
||||||
|
num compareYear = state.compareYear;
|
||||||
|
|
||||||
|
if (compareMonth == 1 && compareYear == 2000) {
|
||||||
|
compareMonth = lastDate.month;
|
||||||
|
compareYear = lastDate.year;
|
||||||
|
}
|
||||||
|
|
||||||
|
final compareResult = _computeCompareBudget(state.budgets, compareYear, compareMonth);
|
||||||
|
emit(state.copyWith(
|
||||||
|
firstDate: firstDate,
|
||||||
|
lastDate: lastDate,
|
||||||
|
compareMonth: compareMonth,
|
||||||
|
compareYear: compareYear,
|
||||||
|
compareBudgets: compareResult.$1,
|
||||||
|
otherBudgets: compareResult.$2,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
void _onBudgetSetInitial(BudgetSetInitial event, Emitter<BudgetState> emit) {
|
||||||
|
emit(_computeState(state.budgets, event.value));
|
||||||
|
}
|
||||||
|
|
||||||
|
List<Budget> _computeBudgets(Budget budgetToSave) {
|
||||||
List<Budget> budgets = _metadataRepository.getBudgets();
|
List<Budget> budgets = _metadataRepository.getBudgets();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -85,14 +190,48 @@ class BudgetBloc extends Bloc<BudgetEvent, BudgetState> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// _metadataRepository.saveBudgets(budgets);
|
|
||||||
return budgets;
|
return budgets;
|
||||||
}
|
}
|
||||||
|
|
||||||
BudgetState _computeState(List<Budget> budgets) {
|
List<Budget> _saveBudget(List<Budget> budgets) {
|
||||||
|
return _metadataRepository.saveBudgets(budgets);
|
||||||
|
}
|
||||||
|
|
||||||
|
(List<Budget> compareBudgets, List<Budget> otherBudgets) _computeCompareBudget(List<Budget> budgets, num year, num month) {
|
||||||
|
Map<String, Budget> compareBudgetMap = { for (var budget in budgets) budget.label : Budget(label: budget.label)};
|
||||||
|
Budget otherBudget = Budget(label: 'Hors budget');
|
||||||
|
Map<String, Budget> otherBudgetMap = {};
|
||||||
|
List<Budget> compareBudgets = [];
|
||||||
|
_transactionsRepository.getTransactions()
|
||||||
|
.where((transaction) => transaction.value < 0 && transaction.date.year == year && transaction.date.month == month)
|
||||||
|
.forEach((transaction) {
|
||||||
|
Budget? budget = compareBudgetMap[transaction.category];
|
||||||
|
if (budget == null) {
|
||||||
|
otherBudget.value += transaction.value.abs();
|
||||||
|
Budget? otherBudgetFromMap = otherBudgetMap[transaction.category];
|
||||||
|
if (otherBudgetFromMap == null) {
|
||||||
|
otherBudgetMap[transaction.category] = Budget(label: transaction.category, value: transaction.value.abs());
|
||||||
|
} else {
|
||||||
|
otherBudgetFromMap.value += transaction.value.abs();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
budget.value += transaction.value.abs();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
compareBudgets.addAll(compareBudgetMap.values);
|
||||||
|
compareBudgets.add(otherBudget);
|
||||||
|
return (compareBudgets, otherBudgetMap.values.toList()..sort((a, b) => b.value.compareTo(a.value)));
|
||||||
|
}
|
||||||
|
|
||||||
|
BudgetState _computeState(List<Budget> budgets, double? initialBudget) {
|
||||||
|
final compareResult = _computeCompareBudget(state.budgets, state.compareYear, state.compareMonth);
|
||||||
return state.copyWith(
|
return state.copyWith(
|
||||||
budgets: budgets,
|
budgets: budgets,
|
||||||
remainingBudget: state.initialBudget - budgets.map((budget) => budget.value).reduce((value, element) => value + element),
|
initialBudget: (initialBudget ?? state.initialBudget),
|
||||||
|
remainingBudget: (initialBudget ?? state.initialBudget) - budgets.map((budget) => budget.value).reduce((value, element) => value + element),
|
||||||
|
compareBudgets: compareResult.$1,
|
||||||
|
otherBudgets: compareResult.$2,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -43,5 +43,24 @@ final class BudgetSetValue extends BudgetEvent {
|
|||||||
List<Object> get props => [budget, value];
|
List<Object> get props => [budget, value];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
final class BudgetSetLabel extends BudgetEvent {
|
||||||
|
final Budget budget;
|
||||||
|
final String label;
|
||||||
|
|
||||||
|
const BudgetSetLabel(this.budget, this.label);
|
||||||
|
|
||||||
|
@override
|
||||||
|
List<Object> get props => [budget, label];
|
||||||
|
}
|
||||||
|
|
||||||
final class BudgetCompareNext extends BudgetEvent {}
|
final class BudgetCompareNext extends BudgetEvent {}
|
||||||
final class BudgetComparePrevious extends BudgetEvent {}
|
final class BudgetComparePrevious extends BudgetEvent {}
|
||||||
|
final class BudgetSetCompare extends BudgetEvent {}
|
||||||
|
final class BudgetSetInitial extends BudgetEvent {
|
||||||
|
final double value;
|
||||||
|
|
||||||
|
const BudgetSetInitial(this.value);
|
||||||
|
|
||||||
|
@override
|
||||||
|
List<Object> get props => [value];
|
||||||
|
}
|
||||||
@@ -5,21 +5,46 @@ final class BudgetState {
|
|||||||
final double initialBudget;
|
final double initialBudget;
|
||||||
final double remainingBudget;
|
final double remainingBudget;
|
||||||
|
|
||||||
|
final List<Budget> compareBudgets;
|
||||||
|
final List<Budget> otherBudgets;
|
||||||
|
final num compareYear;
|
||||||
|
final num compareMonth;
|
||||||
|
final DateTime? firstDate;
|
||||||
|
final DateTime? lastDate;
|
||||||
|
|
||||||
const BudgetState({
|
const BudgetState({
|
||||||
this.budgets = const [],
|
this.budgets = const [],
|
||||||
this.initialBudget = 2300.0,
|
this.initialBudget = 2300.0,
|
||||||
this.remainingBudget = 2300.0,
|
this.remainingBudget = 2300.0,
|
||||||
|
this.compareBudgets = const [],
|
||||||
|
this.otherBudgets = const [],
|
||||||
|
this.compareYear = 2000,
|
||||||
|
this.compareMonth = 1,
|
||||||
|
this.firstDate,
|
||||||
|
this.lastDate,
|
||||||
});
|
});
|
||||||
|
|
||||||
BudgetState copyWith({
|
BudgetState copyWith({
|
||||||
List<Budget>? budgets,
|
List<Budget>? budgets,
|
||||||
double? initialBudget,
|
double? initialBudget,
|
||||||
double? remainingBudget,
|
double? remainingBudget,
|
||||||
|
List<Budget>? compareBudgets,
|
||||||
|
List<Budget>? otherBudgets,
|
||||||
|
num? compareYear,
|
||||||
|
num? compareMonth,
|
||||||
|
DateTime? firstDate,
|
||||||
|
DateTime? lastDate,
|
||||||
}) {
|
}) {
|
||||||
return BudgetState(
|
return BudgetState(
|
||||||
budgets: budgets ?? this.budgets,
|
budgets: budgets ?? this.budgets,
|
||||||
initialBudget: initialBudget ?? this.initialBudget,
|
initialBudget: initialBudget ?? this.initialBudget,
|
||||||
remainingBudget: remainingBudget ?? this.remainingBudget,
|
remainingBudget: remainingBudget ?? this.remainingBudget,
|
||||||
|
compareBudgets: compareBudgets ?? this.compareBudgets,
|
||||||
|
otherBudgets: otherBudgets ?? this.otherBudgets,
|
||||||
|
compareYear: compareYear ?? this.compareYear,
|
||||||
|
compareMonth: compareMonth ?? this.compareMonth,
|
||||||
|
firstDate: firstDate ?? this.firstDate,
|
||||||
|
lastDate: lastDate ?? this.lastDate,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -42,29 +42,29 @@ class CategoryBloc extends Bloc<CategoryEvent, CategoryState> {
|
|||||||
emit(_computeState(event.categories));
|
emit(_computeState(event.categories));
|
||||||
}
|
}
|
||||||
|
|
||||||
FutureOr<void> _onCategoryEditColor(CategoryEditColor event, Emitter<CategoryState> emit) async {
|
FutureOr<void> _onCategoryEditColor(CategoryEditColor event, Emitter<CategoryState> emit) {
|
||||||
Category category = event.category;
|
Category category = event.category;
|
||||||
category.color = event.color;
|
category.color = event.color;
|
||||||
emit(_computeState(await _saveCategory(category)));
|
emit(_computeState(_saveCategory(category)));
|
||||||
}
|
}
|
||||||
|
|
||||||
FutureOr<void> _onCategoryEditTransfert(CategoryEditTransfert event, Emitter<CategoryState> emit) async {
|
FutureOr<void> _onCategoryEditTransfert(CategoryEditTransfert event, Emitter<CategoryState> emit) {
|
||||||
Category category = event.category;
|
Category category = event.category;
|
||||||
category.transfert = event.transfert;
|
category.transfert = event.transfert;
|
||||||
emit(_computeState(await _saveCategory(category)));
|
emit(_computeState(_saveCategory(category)));
|
||||||
}
|
}
|
||||||
|
|
||||||
FutureOr<void> _onCategoryEditEssential(CategoryEditEssential event, Emitter<CategoryState> emit) async {
|
FutureOr<void> _onCategoryEditEssential(CategoryEditEssential event, Emitter<CategoryState> emit) {
|
||||||
Category category = event.category;
|
Category category = event.category;
|
||||||
category.essential = event.essential;
|
category.essential = event.essential;
|
||||||
emit(_computeState(await _saveCategory(category)));
|
emit(_computeState(_saveCategory(category)));
|
||||||
}
|
}
|
||||||
|
|
||||||
FutureOr<void> _onCategoryEditLabel(CategoryEditLabel event, Emitter<CategoryState> emit) async {
|
FutureOr<void> _onCategoryEditLabel(CategoryEditLabel event, Emitter<CategoryState> emit) {
|
||||||
// TODO check for existance, rename every transaction
|
// TODO check for existance, rename every transaction
|
||||||
}
|
}
|
||||||
|
|
||||||
FutureOr<void> _onCategoryRemove(CategoryRemove event, Emitter<CategoryState> emit) async {
|
FutureOr<void> _onCategoryRemove(CategoryRemove event, Emitter<CategoryState> emit) {
|
||||||
CategoryState originalCategoryState = state.copyWith();
|
CategoryState originalCategoryState = state.copyWith();
|
||||||
Category categoryToRemove = event.category;
|
Category categoryToRemove = event.category;
|
||||||
List<Category> categories = state.categories;
|
List<Category> categories = state.categories;
|
||||||
@@ -76,21 +76,21 @@ class CategoryBloc extends Bloc<CategoryEvent, CategoryState> {
|
|||||||
} else {
|
} else {
|
||||||
categories.removeWhere((category) => category.label == categoryToRemove.label);
|
categories.removeWhere((category) => category.label == categoryToRemove.label);
|
||||||
emit(CategoryRemoveSucess());
|
emit(CategoryRemoveSucess());
|
||||||
emit(_computeState(await _metadataRepository.saveCategories(categories)));
|
emit(_computeState(_metadataRepository.saveCategories(categories)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
FutureOr<void> _onCategoryAdd(CategoryAdd event, Emitter<CategoryState> emit) async {
|
FutureOr<void> _onCategoryAdd(CategoryAdd event, Emitter<CategoryState> emit) {
|
||||||
String uuid = const Uuid().v8();
|
String uuid = const Uuid().v8();
|
||||||
Category category = Category(
|
Category category = Category(
|
||||||
label: 'Category $uuid',
|
label: 'Category $uuid',
|
||||||
color: 'FF74feff',
|
color: 'FF74feff',
|
||||||
);
|
);
|
||||||
|
|
||||||
emit(_computeState(await _saveCategory(category)));
|
emit(_computeState(_saveCategory(category)));
|
||||||
}
|
}
|
||||||
|
|
||||||
_saveCategory(Category categoryToSave) async {
|
List<Category> _saveCategory(Category categoryToSave) {
|
||||||
List<Category> categories = _metadataRepository.getCategories();
|
List<Category> categories = _metadataRepository.getCategories();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -106,7 +106,7 @@ class CategoryBloc extends Bloc<CategoryEvent, CategoryState> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
await _metadataRepository.saveCategories(categories);
|
_metadataRepository.saveCategories(categories);
|
||||||
return categories;
|
return categories;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -145,7 +145,7 @@ class TransactionBloc extends Bloc<TransactionEvent, TransactionState> {
|
|||||||
|
|
||||||
_onTransactionAddDialog(
|
_onTransactionAddDialog(
|
||||||
TransactionAdd event, Emitter<TransactionState> emit
|
TransactionAdd event, Emitter<TransactionState> emit
|
||||||
) async {
|
) {
|
||||||
if (state.isValid) {
|
if (state.isValid) {
|
||||||
List<Transaction> transactions = state.transactions;
|
List<Transaction> transactions = state.transactions;
|
||||||
Transaction? currentTransaction = state.currentTransaction;
|
Transaction? currentTransaction = state.currentTransaction;
|
||||||
@@ -163,7 +163,7 @@ class TransactionBloc extends Bloc<TransactionEvent, TransactionState> {
|
|||||||
));
|
));
|
||||||
final computeResult = _computeTransactionLine(transactions);
|
final computeResult = _computeTransactionLine(transactions);
|
||||||
|
|
||||||
await _transactionsRepository.saveTransactions(transactions);
|
_transactionsRepository.saveTransactions(transactions);
|
||||||
|
|
||||||
emit(state.copyWith(
|
emit(state.copyWith(
|
||||||
currentTransaction: null,
|
currentTransaction: null,
|
||||||
@@ -208,13 +208,13 @@ class TransactionBloc extends Bloc<TransactionEvent, TransactionState> {
|
|||||||
|
|
||||||
_onTransactionDeleteCurrent(
|
_onTransactionDeleteCurrent(
|
||||||
TransactionDeleteCurrent event, Emitter<TransactionState> emit
|
TransactionDeleteCurrent event, Emitter<TransactionState> emit
|
||||||
) async {
|
) {
|
||||||
Transaction? currentTransaction = state.currentTransaction;
|
Transaction? currentTransaction = state.currentTransaction;
|
||||||
if (currentTransaction != null) {
|
if (currentTransaction != null) {
|
||||||
List<Transaction> transactions = state.transactions;
|
List<Transaction> transactions = state.transactions;
|
||||||
transactions.removeWhere((transaction) => transaction.uuid == currentTransaction.uuid);
|
transactions.removeWhere((transaction) => transaction.uuid == currentTransaction.uuid);
|
||||||
final computeResult = _computeTransactionLine(transactions);
|
final computeResult = _computeTransactionLine(transactions);
|
||||||
await _transactionsRepository.saveTransactions(transactions);
|
_transactionsRepository.saveTransactions(transactions);
|
||||||
emit(state.copyWith(
|
emit(state.copyWith(
|
||||||
currentTransaction: null,
|
currentTransaction: null,
|
||||||
transactionDate: const TransactionDate.pure(),
|
transactionDate: const TransactionDate.pure(),
|
||||||
|
|||||||
135
lib/pages/budgets/widgets/budget_comparator.dart
Normal file
135
lib/pages/budgets/widgets/budget_comparator.dart
Normal file
@@ -0,0 +1,135 @@
|
|||||||
|
import 'package:fl_chart/fl_chart.dart';
|
||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||||
|
import 'package:intl/intl.dart';
|
||||||
|
import 'package:tunas/domains/budget/budget_bloc.dart';
|
||||||
|
import 'package:tunas/pages/common/titled_container.dart';
|
||||||
|
import 'package:tunas/repositories/metadata/models/budget.dart';
|
||||||
|
|
||||||
|
class BudgetComparator extends StatelessWidget {
|
||||||
|
const BudgetComparator({super.key});
|
||||||
|
|
||||||
|
List<RadarDataSet> _computeDataSet(BuildContext context, List<Budget> targetBudgets, List<Budget> realBudgets) {
|
||||||
|
RadarDataSet targetDataSet = RadarDataSet(
|
||||||
|
fillColor: Theme.of(context).colorScheme.primary.withAlpha(100),
|
||||||
|
borderColor: Theme.of(context).colorScheme.primary,
|
||||||
|
entryRadius: 5,
|
||||||
|
dataEntries: targetBudgets.map((budget) => RadarEntry(value: budget.value)).toList(),
|
||||||
|
);
|
||||||
|
if (realBudgets.isNotEmpty) {
|
||||||
|
targetDataSet.dataEntries.add(const RadarEntry(value: 0));
|
||||||
|
}
|
||||||
|
RadarDataSet realDataSet = RadarDataSet(
|
||||||
|
fillColor: Theme.of(context).colorScheme.onPrimary.withAlpha(100),
|
||||||
|
borderColor: Theme.of(context).colorScheme.onPrimary,
|
||||||
|
entryRadius: 8,
|
||||||
|
dataEntries: realBudgets.map((budget) => RadarEntry(value: budget.value)).toList(),
|
||||||
|
);
|
||||||
|
return realBudgets.isEmpty ? [RadarDataSet(dataEntries: [const RadarEntry(value: 1), const RadarEntry(value: 2), const RadarEntry(value: 3)])] : [realDataSet, targetDataSet];
|
||||||
|
}
|
||||||
|
|
||||||
|
RadarChartTitle _computeDataSetTitle(int index, List<Budget> realBudgets) {
|
||||||
|
return realBudgets.isEmpty ? const RadarChartTitle(text: 'No data') : RadarChartTitle(text: realBudgets[index].label);
|
||||||
|
}
|
||||||
|
|
||||||
|
List<Widget> _computeOtherBudgets(BuildContext context, List<Budget> otherBudgets) {
|
||||||
|
List<Widget> list = [
|
||||||
|
const Text('Hors budget'),
|
||||||
|
Container(
|
||||||
|
margin: const EdgeInsets.only(bottom: 10),
|
||||||
|
decoration: BoxDecoration(
|
||||||
|
border: Border(
|
||||||
|
bottom: BorderSide(
|
||||||
|
width: 2,
|
||||||
|
color: Theme.of(context).colorScheme.onPrimaryContainer
|
||||||
|
)
|
||||||
|
)
|
||||||
|
),
|
||||||
|
)
|
||||||
|
];
|
||||||
|
|
||||||
|
list.addAll(otherBudgets.map((budget) => Row(
|
||||||
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||||
|
children: [
|
||||||
|
Row(
|
||||||
|
children: [
|
||||||
|
Container(width: 5),
|
||||||
|
Text(budget.label),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
Text(
|
||||||
|
NumberFormat("#00 €").format(budget.value.abs()),
|
||||||
|
style: const TextStyle(
|
||||||
|
fontFamily: 'NovaMono',
|
||||||
|
)
|
||||||
|
)
|
||||||
|
],
|
||||||
|
)));
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
return BlocBuilder<BudgetBloc, BudgetState>(
|
||||||
|
builder: (context, state) => TitledContainer(
|
||||||
|
title: 'Compare',
|
||||||
|
child: Column(
|
||||||
|
children: [
|
||||||
|
Row(
|
||||||
|
mainAxisAlignment: MainAxisAlignment.end,
|
||||||
|
children: [
|
||||||
|
IconButton(
|
||||||
|
onPressed: () => context.read<BudgetBloc>().add(BudgetComparePrevious()),
|
||||||
|
icon: const Icon(Icons.skip_previous)
|
||||||
|
),
|
||||||
|
Text('${NumberFormat('00', 'fr_FR').format(state.compareMonth)} - ${state.compareYear}'),
|
||||||
|
IconButton(
|
||||||
|
onPressed: () => context.read<BudgetBloc>().add(BudgetCompareNext()),
|
||||||
|
icon: const Icon(Icons.skip_next)
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
Row(
|
||||||
|
children: [
|
||||||
|
Container(
|
||||||
|
height: 300,
|
||||||
|
width: 250,
|
||||||
|
padding: const EdgeInsets.all(10),
|
||||||
|
decoration: BoxDecoration(
|
||||||
|
borderRadius: BorderRadius.circular(5),
|
||||||
|
color: Theme.of(context).colorScheme.secondaryContainer
|
||||||
|
),
|
||||||
|
child: SingleChildScrollView(
|
||||||
|
scrollDirection: Axis.vertical,
|
||||||
|
child: Column(
|
||||||
|
mainAxisAlignment: MainAxisAlignment.center,
|
||||||
|
crossAxisAlignment: CrossAxisAlignment.start,
|
||||||
|
children: _computeOtherBudgets(context, state.otherBudgets),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
),
|
||||||
|
Expanded(
|
||||||
|
child: AspectRatio(
|
||||||
|
aspectRatio: 1.3,
|
||||||
|
child: RadarChart(
|
||||||
|
RadarChartData(
|
||||||
|
titlePositionPercentageOffset: 0.15,
|
||||||
|
tickBorderData: BorderSide(color: Theme.of(context).colorScheme.secondary, width: 1),
|
||||||
|
gridBorderData: BorderSide(color: Theme.of(context).colorScheme.secondary, width: 2),
|
||||||
|
radarBorderData: BorderSide(color: Theme.of(context).colorScheme.secondary, width: 3),
|
||||||
|
radarBackgroundColor: Theme.of(context).colorScheme.secondaryContainer.withAlpha(100),
|
||||||
|
radarShape: RadarShape.circle,
|
||||||
|
dataSets: _computeDataSet(context, state.budgets, state.compareBudgets),
|
||||||
|
getTitle: (index, angle) => _computeDataSetTitle(index, state.compareBudgets),
|
||||||
|
)
|
||||||
|
),
|
||||||
|
)
|
||||||
|
),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
133
lib/pages/budgets/widgets/budget_maker.dart
Normal file
133
lib/pages/budgets/widgets/budget_maker.dart
Normal file
@@ -0,0 +1,133 @@
|
|||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||||
|
import 'package:intl/intl.dart';
|
||||||
|
import 'package:tunas/domains/budget/budget_bloc.dart';
|
||||||
|
import 'package:tunas/domains/category/category_bloc.dart';
|
||||||
|
import 'package:tunas/pages/common/titled_container.dart';
|
||||||
|
import 'package:tunas/repositories/metadata/models/budget.dart';
|
||||||
|
|
||||||
|
class BudgetMaker extends StatelessWidget {
|
||||||
|
const BudgetMaker({super.key});
|
||||||
|
|
||||||
|
List<Widget> _computeBudgetLines(BuildContext context, List<Budget> budgets, double initialBudget, double remainingBudget) {
|
||||||
|
final categoryState = context.watch<CategoryBloc>().state;
|
||||||
|
List<Widget> list = [];
|
||||||
|
|
||||||
|
list.add(
|
||||||
|
Container(
|
||||||
|
margin: const EdgeInsets.only(top: 5, bottom: 5),
|
||||||
|
padding: const EdgeInsets.only(bottom: 10),
|
||||||
|
child: Flex(
|
||||||
|
direction: Axis.horizontal,
|
||||||
|
children: [Expanded(
|
||||||
|
child:
|
||||||
|
TextFormField(
|
||||||
|
keyboardType: const TextInputType.numberWithOptions(decimal: false),
|
||||||
|
decoration: InputDecoration(
|
||||||
|
icon: const Icon(Icons.euro),
|
||||||
|
hintText: '\$\$\$',
|
||||||
|
border: OutlineInputBorder(
|
||||||
|
borderRadius: BorderRadius.circular(5),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
initialValue: initialBudget.toString(),
|
||||||
|
onChanged: (value) => context.read<BudgetBloc>().add(BudgetSetInitial(double.parse(value)))
|
||||||
|
)
|
||||||
|
|
||||||
|
)]
|
||||||
|
))
|
||||||
|
);
|
||||||
|
|
||||||
|
list.addAll(budgets.map((budget) => Row(
|
||||||
|
children: [
|
||||||
|
Expanded(
|
||||||
|
child: Row(
|
||||||
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||||
|
children: [
|
||||||
|
Expanded(
|
||||||
|
child: DropdownButtonFormField<String>(
|
||||||
|
value: budget.label,
|
||||||
|
onChanged: (value) => context.read<BudgetBloc>().add(BudgetSetLabel(budget, value!)),
|
||||||
|
items: categoryState.categories.map((e) => DropdownMenuItem(value: e.label, child: Text(e.label))).toList(),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
const SizedBox(width: 20),
|
||||||
|
Text(
|
||||||
|
NumberFormat('####000 €', 'fr_FR').format(budget.value),
|
||||||
|
style: const TextStyle(
|
||||||
|
fontFamily: 'NovaMono',
|
||||||
|
fontWeight: FontWeight.bold,
|
||||||
|
fontSize: 15,
|
||||||
|
)
|
||||||
|
),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
),
|
||||||
|
Expanded(
|
||||||
|
child: SliderTheme(
|
||||||
|
data: const SliderThemeData(),
|
||||||
|
child: Slider(
|
||||||
|
min: 0,
|
||||||
|
max: initialBudget,
|
||||||
|
label: budget.value.round().toString(),
|
||||||
|
value: budget.value,
|
||||||
|
secondaryTrackValue: remainingBudget + budget.value,
|
||||||
|
onChanged: (value) => context.read<BudgetBloc>().add(BudgetSetValue(budget, value.round().toDouble())),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
),
|
||||||
|
IconButton(
|
||||||
|
onPressed: () => context.read<BudgetBloc>().add(BudgetRemove(budget)),
|
||||||
|
icon: const Icon(Icons.delete),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
)).toList());
|
||||||
|
|
||||||
|
list.add(
|
||||||
|
Container(
|
||||||
|
margin: const EdgeInsets.only(top: 25, bottom: 5),
|
||||||
|
decoration: BoxDecoration(
|
||||||
|
border: Border(
|
||||||
|
bottom: BorderSide(
|
||||||
|
width: 2,
|
||||||
|
color: Theme.of(context).colorScheme.onPrimaryContainer
|
||||||
|
)
|
||||||
|
)
|
||||||
|
),
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
list.add(
|
||||||
|
Row(
|
||||||
|
children: [
|
||||||
|
Text(
|
||||||
|
' Remaining ${NumberFormat('#####00 €', 'fr_FR').format(remainingBudget)} out of ${NumberFormat('#####00 €', 'fr_FR').format(initialBudget)}',
|
||||||
|
style: const TextStyle(
|
||||||
|
fontFamily: 'NovaMono',
|
||||||
|
fontWeight: FontWeight.bold,
|
||||||
|
fontSize: 15,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
],
|
||||||
|
)
|
||||||
|
);
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
final categoryState = context.watch<CategoryBloc>().state;
|
||||||
|
return BlocBuilder<BudgetBloc, BudgetState>(
|
||||||
|
builder: (context, state) => TitledContainer(
|
||||||
|
title: 'Prepare',
|
||||||
|
action: IconButton(
|
||||||
|
onPressed: () => context.read<BudgetBloc>().add(BudgetAdd(categoryState.categories.first.label)),
|
||||||
|
icon: const Icon(Icons.add),
|
||||||
|
),
|
||||||
|
child: Column(
|
||||||
|
children: _computeBudgetLines(context, state.budgets, state.initialBudget, state.remainingBudget),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,102 +1,20 @@
|
|||||||
import 'package:fl_chart/fl_chart.dart';
|
|
||||||
import 'package:flutter/foundation.dart';
|
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||||
import 'package:intl/intl.dart';
|
|
||||||
import 'package:tunas/domains/budget/budget_bloc.dart';
|
import 'package:tunas/domains/budget/budget_bloc.dart';
|
||||||
import 'package:tunas/pages/common/titled_container.dart';
|
import 'package:tunas/pages/budgets/widgets/budget_comparator.dart';
|
||||||
import 'package:tunas/repositories/metadata/models/budget.dart';
|
import 'package:tunas/pages/budgets/widgets/budget_maker.dart';
|
||||||
import 'package:uuid/uuid.dart';
|
|
||||||
|
|
||||||
class MonthDistribution extends StatelessWidget {
|
class MonthDistribution extends StatelessWidget {
|
||||||
const MonthDistribution({super.key});
|
const MonthDistribution({super.key});
|
||||||
|
|
||||||
List<Widget> _computeBudgetLines(BuildContext context, List<Budget> budgets, double initialBudget, double remainingBudget) {
|
|
||||||
List<Widget> list = [
|
|
||||||
Text('Money to spare: ${NumberFormat('#####00.00 €', 'fr_FR').format(remainingBudget)} € / $initialBudget €'),
|
|
||||||
];
|
|
||||||
|
|
||||||
list.addAll(budgets.map((budget) => Row(
|
|
||||||
children: [
|
|
||||||
Expanded(
|
|
||||||
child: Row(
|
|
||||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
|
||||||
children: [
|
|
||||||
Text(budget.label),
|
|
||||||
Text(NumberFormat('#####00.00 €', 'fr_FR').format(budget.value)),
|
|
||||||
],
|
|
||||||
)
|
|
||||||
),
|
|
||||||
Expanded(
|
|
||||||
child: SliderTheme(
|
|
||||||
data: SliderThemeData(
|
|
||||||
|
|
||||||
),
|
|
||||||
child: Slider(
|
|
||||||
min: 0,
|
|
||||||
max: initialBudget,
|
|
||||||
label: budget.value.round().toString(),
|
|
||||||
value: budget.value,
|
|
||||||
secondaryTrackValue: remainingBudget + budget.value,
|
|
||||||
onChanged: (value) => context.read<BudgetBloc>().add(BudgetSetValue(budget, value)),
|
|
||||||
),
|
|
||||||
)
|
|
||||||
)
|
|
||||||
],
|
|
||||||
)).toList());
|
|
||||||
return list;
|
|
||||||
}
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
|
|
||||||
return BlocBuilder<BudgetBloc, BudgetState>(
|
return BlocBuilder<BudgetBloc, BudgetState>(
|
||||||
builder: (context, state) => Column(
|
builder: (context, state) => const Column(
|
||||||
children: [
|
children: [
|
||||||
TitledContainer(
|
BudgetMaker(),
|
||||||
title: 'Prepare',
|
BudgetComparator(),
|
||||||
action: IconButton(
|
|
||||||
onPressed: () => context.read<BudgetBloc>().add(BudgetAdd(const Uuid().v8())),
|
|
||||||
icon: const Icon(Icons.add),
|
|
||||||
),
|
|
||||||
child: Column(
|
|
||||||
children: _computeBudgetLines(context, state.budgets, state.initialBudget, state.remainingBudget),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
TitledContainer(
|
|
||||||
title: 'Compare',
|
|
||||||
height: 500,
|
|
||||||
child: Row(
|
|
||||||
children: [
|
|
||||||
Expanded(
|
|
||||||
child: Column(
|
|
||||||
crossAxisAlignment: CrossAxisAlignment.start,
|
|
||||||
children: [
|
|
||||||
Text('Budget'),
|
|
||||||
],
|
|
||||||
),
|
|
||||||
),
|
|
||||||
Expanded(
|
|
||||||
child: Column(
|
|
||||||
children: [
|
|
||||||
Row(
|
|
||||||
children: [
|
|
||||||
IconButton(
|
|
||||||
onPressed: () => {},
|
|
||||||
icon: const Icon(Icons.skip_previous)
|
|
||||||
),
|
|
||||||
Text('Fev 2024'),
|
|
||||||
IconButton(
|
|
||||||
onPressed: () => {},
|
|
||||||
icon: const Icon(Icons.skip_next)
|
|
||||||
),
|
|
||||||
],
|
|
||||||
),
|
|
||||||
],
|
|
||||||
)
|
|
||||||
)
|
|
||||||
],
|
|
||||||
),
|
|
||||||
),
|
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -93,7 +93,7 @@ class HomePage extends StatelessWidget {
|
|||||||
BlocProvider(create: (context) => AccountBloc(metadataRepository: RepositoryProvider.of<MetadataRepository>(context), transactionsRepository: RepositoryProvider.of<TransactionsRepository>(context))),
|
BlocProvider(create: (context) => AccountBloc(metadataRepository: RepositoryProvider.of<MetadataRepository>(context), transactionsRepository: RepositoryProvider.of<TransactionsRepository>(context))),
|
||||||
BlocProvider(create: (context) => TransactionBloc(transactionsRepository: RepositoryProvider.of<TransactionsRepository>(context))),
|
BlocProvider(create: (context) => TransactionBloc(transactionsRepository: RepositoryProvider.of<TransactionsRepository>(context))),
|
||||||
BlocProvider(create: (context) => CategoryBloc(metadataRepository: RepositoryProvider.of<MetadataRepository>(context), transactionsRepository: RepositoryProvider.of<TransactionsRepository>(context))),
|
BlocProvider(create: (context) => CategoryBloc(metadataRepository: RepositoryProvider.of<MetadataRepository>(context), transactionsRepository: RepositoryProvider.of<TransactionsRepository>(context))),
|
||||||
BlocProvider(create: (context) => BudgetBloc(metadataRepository: RepositoryProvider.of<MetadataRepository>(context))),
|
BlocProvider(create: (context) => BudgetBloc(metadataRepository: RepositoryProvider.of<MetadataRepository>(context), transactionsRepository: RepositoryProvider.of<TransactionsRepository>(context))),
|
||||||
BlocProvider(create: (context) => ChartBloc(metadataRepository: RepositoryProvider.of<MetadataRepository>(context), transactionsRepository: RepositoryProvider.of<TransactionsRepository>(context))),
|
BlocProvider(create: (context) => ChartBloc(metadataRepository: RepositoryProvider.of<MetadataRepository>(context), transactionsRepository: RepositoryProvider.of<TransactionsRepository>(context))),
|
||||||
],
|
],
|
||||||
child: DefaultTabController(
|
child: DefaultTabController(
|
||||||
|
|||||||
@@ -60,6 +60,7 @@ class TransactionsHeader extends StatelessWidget {
|
|||||||
decoration: BoxDecoration(
|
decoration: BoxDecoration(
|
||||||
border: Border(
|
border: Border(
|
||||||
bottom: BorderSide(
|
bottom: BorderSide(
|
||||||
|
width: 2,
|
||||||
color: Theme.of(context).colorScheme.onPrimaryContainer
|
color: Theme.of(context).colorScheme.onPrimaryContainer
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -1,19 +1,31 @@
|
|||||||
|
import 'dart:async';
|
||||||
import 'dart:convert';
|
import 'dart:convert';
|
||||||
|
|
||||||
import 'package:tunas/clients/storage/storage_client.dart';
|
import 'package:tunas/clients/storage/json_storage_client.dart';
|
||||||
import 'package:tunas/repositories/json/models/json.dart';
|
import 'package:tunas/repositories/json/models/json.dart';
|
||||||
|
|
||||||
class JsonRepository {
|
class JsonRepository {
|
||||||
String accountFile = 'tunas_main_account.json';
|
String accountFile = 'tunas_main_account.json';
|
||||||
|
|
||||||
final StorageClient _storageClient;
|
final JsonStorageClient _storageClient;
|
||||||
|
|
||||||
|
Map<String, Timer> saveTimerMap = {};
|
||||||
|
|
||||||
JsonRepository({
|
JsonRepository({
|
||||||
required storageClient,
|
required storageClient,
|
||||||
}) : _storageClient = storageClient;
|
}) : _storageClient = storageClient;
|
||||||
|
|
||||||
saveJson(Json json) async {
|
void saveJson(Json json) {
|
||||||
await _storageClient.save(json.getJsonFileName(), jsonEncode(json.toJson()));
|
Timer? saveTimer = saveTimerMap[json.getJsonFileName()];
|
||||||
|
|
||||||
|
if (saveTimer != null) {
|
||||||
|
saveTimer.cancel();
|
||||||
|
}
|
||||||
|
|
||||||
|
saveTimer = Timer(const Duration(milliseconds: 500), () {
|
||||||
|
saveTimerMap.remove(json.getJsonFileName());
|
||||||
|
_storageClient.save(json.getJsonFileName(), jsonEncode(json.toJson()));
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<T> loadJson<T extends Json>(Json json, JsonFactory<T> jsonFactory) async {
|
Future<T> loadJson<T extends Json>(Json json, JsonFactory<T> jsonFactory) async {
|
||||||
|
|||||||
@@ -45,37 +45,37 @@ class MetadataRepository {
|
|||||||
_broadcastMetadata(metadata);
|
_broadcastMetadata(metadata);
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<List<Category>> saveCategories(List<Category> categories) async {
|
List<Category> saveCategories(List<Category> categories) {
|
||||||
Metadata metadata = _constructMetadataFromControllers();
|
Metadata metadata = _constructMetadataFromControllers();
|
||||||
metadata.categories = categories;
|
metadata.categories = categories;
|
||||||
await _jsonRepository.saveJson(metadata);
|
_jsonRepository.saveJson(metadata);
|
||||||
_categoriesController.add(categories);
|
_categoriesController.add(categories);
|
||||||
return categories;
|
return categories;
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<List<Budget>> saveBudgets(List<Budget> budgets) async {
|
List<Budget> saveBudgets(List<Budget> budgets) {
|
||||||
Metadata metadata = _constructMetadataFromControllers();
|
Metadata metadata = _constructMetadataFromControllers();
|
||||||
metadata.budgets = budgets;
|
metadata.budgets = budgets;
|
||||||
await _jsonRepository.saveJson(metadata);
|
_jsonRepository.saveJson(metadata);
|
||||||
_budgetController.add(budgets);
|
_budgetController.add(budgets);
|
||||||
return budgets;
|
return budgets;
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<List<Account>> saveAccounts(List<Account> accounts) async {
|
List<Account> saveAccounts(List<Account> accounts) {
|
||||||
Metadata metadata = _constructMetadataFromControllers();
|
Metadata metadata = _constructMetadataFromControllers();
|
||||||
metadata.accounts = accounts;
|
metadata.accounts = accounts;
|
||||||
await _jsonRepository.saveJson(metadata);
|
_jsonRepository.saveJson(metadata);
|
||||||
_accountController.add(accounts);
|
_accountController.add(accounts);
|
||||||
return accounts;
|
return accounts;
|
||||||
}
|
}
|
||||||
|
|
||||||
deleteMetadata() async {
|
void deleteMetadata() {
|
||||||
Metadata metadata = Metadata();
|
Metadata metadata = Metadata();
|
||||||
await _jsonRepository.saveJson(metadata);
|
_jsonRepository.saveJson(metadata);
|
||||||
_broadcastMetadata(metadata);
|
_broadcastMetadata(metadata);
|
||||||
}
|
}
|
||||||
|
|
||||||
_broadcastMetadata(Metadata metadata) {
|
void _broadcastMetadata(Metadata metadata) {
|
||||||
_categoriesController.add(metadata.categories);
|
_categoriesController.add(metadata.categories);
|
||||||
_budgetController.add(metadata.budgets);
|
_budgetController.add(metadata.budgets);
|
||||||
_accountController.add(metadata.accounts);
|
_accountController.add(metadata.accounts);
|
||||||
|
|||||||
@@ -20,20 +20,20 @@ class TransactionsRepository {
|
|||||||
return _transactionsController.asBroadcastStream();
|
return _transactionsController.asBroadcastStream();
|
||||||
}
|
}
|
||||||
|
|
||||||
loadTransactions() async {
|
Future<void> loadTransactions() async {
|
||||||
Transactions transactions = await _jsonRepository.loadJson(Transactions(), TransactionsFactory());
|
Transactions transactions = await _jsonRepository.loadJson(Transactions(), TransactionsFactory());
|
||||||
_transactionsController.add(transactions.transactions);
|
_transactionsController.add(transactions.transactions);
|
||||||
}
|
}
|
||||||
|
|
||||||
saveTransactions(List<Transaction> transactionsList) async {
|
void saveTransactions(List<Transaction> transactionsList) {
|
||||||
Transactions transactions = Transactions(transactions: transactionsList);
|
Transactions transactions = Transactions(transactions: transactionsList);
|
||||||
await _jsonRepository.saveJson(transactions);
|
_jsonRepository.saveJson(transactions);
|
||||||
_transactionsController.add(transactionsList);
|
_transactionsController.add(transactionsList);
|
||||||
}
|
}
|
||||||
|
|
||||||
deleteTransactions() async {
|
void deleteTransactions() {
|
||||||
Transactions transactions = Transactions();
|
Transactions transactions = Transactions();
|
||||||
await _jsonRepository.saveJson(transactions);
|
_jsonRepository.saveJson(transactions);
|
||||||
_transactionsController.add([]);
|
_transactionsController.add([]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user