Skip to content

Commit 4ab7803

Browse files
committed
allow skip full database updates, removed automatic product updates from cart
1 parent cf757fd commit 4ab7803

File tree

4 files changed

+73
-79
lines changed

4 files changed

+73
-79
lines changed

core/src/main/java/io/snabble/sdk/ProductDatabase.java

Lines changed: 54 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,19 @@ synchronized void applyFullUpdate(InputStream inputStream) throws IOException {
438438
Logger.d("Full update took %d ms", time2);
439439
}
440440

441+
/**
442+
* Updates the current database in the background by requesting new data from the backend.
443+
* <p>
444+
* Updates can be either delta updates or full updates depending on how big the difference between
445+
* the last update was.
446+
* <p>
447+
* While updating, the database can still be queried for data, after the update completes calls to the database
448+
* return the updated data.
449+
*/
450+
public void update() {
451+
update(null);
452+
}
453+
441454
/**
442455
* Updates the current database in the background by requesting new data from the backend.
443456
* <p>
@@ -451,12 +464,29 @@ synchronized void applyFullUpdate(InputStream inputStream) throws IOException {
451464
* Or error() in case a network error occurred.
452465
*/
453466
public void update(final UpdateCallback callback) {
467+
update(callback, false);
468+
}
469+
470+
/**
471+
* Updates the current database in the background by requesting new data from the backend.
472+
* <p>
473+
* Updates can be either delta updates or full updates depending on how big the difference between
474+
* the last update was.
475+
* <p>
476+
* While updating, the database can still be queried for data, after the update completes calls to the database
477+
* return the updated data.
478+
*
479+
* @param callback A {@link UpdateCallback} that returns success when the operation is successfully completed.
480+
* Or error() in case a network error occurred. Can be null.
481+
* @param deltaUpdateOnly set to true if you want to only update when the update would be an delta update
482+
*/
483+
public void update(final UpdateCallback callback, boolean deltaUpdateOnly) {
454484
if(dbName == null){
455485
return;
456486
}
457487

458488
productDatabaseDownloader.invalidate();
459-
productDatabaseDownloader.loadAsync(new Downloader.Callback() {
489+
productDatabaseDownloader.update(new Downloader.Callback() {
460490
@Override
461491
protected void onDataLoaded(boolean wasStillValid) {
462492
updateLastUpdateTimestamp(System.currentTimeMillis());
@@ -482,9 +512,31 @@ protected void onError() {
482512
}
483513
}
484514
}
485-
});
515+
}, deltaUpdateOnly);
486516
}
487517

518+
/**
519+
* Cancels a database update, if one is currently running.
520+
*/
521+
public void cancelUpdate() {
522+
productDatabaseDownloader.cancel();
523+
}
524+
525+
/**
526+
* @return true if a database update is currently running, false otherwise.
527+
*/
528+
public boolean isUpdating() {
529+
return productDatabaseDownloader.isLoading();
530+
}
531+
532+
private boolean deleteDatabase(File dbFile) {
533+
if (dbFile.exists()) {
534+
return application.deleteDatabase(dbFile.getName());
535+
}
536+
return true;
537+
}
538+
539+
488540
/**
489541
* Closes and deletes the locally stored database and falls back to online only mode.
490542
*/
@@ -513,40 +565,6 @@ public long size() {
513565
return dbFile.length();
514566
}
515567

