From e90cdc752fe437ebab447126349bb5659b17e39d Mon Sep 17 00:00:00 2001 From: smishra <8684346+HackersSpirit@users.noreply.github.com> Date: Thu, 23 Oct 2025 12:15:41 +1100 Subject: [PATCH] money: purge placeholder entries on startup and block future test data - add expenses_database.purgeTestData() to remove case-insensitive exact matches for [asd,ok,new] - call purge at app startup after Isar init - add UI validation in Homepage: block placeholder names, require name length >= 2, and amount > 0 on create/edit; show SnackBar on invalid input - no schema changes; safe no-op if no placeholders present --- lib/Homepage.dart | 57 +++++++++++++++++++---- lib/main.dart | 2 + lib/money/database/expenses_database.dart | 19 ++++++++ 3 files changed, 69 insertions(+), 9 deletions(-) diff --git a/lib/Homepage.dart b/lib/Homepage.dart index 4efc2da..a1bc7dd 100644 --- a/lib/Homepage.dart +++ b/lib/Homepage.dart @@ -17,6 +17,25 @@ class _HomepageState extends State { TextEditingController nameController = TextEditingController(); TextEditingController amountController = TextEditingController(); + final List _blacklist = const ["asd", "ok", "new"]; + + bool _isNameAllowed(String raw) { + final name = raw.trim(); + if (name.length < 2) return false; + return !_blacklist.any((b) => b.toLowerCase() == name.toLowerCase()); + } + + bool _isAmountAllowed(String raw) { + final v = convertStringToDouble(raw); + return v > 0; + } + + void _showError(String message) { + ScaffoldMessenger.of(context).showSnackBar( + SnackBar(content: Text(message)), + ); + } + @override void initState() { Provider.of(context, listen: false).readExpenses(); @@ -151,16 +170,25 @@ class _HomepageState extends State { Widget _createNewExpensesButton() { return MaterialButton( onPressed: () async { + final name = nameController.text; + final amountText = amountController.text; + if (!_isNameAllowed(name)) { + _showError('Please enter a valid name (no placeholders).'); + return; + } + if (!_isAmountAllowed(amountText)) { + _showError('Please enter a positive amount.'); + return; + } //only save if there is something in the textfield to save - if (nameController.text.isNotEmpty && - amountController.text.isNotEmpty) { + if (name.isNotEmpty && amountText.isNotEmpty) { //POP BOX Navigator.pop(context); //create new expenses Expenses newExpenses = Expenses( - name: nameController.text, - amount: convertStringToDouble(amountController.text), + name: name.trim(), + amount: convertStringToDouble(amountText), date: DateTime.now()); // save to db @@ -181,6 +209,19 @@ class _HomepageState extends State { Widget _editExpensesButton(Expenses expenses) { return MaterialButton( onPressed: () async { + final newName = nameController.text.trim(); + final newAmountText = amountController.text.trim(); + + // if user provided new values, validate them + if (newName.isNotEmpty && !_isNameAllowed(newName)) { + _showError('Please enter a valid name (no placeholders).'); + return; + } + if (newAmountText.isNotEmpty && !_isAmountAllowed(newAmountText)) { + _showError('Please enter a positive amount.'); + return; + } + // save as long as at least one textfield has been changes if (nameController.text.isNotEmpty || amountController.text.isNotEmpty) { @@ -189,11 +230,9 @@ class _HomepageState extends State { //create a new updates box Expenses updatedExpenses = Expenses( - name: nameController.text.isNotEmpty - ? nameController.text - : expenses.name, - amount: amountController.text.isNotEmpty - ? convertStringToDouble(amountController.text) + name: newName.isNotEmpty ? newName : expenses.name, + amount: newAmountText.isNotEmpty + ? convertStringToDouble(newAmountText) : expenses.amount, date: DateTime.now()); diff --git a/lib/main.dart b/lib/main.dart index 78f2f8a..7695475 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -9,6 +9,8 @@ void main() async { //initialize money db await expenses_database.initialize(); + // Purge known test data once at startup (safe no-op if none exist) + await expenses_database.purgeTestData(); runApp(MultiProvider( providers: [ diff --git a/lib/money/database/expenses_database.dart b/lib/money/database/expenses_database.dart index d340c9f..54282b7 100644 --- a/lib/money/database/expenses_database.dart +++ b/lib/money/database/expenses_database.dart @@ -15,6 +15,25 @@ static Future initialize() async{ final dir = await getApplicationDocumentsDirectory(); isar = await Isar.open([ExpensesSchema], directory: dir.path); } + +// Purge known test/placeholder data +static Future purgeTestData({List blacklist = const ["asd", "ok", "new"]}) async { + int deleted = 0; + // delete any expenses whose name matches any blacklisted value (case-insensitive) + for (final raw in blacklist) { + final name = raw.toLowerCase(); + final ids = await isar.expenses + .filter() + .nameEqualTo(name, caseSensitive: false) + .idProperty() + .findAll(); + if (ids.isEmpty) continue; + await isar.writeTxn(() async { + deleted += await isar.expenses.deleteAll(ids); + }); + } + return deleted; +} /* GETTERS */