Skip to content
Open
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
57 changes: 48 additions & 9 deletions lib/Homepage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,25 @@ class _HomepageState extends State<Homepage> {
TextEditingController nameController = TextEditingController();
TextEditingController amountController = TextEditingController();

final List<String> _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<expenses_database>(context, listen: false).readExpenses();
Expand Down Expand Up @@ -151,16 +170,25 @@ class _HomepageState extends State<Homepage> {
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
Expand All @@ -181,6 +209,19 @@ class _HomepageState extends State<Homepage> {
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) {
Expand All @@ -189,11 +230,9 @@ class _HomepageState extends State<Homepage> {

//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());

Expand Down
2 changes: 2 additions & 0 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand Down
19 changes: 19 additions & 0 deletions lib/money/database/expenses_database.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,25 @@ static Future<void> initialize() async{
final dir = await getApplicationDocumentsDirectory();
isar = await Isar.open([ExpensesSchema], directory: dir.path);
}

// Purge known test/placeholder data
static Future<int> purgeTestData({List<String> 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
*/
Expand Down