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

@@ -6,12 +6,33 @@ import 'package:equatable/equatable.dart';
import 'package:file_picker/file_picker.dart'; import 'package:file_picker/file_picker.dart';
import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:tunas/repositories/account/account_repository.dart'; import 'package:tunas/repositories/account/account_repository.dart';
import 'package:tunas/repositories/account/models/account.dart';
import 'package:tunas/repositories/account/models/category.dart';
import 'package:tunas/repositories/account/models/transaction.dart'; import 'package:tunas/repositories/account/models/transaction.dart';
import 'package:uuid/uuid.dart'; import 'package:uuid/uuid.dart';
part 'account_event.dart'; part 'account_event.dart';
part 'account_state.dart'; part 'account_state.dart';
final colors = [
'FF74feff',
'FF64c0ff',
'FF5873fe',
'FF4b4cff',
'FFc0fcfd',
'FFa7caff',
'FF8d7efd',
'FF7f65fe',
'FFe7ffff',
'FFd7dafd',
'FFd8a6ff',
'FFc065fe',
'FFffffff',
'FFffe6fe',
'FFffbdff',
'FFff80fe',
];
class AccountBloc extends Bloc<AccountEvent, AccountState> { class AccountBloc extends Bloc<AccountEvent, AccountState> {
final AccountRepository _accountRepository; final AccountRepository _accountRepository;
@@ -38,6 +59,7 @@ class AccountBloc extends Bloc<AccountEvent, AccountState> {
_onAccountImportCSV( _onAccountImportCSV(
AccountImportCSV event, Emitter<AccountState> emit) async { AccountImportCSV event, Emitter<AccountState> emit) async {
int colorIndex = 0;
FilePickerResult? result = await FilePicker.platform.pickFiles(); FilePickerResult? result = await FilePicker.platform.pickFiles();
final csvPath = result?.files.first.path; final csvPath = result?.files.first.path;
@@ -46,19 +68,39 @@ class AccountBloc extends Bloc<AccountEvent, AccountState> {
final String csvFileContent = await csv.readAsString(); final String csvFileContent = await csv.readAsString();
final List<List<dynamic>> csvList = const CsvToListConverter(fieldDelimiter: '|', eol: '\n').convert(csvFileContent); final List<List<dynamic>> csvList = const CsvToListConverter(fieldDelimiter: '|', eol: '\n').convert(csvFileContent);
final Map<String, Category> categoriesMap = {};
final transactions = csvList final transactions = csvList
.map((line) => Transaction( .map((line) {
String? categoryLabel = line[1];
if (categoryLabel == null || categoryLabel == '') {
categoryLabel = 'N/A';
}
if (categoriesMap[categoryLabel] == null) {
if (categoryLabel == 'N/A') {
categoriesMap[categoryLabel] = Category(label: 'N/A', color: 'FFFF0000' );
} else {
String color = colorIndex >= colors.length ? 'FF0000FF' : colors[colorIndex];
colorIndex++;
categoriesMap[categoryLabel] = Category(label: categoryLabel, color: color );
}
}
return Transaction(
uuid: const Uuid().v8(), uuid: const Uuid().v8(),
date: DateTime.parse(line[0]), date: DateTime.parse(line[0]),
category: line[1], category: categoryLabel,
description: line[2], description: line[2],
account: line[3], account: line[3],
value: _universalConvertToDouble(line[4])) value: _universalConvertToDouble(line[4]));
) })
.toList(); .toList();
await _accountRepository.deleteAccount(); await _accountRepository.deleteAccount();
await _accountRepository.saveTransactions(transactions); final account = Account(transactions: transactions, categories: categoriesMap.values.toList());
await _accountRepository.saveAccount(account);
} }
} }

View File

@@ -1,3 +1,5 @@
import 'dart:ui';
import 'package:equatable/equatable.dart'; import 'package:equatable/equatable.dart';
import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:tunas/repositories/account/account_repository.dart'; import 'package:tunas/repositories/account/account_repository.dart';
@@ -22,6 +24,7 @@ class CategoryBloc extends Bloc<CategoryEvent, CategoryState> {
) { ) {
emit(state.copyWith( emit(state.copyWith(
categories: event.categories, categories: event.categories,
categoriesColors: { for (var category in event.categories) category.label : category.rgbToColor() }
)); ));
} }
} }