516-
/**
517-
* Updates the current database in the background by requesting new data from the backend.
518-
* <p>
519-
* Updates can be either delta updates or full updates depending on how big the difference between
520-
* the last update was.
521-
* <p>
522-
* While updating, the database can still be queried for data, after the update completes calls to the database
523-
* return the updated data.
524-
*/
525-
public void update() {
526-
update(null);
527-
}
528-
529-
/**
530-
* Cancels a database update, if one is currently running.
531-
*/
532-
public void cancelUpdate() {
533-
productDatabaseDownloader.cancel();
534-
}
535-
536-
/**
537-
* @return true if a database update is currently running, false otherwise.
538-
*/
539-
public boolean isUpdating() {
540-
return productDatabaseDownloader.isLoading();
541-
}
542-
543-
private boolean deleteDatabase(File dbFile) {
544-
if (dbFile.exists()) {
545-
return application.deleteDatabase(dbFile.getName());
546-
}
547-
return true;
548-
}
549-
550568
private void swap(File otherDbFile) throws IOException {
551569
boolean ok = true;
552570

core/src/main/java/io/snabble/sdk/ProductDatabaseDownloader.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class ProductDatabaseDownloader extends Downloader {
1414
private ProductDatabase productDatabase;
1515

1616
private boolean sameRevision;
17+
private boolean deltaUpdateOnly;
1718

1819
public ProductDatabaseDownloader(SnabbleSdk sdk,
1920
ProductDatabase productDatabase) {
@@ -23,6 +24,11 @@ public ProductDatabaseDownloader(SnabbleSdk sdk,
2324
this.productDatabase = productDatabase;
2425
}
2526

27+
public void update(Callback callback, boolean deltaUpdateOnly) {
28+
this.deltaUpdateOnly = deltaUpdateOnly;
29+
loadAsync(callback);
30+
}
31+
2632
@Override
2733
public void onStartDownload() {
2834
getHeaders().put("Accept", MIMETYPE_DELTA);
@@ -41,11 +47,7 @@ public void onStartDownload() {
4147

4248
@Override
4349
protected void onDownloadFailed(Response response) {
44-
if (response != null && response.code() == 304) {
45-
sameRevision = true;
46-
} else {
47-
sameRevision = false;
48-
}
50+
sameRevision = response != null && response.code() == 304;
4951
}
5052

5153
public boolean wasSameRevision() {
@@ -65,6 +67,11 @@ protected void onResponse(Response response) throws IOException {
6567
productDatabase.applyDeltaUpdate(body.byteStream());
6668
break;
6769
case MIMETYPE_FULL:
70+
if(deltaUpdateOnly){
71+
body.close();
72+
throw new IOException();
73+
}
74+
6875
productDatabase.applyFullUpdate(body.byteStream());
6976
break;
7077
default:

core/src/main/java/io/snabble/sdk/ShoppingCart.java

Lines changed: 6 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
public class ShoppingCart {
1717
public static final int MAX_QUANTITY = 99999;
18-
public static final long KEEP_ALIVE_TIME = TimeUnit.HOURS.toMillis(4);
18+
public static final long TIMEOUT = TimeUnit.HOURS.toMillis(4);
1919

2020
private static class Entry {
2121
private Product product;
@@ -54,7 +54,6 @@ public void setScannedCode(ScannableCode scannedCode){
5454
private long lastModificationTime;
5555
private List<Entry> items = new ArrayList<>();
5656
private transient List<ShoppingCartListener> listeners = new CopyOnWriteArrayList<>();
57-
private transient ProductDatabase productDatabase;
5857
private transient Handler handler = new Handler(Looper.getMainLooper());
5958
private transient SnabbleSdk sdkInstance;
6059

@@ -71,38 +70,7 @@ protected ShoppingCart() {
7170

7271
void initWithSdkInstance(SnabbleSdk sdkInstance) {
7372
this.sdkInstance = sdkInstance;
74-
75-
productDatabase = sdkInstance.getProductDatabase();
76-
productDatabase.addOnDatabaseUpdateListener(new ProductDatabase.OnDatabaseUpdateListener() {
77-
@Override
78-
public void onDatabaseUpdated() {
79-
updateEntries();
80-
}
81-
});
82-
83-
validate();
84-
updateEntries();
85-
}
86-
87-
private void updateEntries() {
88-
synchronized (lock) {
89-
List<Entry> removables = new ArrayList<>();
90-
91-
for (Entry e : items) {
92-
Product product = productDatabase.findBySku(e.sku);
93-
if (product != null) {
94-
// update the product when there is a local version available
95-
e.product = product;
96-
} else if (e.product == null) {
97-
// when there is no serialized product of an online only version
98-
// (in case of an upgrade of an older version of the shopping list)
99-
// we remove the product
100-
removables.add(e);
101-
}
102-
}
103-
104-
items.removeAll(removables);
105-
}
73+
checkForTimeout();
10674
}
10775

10876
public String getId() {
@@ -293,10 +261,11 @@ public void invalidate() {
293261
clear();
294262
}
295263

296-
public void validate() {
264+
public void checkForTimeout() {
297265
long currentTime = SystemClock.elapsedRealtime();
298-
if(lastModificationTime + KEEP_ALIVE_TIME < currentTime){
299-
clear();
266+
267+
if(lastModificationTime + TIMEOUT < currentTime){
268+
invalidate();
300269
}
301270
}
302271

core/src/main/java/io/snabble/sdk/SnabbleSdk.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -643,7 +643,7 @@ protected void onDataLoaded(boolean wasStillValid) {
643643
@Override
644644
public void onActivityStarted(Activity activity) {
645645
updateMetadata();
646-
getShoppingCart().validate();
646+
getShoppingCart().checkForTimeout();
647647
}
648648
};
649649

0 commit comments

Comments
 (0)