Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #220 & #213 + Untracked bugs #230

Merged
merged 2 commits into from
Mar 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/custom_widgets/line_chart.dart
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,7 @@ class _LineChartSample2State extends State<LineChartWidget> {
LineChartBarData(
spots: widget.lineData,
isCurved: true,
curveSmoothness: 0.15,
barWidth: 1.5,
isStrokeCapRound: true,
color: lineColor,
Expand All @@ -313,6 +314,7 @@ class _LineChartSample2State extends State<LineChartWidget> {
LineChartBarData(
spots: widget.line2Data,
isCurved: true,
curveSmoothness: 0.15,
barWidth: 1,
isStrokeCapRound: true,
color: line2Color,
Expand All @@ -321,4 +323,4 @@ class _LineChartSample2State extends State<LineChartWidget> {
],
);
}
}
}
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
1 change: 1 addition & 0 deletions lib/pages/graphs_page/graphs_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ class _GraphsPageState extends ConsumerState<GraphsPage> with Functions {
LineChartWidget(
lineData: currentYearMonthlyTransactions,
enableGapFilling: false,
period: Period.year,
),
],
);
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