More work to make the Expense page arbitrary for re-use.

This commit is contained in:
Hyperling 2025-02-11 15:16:46 -07:00
parent bea9a4bc36
commit 737264fa2f
4 changed files with 44 additions and 29 deletions

View File

@ -3,6 +3,7 @@
// SQLite
import 'dart:io';
import 'dart:async';
import 'package:flutter_expense_tracker/models/recurring_tracked_type.dart';
import 'package:path/path.dart';
import 'package:path_provider/path_provider.dart';
import 'package:sqflite/sqflite.dart';
@ -76,7 +77,7 @@ class DatabaseHelper {
return expenseList;
}
Future<int> addExpense(Expense expense) async {
Future<int> addExpense(RecurringTrackedType expense) async {
Database db = await instance.db;
return await db.insert(
"expense",
@ -93,7 +94,7 @@ class DatabaseHelper {
);
}
Future<int> updateExpense(Expense expense) async {
Future<int> updateExpense(RecurringTrackedType expense) async {
Database db = await instance.db;
return await db.update(
"expense",

View File

@ -11,6 +11,9 @@ abstract class TrackedType {
required this.description,
});
static String amountText = "Amount";
String getAmountText() => amountText;
@override
String toString() {
return toMap().toString();

View File

@ -1,6 +1,8 @@
// Flutter
import 'package:flutter/material.dart';
import 'package:flutter_expense_tracker/db.dart';
import 'package:flutter_expense_tracker/models/recurring_tracked_type.dart';
import 'package:flutter_expense_tracker/models/tracked_type.dart';
// Local
import '/models/expense.dart';
@ -26,9 +28,10 @@ class _ExpensePageState extends State<ExpensePage> {
Widget build(BuildContext context) {
final theme = Theme.of(context);
return FutureBuilder<List<Expense>>(
return FutureBuilder<List<RecurringTrackedType>>(
future: DatabaseHelper.instance.getExpenses(),
builder: (BuildContext context, AsyncSnapshot<List<Expense>> snapshot) {
builder: (BuildContext context,
AsyncSnapshot<List<RecurringTrackedType>> snapshot) {
if (!snapshot.hasData) {
return Text('Loading...');
}
@ -45,8 +48,8 @@ class _ExpensePageState extends State<ExpensePage> {
: ListView.builder(
itemCount: snapshot.data!.length,
itemBuilder: (_, index) {
List<Expense> expenses = snapshot.data!;
final Expense curr = expenses[index];
//List<Expense> expenses = snapshot.data!;
final RecurringTrackedType curr = snapshot.data![index];
final String estimateSymbolYearly = curr
.frequency.timesPerYear
.toStringAsFixed(2)
@ -92,7 +95,7 @@ class _ExpensePageState extends State<ExpensePage> {
),
onDismissed: (direction) {
setState(() {
expenses.remove(curr);
snapshot.data!.remove(curr);
switch (direction) {
case DismissDirection.startToEnd:
DatabaseHelper.instance.removeExpense(curr.id!);
@ -102,9 +105,10 @@ class _ExpensePageState extends State<ExpensePage> {
showDialog(
context: context,
builder: (_) => AlertDialog(
content: ExpenseInputDialog(
content: RecurringTrackedTypeInputDialog(
notifyParent: refresh,
expense: curr,
entry: curr,
amountText: curr.getAmountText(),
),
),
);
@ -179,21 +183,25 @@ class _ExpensePageState extends State<ExpensePage> {
}
}
class ExpenseInputDialog extends StatefulWidget {
class RecurringTrackedTypeInputDialog extends StatefulWidget {
final Function() notifyParent;
final Expense? expense;
final RecurringTrackedType? entry;
final String? amountText;
const ExpenseInputDialog({
const RecurringTrackedTypeInputDialog({
super.key,
required this.notifyParent,
this.expense,
this.entry,
this.amountText,
});
@override
State<ExpenseInputDialog> createState() => _ExpenseInputDialogState();
State<RecurringTrackedTypeInputDialog> createState() =>
_RecurringTrackedTypeInputDialogState();
}
class _ExpenseInputDialogState extends State<ExpenseInputDialog> {
class _RecurringTrackedTypeInputDialogState
extends State<RecurringTrackedTypeInputDialog> {
final _expenseFormKey = GlobalKey<FormState>();
int? _id;
@ -204,14 +212,17 @@ class _ExpenseInputDialogState extends State<ExpenseInputDialog> {
@override
Widget build(BuildContext context) {
if (widget.expense != null) {
_id = widget.expense!.id;
_name = widget.expense!.name;
_amount = widget.expense!.amount;
_freq = widget.expense!.frequency;
_desc = widget.expense!.description;
if (widget.entry != null) {
_id = widget.entry!.id;
_name = widget.entry!.name;
_amount = widget.entry!.amount;
_freq = widget.entry!.frequency;
_desc = widget.entry!.description;
}
String amountText =
widget.amountText != null ? widget.amountText! : TrackedType.amountText;
return Column(
// prevent AlertDialog from taking full vertical height.
mainAxisSize: MainAxisSize.min,
@ -220,9 +231,9 @@ class _ExpenseInputDialogState extends State<ExpenseInputDialog> {
alignment: FractionalOffset.topRight,
child: IconButton(
onPressed: () {
if (widget.expense != null) {
if (widget.entry != null) {
setState(() {
DatabaseHelper.instance.addExpense(widget.expense!);
DatabaseHelper.instance.addExpense(widget.entry!);
widget.notifyParent();
});
}
@ -234,7 +245,7 @@ class _ExpenseInputDialogState extends State<ExpenseInputDialog> {
AlertDialog(
insetPadding: EdgeInsets.all(0),
title: Center(
child: widget.expense == null
child: widget.entry == null
? Text("New Expense")
: Text("Edit Expense"),
),
@ -279,7 +290,7 @@ class _ExpenseInputDialogState extends State<ExpenseInputDialog> {
keyboardType:
TextInputType.numberWithOptions(decimal: true),
decoration: InputDecoration(
labelText: "${Expense.amountText}",
labelText: amountText,
hintText: "Example: 10.00",
hintStyle: TextStyle(fontSize: 10.0),
errorStyle: TextStyle(fontSize: 10.0),
@ -287,16 +298,16 @@ class _ExpenseInputDialogState extends State<ExpenseInputDialog> {
initialValue: _amount != 0 ? _amount.toString() : "",
validator: (value) {
if (value == null || value.isEmpty) {
return "${Expense.amountText} must be provided.";
return "$amountText must be provided.";
}
if (double.tryParse(value) == null) {
return "${Expense.amountText} must be a valid number.";
return "$amountText must be a valid number.";
}
if (double.parse(value) < 0) {
return "Please use the Income page rather than having negative expenses.";
}
if (double.parse(value) < 0.01) {
return "${Expense.amountText} must be one hundreth (0.01) or higher.";
return "$amountText must be one hundreth (0.01) or higher.";
}
return null;
},

View File

@ -33,7 +33,7 @@ class _HomePageState extends State<HomePage> {
switch (pageSelected) {
case 0:
page = ExpensePage();
dialog = ExpenseInputDialog(
dialog = RecurringTrackedTypeInputDialog(
notifyParent: refresh,
);
case 1: