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);
}
}