basic csv loader, transaction list & half done stats page

This commit is contained in:
2024-02-04 22:34:28 +01:00
commit 3abee9ff6f
179 changed files with 6999 additions and 0 deletions

View File

@@ -0,0 +1,207 @@
import 'package:equatable/equatable.dart';
import 'package:fl_chart/fl_chart.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:tunas/domains/account/models/transaction_line.dart';
import 'package:tunas/domains/charts/models/chart_item.dart';
import 'package:tunas/repositories/account/account_repository.dart';
import 'package:tunas/repositories/account/models/transaction.dart';
part 'chart_event.dart';
part 'chart_state.dart';
class ChartBloc extends Bloc<ChartEvent, ChartState> {
final AccountRepository _accountRepository;
ChartBloc({required AccountRepository accountRepository}) :
_accountRepository = accountRepository, super(const ChartState()) {
on<ChartAccountLoad>(_onAccountLoad);
on<ChartNextYear>(_onNextYear);
on<ChartPreviousYear>(_onPreviousYear);
_accountRepository
.getTransactionsStream()
.listen((transactions) => add(ChartAccountLoad(transactions)));
}
_onAccountLoad(ChartAccountLoad event, Emitter<ChartState> emit) {
ChartState localState = state.copyWith(transactions: event.transactions);
emit(_computeStateStats(localState));
}
_onNextYear(ChartNextYear event, Emitter<ChartState> emit) {
ChartState localState = state.copyWith(currentYear: state.currentYear + 1);
emit(_computeStateScopedStats(localState));
}
_onPreviousYear(ChartPreviousYear event, Emitter<ChartState> emit) {
ChartState localState = state.copyWith(currentYear: state.currentYear - 1);
emit(_computeStateScopedStats(localState));
}
ChartState _computeStateStats(ChartState state) {
double globalTotal = 0;
Map<String, double> accountsTotals = <String, double>{};
List<TransactionLine> transactionsLines = [];
Map<String, double> categoriesTotals = {};
List<FlSpot> monthlyTotals = [];
Map<String, Map<String, double>> categoriesMonthlyTotals = {};
DateTime firstDate = DateTime.now();
DateTime lastDate = DateTime.fromMicrosecondsSinceEpoch(0);
for(var transaction in state.transactions) {
double subTotal = globalTotal + transaction.value;
globalTotal = subTotal;
double accountTotal = accountsTotals[transaction.account] ?? 0;
accountTotal += transaction.value;
accountsTotals[transaction.account] = accountTotal;
TransactionLine transactionLine = TransactionLine(transaction: transaction, subTotal: subTotal);
transactionsLines.add(transactionLine);
monthlyTotals.add(FlSpot(transactionLine.transaction.date.microsecondsSinceEpoch.toDouble(), transactionLine.subTotal));
if (firstDate.compareTo(transaction.date) < 0) {
firstDate = transaction.date;
}
if (lastDate.compareTo(transaction.date) > 0) {
lastDate = transaction.date;
}
double categoryTotal = categoriesTotals[transaction.category] ?? 0;
categoryTotal += transaction.value;
categoriesTotals[transaction.category] = categoryTotal;
}
return _computeStateScopedStats(state.copyWith(
transactionsLines: transactionsLines,
globalTotal: globalTotal,
accountsTotals: accountsTotals,
categoriesTotals: categoriesTotals,
monthlyTotals: monthlyTotals,
categoriesMonthlyTotals: categoriesMonthlyTotals,
firstDate: firstDate,
lastDate: lastDate,
currentYear: DateTime.now().year,
));
}
ChartState _computeStateScopedStats(ChartState state) {
double globalTotal = 0;
List<ChartItem> scopedCategoriesPositiveTotals = [];
List<ChartItem> scopedCategoriesNegativeTotals = [];
Map<int, FlSpot> scopedMonthlyTotals = {};
Map<String, Map<String, double>> scopedCategoriesMonthlyTotals = {};
for(var transaction in state.transactions) {
double subTotal = globalTotal + transaction.value;
globalTotal = subTotal;
if (transaction.date.year != state.currentYear) {
continue;
}
if (state.accountsTotals.containsKey(transaction.category)) {
continue;
}
TransactionLine transactionLine = TransactionLine(transaction: transaction, subTotal: subTotal);
DateTime transactionDate = transactionLine.transaction.date;
int transactionDateDay = transactionDate.difference(DateTime(transactionDate.year,1,1)).inDays + 1;
scopedMonthlyTotals[transactionDateDay] = FlSpot(transactionDateDay.toDouble(), transactionLine.subTotal);
if (transaction.category.isEmpty) {
continue;
}
if (transaction.value >= 0) {
ChartItem? chartItem = scopedCategoriesPositiveTotals.firstWhere(
(item) => item.label == transaction.category,
orElse: () {
ChartItem newChartItem = ChartItem(label: transaction.category, value: 0);
scopedCategoriesPositiveTotals.add(newChartItem);
return newChartItem;
}
);
chartItem.value += transaction.value;
}
if (transaction.value < 0) {
ChartItem? chartItem = scopedCategoriesNegativeTotals.firstWhere(
(item) => item.label == transaction.category,
orElse: () {
ChartItem newChartItem = ChartItem(label: transaction.category, value: 0);
scopedCategoriesNegativeTotals.add(newChartItem);
return newChartItem;
}
);
chartItem.value += transaction.value;
}
}
List<ChartItem> scopedCategoriesPositiveTotalsPercents = [];
List<ChartItem> scopedCategoriesNegativeTotalsPercents = [];
List<ChartItem> scopedSimplifiedCategoriesPositiveTotalsPercents = [];
List<ChartItem> scopedSimplifiedCategoriesNegativeTotalsPercents = [];
if (scopedCategoriesPositiveTotals.isNotEmpty) {
double catergoriesTotal = scopedCategoriesPositiveTotals.map((item) => item.value).reduce((value, element) => value + element);
scopedCategoriesPositiveTotalsPercents = scopedCategoriesPositiveTotals.map((item) => ChartItem(label: item.label, value: item.value / catergoriesTotal * 100)).toList();
double otherPercentTotal = 0;
for (var item in scopedCategoriesPositiveTotalsPercents) {
if (item.value > 1) {
scopedSimplifiedCategoriesPositiveTotalsPercents.add(ChartItem(label: item.label, value: item.value));
} else {
otherPercentTotal += item.value;
}
}
scopedSimplifiedCategoriesPositiveTotalsPercents.add(ChartItem(label: 'Autre', value: otherPercentTotal));
}
if (scopedCategoriesNegativeTotals.isNotEmpty) {
double catergoriesTotal = scopedCategoriesNegativeTotals.map((item) => item.value).reduce((value, element) => value + element);
scopedCategoriesNegativeTotalsPercents = scopedCategoriesNegativeTotals.map((item) => ChartItem(label: item.label, value: item.value / catergoriesTotal * 100)).toList();
double otherPercentTotal = 0;
for (var item in scopedCategoriesNegativeTotalsPercents) {
if (item.value > 1) {
scopedSimplifiedCategoriesNegativeTotalsPercents.add(ChartItem(label: item.label, value: item.value));
} else {
otherPercentTotal += item.value;
}
}
scopedSimplifiedCategoriesNegativeTotalsPercents.add(ChartItem(label: 'Autre', value: otherPercentTotal));
}
List<ChartItem> scopedSimplifiedCategoriesPositiveTotals = [];
List<ChartItem> scopedSimplifiedCategoriesNegativeTotals = [];
scopedCategoriesPositiveTotals.sort((a, b) => a.value.compareTo(b.value));
scopedCategoriesPositiveTotalsPercents.sort((a, b) => a.value.compareTo(b.value));
scopedCategoriesNegativeTotals.sort((a, b) => a.value.compareTo(b.value));
scopedCategoriesNegativeTotalsPercents.sort((a, b) => a.value.compareTo(b.value));
scopedSimplifiedCategoriesPositiveTotals.sort((a, b) => a.value.compareTo(b.value));
scopedSimplifiedCategoriesPositiveTotalsPercents.sort((a, b) => a.value.compareTo(b.value));
scopedSimplifiedCategoriesNegativeTotals.sort((a, b) => a.value.compareTo(b.value));
scopedSimplifiedCategoriesNegativeTotalsPercents.sort((a, b) => a.value.compareTo(b.value));
return state.copyWith(
scopedCategoriesPositiveTotals: scopedCategoriesPositiveTotals,
scopedCategoriesPositiveTotalsPercents: scopedCategoriesPositiveTotalsPercents,
scopedCategoriesNegativeTotals: scopedCategoriesNegativeTotals,
scopedCategoriesNegativeTotalsPercents: scopedCategoriesNegativeTotalsPercents,
scopedSimplifiedCategoriesPositiveTotals: scopedSimplifiedCategoriesPositiveTotals,
scopedSimplifiedCategoriesPositiveTotalsPercents: scopedSimplifiedCategoriesPositiveTotalsPercents,
scopedSimplifiedCategoriesNegativeTotals: scopedSimplifiedCategoriesNegativeTotals,
scopedSimplifiedCategoriesNegativeTotalsPercents: scopedSimplifiedCategoriesNegativeTotalsPercents,
scopedMonthlyTotals: scopedMonthlyTotals.values.toList(),
scopedCategoriesMonthlyTotals: scopedCategoriesMonthlyTotals,
);
}
}

