@@ -60,6 +60,7 @@ public String getValue() {
6060 private long lastModificationTime ;
6161 private List <Item > oldItems ;
6262 private List <Item > items = new ArrayList <>();
63+ private final List <ViolationNotification > violationNotifications = new ArrayList <>();
6364 private int modCount = 0 ;
6465 private int addCount = 0 ;
6566 private Integer onlineTotalPrice ;
@@ -599,7 +600,8 @@ public static class Item {
599600 private transient ShoppingCart cart ;
600601 private boolean isManualCouponApplied ;
601602 private Coupon coupon ;
602- private String couponId ;
603+ // The local generated UUID of a coupon which which will be used by the backend
604+ private String backendCouponId ;
603605
604606 protected Item () {
605607 // for gson
@@ -610,7 +612,7 @@ private Item(ShoppingCart cart, Coupon coupon, ScannedCode scannedCode) {
610612 this .cart = cart ;
611613 this .scannedCode = scannedCode ;
612614 this .coupon = coupon ;
613- this .couponId = UUID .randomUUID ().toString ();
615+ this .backendCouponId = UUID .randomUUID ().toString ();
614616 }
615617
616618 private Item (ShoppingCart cart , Product product , ScannedCode scannedCode ) {
@@ -1174,7 +1176,7 @@ public BackendCart toBackendCart() {
11741176 }
11751177 } else if (cartItem .getType () == ItemType .COUPON ) {
11761178 BackendCartItem item = new BackendCartItem ();
1177- item .id = cartItem .couponId ;
1179+ item .id = cartItem .backendCouponId ;
11781180 item .amount = 1 ;
11791181
11801182 ScannedCode scannedCode = cartItem .getScannedCode ();
@@ -1192,6 +1194,47 @@ public BackendCart toBackendCart() {
11921194 return backendCart ;
11931195 }
11941196
1197+ @ RestrictTo (RestrictTo .Scope .LIBRARY )
1198+ void resolveViolations (List <Violation > violations ) {
1199+ for (Violation violation : violations ) {
1200+ for (int i = items .size () - 1 ; i >= 0 ; i --) {
1201+ if (items .get (i ).coupon != null && items .get (i ).backendCouponId .equals (violation .getRefersTo ())) {
1202+ Item item = items .get (i );
1203+ items .remove (item );
1204+ boolean found = false ;
1205+ for (ViolationNotification notification : violationNotifications ) {
1206+ if (notification .getRefersTo ().equals (violation .getRefersTo ())) {
1207+ found = true ;
1208+ break ;
1209+ }
1210+ }
1211+ if (!found ) {
1212+ violationNotifications .add (new ViolationNotification (
1213+ item .coupon .getName (),
1214+ violation .getRefersTo (),
1215+ violation .getType (),
1216+ violation .getMessage ()
1217+ ));
1218+ }
1219+ }
1220+ }
1221+ }
1222+ notifyViolations ();
1223+ }
1224+
1225+ /**
1226+ * Remove the handled ViolationNotifications.
1227+ * @param violations the handled ViolationNotifications.
1228+ */
1229+ public void removeViolationNotification (List <ViolationNotification > violations ) {
1230+ violationNotifications .removeAll (violations );
1231+ }
1232+
1233+ @ NonNull
1234+ public List <ViolationNotification > getViolationNotifications () {
1235+ return violationNotifications ;
1236+ }
1237+
11951238 /**
11961239 * Adds a {@link ShoppingCartListener} to the list of listeners if it does not already exist.
11971240 *
@@ -1201,6 +1244,9 @@ public void addListener(ShoppingCartListener listener) {
12011244 if (!listeners .contains (listener )) {
12021245 listeners .add (listener );
12031246 }
1247+ if (!violationNotifications .isEmpty ()) {
1248+ listener .onViolationDetected (violationNotifications );
1249+ }
12041250 }
12051251
12061252 /**
@@ -1234,7 +1280,7 @@ public interface ShoppingCartListener {
12341280
12351281 void onTaxationChanged (ShoppingCart list , Taxation taxation );
12361282
1237- void onViolationDetected (@ NonNull List <Violation > violations );
1283+ void onViolationDetected (@ NonNull List <ViolationNotification > violations );
12381284 }
12391285
12401286 public static abstract class SimpleShoppingCartListener implements ShoppingCartListener {
@@ -1282,7 +1328,7 @@ public void onCheckoutLimitReached(ShoppingCart list) {}
12821328 public void onOnlinePaymentLimitReached (ShoppingCart list ) {}
12831329
12841330 @ Override
1285- public void onViolationDetected (List <Violation > violations ) {}
1331+ public void onViolationDetected (List <ViolationNotification > violations ) {}
12861332 }
12871333
12881334 private void notifyItemAdded (final ShoppingCart list , final Item item ) {
@@ -1361,31 +1407,14 @@ void notifyOnlinePaymentLimitReached(final ShoppingCart list) {
13611407 });
13621408 }
13631409
1364- public void notifyViolations (@ NonNull List < Violation > violations ) {
1410+ public void notifyViolations () {
13651411 Dispatch .mainThread (() -> {
13661412 for (ShoppingCartListener listener : listeners ) {
1367- listener .onViolationDetected (violations );
1368- }
1369- for (Violation violation : violations ) {
1370- for (int i = items .size () - 1 ; i >= 0 ; i --) {
1371- if (items .get (i ).coupon != null && items .get (i ).couponId .equals (violation .getRefersTo ())) {
1372- items .remove (items .get (i ));
1373- }
1374- }
1413+ listener .onViolationDetected (violationNotifications );
13751414 }
13761415 });
13771416 }
13781417
1379- @ Nullable
1380- public Coupon getCouponByCartId (String id ) {
1381- for (int i = items .size () - 1 ; i >= 0 ; i --) {
1382- if (items .get (i ).coupon != null && items .get (i ).couponId .equals (id )) {
1383- return items .get (i ).coupon ;
1384- }
1385- }
1386- return null ;
1387- }
1388-
13891418 /**
13901419 * Notifies all {@link #listeners} that the shopping list was cleared of all entries.
13911420 *
0 commit comments