basic csv loader, transaction list & half done stats page
This commit is contained in:
69
lib/app.dart
Normal file
69
lib/app.dart
Normal file
@@ -0,0 +1,69 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:flutter_localizations/flutter_localizations.dart';
|
||||
import 'package:tunas/clients/storage/storage_client.dart';
|
||||
import 'package:tunas/pages/home/home_page.dart';
|
||||
import 'package:tunas/repositories/account/account_repository.dart';
|
||||
|
||||
class App extends StatefulWidget {
|
||||
const App({super.key});
|
||||
|
||||
@override
|
||||
State<App> createState() => _AppState();
|
||||
}
|
||||
|
||||
class _AppState extends State<App> {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return const AppView();
|
||||
}
|
||||
}
|
||||
|
||||
class AppView extends StatefulWidget {
|
||||
const AppView({super.key});
|
||||
|
||||
@override
|
||||
State<AppView> createState() => _AppViewState();
|
||||
}
|
||||
|
||||
class _AppViewState extends State<AppView> {
|
||||
late final StorageClient _storageClient;
|
||||
late final AccountRepository _accountRepository;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
|
||||
_storageClient = StorageClient();
|
||||
_accountRepository = AccountRepository(storageClient: _storageClient);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MultiRepositoryProvider(
|
||||
providers: [RepositoryProvider.value(value: _accountRepository)],
|
||||
child: MaterialApp(
|
||||
title: 'Tunas',
|
||||
theme: ThemeData(
|
||||
colorScheme: ColorScheme.fromSeed(
|
||||
seedColor: const Color.fromARGB(255, 55, 55, 55),
|
||||
brightness: Brightness.dark
|
||||
),
|
||||
useMaterial3: true
|
||||
),
|
||||
initialRoute: '/home',
|
||||
routes: {
|
||||
'/home':(context) => const HomePage(),
|
||||
},
|
||||
localizationsDelegates: const [
|
||||
GlobalMaterialLocalizations.delegate,
|
||||
GlobalWidgetsLocalizations.delegate,
|
||||
GlobalCupertinoLocalizations.delegate
|
||||
],
|
||||
supportedLocales: const [
|
||||
Locale('fr')
|
||||
],
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
24
lib/clients/storage/storage_client.dart
Normal file
24
lib/clients/storage/storage_client.dart
Normal file
@@ -0,0 +1,24 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
|
||||
class StorageClient {
|
||||
save(String filename, String data) async {
|
||||
File file = await _getJson(filename);
|
||||
await file.writeAsString(data);
|
||||
}
|
||||
|
||||
Future<String> load(String filename) async {
|
||||
File file = await _getJson(filename);
|
||||
return file.readAsString();
|
||||
}
|
||||
|
||||
Future<File> _getJson(String filename) async {
|
||||
final dir = await getApplicationDocumentsDirectory();
|
||||
final file = File('${dir.path}/$filename');
|
||||
if (!file.existsSync()) {
|
||||
file.createSync();
|
||||
}
|
||||
return file;
|
||||
}
|
||||
}
|
||||
221
lib/domains/account/account_bloc.dart
Normal file
221
lib/domains/account/account_bloc.dart
Normal file
@@ -0,0 +1,221 @@
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:csv/csv.dart';
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:file_picker/file_picker.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:formz/formz.dart';
|
||||
import 'package:tunas/domains/account/models/transaction_account.dart';
|
||||
import 'package:tunas/domains/account/models/transaction_category.dart';
|
||||
import 'package:tunas/domains/account/models/transaction_date.dart';
|
||||
import 'package:tunas/domains/account/models/transaction_description.dart';
|
||||
import 'package:tunas/domains/account/models/transaction_line.dart';
|
||||
import 'package:tunas/domains/account/models/transaction_value.dart';
|
||||
import 'package:tunas/repositories/account/account_repository.dart';
|
||||
import 'package:tunas/repositories/account/models/transaction.dart';
|
||||
|
||||
part 'account_event.dart';
|
||||
part 'account_state.dart';
|
||||
|
||||
class AccountBloc extends Bloc<AccountEvent, AccountState> {
|
||||
final AccountRepository _accountRepository;
|
||||
|
||||
AccountBloc({required AccountRepository accountRepository})
|
||||
: _accountRepository = accountRepository,
|
||||
super(const AccountState()) {
|
||||
on<AccountAddTransaction>(_onTransactionAdd);
|
||||
on<AccountLoad>(_onAccountLoad);
|
||||
on<AccountImportJSON>(_onAccountImportJSON);
|
||||
on<AccountImportCSV>(_onAccountImportCSV);
|
||||
// on<AccountExportJSON>(_onAccountImportJSON);
|
||||
// on<AccountExportCSV>(_onAccountImportJSON);
|
||||
on<TransactionDateChange>(_onTransactionDateChange);
|
||||
on<TransactionCategoryChange>(_onTransactionCategoryChange);
|
||||
on<TransactionDescriptionChange>(_onTransactionDescriptionChange);
|
||||
on<TransactionAccountChange>(_onTransactionAccountChange);
|
||||
on<TransactionValueChange>(_onTransactionValueChange);
|
||||
on<TransactionOpenAddDialog>(_onTransactionOpenAddDialog);
|
||||
on<TransactionHideAddDialog>(_onTransactionHideAddDialog);
|
||||
on<TransactionAdd>(_onTransactionAddDialog);
|
||||
|
||||
_accountRepository
|
||||
.getTransactionsStream()
|
||||
.listen((transactions) => add(AccountLoad(transactions)));
|
||||
}
|
||||
|
||||
_onTransactionAdd(AccountAddTransaction event, Emitter<AccountState> emit) {
|
||||
state.transactions.add(event.transaction);
|
||||
var computeResult = _computeTransactionLine(state.transactions);
|
||||
|
||||
emit(state.copyWith(
|
||||
transactionsLines: computeResult.list,
|
||||
globalTotal: computeResult.globalTotal,
|
||||
accountsTotals: computeResult.accountsTotals,
|
||||
));
|
||||
}
|
||||
|
||||
_onAccountLoad(AccountLoad event, Emitter<AccountState> emit) {
|
||||
var computeResult = _computeTransactionLine(event.transactions);
|
||||
emit(state.copyWith(
|
||||
transactions: event.transactions,
|
||||
transactionsLines: computeResult.list,
|
||||
globalTotal: computeResult.globalTotal,
|
||||
accountsTotals: computeResult.accountsTotals,
|
||||
categories: computeResult.categories
|
||||
));
|
||||
}
|
||||
|
||||
_onAccountImportCSV(
|
||||
AccountImportCSV event, Emitter<AccountState> emit) async {
|
||||
FilePickerResult? result = await FilePicker.platform.pickFiles();
|
||||
|
||||
final csvPath = result?.files.first.path;
|
||||
if (csvPath != null) {
|
||||
final File csv = File(csvPath);
|
||||
final String csvFileContent = await csv.readAsString();
|
||||
final List<List<dynamic>> csvList = const CsvToListConverter(fieldDelimiter: '|').convert(csvFileContent);
|
||||
|
||||
final transactions = csvList
|
||||
.map((line) => Transaction(
|
||||
date: DateTime.parse(line[0]),
|
||||
category: line[1],
|
||||
description: line[2],
|
||||
account: line[3],
|
||||
value: double.parse(line[4]))
|
||||
)
|
||||
.toList();
|
||||
|
||||
await _accountRepository.saveTransactions(transactions);
|
||||
}
|
||||
}
|
||||
|
||||
_onAccountImportJSON(
|
||||
AccountImportJSON event, Emitter<AccountState> emit) async {
|
||||
FilePickerResult? result = await FilePicker.platform.pickFiles();
|
||||
|
||||
final jsonPath = result?.files.first.path;
|
||||
if (jsonPath != null) {
|
||||
final File json = File(jsonPath);
|
||||
final String jsonString = await json.readAsString();
|
||||
final List<dynamic> jsonList = jsonDecode(jsonString);
|
||||
final List<Transaction> transactions = jsonList.map((transaction) => Transaction.fromJson(transaction)).toList();
|
||||
|
||||
await _accountRepository.saveTransactions(transactions);
|
||||
}
|
||||
}
|
||||
|
||||
({List<TransactionLine> list, double globalTotal, Map<String, double> accountsTotals, List<String> categories}) _computeTransactionLine(List<Transaction> transactions) {
|
||||
double globalTotal = 0;
|
||||
Map<String, double> accountsTotals = <String, double>{};
|
||||
List<TransactionLine> output = [];
|
||||
Set<String> categories = {};
|
||||
|
||||
for(var transaction in transactions) {
|
||||
double subTotal = globalTotal + transaction.value;
|
||||
globalTotal = subTotal;
|
||||
|
||||
double accountTotal = accountsTotals[transaction.account] ?? 0;
|
||||
accountTotal += transaction.value;
|
||||
accountsTotals[transaction.account] = accountTotal;
|
||||
categories.add(transaction.category);
|
||||
|
||||
output.add(TransactionLine(transaction: transaction, subTotal: subTotal));
|
||||
}
|
||||
|
||||
output.sort((a, b) => b.transaction.date.compareTo(a.transaction.date));
|
||||
|
||||
return (list: output, globalTotal: globalTotal, accountsTotals: accountsTotals, categories: categories.toList());
|
||||
}
|
||||
|
||||
_onTransactionDateChange(
|
||||
TransactionDateChange event, Emitter<AccountState> emit
|
||||
) {
|
||||
final transactionDate = TransactionDate.dirty(event.date);
|
||||
emit(state.copyWith(
|
||||
transactionDate: transactionDate,
|
||||
isValid: Formz.validate([transactionDate, state.transactionCategory, state.transactionDescription, state.transactionAccount, state.transactionValue]),
|
||||
));
|
||||
}
|
||||
|
||||
_onTransactionCategoryChange(
|
||||
TransactionCategoryChange event, Emitter<AccountState> emit
|
||||
) {
|
||||
final transactionCategory = TransactionCategory.dirty(event.category);
|
||||
emit(state.copyWith(
|
||||
transactionCategory: transactionCategory,
|
||||
isValid: Formz.validate([state.transactionDate, transactionCategory, state.transactionDescription, state.transactionAccount, state.transactionValue]),
|
||||
));
|
||||
}
|
||||
|
||||
_onTransactionDescriptionChange(
|
||||
TransactionDescriptionChange event, Emitter<AccountState> emit
|
||||
) {
|
||||
final transactionDescription = TransactionDescription.dirty(event.description);
|
||||
emit(state.copyWith(
|
||||
transactionDescription: transactionDescription,
|
||||
isValid: Formz.validate([state.transactionDate, state.transactionCategory, transactionDescription, state.transactionAccount, state.transactionValue]),
|
||||
));
|
||||
}
|
||||
|
||||
_onTransactionAccountChange(
|
||||
TransactionAccountChange event, Emitter<AccountState> emit
|
||||
) {
|
||||
final transactionAccount = TransactionAccount.dirty(event.account);
|
||||
emit(state.copyWith(
|
||||
transactionAccount: transactionAccount,
|
||||
isValid: Formz.validate([state.transactionDate, state.transactionCategory, state.transactionDescription, transactionAccount, state.transactionValue]),
|
||||
));
|
||||
}
|
||||
|
||||
_onTransactionValueChange(
|
||||
TransactionValueChange event, Emitter<AccountState> emit
|
||||
) {
|
||||
try {
|
||||
final transactionValue = TransactionValue.dirty(double.parse(event.value));
|
||||
emit(state.copyWith(
|
||||
transactionValue: transactionValue,
|
||||
isValid: Formz.validate([state.transactionDate, state.transactionCategory, state.transactionDescription, state.transactionAccount, transactionValue]),
|
||||
));
|
||||
} catch (e) {
|
||||
const transactionValue = TransactionValue.dirty(double.infinity);
|
||||
emit(state.copyWith(
|
||||
transactionValue: transactionValue,
|
||||
isValid: Formz.validate([state.transactionDate, state.transactionCategory, state.transactionDescription, state.transactionAccount, transactionValue]),
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
_onTransactionOpenAddDialog(
|
||||
TransactionOpenAddDialog event, Emitter<AccountState> emit
|
||||
) {
|
||||
emit(state.copyWith(showAddDialog: true));
|
||||
}
|
||||
|
||||
_onTransactionHideAddDialog(
|
||||
TransactionHideAddDialog event, Emitter<AccountState> emit
|
||||
) {
|
||||
emit(state.copyWith(showAddDialog: false));
|
||||
}
|
||||
|
||||
_onTransactionAddDialog(
|
||||
TransactionAdd event, Emitter<AccountState> emit
|
||||
) {
|
||||
if (state.isValid) {
|
||||
state.transactions.add(Transaction(
|
||||
date: state.transactionDate.value ?? DateTime.now(),
|
||||
category: state.transactionCategory.value,
|
||||
description: state.transactionDescription.value,
|
||||
account: state.transactionAccount.value,
|
||||
value: state.transactionValue.value
|
||||
));
|
||||
var computeResult = _computeTransactionLine(state.transactions);
|
||||
|
||||
emit(state.copyWith(
|
||||
transactionsLines: computeResult.list,
|
||||
globalTotal: computeResult.globalTotal,
|
||||
accountsTotals: computeResult.accountsTotals,
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
89
lib/domains/account/account_event.dart
Normal file
89
lib/domains/account/account_event.dart
Normal file
@@ -0,0 +1,89 @@
|
||||
part of 'account_bloc.dart';
|
||||
|
||||
sealed class AccountEvent extends Equatable {
|
||||
const AccountEvent();
|
||||
|
||||
@override
|
||||
List<Object> get props => [];
|
||||
}
|
||||
|
||||
final class AccountAddTransaction extends AccountEvent {
|
||||
final Transaction transaction;
|
||||
const AccountAddTransaction(this.transaction);
|
||||
|
||||
@override
|
||||
List<Object> get props => [transaction];
|
||||
}
|
||||
|
||||
final class AccountLoad extends AccountEvent {
|
||||
final List<Transaction> transactions;
|
||||
const AccountLoad(this.transactions);
|
||||
|
||||
@override
|
||||
List<Object> get props => [transactions];
|
||||
}
|
||||
|
||||
final class AccountImportCSV extends AccountEvent {
|
||||
const AccountImportCSV();
|
||||
}
|
||||
|
||||
final class AccountImportJSON extends AccountEvent {
|
||||
const AccountImportJSON();
|
||||
}
|
||||
|
||||
final class AccountExportJSON extends AccountEvent {
|
||||
const AccountExportJSON();
|
||||
}
|
||||
|
||||
final class AccountExportCSV extends AccountEvent {
|
||||
const AccountExportCSV();
|
||||
}
|
||||
|
||||
final class TransactionDateChange extends AccountEvent {
|
||||
final DateTime? date;
|
||||
const TransactionDateChange(this.date);
|
||||
}
|
||||
|
||||
final class TransactionCategoryChange extends AccountEvent {
|
||||
final String category;
|
||||
const TransactionCategoryChange(this.category);
|
||||
|
||||
@override
|
||||
List<Object> get props => [category];
|
||||
}
|
||||
|
||||
final class TransactionDescriptionChange extends AccountEvent {
|
||||
final String description;
|
||||
const TransactionDescriptionChange(this.description);
|
||||
|
||||
@override
|
||||
List<Object> get props => [description];
|
||||
}
|
||||
|
||||
final class TransactionAccountChange extends AccountEvent {
|
||||
final String account;
|
||||
const TransactionAccountChange(this.account);
|
||||
|
||||
@override
|
||||
List<Object> get props => [account];
|
||||
}
|
||||
|
||||
final class TransactionValueChange extends AccountEvent {
|
||||
final String value;
|
||||
const TransactionValueChange(this.value);
|
||||
|
||||
@override
|
||||
List<Object> get props => [value];
|
||||
}
|
||||
|
||||
final class TransactionAdd extends AccountEvent {
|
||||
const TransactionAdd();
|
||||
}
|
||||
|
||||
final class TransactionOpenAddDialog extends AccountEvent {
|
||||
const TransactionOpenAddDialog();
|
||||
}
|
||||
|
||||
final class TransactionHideAddDialog extends AccountEvent {
|
||||
const TransactionHideAddDialog();
|
||||
}
|
||||
79
lib/domains/account/account_state.dart
Normal file
79
lib/domains/account/account_state.dart
Normal file
@@ -0,0 +1,79 @@
|
||||
part of 'account_bloc.dart';
|
||||
|
||||
final class AccountState extends Equatable {
|
||||
final List<Transaction> transactions;
|
||||
final List<TransactionLine> transactionsLines;
|
||||
final double globalTotal;
|
||||
final Map<String, double> accountsTotals;
|
||||
|
||||
final List<String> categories;
|
||||
|
||||
final TransactionDate transactionDate;
|
||||
final TransactionCategory transactionCategory;
|
||||
final TransactionDescription transactionDescription;
|
||||
final TransactionAccount transactionAccount;
|
||||
final TransactionValue transactionValue;
|
||||
final bool isValid;
|
||||
final bool showAddDialog;
|
||||
|
||||
const AccountState({
|
||||
this.transactions = const [],
|
||||
this.transactionsLines = const [],
|
||||
this.globalTotal = 0,
|
||||
this.accountsTotals = const <String, double>{},
|
||||
this.categories = const [],
|
||||
this.transactionDate = const TransactionDate.pure(),
|
||||
this.transactionCategory = const TransactionCategory.pure(),
|
||||
this.transactionDescription = const TransactionDescription.pure(),
|
||||
this.transactionAccount = const TransactionAccount.pure(),
|
||||
this.transactionValue = const TransactionValue.pure(),
|
||||
this.isValid = false,
|
||||
this.showAddDialog = false,
|
||||
});
|
||||
|
||||
AccountState copyWith({
|
||||
List<Transaction>? transactions,
|
||||
List<TransactionLine>? transactionsLines,
|
||||
double? globalTotal,
|
||||
Map<String, double>? accountsTotals,
|
||||
List<String>? categories,
|
||||
TransactionDate? transactionDate,
|
||||
TransactionCategory? transactionCategory,
|
||||
TransactionDescription? transactionDescription,
|
||||
TransactionAccount? transactionAccount,
|
||||
TransactionValue? transactionValue,
|
||||
bool? isValid,
|
||||
bool? showAddDialog,
|
||||
}) {
|
||||
return AccountState(
|
||||
transactions: transactions ?? this.transactions,
|
||||
transactionsLines: transactionsLines ?? this.transactionsLines,
|
||||
globalTotal: globalTotal ?? this.globalTotal,
|
||||
accountsTotals: accountsTotals ?? this.accountsTotals,
|
||||
categories: categories ?? this.categories,
|
||||
transactionDate: transactionDate ?? this.transactionDate,
|
||||
transactionCategory: transactionCategory ?? this.transactionCategory,
|
||||
transactionDescription: transactionDescription ?? this.transactionDescription,
|
||||
transactionAccount: transactionAccount ?? this.transactionAccount,
|
||||
transactionValue: transactionValue ?? this.transactionValue,
|
||||
isValid: isValid ?? this.isValid,
|
||||
showAddDialog: showAddDialog ?? this.showAddDialog,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
List<Object?> get props => [
|
||||
transactions,
|
||||
transactionsLines,
|
||||
globalTotal,
|
||||
accountsTotals,
|
||||
categories,
|
||||
transactionDate,
|
||||
transactionCategory,
|
||||
transactionDescription,
|
||||
transactionAccount,
|
||||
transactionValue,
|
||||
isValid,
|
||||
showAddDialog,
|
||||
];
|
||||
}
|
||||
19
lib/domains/account/models/transaction_account.dart
Normal file
19
lib/domains/account/models/transaction_account.dart
Normal file
@@ -0,0 +1,19 @@
|
||||
import 'package:formz/formz.dart';
|
||||
|
||||
enum TransactionAccountValidationError {
|
||||
empty('Account empty'),
|
||||
invalid('Account invalid');
|
||||
|
||||
final String message;
|
||||
const TransactionAccountValidationError(this.message);
|
||||
}
|
||||
|
||||
class TransactionAccount extends FormzInput<String, TransactionAccountValidationError> {
|
||||
const TransactionAccount.pure() : super.pure('');
|
||||
const TransactionAccount.dirty([super.value = '']) : super.dirty();
|
||||
|
||||
@override
|
||||
TransactionAccountValidationError? validator(String value) {
|
||||
return value.isEmpty ? TransactionAccountValidationError.empty : null;
|
||||
}
|
||||
}
|
||||
19
lib/domains/account/models/transaction_category.dart
Normal file
19
lib/domains/account/models/transaction_category.dart
Normal file
@@ -0,0 +1,19 @@
|
||||
import 'package:formz/formz.dart';
|
||||
|
||||
enum TransactionCategoryValidationError {
|
||||
empty('Category empty'),
|
||||
invalid('Category invalid');
|
||||
|
||||
final String message;
|
||||
const TransactionCategoryValidationError(this.message);
|
||||
}
|
||||
|
||||
class TransactionCategory extends FormzInput<String, TransactionCategoryValidationError> {
|
||||
const TransactionCategory.pure() : super.pure('');
|
||||
const TransactionCategory.dirty([super.value = '']) : super.dirty();
|
||||
|
||||
@override
|
||||
TransactionCategoryValidationError? validator(String value) {
|
||||
return value.isEmpty ? TransactionCategoryValidationError.empty : null;
|
||||
}
|
||||
}
|
||||
19
lib/domains/account/models/transaction_date.dart
Normal file
19
lib/domains/account/models/transaction_date.dart
Normal file
@@ -0,0 +1,19 @@
|
||||
import 'package:formz/formz.dart';
|
||||
|
||||
enum TransactionDateValidationError {
|
||||
empty('Date empty'),
|
||||
invalid('Date invalid');
|
||||
|
||||
final String message;
|
||||
const TransactionDateValidationError(this.message);
|
||||
}
|
||||
|
||||
class TransactionDate extends FormzInput<DateTime?, TransactionDateValidationError> {
|
||||
const TransactionDate.pure() : super.pure(null);
|
||||
const TransactionDate.dirty([super.value]) : super.dirty();
|
||||
|
||||
@override
|
||||
TransactionDateValidationError? validator(DateTime? value) {
|
||||
return value == null ? TransactionDateValidationError.empty : null;
|
||||
}
|
||||
}
|
||||
19
lib/domains/account/models/transaction_description.dart
Normal file
19
lib/domains/account/models/transaction_description.dart
Normal file
@@ -0,0 +1,19 @@
|
||||
import 'package:formz/formz.dart';
|
||||
|
||||
enum TransactionDescriptionValidationError {
|
||||
empty('Description empty'),
|
||||
invalid('Description invalid');
|
||||
|
||||
final String message;
|
||||
const TransactionDescriptionValidationError(this.message);
|
||||
}
|
||||
|
||||
class TransactionDescription extends FormzInput<String, TransactionDescriptionValidationError> {
|
||||
const TransactionDescription.pure() : super.pure('');
|
||||
const TransactionDescription.dirty([super.value = '']) : super.dirty();
|
||||
|
||||
@override
|
||||
TransactionDescriptionValidationError? validator(String value) {
|
||||
return value.isEmpty ? TransactionDescriptionValidationError.empty : null;
|
||||
}
|
||||
}
|
||||
11
lib/domains/account/models/transaction_line.dart
Normal file
11
lib/domains/account/models/transaction_line.dart
Normal file
@@ -0,0 +1,11 @@
|
||||
import 'package:tunas/repositories/account/models/transaction.dart';
|
||||
|
||||
class TransactionLine {
|
||||
Transaction transaction;
|
||||
double subTotal;
|
||||
|
||||
TransactionLine({
|
||||
required this.transaction,
|
||||
required this.subTotal,
|
||||
});
|
||||
}
|
||||
23
lib/domains/account/models/transaction_value.dart
Normal file
23
lib/domains/account/models/transaction_value.dart
Normal file
@@ -0,0 +1,23 @@
|
||||
import 'package:formz/formz.dart';
|
||||
|
||||
enum TransactionValueValidationError {
|
||||
empty('Value empty'),
|
||||
invalid('Value invalid');
|
||||
|
||||
final String message;
|
||||
const TransactionValueValidationError(this.message);
|
||||
}
|
||||
|
||||
class TransactionValue extends FormzInput<double, TransactionValueValidationError> {
|
||||
const TransactionValue.pure() : super.pure(0);
|
||||
const TransactionValue.dirty([super.value = 0]) : super.dirty();
|
||||
|
||||
@override
|
||||
TransactionValueValidationError? validator(double value) {
|
||||
return value.isNaN
|
||||
? TransactionValueValidationError.empty
|
||||
: value.isInfinite
|
||||
? TransactionValueValidationError.invalid
|
||||
: null;
|
||||
}
|
||||
}
|
||||
207
lib/domains/charts/chart_bloc.dart
Normal file
207
lib/domains/charts/chart_bloc.dart
Normal file
@@ -0,0 +1,207 @@
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:fl_chart/fl_chart.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:tunas/domains/account/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';
|
||||
|
||||
class ChartBloc extends Bloc<ChartEvent, ChartState> {
|
||||
final AccountRepository _accountRepository;
|
||||
|
||||
ChartBloc({required AccountRepository accountRepository}) :
|
||||
_accountRepository = accountRepository, super(const ChartState()) {
|
||||
|
||||
on<ChartAccountLoad>(_onAccountLoad);
|
||||
on<ChartNextYear>(_onNextYear);
|
||||
on<ChartPreviousYear>(_onPreviousYear);
|
||||
|
||||
_accountRepository
|
||||
.getTransactionsStream()
|
||||
.listen((transactions) => add(ChartAccountLoad(transactions)));
|
||||
}
|
||||
|
||||
_onAccountLoad(ChartAccountLoad event, Emitter<ChartState> emit) {
|
||||
ChartState localState = state.copyWith(transactions: event.transactions);
|
||||
emit(_computeStateStats(localState));
|
||||
}
|
||||
|
||||
_onNextYear(ChartNextYear event, Emitter<ChartState> emit) {
|
||||
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));
|
||||
}
|
||||
|
||||
ChartState _computeStateStats(ChartState state) {
|
||||
double globalTotal = 0;
|
||||
Map<String, double> accountsTotals = <String, double>{};
|
||||
List<TransactionLine> transactionsLines = [];
|
||||
|
||||
Map<String, double> categoriesTotals = {};
|
||||
List<FlSpot> monthlyTotals = [];
|
||||
Map<String, Map<String, double>> 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<ChartItem> scopedCategoriesPositiveTotals = [];
|
||||
List<ChartItem> scopedCategoriesNegativeTotals = [];
|
||||
Map<int, FlSpot> scopedMonthlyTotals = {};
|
||||
Map<String, Map<String, double>> scopedCategoriesMonthlyTotals = {};
|
||||
|
||||
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 (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;
|
||||
}
|
||||
}
|
||||
|
||||
List<ChartItem> scopedCategoriesPositiveTotalsPercents = [];
|
||||
List<ChartItem> scopedCategoriesNegativeTotalsPercents = [];
|
||||
List<ChartItem> scopedSimplifiedCategoriesPositiveTotalsPercents = [];
|
||||
List<ChartItem> 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<ChartItem> scopedSimplifiedCategoriesPositiveTotals = [];
|
||||
List<ChartItem> 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,
|
||||
);
|
||||
}
|
||||
}
|
||||
19
lib/domains/charts/chart_event.dart
Normal file
19
lib/domains/charts/chart_event.dart
Normal file
@@ -0,0 +1,19 @@
|
||||
part of 'chart_bloc.dart';
|
||||
|
||||
sealed class ChartEvent extends Equatable {
|
||||
const ChartEvent();
|
||||
|
||||
@override
|
||||
List<Object> get props => [];
|
||||
}
|
||||
|
||||
final class ChartAccountLoad extends ChartEvent {
|
||||
final List<Transaction> transactions;
|
||||
const ChartAccountLoad(this.transactions);
|
||||
|
||||
@override
|
||||
List<Object> get props => [transactions];
|
||||
}
|
||||
|
||||
final class ChartNextYear extends ChartEvent {}
|
||||
final class ChartPreviousYear extends ChartEvent {}
|
||||
123
lib/domains/charts/chart_state.dart
Normal file
123
lib/domains/charts/chart_state.dart
Normal file
@@ -0,0 +1,123 @@
|
||||
part of 'chart_bloc.dart';
|
||||
|
||||
final class ChartState extends Equatable {
|
||||
final List<Transaction> transactions;
|
||||
final List<TransactionLine> transactionsLines;
|
||||
|
||||
final double globalTotal;
|
||||
final Map<String, double> accountsTotals;
|
||||
|
||||
final DateTime? firstDate;
|
||||
final DateTime? lastDate;
|
||||
|
||||
final num currentYear;
|
||||
|
||||
final Map<String, double> categoriesTotals;
|
||||
final List<FlSpot> monthlyTotals;
|
||||
final Map<String, Map<String, double>> categoriesMonthlyTotals;
|
||||
|
||||
final List<ChartItem> scopedCategoriesPositiveTotals;
|
||||
final List<ChartItem> scopedCategoriesPositiveTotalsPercents;
|
||||
final List<ChartItem> scopedCategoriesNegativeTotals;
|
||||
final List<ChartItem> scopedCategoriesNegativeTotalsPercents;
|
||||
|
||||
final List<ChartItem> scopedSimplifiedCategoriesPositiveTotals;
|
||||
final List<ChartItem> scopedSimplifiedCategoriesPositiveTotalsPercents;
|
||||
final List<ChartItem> scopedSimplifiedCategoriesNegativeTotals;
|
||||
final List<ChartItem> scopedSimplifiedCategoriesNegativeTotalsPercents;
|
||||
|
||||
final List<FlSpot> scopedMonthlyTotals;
|
||||
final Map<String, Map<String, double>> scopedCategoriesMonthlyTotals;
|
||||
|
||||
const ChartState({
|
||||
this.transactions = const [],
|
||||
this.transactionsLines = const [],
|
||||
this.globalTotal = 0,
|
||||
this.accountsTotals = const {},
|
||||
this.firstDate,
|
||||
this.lastDate,
|
||||
this.currentYear = 2000,
|
||||
this.categoriesTotals = const {},
|
||||
this.monthlyTotals = const [],
|
||||
this.categoriesMonthlyTotals = const {},
|
||||
this.scopedCategoriesPositiveTotals = const [],
|
||||
this.scopedCategoriesPositiveTotalsPercents = const [],
|
||||
this.scopedCategoriesNegativeTotals = const [],
|
||||
this.scopedCategoriesNegativeTotalsPercents = const [],
|
||||
this.scopedSimplifiedCategoriesPositiveTotals = const [],
|
||||
this.scopedSimplifiedCategoriesPositiveTotalsPercents = const [],
|
||||
this.scopedSimplifiedCategoriesNegativeTotals = const [],
|
||||
this.scopedSimplifiedCategoriesNegativeTotalsPercents = const [],
|
||||
this.scopedMonthlyTotals = const [],
|
||||
this.scopedCategoriesMonthlyTotals = const {},
|
||||
});
|
||||
|
||||
ChartState copyWith({
|
||||
List<Transaction>? transactions,
|
||||
List<TransactionLine>? transactionsLines,
|
||||
double? globalTotal,
|
||||
Map<String, double>? accountsTotals,
|
||||
DateTime? firstDate,
|
||||
DateTime? lastDate,
|
||||
num? currentYear,
|
||||
Map<String, double>? categoriesTotals,
|
||||
List<FlSpot>? monthlyTotals,
|
||||
Map<String, Map<String, double>>? categoriesMonthlyTotals,
|
||||
List<ChartItem>? scopedCategoriesPositiveTotals,
|
||||
List<ChartItem>? scopedCategoriesPositiveTotalsPercents,
|
||||
List<ChartItem>? scopedCategoriesNegativeTotals,
|
||||
List<ChartItem>? scopedCategoriesNegativeTotalsPercents,
|
||||
List<ChartItem>? scopedSimplifiedCategoriesPositiveTotals,
|
||||
List<ChartItem>? scopedSimplifiedCategoriesPositiveTotalsPercents,
|
||||
List<ChartItem>? scopedSimplifiedCategoriesNegativeTotals,
|
||||
List<ChartItem>? scopedSimplifiedCategoriesNegativeTotalsPercents,
|
||||
List<FlSpot>? scopedMonthlyTotals,
|
||||
Map<String, Map<String, double>>? scopedCategoriesMonthlyTotals,
|
||||
}) {
|
||||
return ChartState(
|
||||
transactions: transactions ?? this.transactions,
|
||||
transactionsLines: transactionsLines ?? this.transactionsLines,
|
||||
globalTotal: globalTotal ?? this.globalTotal,
|
||||
accountsTotals: accountsTotals ?? this.accountsTotals,
|
||||
firstDate: firstDate ?? this.firstDate,
|
||||
lastDate: lastDate ?? this.lastDate,
|
||||
currentYear: currentYear ?? this.currentYear,
|
||||
categoriesTotals: categoriesTotals ?? this.categoriesTotals,
|
||||
monthlyTotals: monthlyTotals ?? this.monthlyTotals,
|
||||
categoriesMonthlyTotals: categoriesMonthlyTotals ?? this.categoriesMonthlyTotals,
|
||||
scopedCategoriesPositiveTotals: scopedCategoriesPositiveTotals ?? this.scopedCategoriesPositiveTotals,
|
||||
scopedCategoriesPositiveTotalsPercents: scopedCategoriesPositiveTotalsPercents ?? this.scopedCategoriesPositiveTotalsPercents,
|
||||
scopedCategoriesNegativeTotals: scopedCategoriesNegativeTotals ?? this.scopedCategoriesNegativeTotals,
|
||||
scopedCategoriesNegativeTotalsPercents: scopedCategoriesNegativeTotalsPercents ?? this.scopedCategoriesNegativeTotalsPercents,
|
||||
scopedSimplifiedCategoriesPositiveTotals: scopedSimplifiedCategoriesPositiveTotals ?? this.scopedSimplifiedCategoriesPositiveTotals,
|
||||
scopedSimplifiedCategoriesPositiveTotalsPercents: scopedSimplifiedCategoriesPositiveTotalsPercents ?? this.scopedSimplifiedCategoriesPositiveTotalsPercents,
|
||||
scopedSimplifiedCategoriesNegativeTotals: scopedSimplifiedCategoriesNegativeTotals ?? this.scopedSimplifiedCategoriesNegativeTotals,
|
||||
scopedSimplifiedCategoriesNegativeTotalsPercents: scopedSimplifiedCategoriesNegativeTotalsPercents ?? this.scopedSimplifiedCategoriesNegativeTotalsPercents,
|
||||
scopedMonthlyTotals: scopedMonthlyTotals ?? this.scopedMonthlyTotals,
|
||||
scopedCategoriesMonthlyTotals: scopedCategoriesMonthlyTotals ?? this.scopedCategoriesMonthlyTotals,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
List<Object> get props => [
|
||||
transactions,
|
||||
transactionsLines,
|
||||
globalTotal,
|
||||
accountsTotals,
|
||||
currentYear,
|
||||
categoriesTotals,
|
||||
monthlyTotals,
|
||||
categoriesMonthlyTotals,
|
||||
scopedCategoriesPositiveTotals,
|
||||
scopedCategoriesPositiveTotalsPercents,
|
||||
scopedCategoriesNegativeTotals,
|
||||
scopedCategoriesNegativeTotalsPercents,
|
||||
scopedSimplifiedCategoriesPositiveTotals,
|
||||
scopedSimplifiedCategoriesPositiveTotalsPercents,
|
||||
scopedSimplifiedCategoriesNegativeTotals,
|
||||
scopedSimplifiedCategoriesNegativeTotalsPercents,
|
||||
scopedMonthlyTotals,
|
||||
scopedCategoriesMonthlyTotals,
|
||||
];
|
||||
}
|
||||
|
||||
9
lib/domains/charts/models/chart_item.dart
Normal file
9
lib/domains/charts/models/chart_item.dart
Normal file
@@ -0,0 +1,9 @@
|
||||
class ChartItem {
|
||||
String label;
|
||||
double value;
|
||||
|
||||
ChartItem({
|
||||
required this.label,
|
||||
required this.value,
|
||||
});
|
||||
}
|
||||
8
lib/main.dart
Normal file
8
lib/main.dart
Normal file
@@ -0,0 +1,8 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:intl/date_symbol_data_local.dart';
|
||||
import 'package:tunas/app.dart';
|
||||
|
||||
void main() {
|
||||
initializeDateFormatting('fr_FR', null);
|
||||
return runApp(const App());
|
||||
}
|
||||
10
lib/pages/budgets/budgets_page.dart
Normal file
10
lib/pages/budgets/budgets_page.dart
Normal file
@@ -0,0 +1,10 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class BudgetsPage extends StatelessWidget {
|
||||
const BudgetsPage({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return const Text('Budgets');
|
||||
}
|
||||
}
|
||||
33
lib/pages/data/data_page.dart
Normal file
33
lib/pages/data/data_page.dart
Normal file
@@ -0,0 +1,33 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:tunas/domains/account/account_bloc.dart';
|
||||
|
||||
class DataPage extends StatelessWidget {
|
||||
const DataPage({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return BlocBuilder<AccountBloc, AccountState>(
|
||||
builder: (context, state) => Column(
|
||||
children: [
|
||||
FilledButton(
|
||||
onPressed: () => context.read<AccountBloc>().add(const AccountImportCSV()),
|
||||
child: const Text('Import CSV')
|
||||
),
|
||||
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')
|
||||
),
|
||||
],
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
43
lib/pages/home/home_page.dart
Normal file
43
lib/pages/home/home_page.dart
Normal file
@@ -0,0 +1,43 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:tunas/domains/account/account_bloc.dart';
|
||||
import 'package:tunas/pages/budgets/budgets_page.dart';
|
||||
import 'package:tunas/pages/data/data_page.dart';
|
||||
import 'package:tunas/pages/stats/stats_page.dart';
|
||||
import 'package:tunas/pages/transactions/transactions_page.dart';
|
||||
import 'package:tunas/repositories/account/account_repository.dart';
|
||||
|
||||
class HomePage extends StatelessWidget {
|
||||
const HomePage({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return BlocProvider(
|
||||
create: (context) => AccountBloc(accountRepository: RepositoryProvider.of<AccountRepository>(context)),
|
||||
child: DefaultTabController(
|
||||
length: 4,
|
||||
child: Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('Tunas'),
|
||||
bottom: const TabBar(
|
||||
tabs: [
|
||||
Tab(text: 'Dashboard'),
|
||||
Tab(text: 'Transactions'),
|
||||
Tab(text: 'Budgets'),
|
||||
Tab(text: 'Data'),
|
||||
],
|
||||
),
|
||||
),
|
||||
body: const TabBarView(
|
||||
children: [
|
||||
StatsPage(),
|
||||
TransactionsPage(),
|
||||
BudgetsPage(),
|
||||
DataPage()
|
||||
],
|
||||
),
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
21
lib/pages/startup/startup_page.dart
Normal file
21
lib/pages/startup/startup_page.dart
Normal file
@@ -0,0 +1,21 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class StartupPage extends StatelessWidget {
|
||||
const StartupPage({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return const Scaffold(
|
||||
body: Center(
|
||||
child: Column(
|
||||
children: [
|
||||
CircularProgressIndicator(
|
||||
value: 5,
|
||||
),
|
||||
Text('Chargement...')
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
65
lib/pages/stats/stats_page.dart
Normal file
65
lib/pages/stats/stats_page.dart
Normal file
@@ -0,0 +1,65 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:tunas/domains/charts/chart_bloc.dart';
|
||||
import 'package:tunas/pages/stats/widgets/account_counters.dart';
|
||||
import 'package:tunas/pages/stats/widgets/categories_totals_chart.dart';
|
||||
import 'package:tunas/pages/stats/widgets/main_counter.dart';
|
||||
import 'package:tunas/pages/stats/widgets/monthly_categories_total_chart.dart';
|
||||
import 'package:tunas/pages/stats/widgets/monthly_total_chart.dart';
|
||||
import 'package:tunas/pages/stats/widgets/profit_indicator.dart';
|
||||
import 'package:tunas/pages/stats/widgets/year_selector.dart';
|
||||
import 'package:tunas/repositories/account/account_repository.dart';
|
||||
|
||||
class StatsPage extends StatelessWidget {
|
||||
const StatsPage({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return BlocProvider(
|
||||
create: (context) => ChartBloc(accountRepository: RepositoryProvider.of<AccountRepository>(context)),
|
||||
child: BlocBuilder<ChartBloc, ChartState>(
|
||||
builder: (context, state) => ListView(
|
||||
children: [
|
||||
Row(
|
||||
children: [
|
||||
Expanded(
|
||||
flex: 2,
|
||||
child: MainCounter(value: state.globalTotal)
|
||||
),
|
||||
Expanded(
|
||||
flex: 1,
|
||||
child: AccountCounter(accountsTotals: state.accountsTotals)
|
||||
),
|
||||
],
|
||||
),
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
const YearSelector(),
|
||||
ProfitIndicator(monthlyTotals: state.scopedMonthlyTotals)
|
||||
],
|
||||
),
|
||||
SizedBox(
|
||||
height: 200,
|
||||
child: GlobalTotalChart(monthlyTotals: state.scopedMonthlyTotals)
|
||||
),
|
||||
SizedBox(
|
||||
height: 500,
|
||||
child: MonthlyCategoriesTotalChart(categoriesMonthlyTotals: state.scopedCategoriesMonthlyTotals)
|
||||
),
|
||||
SizedBox(
|
||||
height: 450,
|
||||
child: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
CategoriesTotalsChart(categoriesTotals: state.scopedCategoriesPositiveTotals, categoriesTotalsPercents: state.scopedCategoriesPositiveTotalsPercents,),
|
||||
CategoriesTotalsChart(categoriesTotals: state.scopedCategoriesNegativeTotals, categoriesTotalsPercents: state.scopedSimplifiedCategoriesNegativeTotalsPercents,),
|
||||
],
|
||||
)
|
||||
),
|
||||
],
|
||||
)
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
41
lib/pages/stats/widgets/account_counters.dart
Normal file
41
lib/pages/stats/widgets/account_counters.dart
Normal file
@@ -0,0 +1,41 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:intl/intl.dart';
|
||||
|
||||
class AccountCounter extends StatelessWidget {
|
||||
final Map<String, double> accountsTotals;
|
||||
|
||||
const AccountCounter({super.key, required this.accountsTotals});
|
||||
|
||||
List<Row> _renderAccountTotals() {
|
||||
return accountsTotals.entries.toList().map((entry) => Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
Text("${entry.key}:"),
|
||||
Text(
|
||||
NumberFormat('#######.00 €', 'fr_FR').format(entry.value),
|
||||
style: TextStyle(
|
||||
fontFamily: 'NovaMono',
|
||||
fontSize: 15,
|
||||
color: entry.value > 0 ? const Color.fromARGB(255, 0, 255, 8) : Colors.red
|
||||
)),
|
||||
],
|
||||
)).toList();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Container(
|
||||
padding: EdgeInsets.all(10),
|
||||
margin: EdgeInsets.all(20),
|
||||
decoration: BoxDecoration(
|
||||
borderRadius: BorderRadius.circular(5),
|
||||
color: Colors.blue
|
||||
),
|
||||
alignment: Alignment.centerRight,
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.end,
|
||||
children: _renderAccountTotals(),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
121
lib/pages/stats/widgets/categories_totals_chart.dart
Normal file
121
lib/pages/stats/widgets/categories_totals_chart.dart
Normal file
@@ -0,0 +1,121 @@
|
||||
import 'package:fl_chart/fl_chart.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:intl/intl.dart';
|
||||
import 'package:tunas/domains/charts/models/chart_item.dart';
|
||||
|
||||
class CategoriesTotalsChart extends StatelessWidget {
|
||||
final List<ChartItem> categoriesTotals;
|
||||
final List<ChartItem> categoriesTotalsPercents;
|
||||
|
||||
const CategoriesTotalsChart({super.key, required this.categoriesTotals, required this.categoriesTotalsPercents});
|
||||
|
||||
List<PieChartSectionData> _convertDataForChart() {
|
||||
var count = 1;
|
||||
var 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,
|
||||
];
|
||||
return categoriesTotalsPercents
|
||||
.map((item) =>
|
||||
PieChartSectionData(
|
||||
value: item.value,
|
||||
title: item.label,
|
||||
titleStyle: const TextStyle(
|
||||
fontSize: 15,
|
||||
fontWeight: FontWeight.w300
|
||||
),
|
||||
titlePositionPercentageOffset: 0.8,
|
||||
borderSide: const BorderSide(width: 0),
|
||||
radius: 150,
|
||||
color: colors[count++]
|
||||
))
|
||||
.toList();
|
||||
}
|
||||
|
||||
List<Row> _computeLegend() {
|
||||
return categoriesTotals
|
||||
.map((item) => Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
Text("${item.label}: "),
|
||||
Text(
|
||||
NumberFormat("#00 €").format(item.value),
|
||||
style: const TextStyle(
|
||||
fontFamily: 'NovaMono',
|
||||
)
|
||||
)
|
||||
],
|
||||
)).toList();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Container(
|
||||
height: 320,
|
||||
width: 550,
|
||||
padding: const EdgeInsets.all(10),
|
||||
margin: const EdgeInsets.all(20),
|
||||
decoration: BoxDecoration(
|
||||
borderRadius: BorderRadius.circular(5),
|
||||
color: Colors.blue
|
||||
),
|
||||
child: Expanded(
|
||||
child: AspectRatio(
|
||||
aspectRatio: 1.3,
|
||||
child: Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: AspectRatio(
|
||||
aspectRatio: 1,
|
||||
child: PieChart(
|
||||
PieChartData(
|
||||
sections: _convertDataForChart(),
|
||||
borderData: FlBorderData(
|
||||
show: false
|
||||
),
|
||||
centerSpaceRadius: 0,
|
||||
sectionsSpace: 2
|
||||
)
|
||||
),
|
||||
)
|
||||
),
|
||||
Container(
|
||||
height: 300,
|
||||
padding: const EdgeInsets.all(10),
|
||||
decoration: BoxDecoration(
|
||||
borderRadius: BorderRadius.circular(5),
|
||||
color: Colors.blueGrey
|
||||
),
|
||||
child: SingleChildScrollView(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: _computeLegend(),
|
||||
),
|
||||
)
|
||||
),
|
||||
],
|
||||
),
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
30
lib/pages/stats/widgets/main_counter.dart
Normal file
30
lib/pages/stats/widgets/main_counter.dart
Normal file
@@ -0,0 +1,30 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:intl/intl.dart';
|
||||
|
||||
class MainCounter extends StatelessWidget {
|
||||
final double value;
|
||||
|
||||
const MainCounter({super.key, required this.value});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Container(
|
||||
padding: const EdgeInsets.all(10),
|
||||
margin: const EdgeInsets.all(20),
|
||||
decoration: BoxDecoration(
|
||||
borderRadius: BorderRadius.circular(5),
|
||||
color: Colors.blue
|
||||
),
|
||||
alignment: Alignment.centerRight,
|
||||
child: Text(
|
||||
NumberFormat('000.00 €', 'fr_FR').format(value),
|
||||
style: TextStyle(
|
||||
fontFamily: 'NovaMono',
|
||||
fontSize: 60,
|
||||
fontWeight: FontWeight.w500,
|
||||
color: value > 0 ? const Color.fromARGB(255, 0, 255, 8) : Colors.red
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
15
lib/pages/stats/widgets/monthly_categories_total_chart.dart
Normal file
15
lib/pages/stats/widgets/monthly_categories_total_chart.dart
Normal file
@@ -0,0 +1,15 @@
|
||||
import 'package:fl_chart/fl_chart.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class MonthlyCategoriesTotalChart extends StatelessWidget {
|
||||
final Map<String, Map<String, double>> categoriesMonthlyTotals;
|
||||
|
||||
const MonthlyCategoriesTotalChart({super.key, required this.categoriesMonthlyTotals});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return BarChart(
|
||||
BarChartData()
|
||||
);
|
||||
}
|
||||
}
|
||||
92
lib/pages/stats/widgets/monthly_total_chart.dart
Normal file
92
lib/pages/stats/widgets/monthly_total_chart.dart
Normal file
@@ -0,0 +1,92 @@
|
||||
import 'package:fl_chart/fl_chart.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:intl/intl.dart';
|
||||
|
||||
class GlobalTotalChart extends StatelessWidget {
|
||||
final List<FlSpot> monthlyTotals;
|
||||
|
||||
const GlobalTotalChart({super.key, required this.monthlyTotals});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
var maxY = 1.0;
|
||||
var minY = -1.0;
|
||||
if (monthlyTotals.isNotEmpty) {
|
||||
maxY = monthlyTotals.map((e) => e.y).toList().reduce((value, element) => value > element ? value : element) + 1000;
|
||||
minY = monthlyTotals.map((e) => e.y).toList().reduce((value, element) => value < element ? value : element) - 1000;
|
||||
}
|
||||
|
||||
return Padding(
|
||||
padding: const EdgeInsets.only(
|
||||
left: 12,
|
||||
bottom: 12,
|
||||
right: 20,
|
||||
top: 20,
|
||||
),
|
||||
child: AspectRatio(
|
||||
aspectRatio: 1,
|
||||
child: LayoutBuilder(
|
||||
builder: (context, constraints) => LineChart(
|
||||
LineChartData(
|
||||
lineTouchData: LineTouchData(
|
||||
touchTooltipData: LineTouchTooltipData(
|
||||
maxContentWidth: 100,
|
||||
tooltipBgColor: Colors.black,
|
||||
getTooltipItems: (touchedSpots) {
|
||||
return touchedSpots.map((LineBarSpot touchedSpot) {
|
||||
final textStyle = TextStyle(
|
||||
color: touchedSpot.bar.gradient?.colors[0] ?? touchedSpot.bar.color,
|
||||
fontWeight: FontWeight.bold,
|
||||
fontSize: 14,
|
||||
);
|
||||
final date = DateTime(DateTime.now().year).add(Duration(days: touchedSpot.x.toInt() - 1));
|
||||
return LineTooltipItem(
|
||||
"${NumberFormat('#######.00 €', 'fr_FR').format(touchedSpot.y )} ${date.day}/${date.month}",
|
||||
textStyle,
|
||||
);
|
||||
}).toList();
|
||||
},
|
||||
),
|
||||
handleBuiltInTouches: true,
|
||||
getTouchLineStart: (data, index) => 0,
|
||||
),
|
||||
lineBarsData: [
|
||||
LineChartBarData(
|
||||
color: Colors.pink,
|
||||
spots: monthlyTotals,
|
||||
isCurved: true,
|
||||
isStrokeCapRound: true,
|
||||
barWidth: 3,
|
||||
belowBarData: BarAreaData(
|
||||
show: false,
|
||||
),
|
||||
dotData: const FlDotData(show: false),
|
||||
),
|
||||
],
|
||||
minY: minY,
|
||||
maxY: maxY,
|
||||
titlesData: const FlTitlesData(
|
||||
leftTitles: AxisTitles(
|
||||
sideTitles: SideTitles(showTitles: false),
|
||||
),
|
||||
rightTitles: AxisTitles(
|
||||
sideTitles: SideTitles(showTitles: false),
|
||||
),
|
||||
topTitles: AxisTitles(
|
||||
sideTitles: SideTitles(showTitles: false),
|
||||
),
|
||||
bottomTitles: AxisTitles(
|
||||
sideTitles: SideTitles(showTitles: false),
|
||||
),
|
||||
),
|
||||
gridData: const FlGridData(
|
||||
show: false,
|
||||
),
|
||||
borderData: FlBorderData(show: false),
|
||||
),
|
||||
)
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
41
lib/pages/stats/widgets/profit_indicator.dart
Normal file
41
lib/pages/stats/widgets/profit_indicator.dart
Normal file
@@ -0,0 +1,41 @@
|
||||
import 'package:fl_chart/fl_chart.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:intl/intl.dart';
|
||||
|
||||
class ProfitIndicator extends StatelessWidget {
|
||||
final List<FlSpot> monthlyTotals;
|
||||
|
||||
const ProfitIndicator({super.key, required this.monthlyTotals});
|
||||
|
||||
double _profit() {
|
||||
if (monthlyTotals.isEmpty) {
|
||||
return 0;
|
||||
}
|
||||
final maxDateSpot = monthlyTotals.reduce((value, element) => value.x > element.x ? value : element);
|
||||
final minDateSpot = monthlyTotals.reduce((value, element) => value.x < element.x ? value : element);
|
||||
|
||||
return maxDateSpot.y - minDateSpot.y;
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final profit = _profit();
|
||||
return Container(
|
||||
margin: const EdgeInsets.fromLTRB(0, 0, 20, 0),
|
||||
padding: const EdgeInsets.fromLTRB(10, 5, 10, 5),
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.blue,
|
||||
borderRadius: BorderRadius.circular(5)
|
||||
),
|
||||
child: Text(
|
||||
"Profit ${NumberFormat('#####00.00 €', 'fr_FR').format(profit)}",
|
||||
style: TextStyle(
|
||||
fontFamily: 'NovaMono',
|
||||
fontSize: 20,
|
||||
fontWeight: FontWeight.w500,
|
||||
color: profit > 0 ? const Color.fromARGB(255, 0, 255, 8) : Colors.red
|
||||
),
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
34
lib/pages/stats/widgets/year_selector.dart
Normal file
34
lib/pages/stats/widgets/year_selector.dart
Normal file
@@ -0,0 +1,34 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:tunas/domains/charts/chart_bloc.dart';
|
||||
|
||||
class YearSelector extends StatelessWidget {
|
||||
const YearSelector({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return BlocBuilder<ChartBloc, ChartState>(
|
||||
builder: (context, state) => Container(
|
||||
margin: const EdgeInsets.fromLTRB(20, 0, 0, 0),
|
||||
padding: const EdgeInsets.fromLTRB(5, 0, 5, 0),
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.blue,
|
||||
borderRadius: BorderRadius.circular(5)
|
||||
),
|
||||
child: Row(
|
||||
children: [
|
||||
IconButton(
|
||||
onPressed: () => context.read<ChartBloc>().add(ChartPreviousYear()),
|
||||
icon: const Icon(Icons.skip_previous)
|
||||
),
|
||||
Text(state.currentYear.toString()),
|
||||
IconButton(
|
||||
onPressed: () => context.read<ChartBloc>().add(ChartNextYear()),
|
||||
icon: const Icon(Icons.skip_next)
|
||||
),
|
||||
],
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
35
lib/pages/transactions/transactions_page.dart
Normal file
35
lib/pages/transactions/transactions_page.dart
Normal file
@@ -0,0 +1,35 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:tunas/domains/account/account_bloc.dart';
|
||||
import 'package:tunas/pages/transactions/widgets/transactions_actions.dart';
|
||||
import 'package:tunas/pages/transactions/widgets/transactions_header.dart';
|
||||
import 'package:tunas/pages/transactions/widgets/transactions_list.dart';
|
||||
|
||||
|
||||
class TransactionsPage extends StatelessWidget {
|
||||
const TransactionsPage({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return BlocListener<AccountBloc, AccountState>(
|
||||
listener: (context, state) {
|
||||
if (state.showAddDialog) {
|
||||
// TransactionAddDialog.show(context);
|
||||
}
|
||||
},
|
||||
child: const Flex(
|
||||
direction: Axis.horizontal,
|
||||
children: [
|
||||
Expanded(
|
||||
child: Column(
|
||||
children: [
|
||||
TransactionsActions(),
|
||||
TransactionsHeader(),
|
||||
TransactionsList(),
|
||||
],
|
||||
))
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
72
lib/pages/transactions/widgets/autocomplete_input.dart
Normal file
72
lib/pages/transactions/widgets/autocomplete_input.dart
Normal file
@@ -0,0 +1,72 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class AutocompleteInput extends StatelessWidget {
|
||||
final List<String> options;
|
||||
final String hintText;
|
||||
final String? errorText;
|
||||
final ValueChanged<String>? onChanged;
|
||||
|
||||
const AutocompleteInput({
|
||||
super.key,
|
||||
required this.options,
|
||||
required this.hintText,
|
||||
required this.errorText,
|
||||
required this.onChanged,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return RawAutocomplete<String>(
|
||||
optionsBuilder: (TextEditingValue textEditingValue) => options.where((String option) =>option.contains(textEditingValue.text.toLowerCase())),
|
||||
fieldViewBuilder: (
|
||||
BuildContext context,
|
||||
TextEditingController textEditingController,
|
||||
FocusNode focusNode,
|
||||
VoidCallback onFieldSubmitted,
|
||||
) {
|
||||
return TextFormField(
|
||||
controller: textEditingController,
|
||||
focusNode: focusNode,
|
||||
decoration: InputDecoration(
|
||||
hintText: hintText,
|
||||
errorText: errorText
|
||||
),
|
||||
onFieldSubmitted: (String value) {
|
||||
onFieldSubmitted();
|
||||
},
|
||||
onChanged: onChanged,
|
||||
);
|
||||
},
|
||||
optionsViewBuilder: (
|
||||
BuildContext context,
|
||||
AutocompleteOnSelected<String> onSelected,
|
||||
Iterable<String> options,
|
||||
) {
|
||||
return Align(
|
||||
alignment: Alignment.topLeft,
|
||||
child: Material(
|
||||
elevation: 4.0,
|
||||
child: SizedBox(
|
||||
height: 200.0,
|
||||
child: ListView.builder(
|
||||
padding: const EdgeInsets.all(8.0),
|
||||
itemCount: options.length,
|
||||
itemBuilder: (BuildContext context, int index) {
|
||||
final String option = options.elementAt(index);
|
||||
return GestureDetector(
|
||||
onTap: () {
|
||||
onSelected(option);
|
||||
},
|
||||
child: ListTile(
|
||||
title: Text(option),
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
41
lib/pages/transactions/widgets/transaction_add_dialog.dart
Normal file
41
lib/pages/transactions/widgets/transaction_add_dialog.dart
Normal file
@@ -0,0 +1,41 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:tunas/domains/account/account_bloc.dart';
|
||||
import 'package:tunas/pages/transactions/widgets/transaction_add_form.dart';
|
||||
|
||||
class TransactionAddDialog extends StatelessWidget {
|
||||
const TransactionAddDialog({super.key});
|
||||
|
||||
static void show(BuildContext context) => showDialog(
|
||||
context: context,
|
||||
barrierDismissible: false,
|
||||
useRootNavigator: false,
|
||||
builder: (_) => BlocProvider.value(
|
||||
value: BlocProvider.of<AccountBloc>(context),
|
||||
child: const TransactionAddDialog()
|
||||
)
|
||||
);
|
||||
|
||||
static void hide(BuildContext context) => Navigator.pop(context);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return AlertDialog(
|
||||
title: Text('Add transaction'),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => TransactionAddDialog.hide(context),
|
||||
child: Text('Close')
|
||||
),
|
||||
TextButton(
|
||||
onPressed: () => context.read<AccountBloc>().add(TransactionAdd()),
|
||||
child: Text('Add')
|
||||
),
|
||||
],
|
||||
content: SizedBox(
|
||||
height: 400,
|
||||
child: TransactionAddForm(),
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
127
lib/pages/transactions/widgets/transaction_add_form.dart
Normal file
127
lib/pages/transactions/widgets/transaction_add_form.dart
Normal file
@@ -0,0 +1,127 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:intl/intl.dart';
|
||||
import 'package:tunas/domains/account/account_bloc.dart';
|
||||
|
||||
import 'autocomplete_input.dart';
|
||||
|
||||
class TransactionAddForm extends StatelessWidget {
|
||||
const TransactionAddForm({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Column(
|
||||
children: [
|
||||
_TransactionDateInput(),
|
||||
_TransactionCategoryInput(),
|
||||
_TransactionDescriptionInput(),
|
||||
_TransactionAccountInput(),
|
||||
_TransactionValueInput()
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class _TransactionDateInput extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return BlocBuilder<AccountBloc, AccountState>(
|
||||
buildWhen: (previous, current) => previous.transactionDate != current.transactionDate,
|
||||
builder: (context, state) => SizedBox(
|
||||
width: 500,
|
||||
child: TextFormField(
|
||||
initialValue: DateFormat('dd-MM-yyyy', 'fr_FR').format(state.transactionDate.value ?? DateTime.now()),
|
||||
keyboardType: TextInputType.datetime,
|
||||
onTap: () {
|
||||
FocusScope.of(context).requestFocus(FocusNode());
|
||||
showDatePicker(
|
||||
context: context,
|
||||
firstDate: DateTime.fromMicrosecondsSinceEpoch(0),
|
||||
lastDate: DateTime.now()
|
||||
).then((value) => context.read<AccountBloc>().add(TransactionDateChange(value)));
|
||||
},
|
||||
decoration: InputDecoration(
|
||||
hintText: 'Date',
|
||||
errorText: state.transactionDate.isNotValid ? state.transactionDate.error?.message : null
|
||||
),
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _TransactionCategoryInput extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return BlocBuilder<AccountBloc, AccountState>(
|
||||
buildWhen: (previous, current) => previous.transactionCategory != current.transactionCategory,
|
||||
builder: (context, state) => SizedBox(
|
||||
width: 500,
|
||||
child: AutocompleteInput(
|
||||
options: state.categories,
|
||||
hintText: 'Category',
|
||||
errorText: state.transactionCategory.isNotValid ? state.transactionCategory.error?.message : null,
|
||||
onChanged: (value) => context.read<AccountBloc>().add(TransactionCategoryChange(value)),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _TransactionDescriptionInput extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return BlocBuilder<AccountBloc, AccountState>(
|
||||
buildWhen: (previous, current) => previous.transactionDescription != current.transactionDescription,
|
||||
builder: (context, state) => SizedBox(
|
||||
width: 500,
|
||||
child: TextField(
|
||||
decoration: InputDecoration(
|
||||
hintText: 'Description',
|
||||
errorText: state.transactionDescription.isNotValid ? state.transactionDescription.error?.message : null
|
||||
),
|
||||
onChanged: (value) => context.read<AccountBloc>().add(TransactionDescriptionChange(value))
|
||||
)
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _TransactionAccountInput extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return BlocBuilder<AccountBloc, AccountState>(
|
||||
buildWhen: (previous, current) => previous.transactionAccount != current.transactionAccount,
|
||||
builder: (context, state) => SizedBox(
|
||||
width: 500,
|
||||
child: AutocompleteInput(
|
||||
options: state.accountsTotals.keys.toList(),
|
||||
hintText: 'Account',
|
||||
errorText: state.transactionAccount.isNotValid ? state.transactionAccount.error?.message : null,
|
||||
onChanged: (value) => context.read<AccountBloc>().add(TransactionAccountChange(value)),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _TransactionValueInput extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return BlocBuilder<AccountBloc, AccountState>(
|
||||
buildWhen: (previous, current) => previous.transactionValue != current.transactionValue,
|
||||
builder: (context, state) => SizedBox(
|
||||
width: 500,
|
||||
child: TextField(
|
||||
keyboardType: TextInputType.number,
|
||||
decoration: InputDecoration(
|
||||
hintText: '\$\$\$',
|
||||
errorText: state.transactionValue.isNotValid ? state.transactionValue.error?.message : null
|
||||
),
|
||||
onChanged: (value) => context.read<AccountBloc>().add(TransactionValueChange(value))
|
||||
)
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
65
lib/pages/transactions/widgets/transaction_line.dart
Normal file
65
lib/pages/transactions/widgets/transaction_line.dart
Normal file
@@ -0,0 +1,65 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:intl/intl.dart';
|
||||
import 'package:tunas/repositories/account/models/transaction.dart';
|
||||
|
||||
class TransactionLine extends StatelessWidget {
|
||||
final Transaction transaction;
|
||||
final double subTotal;
|
||||
|
||||
const TransactionLine({super.key, required this.transaction, required this.subTotal});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MergeSemantics(
|
||||
child: Container(
|
||||
padding: const EdgeInsets.symmetric(vertical: 9, horizontal: 10),
|
||||
margin: const EdgeInsets.symmetric(vertical: 2, horizontal: 10),
|
||||
child: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
SizedBox(
|
||||
width: 100,
|
||||
child: Text(DateFormat('dd-MM-yyyy', 'fr_FR').format(transaction.date))
|
||||
),
|
||||
Expanded(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
transaction.category,
|
||||
style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 18)
|
||||
),
|
||||
Text(transaction.description),
|
||||
],
|
||||
),
|
||||
),
|
||||
SizedBox(
|
||||
width: 100,
|
||||
child: Text(transaction.account),
|
||||
),
|
||||
SizedBox(
|
||||
width: 120,
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.end,
|
||||
children: [
|
||||
Text(
|
||||
NumberFormat(transaction.value > 0 ? '+#######.00 €' : '#######.00 €', 'fr_FR').format(transaction.value),
|
||||
style: TextStyle(
|
||||
color: transaction.value > 0 ? Colors.green : Colors.red
|
||||
)
|
||||
),
|
||||
Text(
|
||||
NumberFormat('#######.00 €', 'fr_FR').format(subTotal),
|
||||
style: TextStyle(
|
||||
color: subTotal > 0 ? Colors.green : Colors.red
|
||||
)
|
||||
),
|
||||
],
|
||||
),
|
||||
)
|
||||
],
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
37
lib/pages/transactions/widgets/transactions_actions.dart
Normal file
37
lib/pages/transactions/widgets/transactions_actions.dart
Normal file
@@ -0,0 +1,37 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:tunas/domains/account/account_bloc.dart';
|
||||
import 'package:tunas/pages/transactions/widgets/transaction_add_dialog.dart';
|
||||
|
||||
class TransactionsActions extends StatelessWidget {
|
||||
const TransactionsActions({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return BlocBuilder<AccountBloc, AccountState>(
|
||||
builder: (context, state) => Container(
|
||||
padding: const EdgeInsets.symmetric(vertical: 9, horizontal: 10),
|
||||
margin: const EdgeInsets.symmetric(vertical: 2, horizontal: 10),
|
||||
child: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
const Text(
|
||||
'Transactions',
|
||||
style: TextStyle(
|
||||
fontWeight: FontWeight.w900,
|
||||
fontSize: 35,
|
||||
),
|
||||
),
|
||||
ElevatedButton.icon(
|
||||
onPressed: () => TransactionAddDialog.show(context),
|
||||
icon: const Icon(
|
||||
Icons.add
|
||||
),
|
||||
label: const Text('Add'),
|
||||
),
|
||||
],
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
42
lib/pages/transactions/widgets/transactions_header.dart
Normal file
42
lib/pages/transactions/widgets/transactions_header.dart
Normal file
@@ -0,0 +1,42 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class TransactionsHeader extends StatelessWidget {
|
||||
const TransactionsHeader({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Container(
|
||||
padding: const EdgeInsets.symmetric(vertical: 9, horizontal: 10),
|
||||
margin: const EdgeInsets.symmetric(vertical: 2, horizontal: 10),
|
||||
decoration: const BoxDecoration(
|
||||
border: Border(bottom: BorderSide(color: Colors.black))
|
||||
),
|
||||
child: const Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
SizedBox(
|
||||
width: 100,
|
||||
child: Text('Date')
|
||||
),
|
||||
Expanded(
|
||||
child: Text('Description')
|
||||
),
|
||||
SizedBox(
|
||||
width: 100,
|
||||
child: Text('Account'),
|
||||
),
|
||||
SizedBox(
|
||||
width: 120,
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.end,
|
||||
children: [
|
||||
Text('Amount'),
|
||||
Text('SubTotal'),
|
||||
],
|
||||
),
|
||||
)
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
24
lib/pages/transactions/widgets/transactions_list.dart
Normal file
24
lib/pages/transactions/widgets/transactions_list.dart
Normal file
@@ -0,0 +1,24 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:tunas/domains/account/account_bloc.dart';
|
||||
import 'package:tunas/pages/transactions/widgets/transaction_line.dart';
|
||||
|
||||
class TransactionsList extends StatelessWidget {
|
||||
const TransactionsList({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return BlocBuilder<AccountBloc, AccountState>(
|
||||
buildWhen: (previous, current) => previous.transactionsLines != current.transactionsLines,
|
||||
builder: (context, state) => Expanded(
|
||||
child: ListView.builder(
|
||||
itemCount: state.transactionsLines.length,
|
||||
itemBuilder: (context, index) => TransactionLine(
|
||||
transaction: state.transactionsLines[index].transaction,
|
||||
subTotal: state.transactionsLines[index].subTotal
|
||||
)
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
45
lib/repositories/account/account_repository.dart
Normal file
45
lib/repositories/account/account_repository.dart
Normal file
@@ -0,0 +1,45 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:rxdart/subjects.dart';
|
||||
import 'package:tunas/clients/storage/storage_client.dart';
|
||||
import 'package:tunas/repositories/account/models/account.dart';
|
||||
import 'package:tunas/repositories/account/models/transaction.dart';
|
||||
|
||||
class AccountRepository {
|
||||
String accountFile = 'main_account.json';
|
||||
|
||||
final StorageClient _storageClient;
|
||||
|
||||
final _transactionsController = BehaviorSubject<List<Transaction>>.seeded(const []);
|
||||
|
||||
AccountRepository({
|
||||
required storageClient,
|
||||
}) : _storageClient = storageClient {
|
||||
init();
|
||||
}
|
||||
|
||||
init() async {
|
||||
final account = await getAccount();
|
||||
_transactionsController.add(account.transactions);
|
||||
}
|
||||
|
||||
Stream<List<Transaction>> getTransactionsStream() {
|
||||
return _transactionsController.asBroadcastStream();
|
||||
}
|
||||
|
||||
Future<Account> getAccount() async {
|
||||
String json = await _storageClient.load(accountFile);
|
||||
Map<String, dynamic> accountJson = jsonDecode(json);
|
||||
return Account.fromJson(accountJson);
|
||||
}
|
||||
|
||||
saveAccount(Account account) async {
|
||||
await _storageClient.save(accountFile, jsonEncode(account.toJson()));
|
||||
}
|
||||
|
||||
saveTransactions(List<Transaction> transactions) async {
|
||||
final account = Account(transactions: transactions);
|
||||
await saveAccount(account);
|
||||
_transactionsController.add(account.transactions);
|
||||
}
|
||||
}
|
||||
21
lib/repositories/account/models/account.dart
Normal file
21
lib/repositories/account/models/account.dart
Normal file
@@ -0,0 +1,21 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'transaction.dart';
|
||||
|
||||
class Account {
|
||||
List<Transaction> transactions;
|
||||
|
||||
Account({
|
||||
this.transactions = const [],
|
||||
});
|
||||
|
||||
factory Account.fromJson(Map<String, dynamic> json) {
|
||||
return Account(
|
||||
transactions: (jsonDecode(json['transactions']) as List<dynamic>).map((transaction) => Transaction.fromJson(transaction)).toList(),
|
||||
);
|
||||
}
|
||||
|
||||
Map<String, String> toJson() => {
|
||||
'transactions': jsonEncode(transactions.map((transaction) => transaction.toJson()).toList()),
|
||||
};
|
||||
}
|
||||
33
lib/repositories/account/models/transaction.dart
Normal file
33
lib/repositories/account/models/transaction.dart
Normal file
@@ -0,0 +1,33 @@
|
||||
class Transaction {
|
||||
DateTime date;
|
||||
String category;
|
||||
String description;
|
||||
String account;
|
||||
double value;
|
||||
|
||||
Transaction({
|
||||
required this.date,
|
||||
required this.category,
|
||||
required this.description,
|
||||
required this.account,
|
||||
required this.value
|
||||
});
|
||||
|
||||
factory Transaction.fromJson(Map<String, dynamic> json) {
|
||||
return Transaction(
|
||||
date: DateTime.parse(json['date']),
|
||||
category: json['category'],
|
||||
description: json['description'],
|
||||
account: json['account'],
|
||||
value: double.parse(json['value']),
|
||||
);
|
||||
}
|
||||
|
||||
Map<String, String> toJson() => {
|
||||
'date': date.toIso8601String(),
|
||||
'category': category,
|
||||
'description': description,
|
||||
'account': account,
|
||||
'value': value.toString(),
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user