View File

@@ -2,19 +2,23 @@ part of 'category_bloc.dart';
final class CategoryState extends Equatable { final class CategoryState extends Equatable {
final List<Category> categories; final List<Category> categories;
final Map<String, Color> categoriesColors;
const CategoryState({ const CategoryState({
this.categories = const [], this.categories = const [],
this.categoriesColors = const {},
}); });
CategoryState copyWith({ CategoryState copyWith({
List<Category>? categories, List<Category>? categories,
Map<String, Color>? categoriesColors,
}) { }) {
return CategoryState( return CategoryState(
categories: categories ?? this.categories, categories: categories ?? this.categories,
categoriesColors: categoriesColors ?? this.categoriesColors,
); );
} }
@override @override
List<Object> get props => [categories]; List<Object> get props => [categories, categoriesColors];
} }

View File

@@ -1,8 +1,5 @@
import 'dart:ui';
import 'package:equatable/equatable.dart'; import 'package:equatable/equatable.dart';
import 'package:fl_chart/fl_chart.dart'; import 'package:fl_chart/fl_chart.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:tunas/domains/transaction/models/transaction_line.dart'; import 'package:tunas/domains/transaction/models/transaction_line.dart';
import 'package:tunas/domains/charts/models/chart_item.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_event.dart';
part 'chart_state.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> { class ChartBloc extends Bloc<ChartEvent, ChartState> {
final AccountRepository _accountRepository; final AccountRepository _accountRepository;
@@ -56,14 +30,18 @@ class ChartBloc extends Bloc<ChartEvent, ChartState> {
} }
_onNextYear(ChartNextYear event, Emitter<ChartState> emit) { _onNextYear(ChartNextYear event, Emitter<ChartState> emit) {
if (state.lastDate!.year >= state.currentYear + 1) {
ChartState localState = state.copyWith(currentYear: state.currentYear + 1); ChartState localState = state.copyWith(currentYear: state.currentYear + 1);
emit(_computeStateScopedStats(localState)); emit(_computeStateScopedStats(localState));
} }
}
_onPreviousYear(ChartPreviousYear event, Emitter<ChartState> emit) { _onPreviousYear(ChartPreviousYear event, Emitter<ChartState> emit) {
if (state.firstDate!.year <= state.currentYear - 1) {
ChartState localState = state.copyWith(currentYear: state.currentYear - 1); ChartState localState = state.copyWith(currentYear: state.currentYear - 1);
emit(_computeStateScopedStats(localState)); emit(_computeStateScopedStats(localState));
} }
}
ChartState _computeStateStats(ChartState state) { ChartState _computeStateStats(ChartState state) {
double globalTotal = 0; double globalTotal = 0;
@@ -90,11 +68,11 @@ class ChartBloc extends Bloc<ChartEvent, ChartState> {
transactionsLines.add(transactionLine); transactionsLines.add(transactionLine);
monthlyTotals.add(FlSpot(transactionLine.transaction.date.microsecondsSinceEpoch.toDouble(), transactionLine.subTotal)); 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; firstDate = transaction.date;
} }
if (lastDate.compareTo(transaction.date) > 0) { if (lastDate.compareTo(transaction.date) < 0) {
lastDate = transaction.date; lastDate = transaction.date;
} }
@@ -122,8 +100,6 @@ class ChartBloc extends Bloc<ChartEvent, ChartState> {
List<ChartItem> scopedCategoriesNegativeTotals = []; List<ChartItem> scopedCategoriesNegativeTotals = [];
Map<int, FlSpot> scopedMonthlyTotals = {}; Map<int, FlSpot> scopedMonthlyTotals = {};
Map<int, Map<String, double>> scopedCategoriesMonthlyTotals = {}; Map<int, Map<String, double>> scopedCategoriesMonthlyTotals = {};
Map<String, Color> categoriesColors = {};
int colorIndex = 0;
for(var transaction in state.transactions) { for(var transaction in state.transactions) {
double subTotal = globalTotal + transaction.value; double subTotal = globalTotal + transaction.value;
@@ -148,15 +124,6 @@ class ChartBloc extends Bloc<ChartEvent, ChartState> {
continue; 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) { if (transaction.value >= 0) {
ChartItem? chartItem = scopedCategoriesPositiveTotals.firstWhere( ChartItem? chartItem = scopedCategoriesPositiveTotals.firstWhere(
(item) => item.label == transaction.category, (item) => item.label == transaction.category,
@@ -248,7 +215,6 @@ class ChartBloc extends Bloc<ChartEvent, ChartState> {
scopedSimplifiedCategoriesNegativeTotalsPercents: scopedSimplifiedCategoriesNegativeTotalsPercents, scopedSimplifiedCategoriesNegativeTotalsPercents: scopedSimplifiedCategoriesNegativeTotalsPercents,
scopedMonthlyTotals: scopedMonthlyTotals.values.toList(), scopedMonthlyTotals: scopedMonthlyTotals.values.toList(),
scopedCategoriesMonthlyTotals: scopedCategoriesMonthlyTotals, scopedCategoriesMonthlyTotals: scopedCategoriesMonthlyTotals,
categoriesColors: categoriesColors,
); );
} }
} }

View File

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

View File

@@ -1,33 +1,34 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:tunas/pages/data/widgets/categories_settings.dart';
import 'package:tunas/domains/account/account_bloc.dart'; import 'package:tunas/pages/data/widgets/import_settings.dart';
class DataPage extends StatelessWidget { class DataPage extends StatelessWidget {
const DataPage({super.key}); const DataPage({super.key});
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return BlocBuilder<AccountBloc, AccountState>( return Container(
builder: (context, state) => Column( padding: const EdgeInsets.symmetric(vertical: 9, horizontal: 10),
margin: const EdgeInsets.symmetric(vertical: 2, horizontal: 10),
child: const Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [ children: [
FilledButton( Text(
onPressed: () => context.read<AccountBloc>().add(const AccountImportCSV()), 'Settings',
child: const Text('Import CSV') style: TextStyle(
fontWeight: FontWeight.w900,
fontSize: 35,
), ),
FilledButton(
onPressed: () => context.read<AccountBloc>().add(const AccountImportJSON()),
child: const Text('Import JSON')
),
FilledButton(
onPressed: () => context.read<AccountBloc>().add(const AccountExportCSV()),
child: const Text('Export CSV')
),
FilledButton(
onPressed: () => context.read<AccountBloc>().add(const AccountExportJSON()),
child: const Text('Export JSON')
), ),
Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
ImportSettings(),
CategoriesSettings()
], ],
) )
]
)
); );
} }
} }

