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:flutter_bloc/flutter_bloc.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:uuid/uuid.dart';
part 'account_event.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> {
final AccountRepository _accountRepository;
@@ -38,6 +59,7 @@ class AccountBloc extends Bloc<AccountEvent, AccountState> {
_onAccountImportCSV(
AccountImportCSV event, Emitter<AccountState> emit) async {
int colorIndex = 0;
FilePickerResult? result = await FilePicker.platform.pickFiles();
final csvPath = result?.files.first.path;
@@ -46,19 +68,39 @@ class AccountBloc extends Bloc<AccountEvent, AccountState> {
final String csvFileContent = await csv.readAsString();
final List<List<dynamic>> csvList = const CsvToListConverter(fieldDelimiter: '|', eol: '\n').convert(csvFileContent);
final Map<String, Category> categoriesMap = {};
final transactions = csvList
.map((line) => Transaction(
uuid: const Uuid().v8(),
date: DateTime.parse(line[0]),
category: line[1],
description: line[2],
account: line[3],
value: _universalConvertToDouble(line[4]))
)
.toList();
.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(),
date: DateTime.parse(line[0]),
category: categoryLabel,
description: line[2],
account: line[3],
value: _universalConvertToDouble(line[4]));
})
.toList();
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:flutter_bloc/flutter_bloc.dart';
import 'package:tunas/repositories/account/account_repository.dart';
@@ -22,6 +24,7 @@ class CategoryBloc extends Bloc<CategoryEvent, CategoryState> {
) {
emit(state.copyWith(
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 List<Category> categories;
final Map<String, Color> categoriesColors;
const CategoryState({
this.categories = const [],
this.categoriesColors = const {},
});
CategoryState copyWith({
List<Category>? categories,
Map<String, Color>? categoriesColors,
}) {
return CategoryState(
categories: categories ?? this.categories,
categoriesColors: categoriesColors ?? this.categoriesColors,
);
}
@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: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,
];
}