Improved category with colors

This commit is contained in:
2024-02-09 01:22:04 +01:00
parent c5ede79dc4
commit 44f6d433d1
14 changed files with 303 additions and 170 deletions

View File

@@ -1,8 +1,5 @@
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';
@@ -12,29 +9,6 @@ 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<ChartEvent, ChartState> {
final AccountRepository _accountRepository;
@@ -56,13 +30,17 @@ class ChartBloc extends Bloc<ChartEvent, ChartState> {
}
_onNextYear(ChartNextYear event, Emitter<ChartState> emit) {
ChartState localState = state.copyWith(currentYear: state.currentYear + 1);
emit(_computeStateScopedStats(localState));
if (state.lastDate!.year >= state.currentYear + 1) {
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));
if (state.firstDate!.year <= state.currentYear - 1) {
ChartState localState = state.copyWith(currentYear: state.currentYear - 1);
emit(_computeStateScopedStats(localState));
}
}
ChartState _computeStateStats(ChartState state) {
@@ -90,11 +68,11 @@ class ChartBloc extends Bloc<ChartEvent, ChartState> {
transactionsLines.add(transactionLine);
monthlyTotals.add(FlSpot(transactionLine.transaction.date.microsecondsSinceEpoch.toDouble(), transactionLine.subTotal));
if (firstDate.compareTo(transaction.date) < 0) {
if (firstDate.compareTo(transaction.date) > 0) {
firstDate = transaction.date;
}
if (lastDate.compareTo(transaction.date) > 0) {
if (lastDate.compareTo(transaction.date) < 0) {
lastDate = transaction.date;
}
@@ -122,8 +100,6 @@ class ChartBloc extends Bloc<ChartEvent, ChartState> {
List<ChartItem> scopedCategoriesNegativeTotals = [];
Map<int, FlSpot> scopedMonthlyTotals = {};
Map<int, Map<String, double>> scopedCategoriesMonthlyTotals = {};
Map<String, Color> categoriesColors = {};
int colorIndex = 0;
for(var transaction in state.transactions) {
double subTotal = globalTotal + transaction.value;
@@ -148,15 +124,6 @@ class ChartBloc extends Bloc<ChartEvent, ChartState> {
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,
@@ -248,7 +215,6 @@ class ChartBloc extends Bloc<ChartEvent, ChartState> {
scopedSimplifiedCategoriesNegativeTotalsPercents: scopedSimplifiedCategoriesNegativeTotalsPercents,
scopedMonthlyTotals: scopedMonthlyTotals.values.toList(),
scopedCategoriesMonthlyTotals: scopedCategoriesMonthlyTotals,
categoriesColors: categoriesColors,
);
}
}

View File

@@ -29,8 +29,6 @@ final class ChartState extends Equatable {
final List<FlSpot> scopedMonthlyTotals;
final Map<int, Map<String, double>> scopedCategoriesMonthlyTotals;
final Map<String, Color> categoriesColors;
const ChartState({
this.transactions = const [],
this.transactionsLines = const [],
@@ -52,7 +50,6 @@ final class ChartState extends Equatable {
this.scopedSimplifiedCategoriesNegativeTotalsPercents = const [],
this.scopedMonthlyTotals = const [],
this.scopedCategoriesMonthlyTotals = const {},
this.categoriesColors = const {},
});
ChartState copyWith({
@@ -76,7 +73,6 @@ final class ChartState extends Equatable {
List<ChartItem>? scopedSimplifiedCategoriesNegativeTotalsPercents,
List<FlSpot>? scopedMonthlyTotals,
Map<int, Map<String, double>>? scopedCategoriesMonthlyTotals,
Map<String, Color>? categoriesColors,
}) {
return ChartState(
transactions: transactions ?? this.transactions,
@@ -99,7 +95,6 @@ final class ChartState extends Equatable {
scopedSimplifiedCategoriesNegativeTotalsPercents: scopedSimplifiedCategoriesNegativeTotalsPercents ?? this.scopedSimplifiedCategoriesNegativeTotalsPercents,
scopedMonthlyTotals: scopedMonthlyTotals ?? this.scopedMonthlyTotals,
scopedCategoriesMonthlyTotals: scopedCategoriesMonthlyTotals ?? this.scopedCategoriesMonthlyTotals,
categoriesColors: categoriesColors ?? this.categoriesColors,
);
}
@@ -123,7 +118,6 @@ final class ChartState extends Equatable {
scopedSimplifiedCategoriesNegativeTotalsPercents,
scopedMonthlyTotals,
scopedCategoriesMonthlyTotals,
categoriesColors,
];
}