View File

@@ -0,0 +1,57 @@
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:tunas/domains/category/category_bloc.dart';
import 'package:tunas/repositories/account/models/category.dart';
class CategoriesSettings extends StatelessWidget {
const CategoriesSettings({super.key});
List<Widget> _computeCategoryList(List<Category> categories) {
return categories.map((category) => Row(
children: [
Container(
height: 10,
width: 10,
color: category.rgbToColor(),
),
Container(width: 5),
Text(category.label),
],
)).toList();
}
@override
Widget build(BuildContext context) {
return BlocBuilder<CategoryBloc, CategoryState>(
builder: (context, state) => Container(
decoration: BoxDecoration(
color: Colors.blue,
borderRadius: BorderRadius.circular(5),
),
padding: const EdgeInsets.symmetric(vertical: 10, horizontal: 15),
margin: const EdgeInsets.all(5),
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Text(
"Categories",
style: TextStyle(
fontWeight: FontWeight.w900,
fontSize: 20,
),
),
const SizedBox(height: 10),
SingleChildScrollView(
scrollDirection: Axis.vertical,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: _computeCategoryList(state.categories),
),
),
],
)
),
);
}
}

View File

@@ -0,0 +1,54 @@
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:tunas/domains/account/account_bloc.dart';
class ImportSettings extends StatelessWidget {
const ImportSettings({super.key});
@override
Widget build(BuildContext context) {
return BlocBuilder<AccountBloc, AccountState>(
builder: (context, state) => Container(
decoration: BoxDecoration(
color: Colors.blue,
borderRadius: BorderRadius.circular(5)
),
padding: const EdgeInsets.symmetric(vertical: 10, horizontal: 15),
margin: const EdgeInsets.all(5),
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Text(
"Import",
style: TextStyle(
fontWeight: FontWeight.w900,
fontSize: 20,
),
),
const SizedBox(height: 10),
FilledButton(
onPressed: () => context.read<AccountBloc>().add(const AccountImportCSV()),
child: const Text('Import CSV')
),
const SizedBox(height: 5),
FilledButton(
onPressed: () => context.read<AccountBloc>().add(const AccountImportJSON()),
child: const Text('Import JSON')
),
const SizedBox(height: 5),
FilledButton(
onPressed: () => context.read<AccountBloc>().add(const AccountExportCSV()),
child: const Text('Export CSV')
),
const SizedBox(height: 5),
FilledButton(
onPressed: () => context.read<AccountBloc>().add(const AccountExportJSON()),
child: const Text('Export JSON')
),
],
),
)
);
}
}

