import 'dart:ui'; import 'package:equatable/equatable.dart'; import 'package:fl_chart/fl_chart.dart'; import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:tunas/domains/transaction/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'; final colors = [ Colors.purple.shade300, Colors.purple.shade500, Colors.purple.shade700, Colors.purple.shade900, Colors.blue.shade300, Colors.blue.shade500, Colors.blue.shade700, Colors.blue.shade900, Colors.green.shade300, Colors.green.shade500, Colors.green.shade700, Colors.green.shade900, Colors.yellow.shade300, Colors.yellow.shade500, Colors.yellow.shade700, Colors.yellow.shade900, Colors.red.shade300, Colors.red.shade500, Colors.red.shade700, Colors.red.shade900, ]; class ChartBloc extends Bloc { final AccountRepository _accountRepository; ChartBloc({required AccountRepository accountRepository}) : _accountRepository = accountRepository, super(const ChartState()) { on(_onAccountLoad); on(_onNextYear); on(_onPreviousYear); _accountRepository .getTransactionsStream() .listen((transactions) => add(ChartAccountLoad(transactions))); } _onAccountLoad(ChartAccountLoad event, Emitter emit) { ChartState localState = state.copyWith(transactions: event.transactions); emit(_computeStateStats(localState)); } _onNextYear(ChartNextYear event, Emitter emit) { ChartState localState = state.copyWith(currentYear: state.currentYear + 1); emit(_computeStateScopedStats(localState)); } _onPreviousYear(ChartPreviousYear event, Emitter emit) { ChartState localState = state.copyWith(currentYear: state.currentYear - 1); emit(_computeStateScopedStats(localState)); } ChartState _computeStateStats(ChartState state) { double globalTotal = 0; Map accountsTotals = {}; List transactionsLines = []; Map categoriesTotals = {}; List monthlyTotals = []; Map> 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 scopedCategoriesPositiveTotals = []; List scopedCategoriesNegativeTotals = []; Map scopedMonthlyTotals = {}; Map> scopedCategoriesMonthlyTotals = {}; Map categoriesColors = {}; int colorIndex = 0; 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 (categoriesColors[transaction.category] == null) { if (colorIndex >= colors.length) { categoriesColors[transaction.category] = const Color.fromARGB(255, 234, 0, 255); } else { categoriesColors[transaction.category] = colors[colorIndex]; } colorIndex++; } 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; Map? a = scopedCategoriesMonthlyTotals[transaction.date.month]; if (scopedCategoriesMonthlyTotals[transaction.date.month] == null) { a = {}; } a?[transaction.category] = transaction.value.abs() + (a[transaction.category] ?? 0); scopedCategoriesMonthlyTotals[transaction.date.month] = a!; } } List scopedCategoriesPositiveTotalsPercents = []; List scopedCategoriesNegativeTotalsPercents = []; List scopedSimplifiedCategoriesPositiveTotalsPercents = []; List 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 scopedSimplifiedCategoriesPositiveTotals = []; List 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, categoriesColors: categoriesColors, ); } }