Skip to content

Commit

Permalink
budget period start day and filter exp/inc for current period
Browse files Browse the repository at this point in the history
  • Loading branch information
markCwatson committed Oct 4, 2023
1 parent 7036614 commit 3d19e78
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 26 deletions.
65 changes: 45 additions & 20 deletions lib/screens/budget/bloc/budgets_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,14 @@ class BudgetsBloc extends Bloc<BudgetsEvent, BudgetsState> {
try {
// \todo: consider using token on event instead of state??
final expenses = await _expensesRepository.get(token: event.token);
emit(BudgetsState.expensesLoaded(expenses));

// filter by expenses in this period
final expensesInThisPeriod = _getItemsInThisPeriod(
expenses as List<FinanceEntry>,
state.configuration,
) as List<Expense>;

emit(BudgetsState.expensesLoaded(expensesInThisPeriod));
} catch (e) {
print(e);
}
Expand All @@ -111,7 +118,14 @@ class BudgetsBloc extends Bloc<BudgetsEvent, BudgetsState> {
) async {
try {
final incomes = await _incomesRepository.get(token: event.token);
emit(BudgetsState.incomesLoaded(incomes));

// filter by incomes in this period
final incomesInThisPeriod = _getItemsInThisPeriod(
incomes as List<FinanceEntry>,
state.configuration,
) as List<Income>;

emit(BudgetsState.incomesLoaded(incomesInThisPeriod));
} catch (e) {
print(e);
}
Expand Down Expand Up @@ -160,24 +174,15 @@ class BudgetsBloc extends Bloc<BudgetsEvent, BudgetsState> {
Budget budget,
Emitter<BudgetsState> emit,
) {
// Compute the start and end of the current period
final createdAt = DateTime.parse(budget.configuration.startDate);
final today = DateTime.now().toUtc();
final currentPeriod = const PeriodCalculator().calculateCurrentPeriod(
createdAt,
budget.configuration.period,
today,
);

// Compute the total planned expenses and incomes for the current period
final totalPlannedExpenses = _computeTotalItems(
items: budget.plannedExpenses,
currentPeriod: currentPeriod,
configuration: budget.configuration,
);

final totalPlannedIncomes = _computeTotalItems(
items: budget.plannedIncomes,
currentPeriod: currentPeriod,
configuration: budget.configuration,
);

final endAccountBalance = budget.configuration.startAccountBalance +
Expand All @@ -187,12 +192,12 @@ class BudgetsBloc extends Bloc<BudgetsEvent, BudgetsState> {
// compute the actual expenses and incomes for the current period
final totalUnplannedExpenses = _computeTotalItems(
items: budget.unplannedExpenses,
currentPeriod: currentPeriod,
configuration: budget.configuration,
);

final totalUnplannedIncomes = _computeTotalItems(
items: budget.unplannedIncomes,
currentPeriod: currentPeriod,
configuration: budget.configuration,
);

final adjustedEndAccountBalance = endAccountBalance +
Expand All @@ -204,12 +209,12 @@ class BudgetsBloc extends Bloc<BudgetsEvent, BudgetsState> {
// limit income/expense to current period
final plannedIncomes = _getItemsInThisPeriod(
budget.plannedIncomes,
currentPeriod,
budget.configuration,
) as List<Income>;

final plannedExpenses = _getItemsInThisPeriod(
budget.plannedExpenses,
currentPeriod,
budget.configuration,
) as List<Expense>;

emit(
Expand All @@ -218,7 +223,7 @@ class BudgetsBloc extends Bloc<BudgetsEvent, BudgetsState> {
budget.configuration,
plannedExpenses,
plannedIncomes,
currentPeriod,
_getCurrentPeriod(budget.configuration),
totalPlannedExpenses,
totalPlannedIncomes,
totalUnplannedExpenses,
Expand All @@ -230,8 +235,9 @@ class BudgetsBloc extends Bloc<BudgetsEvent, BudgetsState> {

List<FinanceEntry> _getItemsInThisPeriod(
List<FinanceEntry> items,
BudgetPeriod currentPeriod,
Configuration configuration,
) {
final currentPeriod = _getCurrentPeriod(configuration);
final later = currentPeriod.end!.add(const Duration(days: 1));
final itemsInThisPeriod = items.where((element) {
return DateTime.parse(element.date).isBefore(later) &&
Expand All @@ -240,10 +246,29 @@ class BudgetsBloc extends Bloc<BudgetsEvent, BudgetsState> {
return itemsInThisPeriod;
}

BudgetPeriod _getCurrentPeriod(Configuration configuration) {
final createdAt = DateTime.parse(configuration.startDate);
final today = DateTime.now().toUtc();
final currentPeriod = const PeriodCalculator().calculateCurrentPeriod(
createdAt,
configuration.period,
today,
);
return currentPeriod;
}

double _computeTotalItems({
required List<FinanceEntry> items,
required BudgetPeriod currentPeriod,
required Configuration configuration,
}) {
final createdAt = DateTime.parse(configuration.startDate);
final today = DateTime.now().toUtc();
final currentPeriod = const PeriodCalculator().calculateCurrentPeriod(
createdAt,
configuration.period,
today,
);

final later = currentPeriod.end!.add(const Duration(days: 1));
return items.fold<double>(
0,
Expand Down
4 changes: 2 additions & 2 deletions lib/screens/budget/bloc/utilities/period_calculator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,15 @@ class PeriodCalculator {
if (periodType == 'Weekly') {
final weeksSinceCreation = (timeSinceCreation.inDays / 7).floor();
startOfCurrentPeriod = createdAt.add(
Duration(days: weeksSinceCreation * 7),
Duration(days: weeksSinceCreation * 7 - 1),
);
endOfCurrentPeriod = startOfCurrentPeriod.add(
const Duration(days: 6),
);
} else if (periodType == 'Bi-weekly') {
final biWeeksSinceCreation = (timeSinceCreation.inDays / 14).floor();
startOfCurrentPeriod =
createdAt.add(Duration(days: biWeeksSinceCreation * 14));
createdAt.add(Duration(days: biWeeksSinceCreation * 14 - 1));
endOfCurrentPeriod = startOfCurrentPeriod.add(
const Duration(days: 13),
);
Expand Down
2 changes: 0 additions & 2 deletions lib/screens/budget/view/tabs/budget_tab.dart
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,6 @@ class _BudgetedListState<T extends FinanceEntry>
const CacheResetEvent(),
);
// Refresh the data on return
// \todo: use caching or something to avoid api call
context.read<BudgetsBloc>().add(
GetBudgetEvent(
token: context.read<AuthBloc>().state.token,
Expand All @@ -414,7 +413,6 @@ class _BudgetedListState<T extends FinanceEntry>
const CacheResetEvent(),
);
// Refresh the data on return
// \todo: use caching or something to avoid api call
context.read<BudgetsBloc>().add(
GetBudgetEvent(
token: context.read<AuthBloc>().state.token,
Expand Down
1 change: 0 additions & 1 deletion lib/screens/budget/view/tabs/expense_tab.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ class ExpenseTab extends StatelessWidget {
).then(
(value) {
// Refresh the data when return to the Expenses page
// \todo: use caching or something to avoid api call
context.read<BudgetsBloc>().add(
GetExpensesEvent(
token: context.read<AuthBloc>().state.token,
Expand Down
1 change: 0 additions & 1 deletion lib/screens/budget/view/tabs/income_tab.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ class IncomeTab extends StatelessWidget {
).then(
(value) {
// Refresh the data when return to the Expenses page
// \todo: use caching or something to avoid api call
context.read<BudgetsBloc>().add(
GetIncomesEvent(
token: context.read<AuthBloc>().state.token,
Expand Down

0 comments on commit 3d19e78

Please sign in to comment.