@@ -378,19 +378,22 @@ public static List<Row> buildRows(Resources resources, ShoppingCart cart) {
378378 if (item .getType () == ShoppingCart .ItemType .LINE_ITEM ) {
379379 if (item .isDiscount ()) {
380380 SimpleRow row = new SimpleRow ();
381+ row .item = item ;
381382 row .title = resources .getString (R .string .Snabble_Shoppingcart_discounts );
382383 row .imageResId = R .drawable .snabble_ic_percent ;
383384 row .text = sanitize (item .getPriceText ());
384385 rows .add (row );
385386 } else if (item .isGiveaway ()) {
386387 SimpleRow row = new SimpleRow ();
388+ row .item = item ;
387389 row .title = item .getDisplayName ();
388390 row .imageResId = R .drawable .snabble_ic_gift ;
389391 row .text = resources .getString (R .string .Snabble_Shoppingcart_giveaway );
390392 rows .add (row );
391393 }
392394 } else if (item .getType () == ShoppingCart .ItemType .COUPON ) {
393395 SimpleRow row = new SimpleRow ();
396+ row .item = item ;
394397 row .title = resources .getString (R .string .Snabble_Shoppingcart_coupon );
395398 row .text = item .getDisplayName ();
396399 row .isDismissible = true ;
@@ -445,13 +448,29 @@ private static void setTextOrHide(TextView textView, String text) {
445448 }
446449
447450 private static abstract class Row {
451+ ShoppingCart .Item item ;
448452 boolean isDismissible ;
449- }
450453
451- private static class ProductRow extends Row {
454+ @ Override
455+ public boolean equals (Object o ) {
456+ if (this == o ) return true ;
457+ if (o == null || getClass () != o .getClass ()) return false ;
452458
453- ShoppingCart .Item item ;
459+ Row row = (Row ) o ;
460+
461+ if (isDismissible != row .isDismissible ) return false ;
462+ return item != null ? item .equals (row .item ) : row .item == null ;
463+ }
454464
465+ @ Override
466+ public int hashCode () {
467+ int result = item != null ? item .hashCode () : 0 ;
468+ result = 31 * result + (isDismissible ? 1 : 0 );
469+ return result ;
470+ }
471+ }
472+
473+ private static class ProductRow extends Row {
455474 String name ;
456475 String subtitle ;
457476 String imageUrl ;
@@ -466,22 +485,37 @@ private static class ProductRow extends Row {
466485 public boolean equals (Object o ) {
467486 if (this == o ) return true ;
468487 if (o == null || getClass () != o .getClass ()) return false ;
488+ if (!super .equals (o )) return false ;
489+
469490 ProductRow that = (ProductRow ) o ;
470- return quantity == that .quantity &&
471- editable == that .editable &&
472- manualDiscountApplied == that .manualDiscountApplied &&
473- Objects .equals (item , that .item ) &&
474- Objects .equals (name , that .name ) &&
475- Objects .equals (subtitle , that .subtitle ) &&
476- Objects .equals (imageUrl , that .imageUrl ) &&
477- encodingUnit == that .encodingUnit &&
478- Objects .equals (priceText , that .priceText ) &&
479- Objects .equals (quantityText , that .quantityText );
491+
492+ if (quantity != that .quantity ) return false ;
493+ if (editable != that .editable ) return false ;
494+ if (manualDiscountApplied != that .manualDiscountApplied ) return false ;
495+ if (name != null ? !name .equals (that .name ) : that .name != null ) return false ;
496+ if (subtitle != null ? !subtitle .equals (that .subtitle ) : that .subtitle != null )
497+ return false ;
498+ if (imageUrl != null ? !imageUrl .equals (that .imageUrl ) : that .imageUrl != null )
499+ return false ;
500+ if (encodingUnit != that .encodingUnit ) return false ;
501+ if (priceText != null ? !priceText .equals (that .priceText ) : that .priceText != null )
502+ return false ;
503+ return quantityText != null ? quantityText .equals (that .quantityText ) : that .quantityText == null ;
480504 }
481505
482506 @ Override
483507 public int hashCode () {
484- return Objects .hash (item , name , subtitle , imageUrl , encodingUnit , priceText , quantityText , quantity , editable , manualDiscountApplied );
508+ int result = super .hashCode ();
509+ result = 31 * result + (name != null ? name .hashCode () : 0 );
510+ result = 31 * result + (subtitle != null ? subtitle .hashCode () : 0 );
511+ result = 31 * result + (imageUrl != null ? imageUrl .hashCode () : 0 );
512+ result = 31 * result + (encodingUnit != null ? encodingUnit .hashCode () : 0 );
513+ result = 31 * result + (priceText != null ? priceText .hashCode () : 0 );
514+ result = 31 * result + (quantityText != null ? quantityText .hashCode () : 0 );
515+ result = 31 * result + quantity ;
516+ result = 31 * result + (editable ? 1 : 0 );
517+ result = 31 * result + (manualDiscountApplied ? 1 : 0 );
518+ return result ;
485519 }
486520 }
487521
@@ -494,17 +528,21 @@ private static class SimpleRow extends Row {
494528 public boolean equals (Object o ) {
495529 if (this == o ) return true ;
496530 if (o == null || getClass () != o .getClass ()) return false ;
531+ if (!super .equals (o )) return false ;
497532
498533 SimpleRow simpleRow = (SimpleRow ) o ;
499534
500535 if (imageResId != simpleRow .imageResId ) return false ;
536+ if (item != null ? !item .equals (simpleRow .item ) : simpleRow .item != null ) return false ;
501537 if (text != null ? !text .equals (simpleRow .text ) : simpleRow .text != null ) return false ;
502538 return title != null ? title .equals (simpleRow .title ) : simpleRow .title == null ;
503539 }
504540
505541 @ Override
506542 public int hashCode () {
507- int result = text != null ? text .hashCode () : 0 ;
543+ int result = super .hashCode ();
544+ result = 31 * result + (item != null ? item .hashCode () : 0 );
545+ result = 31 * result + (text != null ? text .hashCode () : 0 );
508546 result = 31 * result + (title != null ? title .hashCode () : 0 );
509547 result = 31 * result + imageResId ;
510548 return result ;
@@ -807,16 +845,11 @@ public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
807845 Row oldRow = list .get (oldItemPosition );
808846 Row newRow = newList .get (newItemPosition );
809847
810- if (oldRow instanceof ProductRow && newRow instanceof ProductRow ) {
811- ProductRow productOldRow = (ProductRow ) oldRow ;
812- ProductRow productNewRow = (ProductRow ) newRow ;
813-
814- return productOldRow .item == productNewRow .item ;
815- } else if (oldRow instanceof SimpleRow && newRow instanceof SimpleRow ) {
816- return true ;
848+ if (oldRow .item == null || newRow .item == null ) {
849+ return false ;
817850 }
818851
819- return false ;
852+ return oldRow . item == newRow . item ;
820853 }
821854
822855 @ Override
@@ -881,9 +914,11 @@ public void removeAndShowUndoSnackbar(int adapterPosition, ShoppingCart.Item ite
881914 R .string .Snabble_Shoppingcart_articleRemoved , UIUtils .SNACKBAR_LENGTH_VERY_LONG );
882915 snackbar .setAction (R .string .Snabble_undo , v -> {
883916 cart .insert (item , adapterPosition );
917+ fetchFrom (cart );
884918 Telemetry .event (Telemetry .Event .UndoDeleteFromCart , item .getProduct ());
885919 });
886920 snackbar .show ();
921+ fetchFrom (cart );
887922 }
888923 }
889924}
0 commit comments