View File

@@ -45,15 +45,15 @@ class StatsPage extends StatelessWidget {
), ),
SizedBox( SizedBox(
height: 500, height: 500,
child: MonthlyCategoriesTotalChart(categoriesMonthlyTotals: state.scopedCategoriesMonthlyTotals, categoriesColors: state.categoriesColors) child: MonthlyCategoriesTotalChart(categoriesMonthlyTotals: state.scopedCategoriesMonthlyTotals)
), ),
SizedBox( SizedBox(
height: 450, height: 450,
child: Row( child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween, mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [ children: [
CategoriesTotalsChart(categoriesTotals: state.scopedCategoriesPositiveTotals, categoriesTotalsPercents: state.scopedCategoriesPositiveTotalsPercents, categoriesColors: state.categoriesColors), CategoriesTotalsChart(categoriesTotals: state.scopedCategoriesPositiveTotals, categoriesTotalsPercents: state.scopedSimplifiedCategoriesPositiveTotalsPercents),
CategoriesTotalsChart(categoriesTotals: state.scopedCategoriesNegativeTotals, categoriesTotalsPercents: state.scopedSimplifiedCategoriesNegativeTotalsPercents, categoriesColors: state.categoriesColors), CategoriesTotalsChart(categoriesTotals: state.scopedCategoriesNegativeTotals, categoriesTotalsPercents: state.scopedSimplifiedCategoriesNegativeTotalsPercents),
], ],
) )
), ),

View File

