Skip to content

Commit

Permalink
Fix RIP-Comm#213 + Added text when no budgets are tracked
Browse files Browse the repository at this point in the history
  • Loading branch information
theperu committed Mar 1, 2025
1 parent e865871 commit 93aa05b
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 19 deletions.
22 changes: 21 additions & 1 deletion lib/model/budget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,29 @@ class BudgetMethods extends SossoldiDatabase {
var query =
"SELECT bt.*, SUM(t.amount) as spent FROM $budgetTable as bt LEFT JOIN $categoryTransactionTable as ct ON bt.${BudgetFields.idCategory} = ct.${CategoryTransactionFields.id} LEFT JOIN '$transactionTable' as t ON t.${TransactionFields.idCategory} = ct.${CategoryTransactionFields.id} WHERE bt.active = 1 AND strftime('%m', t.date) = strftime('%m', 'now') AND strftime('%Y', t.date) = strftime('%Y', 'now') GROUP BY bt.${BudgetFields.idCategory};";
final result = await db.rawQuery(query);
return result.map((json) => BudgetStats.fromJson(json)).toList();

List<Budget> allBudgets = await selectAllActive();

List<BudgetStats> statsList = result.map((json) => BudgetStats.fromJson(json)).toList();

Set<int> resultBudgetIds = statsList.map((stats) => stats.idCategory).toSet();

// Check for missing budgets and add them with a spent amount of 0
for (var budget in allBudgets) {
if (!resultBudgetIds.contains(budget.idCategory)) {
statsList.add(BudgetStats(
idCategory: budget.idCategory,
name: budget.name,
spent: 0,
amountLimit: budget.amountLimit,
));
}
}

return statsList;
}


Future<int> updateItem(Budget item) async {
final db = await database;

Expand Down
57 changes: 41 additions & 16 deletions lib/pages/home_widget/budgets_home.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class BudgetsSection extends ConsumerStatefulWidget {
class _BudgetsSectionState extends ConsumerState<BudgetsSection> with Functions {
@override
Widget build(BuildContext context) {
final budgets =ref.watch(budgetsProvider.notifier).getMonthlyBudgetsStats();
final budgets = ref.watch(budgetsProvider.notifier).getMonthlyBudgetsStats();

return Column(
children: [
Expand Down Expand Up @@ -47,23 +47,48 @@ class _BudgetsSectionState extends ConsumerState<BudgetsSection> with Functions
),
),
const SizedBox(height: 16),
SizedBox(
height: 150,
width: MediaQuery.of(context).size.width,
child: ListView.builder(
scrollDirection: Axis.horizontal,
itemCount: budgets!.length,
itemBuilder: (context, index) => Padding(
padding: const EdgeInsets.symmetric(horizontal: 13),
child: BudgetCircularIndicator(
title: budgets[index].name!,
amount: budgets[index].amountLimit - budgets[index].spent > 0 ? budgets[index].amountLimit - budgets[index].spent : 0,
perc: budgets[index].spent / budgets[index].amountLimit > 1 ? 1 : budgets[index].spent / budgets[index].amountLimit,
color: categoryColorList[index % categoryColorList.length],
if (budgets == null || budgets.isEmpty)
Container(
height: 90,
width: MediaQuery.of(context).size.width,
alignment: Alignment.center,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
"No budget set",
style: Theme.of(context).textTheme.titleMedium?.copyWith(
color: Colors.grey[600],
),
),
const SizedBox(height: 8),
Text(
"Create a budget to track your spending",
style: Theme.of(context).textTheme.bodySmall?.copyWith(
color: Colors.grey[500],
),
),
],
),
)
else
SizedBox(
height: 150,
width: MediaQuery.of(context).size.width,
child: ListView.builder(
scrollDirection: Axis.horizontal,
itemCount: budgets.length,
itemBuilder: (context, index) => Padding(
padding: const EdgeInsets.symmetric(horizontal: 13),
child: BudgetCircularIndicator(
title: budgets[index].name!,
amount: budgets[index].amountLimit - budgets[index].spent > 0 ? budgets[index].amountLimit - budgets[index].spent : 0,
perc: budgets[index].spent / budgets[index].amountLimit > 1 ? 1 : budgets[index].spent / budgets[index].amountLimit,
color: categoryColorList[index % categoryColorList.length],
),
),
),
),
),
],
);
}
Expand All @@ -75,4 +100,4 @@ class _BudgetsSectionState extends ConsumerState<BudgetsSection> with Functions
],
);
}
}
}
6 changes: 4 additions & 2 deletions lib/pages/planning_page/manage_budget_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,20 @@ class _ManageBudgetPageState extends ConsumerState<ManageBudgetPage> {
setState(() {});
}

void updateBudget(Budget updatedBudget, int index) {
Future<void> updateBudget(Budget updatedBudget, int index) async {
setState(() {
deletedBudgets.add(budgets[index]);
budgets[index] = updatedBudget;
});
await ref.read(budgetsProvider.notifier).refreshBudgets();
}

void deleteBudget(Budget removedBudget, int index) {
Future<void> deleteBudget(Budget removedBudget, int index) async {
setState(() {
budgets.removeAt(index);
deletedBudgets.add(removedBudget);
});
await ref.read(budgetsProvider.notifier).refreshBudgets();
}

@override
Expand Down
4 changes: 4 additions & 0 deletions lib/providers/budgets_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ class AsyncBudgetsNotifier extends AsyncNotifier<List<Budget>> {
return _getBudgets();
});
}

Future<void> refreshBudgets() async {
ref.invalidateSelf();
}
}

final budgetsProvider =
Expand Down

0 comments on commit 93aa05b

Please sign in to comment.