Skip to content

Commit 8f7ab8c

Browse files
author
Leshoraa
committed
refactor: Store empty discount lists as null
Prevents saving an empty JSON array `[]` for items that have no valid discounts. Instead, the `discounts_json` field will now be set to `null` if all discounts are empty or zero, leading to cleaner data storage and avoiding unnecessary empty array representations.
1 parent 1da948c commit 8f7ab8c

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

app/src/main/java/com/leshoraa/listshop/PreviewItemActivity.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -371,10 +371,6 @@ private void enforceMaxDiscount() {
371371
private void saveItemChanges() {
372372
if (!discounts.isEmpty()) {
373373
Double lastDiscountValue = discounts.get(discounts.size() - 1);
374-
if (lastDiscountValue == null || lastDiscountValue == 0.0) {
375-
Toast.makeText(this, "Please fill or remove the last empty discount field before updating.", Toast.LENGTH_SHORT).show();
376-
return;
377-
}
378374
}
379375

380376
Item item = dbHelper.getItemById(selectedItemId);
@@ -418,7 +414,11 @@ private void saveItemChanges() {
418414
}
419415
}
420416
Gson gson = new Gson();
421-
item.setDiscountsJson(gson.toJson(discountsToSave));
417+
if (discountsToSave.isEmpty()) {
418+
item.setDiscountsJson(null);
419+
} else {
420+
item.setDiscountsJson(gson.toJson(discountsToSave));
421+
}
422422

423423
double totalBasePrice = item.getPrice() * item.getCount();
424424
double totalCombinedDiscountPercentage = 0.0;

0 commit comments

Comments
 (0)