22
33import android .os .Handler ;
44import android .os .Looper ;
5+ import android .os .SystemClock ;
56
67import java .util .ArrayList ;
78import java .util .Collections ;
89import java .util .List ;
910import java .util .UUID ;
1011import java .util .concurrent .CopyOnWriteArrayList ;
12+ import java .util .concurrent .TimeUnit ;
1113
1214import io .snabble .sdk .codes .ScannableCode ;
1315
1416public class ShoppingCart {
1517 public static final int MAX_QUANTITY = 99999 ;
18+ public static final long KEEP_ALIVE_TIME = TimeUnit .HOURS .toMillis (4 );
1619
1720 private static class Entry {
1821 private Product product ;
@@ -48,6 +51,7 @@ public void setScannedCode(ScannableCode scannedCode){
4851 private final transient Object lock = new Object ();
4952
5053 private String id ;
54+ private long lastModificationTime ;
5155 private List <Entry > items = new ArrayList <>();
5256 private transient List <ShoppingCartListener > listeners = new CopyOnWriteArrayList <>();
5357 private transient ProductDatabase productDatabase ;
@@ -58,6 +62,13 @@ protected ShoppingCart() {
5862 //empty constructor for gson
5963 }
6064
65+ ShoppingCart (SnabbleSdk sdkInstance ) {
66+ id = UUID .randomUUID ().toString ();
67+ updateTimestamp ();
68+
69+ initWithSdkInstance (sdkInstance );
70+ }
71+
6172 void initWithSdkInstance (SnabbleSdk sdkInstance ) {
6273 this .sdkInstance = sdkInstance ;
6374
@@ -69,14 +80,10 @@ public void onDatabaseUpdated() {
6980 }
7081 });
7182
83+ validate ();
7284 updateEntries ();
7385 }
7486
75- ShoppingCart (SnabbleSdk sdkInstance ) {
76- id = UUID .randomUUID ().toString ();
77- initWithSdkInstance (sdkInstance );
78- }
79-
8087 private void updateEntries () {
8188 synchronized (lock ) {
8289 List <Entry > removables = new ArrayList <>();
@@ -286,6 +293,13 @@ public void invalidate() {
286293 clear ();
287294 }
288295
296+ public void validate () {
297+ long currentTime = SystemClock .elapsedRealtime ();
298+ if (lastModificationTime + KEEP_ALIVE_TIME < currentTime ){
299+ clear ();
300+ }
301+ }
302+
289303 private void setEntryQuantity (Entry e , int newQuantity ) {
290304 if (e != null ) {
291305 if (newQuantity > 0 ) {
@@ -405,6 +419,10 @@ public int getTotalQuantity() {
405419 }
406420 }
407421
422+ private void updateTimestamp () {
423+ lastModificationTime = SystemClock .elapsedRealtime ();
424+ }
425+
408426 /**
409427 * Adds a {@link ShoppingCartListener} to the list of listeners if it does not already exist.
410428 *
@@ -470,6 +488,8 @@ public void onItemRemoved(ShoppingCart list, Product product) {
470488 }
471489
472490 private void notifyItemAdded (final ShoppingCart list , final Product product ) {
491+ updateTimestamp ();
492+
473493 handler .post (new Runnable () {
474494 @ Override
475495 public void run () {
@@ -481,6 +501,8 @@ public void run() {
481501 }
482502
483503 private void notifyItemRemoved (final ShoppingCart list , final Product product ) {
504+ updateTimestamp ();
505+
484506 handler .post (new Runnable () {
485507 @ Override
486508 public void run () {
@@ -492,6 +514,8 @@ public void run() {
492514 }
493515
494516 private void notifyQuantityChanged (final ShoppingCart list , final Product product ) {
517+ updateTimestamp ();
518+
495519 handler .post (new Runnable () {
496520 @ Override
497521 public void run () {
@@ -503,6 +527,8 @@ public void run() {
503527 }
504528
505529 private void notifyItemMoved (final ShoppingCart list , final int fromIndex , final int toIndex ) {
530+ updateTimestamp ();
531+
506532 handler .post (new Runnable () {
507533 @ Override
508534 public void run () {
@@ -519,6 +545,8 @@ public void run() {
519545 * @param list the {@link ShoppingCart}
520546 */
521547 private void notifyCleared (final ShoppingCart list ) {
548+ updateTimestamp ();
549+
522550 handler .post (new Runnable () {
523551 @ Override
524552 public void run () {
0 commit comments