@@ -10,7 +10,7 @@ class AccountCounter extends StatelessWidget {
return accountsTotals.entries.toList().map((entry) => Row( return accountsTotals.entries.toList().map((entry) => Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween, mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [ children: [
Text("${entry.key}:"), Text(entry.key),
Text( Text(
NumberFormat('#######.00 €', 'fr_FR').format(entry.value), NumberFormat('#######.00 €', 'fr_FR').format(entry.value),
style: TextStyle( style: TextStyle(

View File

@@ -1,21 +1,22 @@
import 'package:fl_chart/fl_chart.dart'; import 'package:fl_chart/fl_chart.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:intl/intl.dart'; import 'package:intl/intl.dart';
import 'package:tunas/domains/category/category_bloc.dart';
import 'package:tunas/domains/charts/models/chart_item.dart'; import 'package:tunas/domains/charts/models/chart_item.dart';
class CategoriesTotalsChart extends StatelessWidget { class CategoriesTotalsChart extends StatelessWidget {
final List<ChartItem> categoriesTotals; final List<ChartItem> categoriesTotals;
final List<ChartItem> categoriesTotalsPercents; final List<ChartItem> categoriesTotalsPercents;
final Map<String, Color> categoriesColors;
const CategoriesTotalsChart({super.key, required this.categoriesTotals, required this.categoriesTotalsPercents, required this.categoriesColors}); const CategoriesTotalsChart({super.key, required this.categoriesTotals, required this.categoriesTotalsPercents});
List<PieChartSectionData> _convertDataForChart() { List<PieChartSectionData> _convertDataForChart(Map<String, Color> categoriesColors) {
return categoriesTotalsPercents return categoriesTotalsPercents
.map((item) => .map((item) =>
PieChartSectionData( PieChartSectionData(
value: item.value, value: item.value,
title: item.label, title: NumberFormat("#0 %").format(item.value / 100),
titleStyle: const TextStyle( titleStyle: const TextStyle(
fontSize: 15, fontSize: 15,
fontWeight: FontWeight.w300 fontWeight: FontWeight.w300
@@ -28,7 +29,7 @@ class CategoriesTotalsChart extends StatelessWidget {
.toList(); .toList();
} }
List<Row> _computeLegend() { List<Row> _computeLegend(Map<String, Color> categoriesColors) {
return categoriesTotals return categoriesTotals
.map((item) => Row( .map((item) => Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween, mainAxisAlignment: MainAxisAlignment.spaceBetween,
@@ -56,7 +57,8 @@ class CategoriesTotalsChart extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Container( return BlocBuilder<CategoryBloc, CategoryState>(
builder: (context, state) => Container(
height: 320, height: 320,
width: 560, width: 560,
padding: const EdgeInsets.all(10), padding: const EdgeInsets.all(10),
@@ -65,17 +67,12 @@ class CategoriesTotalsChart extends StatelessWidget {
borderRadius: BorderRadius.circular(5), borderRadius: BorderRadius.circular(5),
color: Colors.blue color: Colors.blue
), ),
child: Expanded(
child: AspectRatio(
aspectRatio: 1.3,
child: Row( child: Row(
children: [ children: [
Expanded( Expanded(
child: AspectRatio(
aspectRatio: 1,
child: PieChart( child: PieChart(
PieChartData( PieChartData(
sections: _convertDataForChart(), sections: _convertDataForChart(state.categoriesColors),
borderData: FlBorderData( borderData: FlBorderData(
show: false show: false
), ),
@@ -83,7 +80,6 @@ class CategoriesTotalsChart extends StatelessWidget {
sectionsSpace: 2 sectionsSpace: 2
) )
), ),
)
), ),
Container( Container(
height: 300, height: 300,
@@ -98,14 +94,13 @@ class CategoriesTotalsChart extends StatelessWidget {
child: Column( child: Column(
mainAxisAlignment: MainAxisAlignment.center, mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start,
children: _computeLegend(), children: _computeLegend(state.categoriesColors),
), ),
) )
), ),
], ],
), ),
) )
)
); );
} }
} }

View File

@@ -1,13 +1,14 @@
import 'package:fl_chart/fl_chart.dart'; import 'package:fl_chart/fl_chart.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:tunas/domains/category/category_bloc.dart';
class MonthlyCategoriesTotalChart extends StatelessWidget { class MonthlyCategoriesTotalChart extends StatelessWidget {
final Map<int, Map<String, double>> categoriesMonthlyTotals; final Map<int, Map<String, double>> categoriesMonthlyTotals;
final Map<String, Color> categoriesColors;
const MonthlyCategoriesTotalChart({super.key, required this.categoriesMonthlyTotals, required this.categoriesColors}); const MonthlyCategoriesTotalChart({super.key, required this.categoriesMonthlyTotals});
BarChartRodData _computeStack(double barsWidth, MapEntry<int, Map<String, double>> entry) { BarChartRodData _computeStack(double barsWidth, MapEntry<int, Map<String, double>> entry, Map<String, Color> categoriesColors) {
var subcounter = 0.0; var subcounter = 0.0;
var a = entry.value.entries.map((subEntry) => BarChartRodStackItem(subcounter, subcounter += subEntry.value, categoriesColors[subEntry.key] ?? Colors.red)).toList(); var a = entry.value.entries.map((subEntry) => BarChartRodStackItem(subcounter, subcounter += subEntry.value, categoriesColors[subEntry.key] ?? Colors.red)).toList();
return BarChartRodData( return BarChartRodData(
@@ -19,13 +20,13 @@ class MonthlyCategoriesTotalChart extends StatelessWidget {
); );
} }
List<BarChartGroupData> _computeBarGroups(double barsSpace, double barsWidth) { List<BarChartGroupData> _computeBarGroups(double barsSpace, double barsWidth, Map<String, Color> categoriesColors) {
var a = categoriesMonthlyTotals.entries var a = categoriesMonthlyTotals.entries
.map((entry) { .map((entry) {
return BarChartGroupData( return BarChartGroupData(
x: entry.key, x: entry.key,
barsSpace: barsSpace, barsSpace: barsSpace,
barRods: [_computeStack(barsWidth, entry)] barRods: [_computeStack(barsWidth, entry, categoriesColors)]
); );
}) })
.toList(); .toList();
@@ -55,7 +56,8 @@ class MonthlyCategoriesTotalChart extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return AspectRatio( return BlocBuilder<CategoryBloc, CategoryState>(
builder: (context, state) => AspectRatio(
aspectRatio: 1.66, aspectRatio: 1.66,
child: LayoutBuilder( child: LayoutBuilder(
builder: (context, constraints) { builder: (context, constraints) {
@@ -65,7 +67,7 @@ class MonthlyCategoriesTotalChart extends StatelessWidget {
return BarChart( return BarChart(
BarChartData( BarChartData(
maxY: _computeMaxValue(), maxY: _computeMaxValue(),
barGroups: _computeBarGroups(barsSpace, barsWidth), barGroups: _computeBarGroups(barsSpace, barsWidth, state.categoriesColors),
titlesData: FlTitlesData( titlesData: FlTitlesData(
topTitles: const AxisTitles( topTitles: const AxisTitles(
sideTitles: SideTitles(showTitles: false) sideTitles: SideTitles(showTitles: false)
@@ -81,6 +83,7 @@ class MonthlyCategoriesTotalChart extends StatelessWidget {
); );
}, },
) )
)
); );
} }
} }

View File

@@ -9,6 +9,7 @@ import 'package:tunas/repositories/account/models/transaction.dart';
class AccountRepository { class AccountRepository {
String accountFile = 'tunas_main_account.json'; String accountFile = 'tunas_main_account.json';
Account? currentAccount;
final StorageClient _storageClient; final StorageClient _storageClient;
@@ -49,20 +50,27 @@ class AccountRepository {
saveAccount(Account account) async { saveAccount(Account account) async {
await _storageClient.save(accountFile, jsonEncode(account.toJson())); await _storageClient.save(accountFile, jsonEncode(account.toJson()));
_broadcastAccountData(account);
} }
saveTransactions(List<Transaction> transactions) async { saveTransactions(List<Transaction> transactions) async {
final account = Account(transactions: transactions); Account? account = currentAccount;
if (account == null) {
throw Error();
} else {account.transactions = transactions;
await saveAccount(account); await saveAccount(account);
_transactionsController.add(account.transactions); }
_categoriesController.add(account.categories);
_budgetController.add(account.budgets);
} }
deleteAccount() async { deleteAccount() async {
await _storageClient.delete(accountFile); await _storageClient.delete(accountFile);
_transactionsController.add(const []); _broadcastAccountData(Account());
_categoriesController.add(const []); }
_budgetController.add(const []);
_broadcastAccountData(Account account) {
currentAccount = account;
_transactionsController.add(account.transactions);
_categoriesController.add(account.categories);
_budgetController.add(account.budgets);
} }
} }

View File

@@ -1,3 +1,5 @@
import 'dart:ui';
class Category { class Category {
String label; String label;
String color; String color;
@@ -18,4 +20,8 @@ class Category {
'label': label, 'label': label,
'color': color, 'color': color,
}; };
Color rgbToColor() {
return Color(int.parse(color.toUpperCase().replaceAll("#", ""), radix: 16));
}
} }