diff --git a/lib/screens/budget/bloc/budgets_bloc.dart b/lib/screens/budget/bloc/budgets_bloc.dart index 7784350..33ebff1 100644 --- a/lib/screens/budget/bloc/budgets_bloc.dart +++ b/lib/screens/budget/bloc/budgets_bloc.dart @@ -99,7 +99,14 @@ class BudgetsBloc extends Bloc { 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, + state.configuration, + ) as List; + + emit(BudgetsState.expensesLoaded(expensesInThisPeriod)); } catch (e) { print(e); } @@ -111,7 +118,14 @@ class BudgetsBloc extends Bloc { ) 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, + state.configuration, + ) as List; + + emit(BudgetsState.incomesLoaded(incomesInThisPeriod)); } catch (e) { print(e); } @@ -160,24 +174,15 @@ class BudgetsBloc extends Bloc { Budget budget, Emitter 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 + @@ -187,12 +192,12 @@ class BudgetsBloc extends Bloc { // 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 + @@ -204,12 +209,12 @@ class BudgetsBloc extends Bloc { // limit income/expense to current period final plannedIncomes = _getItemsInThisPeriod( budget.plannedIncomes, - currentPeriod, + budget.configuration, ) as List; final plannedExpenses = _getItemsInThisPeriod( budget.plannedExpenses, - currentPeriod, + budget.configuration, ) as List; emit( @@ -218,7 +223,7 @@ class BudgetsBloc extends Bloc { budget.configuration, plannedExpenses, plannedIncomes, - currentPeriod, + _getCurrentPeriod(budget.configuration), totalPlannedExpenses, totalPlannedIncomes, totalUnplannedExpenses, @@ -230,8 +235,9 @@ class BudgetsBloc extends Bloc { List _getItemsInThisPeriod( List 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) && @@ -240,10 +246,29 @@ class BudgetsBloc extends Bloc { 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 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( 0, diff --git a/lib/screens/budget/bloc/utilities/period_calculator.dart b/lib/screens/budget/bloc/utilities/period_calculator.dart index 407bb9a..11643f2 100644 --- a/lib/screens/budget/bloc/utilities/period_calculator.dart +++ b/lib/screens/budget/bloc/utilities/period_calculator.dart @@ -54,7 +54,7 @@ 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), @@ -62,7 +62,7 @@ class PeriodCalculator { } 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), ); diff --git a/lib/screens/budget/view/tabs/budget_tab.dart b/lib/screens/budget/view/tabs/budget_tab.dart index 32913c4..8a79456 100644 --- a/lib/screens/budget/view/tabs/budget_tab.dart +++ b/lib/screens/budget/view/tabs/budget_tab.dart @@ -391,7 +391,6 @@ class _BudgetedListState const CacheResetEvent(), ); // Refresh the data on return - // \todo: use caching or something to avoid api call context.read().add( GetBudgetEvent( token: context.read().state.token, @@ -414,7 +413,6 @@ class _BudgetedListState const CacheResetEvent(), ); // Refresh the data on return - // \todo: use caching or something to avoid api call context.read().add( GetBudgetEvent( token: context.read().state.token, diff --git a/lib/screens/budget/view/tabs/expense_tab.dart b/lib/screens/budget/view/tabs/expense_tab.dart index b9f698d..636b222 100644 --- a/lib/screens/budget/view/tabs/expense_tab.dart +++ b/lib/screens/budget/view/tabs/expense_tab.dart @@ -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().add( GetExpensesEvent( token: context.read().state.token, diff --git a/lib/screens/budget/view/tabs/income_tab.dart b/lib/screens/budget/view/tabs/income_tab.dart index 62dc5a4..326327b 100644 --- a/lib/screens/budget/view/tabs/income_tab.dart +++ b/lib/screens/budget/view/tabs/income_tab.dart @@ -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().add( GetIncomesEvent( token: context.read().state.token,