Basic budget sliders
This commit is contained in:
@@ -1,3 +1,5 @@
|
|||||||
|
import 'dart:async';
|
||||||
|
|
||||||
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/metadata/models/budget.dart';
|
import 'package:tunas/repositories/metadata/models/budget.dart';
|
||||||
@@ -11,6 +13,11 @@ class BudgetBloc extends Bloc<BudgetEvent, BudgetState> {
|
|||||||
|
|
||||||
BudgetBloc({required MetadataRepository metadataRepository}) : _metadataRepository = metadataRepository, super(const BudgetState()) {
|
BudgetBloc({required MetadataRepository metadataRepository}) : _metadataRepository = metadataRepository, super(const BudgetState()) {
|
||||||
on<BudgetsLoad>(_onBudgetsLoad);
|
on<BudgetsLoad>(_onBudgetsLoad);
|
||||||
|
on<BudgetAdd>(_onBudgetAdd);
|
||||||
|
on<BudgetRemove>(_onBudgetRemove);
|
||||||
|
on<BudgetSetValue>(_onBudgetSetValue);
|
||||||
|
on<BudgetCompareNext>(_onBudgetCompareNext);
|
||||||
|
on<BudgetComparePrevious>(_onBudgetComparePrevious);
|
||||||
|
|
||||||
_metadataRepository
|
_metadataRepository
|
||||||
.getBudgetsStream()
|
.getBudgetsStream()
|
||||||
@@ -24,4 +31,68 @@ class BudgetBloc extends Bloc<BudgetEvent, BudgetState> {
|
|||||||
budgets: event.budgets,
|
budgets: event.budgets,
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
FutureOr<void> _onBudgetAdd(BudgetAdd event, Emitter<BudgetState> emit) async {
|
||||||
|
Budget budget = Budget(
|
||||||
|
label: event.label,
|
||||||
|
);
|
||||||
|
|
||||||
|
emit(_computeState(await _saveBudget(budget)));
|
||||||
|
}
|
||||||
|
|
||||||
|
FutureOr<void> _onBudgetRemove(BudgetRemove event, Emitter<BudgetState> emit) async {
|
||||||
|
List<Budget> budgets = _metadataRepository.getBudgets();
|
||||||
|
Budget budgetToRemove = event.budget;
|
||||||
|
|
||||||
|
budgets.removeWhere((budget) => budget.label == budgetToRemove.label);
|
||||||
|
emit(_computeState(await _metadataRepository.saveBudgets(budgets)));
|
||||||
|
}
|
||||||
|
|
||||||
|
FutureOr<void> _onBudgetSetValue(BudgetSetValue event, Emitter<BudgetState> emit) async {
|
||||||
|
Budget budgetToUpdate = event.budget;
|
||||||
|
|
||||||
|
if (state.remainingBudget - (event.value - budgetToUpdate.value) < 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// if (state.remainingBudget - event.value < 0 && state.remainingBudget < 10) {
|
||||||
|
// budgetToUpdate.value =
|
||||||
|
// } else {
|
||||||
|
// budgetToUpdate.value = event.value;
|
||||||
|
// }
|
||||||
|
budgetToUpdate.value = event.value;
|
||||||
|
|
||||||
|
emit(_computeState(await _saveBudget(budgetToUpdate)));
|
||||||
|
}
|
||||||
|
|
||||||
|
FutureOr<void> _onBudgetCompareNext(BudgetCompareNext event, Emitter<BudgetState> emit) {
|
||||||
|
}
|
||||||
|
|
||||||
|
FutureOr<void> _onBudgetComparePrevious(BudgetComparePrevious event, Emitter<BudgetState> emit) {
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<List<Budget>> _saveBudget(Budget budgetToSave) async {
|
||||||
|
List<Budget> budgets = _metadataRepository.getBudgets();
|
||||||
|
|
||||||
|
try {
|
||||||
|
Budget budgetFound = budgets.firstWhere((category) => category.label == budgetToSave.label);
|
||||||
|
budgetFound.value = budgetToSave.value;
|
||||||
|
} catch (e) {
|
||||||
|
if (budgets.isEmpty) {
|
||||||
|
budgets = [budgetToSave];
|
||||||
|
} else {
|
||||||
|
budgets.add(budgetToSave);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// _metadataRepository.saveBudgets(budgets);
|
||||||
|
return budgets;
|
||||||
|
}
|
||||||
|
|
||||||
|
BudgetState _computeState(List<Budget> budgets) {
|
||||||
|
return state.copyWith(
|
||||||
|
budgets: budgets,
|
||||||
|
remainingBudget: state.initialBudget - budgets.map((budget) => budget.value).reduce((value, element) => value + element),
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -14,3 +14,34 @@ final class BudgetsLoad extends BudgetEvent {
|
|||||||
@override
|
@override
|
||||||
List<Object> get props => [budgets];
|
List<Object> get props => [budgets];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
final class BudgetAdd extends BudgetEvent {
|
||||||
|
final String label;
|
||||||
|
|
||||||
|
const BudgetAdd(this.label);
|
||||||
|
|
||||||
|
@override
|
||||||
|
List<Object> get props => [label];
|
||||||
|
}
|
||||||
|
|
||||||
|
final class BudgetRemove extends BudgetEvent {
|
||||||
|
final Budget budget;
|
||||||
|
|
||||||
|
const BudgetRemove(this.budget);
|
||||||
|
|
||||||
|
@override
|
||||||
|
List<Object> get props => [budget];
|
||||||
|
}
|
||||||
|
|
||||||
|
final class BudgetSetValue extends BudgetEvent {
|
||||||
|
final Budget budget;
|
||||||
|
final double value;
|
||||||
|
|
||||||
|
const BudgetSetValue(this.budget, this.value);
|
||||||
|
|
||||||
|
@override
|
||||||
|
List<Object> get props => [budget, value];
|
||||||
|
}
|
||||||
|
|
||||||
|
final class BudgetCompareNext extends BudgetEvent {}
|
||||||
|
final class BudgetComparePrevious extends BudgetEvent {}
|
||||||
@@ -1,20 +1,25 @@
|
|||||||
part of 'budget_bloc.dart';
|
part of 'budget_bloc.dart';
|
||||||
|
|
||||||
final class BudgetState extends Equatable {
|
final class BudgetState {
|
||||||
final List<Budget> budgets;
|
final List<Budget> budgets;
|
||||||
|
final double initialBudget;
|
||||||
|
final double remainingBudget;
|
||||||
|
|
||||||
const BudgetState({
|
const BudgetState({
|
||||||
this.budgets = const [],
|
this.budgets = const [],
|
||||||
|
this.initialBudget = 2300.0,
|
||||||
|
this.remainingBudget = 2300.0,
|
||||||
});
|
});
|
||||||
|
|
||||||
BudgetState copyWith({
|
BudgetState copyWith({
|
||||||
List<Budget>? budgets,
|
List<Budget>? budgets,
|
||||||
|
double? initialBudget,
|
||||||
|
double? remainingBudget,
|
||||||
}) {
|
}) {
|
||||||
return BudgetState(
|
return BudgetState(
|
||||||
budgets: budgets ?? this.budgets,
|
budgets: budgets ?? this.budgets,
|
||||||
|
initialBudget: initialBudget ?? this.initialBudget,
|
||||||
|
remainingBudget: remainingBudget ?? this.remainingBudget,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
|
||||||
List<Object?> get props => [budgets];
|
|
||||||
}
|
}
|
||||||
@@ -113,7 +113,7 @@ class ChartBloc extends Bloc<ChartEvent, ChartState> {
|
|||||||
double scoppedTotal = 0;
|
double scoppedTotal = 0;
|
||||||
List<ChartItem> scopedCategoriesPositiveTotals = [];
|
List<ChartItem> scopedCategoriesPositiveTotals = [];
|
||||||
List<ChartItem> scopedCategoriesNegativeTotals = [];
|
List<ChartItem> scopedCategoriesNegativeTotals = [];
|
||||||
Map<int, FlSpot> scopedMonthlyTotals = {};
|
Map<int, FlSpot> scopedMonthlyTotalsMap = {};
|
||||||
Map<int, MonthTotals> scopedCategoriesMonthlyTotals = {};
|
Map<int, MonthTotals> scopedCategoriesMonthlyTotals = {};
|
||||||
|
|
||||||
Map<int, double> scopedMonthlyPostitiveTotals = {};
|
Map<int, double> scopedMonthlyPostitiveTotals = {};
|
||||||
@@ -134,7 +134,7 @@ class ChartBloc extends Bloc<ChartEvent, ChartState> {
|
|||||||
DateTime transactionDate = transactionLine.transaction.date;
|
DateTime transactionDate = transactionLine.transaction.date;
|
||||||
int transactionDateDay = transactionDate.difference(DateTime(transactionDate.year,1,1)).inDays + 1;
|
int transactionDateDay = transactionDate.difference(DateTime(transactionDate.year,1,1)).inDays + 1;
|
||||||
|
|
||||||
scopedMonthlyTotals[transactionDateDay] = FlSpot(transactionDateDay.toDouble(), transactionLine.subTotal);
|
scopedMonthlyTotalsMap[transactionDateDay] = FlSpot(transactionDateDay.toDouble(), transactionLine.subTotal);
|
||||||
|
|
||||||
final category = state.categories[transaction.category];
|
final category = state.categories[transaction.category];
|
||||||
if (category == null || category.transfert) {
|
if (category == null || category.transfert) {
|
||||||
@@ -229,6 +229,9 @@ class ChartBloc extends Bloc<ChartEvent, ChartState> {
|
|||||||
_sortMapByValues(monthTotals.negatives);
|
_sortMapByValues(monthTotals.negatives);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
List<FlSpot> scopedMonthlyTotals = scopedMonthlyTotalsMap.values.toList();
|
||||||
|
scopedMonthlyTotals.sort((a, b) => a.x.compareTo(b.x));
|
||||||
|
|
||||||
return state.copyWith(
|
return state.copyWith(
|
||||||
scoppedProfit: scoppedTotal,
|
scoppedProfit: scoppedTotal,
|
||||||
scopedCategoriesPositiveTotals: scopedCategoriesPositiveTotals,
|
scopedCategoriesPositiveTotals: scopedCategoriesPositiveTotals,
|
||||||
@@ -239,7 +242,7 @@ class ChartBloc extends Bloc<ChartEvent, ChartState> {
|
|||||||
scopedSimplifiedCategoriesPositiveTotalsPercents: scopedSimplifiedCategoriesPositiveTotalsPercents,
|
scopedSimplifiedCategoriesPositiveTotalsPercents: scopedSimplifiedCategoriesPositiveTotalsPercents,
|
||||||
scopedSimplifiedCategoriesNegativeTotals: scopedSimplifiedCategoriesNegativeTotals,
|
scopedSimplifiedCategoriesNegativeTotals: scopedSimplifiedCategoriesNegativeTotals,
|
||||||
scopedSimplifiedCategoriesNegativeTotalsPercents: scopedSimplifiedCategoriesNegativeTotalsPercents,
|
scopedSimplifiedCategoriesNegativeTotalsPercents: scopedSimplifiedCategoriesNegativeTotalsPercents,
|
||||||
scopedMonthlyTotals: scopedMonthlyTotals.values.toList(),
|
scopedMonthlyTotals: scopedMonthlyTotals,
|
||||||
scopedCategoriesMonthlyTotals: scopedCategoriesMonthlyTotals,
|
scopedCategoriesMonthlyTotals: scopedCategoriesMonthlyTotals,
|
||||||
scopedMonthlyPostitiveTotals: scopedMonthlyPostitiveTotals,
|
scopedMonthlyPostitiveTotals: scopedMonthlyPostitiveTotals,
|
||||||
scopedMonthlyNegativeTotals: scopedMonthlyNegativeTotals,
|
scopedMonthlyNegativeTotals: scopedMonthlyNegativeTotals,
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ class BudgetsPage extends StatelessWidget {
|
|||||||
return Center(
|
return Center(
|
||||||
child: Container(
|
child: Container(
|
||||||
constraints: const BoxConstraints(
|
constraints: const BoxConstraints(
|
||||||
maxWidth: 1000
|
maxWidth: 1000,
|
||||||
),
|
),
|
||||||
child: ListView(
|
child: ListView(
|
||||||
padding: mediaQuery.padding.copyWith(left: 10.0, right: 10.0, bottom: 100.0),
|
padding: mediaQuery.padding.copyWith(left: 10.0, right: 10.0, bottom: 100.0),
|
||||||
|
|||||||
@@ -1,73 +1,104 @@
|
|||||||
import 'package:fl_chart/fl_chart.dart';
|
import 'package:fl_chart/fl_chart.dart';
|
||||||
import 'package:flutter/foundation.dart';
|
import 'package:flutter/foundation.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:tunas/domains/budget/budget_bloc.dart';
|
||||||
import 'package:tunas/pages/common/titled_container.dart';
|
import 'package:tunas/pages/common/titled_container.dart';
|
||||||
|
import 'package:tunas/repositories/metadata/models/budget.dart';
|
||||||
|
import 'package:uuid/uuid.dart';
|
||||||
|
|
||||||
class MonthDistribution extends StatelessWidget {
|
class MonthDistribution extends StatelessWidget {
|
||||||
const MonthDistribution({super.key});
|
const MonthDistribution({super.key});
|
||||||
|
|
||||||
|
List<Widget> _computeBudgetLines(BuildContext context, List<Budget> budgets, double initialBudget, double remainingBudget) {
|
||||||
|
List<Widget> list = [
|
||||||
|
Text('Money to spare: ${NumberFormat('#####00.00 €', 'fr_FR').format(remainingBudget)} € / $initialBudget €'),
|
||||||
|
];
|
||||||
|
|
||||||
|
list.addAll(budgets.map((budget) => Row(
|
||||||
|
children: [
|
||||||
|
Expanded(
|
||||||
|
child: Row(
|
||||||
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||||
|
children: [
|
||||||
|
Text(budget.label),
|
||||||
|
Text(NumberFormat('#####00.00 €', 'fr_FR').format(budget.value)),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
),
|
||||||
|
Expanded(
|
||||||
|
child: SliderTheme(
|
||||||
|
data: SliderThemeData(
|
||||||
|
|
||||||
|
),
|
||||||
|
child: Slider(
|
||||||
|
min: 0,
|
||||||
|
max: initialBudget,
|
||||||
|
label: budget.value.round().toString(),
|
||||||
|
value: budget.value,
|
||||||
|
secondaryTrackValue: remainingBudget + budget.value,
|
||||||
|
onChanged: (value) => context.read<BudgetBloc>().add(BudgetSetValue(budget, value)),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
],
|
||||||
|
)).toList());
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return Column(
|
return BlocBuilder<BudgetBloc, BudgetState>(
|
||||||
children: [
|
builder: (context, state) => Column(
|
||||||
TitledContainer(
|
children: [
|
||||||
title: 'Prepare',
|
TitledContainer(
|
||||||
child: Column(
|
title: 'Prepare',
|
||||||
children: [
|
action: IconButton(
|
||||||
Text('Money to spare: 2300 €'),
|
onPressed: () => context.read<BudgetBloc>().add(BudgetAdd(const Uuid().v8())),
|
||||||
Text('Loyer'),
|
icon: const Icon(Icons.add),
|
||||||
Slider(
|
),
|
||||||
min: 0,
|
child: Column(
|
||||||
max: 2300,
|
children: _computeBudgetLines(context, state.budgets, state.initialBudget, state.remainingBudget),
|
||||||
value: 200,
|
),
|
||||||
onChanged: (value) => {},
|
|
||||||
),
|
|
||||||
Text('Loyer'),
|
|
||||||
Slider(
|
|
||||||
min: 0,
|
|
||||||
max: 2300,
|
|
||||||
value: 200,
|
|
||||||
onChanged: (value) => {},
|
|
||||||
),
|
|
||||||
],
|
|
||||||
),
|
),
|
||||||
),
|
TitledContainer(
|
||||||
TitledContainer(
|
title: 'Compare',
|
||||||
title: 'Compare',
|
height: 500,
|
||||||
height: 500,
|
child: Row(
|
||||||
child: Row(
|
children: [
|
||||||
children: [
|
Expanded(
|
||||||
Expanded(
|
child: Column(
|
||||||
child: Column(
|
crossAxisAlignment: CrossAxisAlignment.start,
|
||||||
crossAxisAlignment: CrossAxisAlignment.start,
|
children: [
|
||||||
children: [
|
Text('Budget'),
|
||||||
Text('Budget'),
|
],
|
||||||
],
|
),
|
||||||
),
|
),
|
||||||
),
|
Expanded(
|
||||||
Expanded(
|
child: Column(
|
||||||
child: Column(
|
children: [
|
||||||
children: [
|
Row(
|
||||||
Row(
|
children: [
|
||||||
children: [
|
IconButton(
|
||||||
IconButton(
|
onPressed: () => {},
|
||||||
onPressed: () => {},
|
icon: const Icon(Icons.skip_previous)
|
||||||
icon: const Icon(Icons.skip_previous)
|
),
|
||||||
),
|
Text('Fev 2024'),
|
||||||
Text('Fev 2024'),
|
IconButton(
|
||||||
IconButton(
|
onPressed: () => {},
|
||||||
onPressed: () => {},
|
icon: const Icon(Icons.skip_next)
|
||||||
icon: const Icon(Icons.skip_next)
|
),
|
||||||
),
|
],
|
||||||
],
|
),
|
||||||
),
|
],
|
||||||
],
|
)
|
||||||
)
|
)
|
||||||
)
|
],
|
||||||
],
|
),
|
||||||
),
|
),
|
||||||
),
|
],
|
||||||
],
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -11,6 +11,7 @@ class DataPage extends StatelessWidget {
|
|||||||
MediaQueryData mediaQuery = MediaQuery.of(context);
|
MediaQueryData mediaQuery = MediaQuery.of(context);
|
||||||
return Center(
|
return Center(
|
||||||
child: Container(
|
child: Container(
|
||||||
|
margin: const EdgeInsets.symmetric(vertical: 11, horizontal: 0),
|
||||||
constraints: const BoxConstraints(
|
constraints: const BoxConstraints(
|
||||||
maxWidth: 1000
|
maxWidth: 1000
|
||||||
),
|
),
|
||||||
|
|||||||
@@ -8,8 +8,6 @@ import 'package:tunas/pages/stats/widgets/monthly_categories_total_chart.dart';
|
|||||||
import 'package:tunas/pages/stats/widgets/global_total_chart.dart';
|
import 'package:tunas/pages/stats/widgets/global_total_chart.dart';
|
||||||
import 'package:tunas/pages/stats/widgets/profit_indicator.dart';
|
import 'package:tunas/pages/stats/widgets/profit_indicator.dart';
|
||||||
import 'package:tunas/pages/stats/widgets/year_selector.dart';
|
import 'package:tunas/pages/stats/widgets/year_selector.dart';
|
||||||
import 'package:tunas/repositories/metadata/metadata_repository.dart';
|
|
||||||
import 'package:tunas/repositories/transactions/transactions_repository.dart';
|
|
||||||
|
|
||||||
class StatsPage extends StatelessWidget {
|
class StatsPage extends StatelessWidget {
|
||||||
const StatsPage({super.key});
|
const StatsPage({super.key});
|
||||||
|
|||||||
@@ -57,8 +57,12 @@ class TransactionsHeader extends StatelessWidget {
|
|||||||
return Container(
|
return Container(
|
||||||
padding: const EdgeInsets.symmetric(vertical: 9, horizontal: 10),
|
padding: const EdgeInsets.symmetric(vertical: 9, horizontal: 10),
|
||||||
margin: const EdgeInsets.symmetric(vertical: 2, horizontal: 10),
|
margin: const EdgeInsets.symmetric(vertical: 2, horizontal: 10),
|
||||||
decoration: const BoxDecoration(
|
decoration: BoxDecoration(
|
||||||
border: Border(bottom: BorderSide(color: Colors.black))
|
border: Border(
|
||||||
|
bottom: BorderSide(
|
||||||
|
color: Theme.of(context).colorScheme.onPrimaryContainer
|
||||||
|
)
|
||||||
|
)
|
||||||
),
|
),
|
||||||
child: Row(
|
child: Row(
|
||||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||||
|
|||||||
@@ -1,20 +1,24 @@
|
|||||||
class Budget {
|
class Budget {
|
||||||
|
String label;
|
||||||
bool monthly;
|
bool monthly;
|
||||||
double value;
|
double value;
|
||||||
|
|
||||||
Budget({
|
Budget({
|
||||||
|
this.label = '',
|
||||||
this.monthly = false,
|
this.monthly = false,
|
||||||
this.value = 0.0,
|
this.value = 0.0,
|
||||||
});
|
});
|
||||||
|
|
||||||
factory Budget.fromJson(Map<String, dynamic> json) {
|
factory Budget.fromJson(Map<String, dynamic> json) {
|
||||||
return Budget(
|
return Budget(
|
||||||
monthly: json['monthly'],
|
label: json['label'],
|
||||||
|
monthly: bool.parse(json['monthly']),
|
||||||
value: double.parse(json['value']),
|
value: double.parse(json['value']),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
Map<String, String> toJson() => {
|
Map<String, String> toJson() => {
|
||||||
|
'label': label,
|
||||||
'monthly': monthly.toString(),
|
'monthly': monthly.toString(),
|
||||||
'value': value.toString(),
|
'value': value.toString(),
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user