View File

@@ -0,0 +1,19 @@
part of 'chart_bloc.dart';
sealed class ChartEvent extends Equatable {
const ChartEvent();
@override
List<Object> get props => [];
}
final class ChartAccountLoad extends ChartEvent {
final List<Transaction> transactions;
const ChartAccountLoad(this.transactions);
@override
List<Object> get props => [transactions];
}
final class ChartNextYear extends ChartEvent {}
final class ChartPreviousYear extends ChartEvent {}

View File

@@ -0,0 +1,123 @@
part of 'chart_bloc.dart';
final class ChartState extends Equatable {
final List<Transaction> transactions;
final List<TransactionLine> transactionsLines;
final double globalTotal;
final Map<String, double> accountsTotals;
final DateTime? firstDate;
final DateTime? lastDate;
final num currentYear;
final Map<String, double> categoriesTotals;
final List<FlSpot> monthlyTotals;
final Map<String, Map<String, double>> categoriesMonthlyTotals;
final List<ChartItem> scopedCategoriesPositiveTotals;
final List<ChartItem> scopedCategoriesPositiveTotalsPercents;
final List<ChartItem> scopedCategoriesNegativeTotals;
final List<ChartItem> scopedCategoriesNegativeTotalsPercents;
final List<ChartItem> scopedSimplifiedCategoriesPositiveTotals;
final List<ChartItem> scopedSimplifiedCategoriesPositiveTotalsPercents;
final List<ChartItem> scopedSimplifiedCategoriesNegativeTotals;
final List<ChartItem> scopedSimplifiedCategoriesNegativeTotalsPercents;
final List<FlSpot> scopedMonthlyTotals;
final Map<String, Map<String, double>> scopedCategoriesMonthlyTotals;
const ChartState({
this.transactions = const [],
this.transactionsLines = const [],
this.globalTotal = 0,
this.accountsTotals = const {},
this.firstDate,
this.lastDate,
this.currentYear = 2000,
this.categoriesTotals = const {},
this.monthlyTotals = const [],
this.categoriesMonthlyTotals = const {},
this.scopedCategoriesPositiveTotals = const [],
this.scopedCategoriesPositiveTotalsPercents = const [],
this.scopedCategoriesNegativeTotals = const [],
this.scopedCategoriesNegativeTotalsPercents = const [],
this.scopedSimplifiedCategoriesPositiveTotals = const [],
this.scopedSimplifiedCategoriesPositiveTotalsPercents = const [],
this.scopedSimplifiedCategoriesNegativeTotals = const [],
this.scopedSimplifiedCategoriesNegativeTotalsPercents = const [],
this.scopedMonthlyTotals = const [],
this.scopedCategoriesMonthlyTotals = const {},
});
ChartState copyWith({
List<Transaction>? transactions,
List<TransactionLine>? transactionsLines,
double? globalTotal,
Map<String, double>? accountsTotals,
DateTime? firstDate,
DateTime? lastDate,
num? currentYear,
Map<String, double>? categoriesTotals,
List<FlSpot>? monthlyTotals,
Map<String, Map<String, double>>? categoriesMonthlyTotals,
List<ChartItem>? scopedCategoriesPositiveTotals,
List<ChartItem>? scopedCategoriesPositiveTotalsPercents,
List<ChartItem>? scopedCategoriesNegativeTotals,
List<ChartItem>? scopedCategoriesNegativeTotalsPercents,
List<ChartItem>? scopedSimplifiedCategoriesPositiveTotals,
List<ChartItem>? scopedSimplifiedCategoriesPositiveTotalsPercents,
List<ChartItem>? scopedSimplifiedCategoriesNegativeTotals,
List<ChartItem>? scopedSimplifiedCategoriesNegativeTotalsPercents,
List<FlSpot>? scopedMonthlyTotals,
Map<String, Map<String, double>>? scopedCategoriesMonthlyTotals,
}) {
return ChartState(
transactions: transactions ?? this.transactions,
transactionsLines: transactionsLines ?? this.transactionsLines,
globalTotal: globalTotal ?? this.globalTotal,
accountsTotals: accountsTotals ?? this.accountsTotals,
firstDate: firstDate ?? this.firstDate,
lastDate: lastDate ?? this.lastDate,
currentYear: currentYear ?? this.currentYear,
categoriesTotals: categoriesTotals ?? this.categoriesTotals,
monthlyTotals: monthlyTotals ?? this.monthlyTotals,
categoriesMonthlyTotals: categoriesMonthlyTotals ?? this.categoriesMonthlyTotals,
scopedCategoriesPositiveTotals: scopedCategoriesPositiveTotals ?? this.scopedCategoriesPositiveTotals,
scopedCategoriesPositiveTotalsPercents: scopedCategoriesPositiveTotalsPercents ?? this.scopedCategoriesPositiveTotalsPercents,
scopedCategoriesNegativeTotals: scopedCategoriesNegativeTotals ?? this.scopedCategoriesNegativeTotals,
scopedCategoriesNegativeTotalsPercents: scopedCategoriesNegativeTotalsPercents ?? this.scopedCategoriesNegativeTotalsPercents,
scopedSimplifiedCategoriesPositiveTotals: scopedSimplifiedCategoriesPositiveTotals ?? this.scopedSimplifiedCategoriesPositiveTotals,
scopedSimplifiedCategoriesPositiveTotalsPercents: scopedSimplifiedCategoriesPositiveTotalsPercents ?? this.scopedSimplifiedCategoriesPositiveTotalsPercents,
scopedSimplifiedCategoriesNegativeTotals: scopedSimplifiedCategoriesNegativeTotals ?? this.scopedSimplifiedCategoriesNegativeTotals,
scopedSimplifiedCategoriesNegativeTotalsPercents: scopedSimplifiedCategoriesNegativeTotalsPercents ?? this.scopedSimplifiedCategoriesNegativeTotalsPercents,
scopedMonthlyTotals: scopedMonthlyTotals ?? this.scopedMonthlyTotals,
scopedCategoriesMonthlyTotals: scopedCategoriesMonthlyTotals ?? this.scopedCategoriesMonthlyTotals,
);
}
@override
List<Object> get props => [
transactions,
transactionsLines,
globalTotal,
accountsTotals,
currentYear,
categoriesTotals,
monthlyTotals,
categoriesMonthlyTotals,
scopedCategoriesPositiveTotals,
scopedCategoriesPositiveTotalsPercents,
scopedCategoriesNegativeTotals,
scopedCategoriesNegativeTotalsPercents,
scopedSimplifiedCategoriesPositiveTotals,
scopedSimplifiedCategoriesPositiveTotalsPercents,
scopedSimplifiedCategoriesNegativeTotals,
scopedSimplifiedCategoriesNegativeTotalsPercents,
scopedMonthlyTotals,
scopedCategoriesMonthlyTotals,
];
}

View File

@@ -0,0 +1,9 @@
class ChartItem {
String label;
double value;
ChartItem({
required this.label,
required this.value,
});
}