Made the input dialog fully item type aware. Fixes issue #1, now all item types can have an item of the same name (such as "Test") rather than only checking the Expense table for the name.

This commit is contained in:
Hyperling 2025-03-27 12:15:55 -07:00
parent 147178e4dd
commit cce878ccaa

View File

@ -297,6 +297,21 @@ class _TrackedItemInputDialogState extends State<TrackedItemInputDialog> {
String amountText =
widget.amountText != null ? widget.amountText! : TrackedItem.amountText;
Future<List<TrackedItem>> items;
switch (_type) {
case ItemType.expense:
items = DatabaseHelper.instance.getExpenses();
break;
case ItemType.income:
items = DatabaseHelper.instance.getIncomes();
break;
case ItemType.asset:
items = DatabaseHelper.instance.getAssets();
break;
default:
throw UnimplementedError("Cannot find unimplemented type.");
}
return Column(
// prevent AlertDialog from taking full vertical height.
mainAxisSize: MainAxisSize.min,
@ -336,15 +351,15 @@ class _TrackedItemInputDialogState extends State<TrackedItemInputDialog> {
? Text("New ${_type!.title}")
: Text("Edit ${_type!.title}"),
),
content: FutureBuilder<List<Expense>>(
content: FutureBuilder<List<TrackedItem>>(
// TODO / TBD -- This should no longer only be Expenses.
future: DatabaseHelper.instance.getExpenses(),
future: items,
builder: (BuildContext context,
AsyncSnapshot<List<Expense>> snapshot) {
AsyncSnapshot<List<TrackedItem>> snapshot) {
if (!snapshot.hasData) {
return Center(child: Text('Loading...'));
}
List<Expense> expenses = snapshot.data!;
List<TrackedItem> expenses = snapshot.data!;
return Form(
key: _formKey,
child: Column(
@ -392,7 +407,16 @@ class _TrackedItemInputDialogState extends State<TrackedItemInputDialog> {
return "$amountText must be a valid number.";
}
if (double.parse(value) < 0) {
return "Please use the Income page rather than having negative expenses.";
switch (_type) {
case ItemType.expense:
return "Please use the Income page.";
case ItemType.income:
return "Please use the Expense page.";
default:
break;
}
}
if (double.parse(value) < 0.01) {
return "$amountText must be one hundreth (0.01) or higher.";