Skip to content

Commit b893bab

Browse files
committed
add function to check if product database is up to date
1 parent 689fd7b commit b893bab

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

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

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.util.List;
2626
import java.util.Scanner;
2727
import java.util.concurrent.CopyOnWriteArrayList;
28+
import java.util.concurrent.TimeUnit;
2829

2930
import io.snabble.sdk.utils.Downloader;
3031
import io.snabble.sdk.utils.Logger;
@@ -754,6 +755,17 @@ public boolean isAvailableOffline() {
754755
return db != null;
755756
}
756757

758+
public boolean isUpToDate() {
759+
if(lastUpdateDate != null){
760+
long time = lastUpdateDate.getTime();
761+
long currentTime = new Date().getTime();
762+
long t = time + TimeUnit.HOURS.toMillis(1);
763+
return t > currentTime;
764+
}
765+
766+
return false;
767+
}
768+
757769
private Cursor productQuery(String appendSql, String[] args, boolean distinct) {
758770
return productQuery(appendSql, args, distinct, null);
759771
}
@@ -775,7 +787,7 @@ private Cursor rawQuery(String sql, String[] args, CancellationSignal cancellati
775787
}
776788
}
777789

778-
//query executes when we call the first function that needs data, not on db.rawQuery
790+
// query executes when we call the first function that needs data, not on db.rawQuery
779791
int count = cursor.getCount();
780792

781793
long time2 = SystemClock.elapsedRealtime() - time;
@@ -897,7 +909,7 @@ public void findBySkuOnline(String sku,
897909
return;
898910
}
899911

900-
if (onlineOnly) {
912+
if (onlineOnly || !isUpToDate()) {
901913
productApi.findBySku(sku, productAvailableListener);
902914
} else {
903915
Product local = findBySku(sku);
@@ -930,7 +942,7 @@ public void findByCodeOnline(String code,
930942
return;
931943
}
932944

933-
if (onlineOnly) {
945+
if (onlineOnly || !isUpToDate()) {
934946
productApi.findByCode(code, productAvailableListener);
935947
} else {
936948
Product local = findByCode(code);
@@ -1024,7 +1036,7 @@ public void findByWeighItemIdOnline(String weighItemId,
10241036
return;
10251037
}
10261038

1027-
if (onlineOnly) {
1039+
if (onlineOnly || !isUpToDate()) {
10281040
productApi.findByWeighItemId(weighItemId, productAvailableListener);
10291041
} else {
10301042
Product local = findByWeighItemId(weighItemId);

ui/src/main/java/io/snabble/sdk/ui/scanner/SelfScanningView.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ private void handleProductError() {
259259
private void onClickEnterBarcode() {
260260
SnabbleUICallback callback = SnabbleUI.getUiCallback();
261261
if (callback != null) {
262-
if(productDatabase.isAvailableOffline()){
262+
if(productDatabase.isAvailableOffline() && productDatabase.isUpToDate()){
263263
callback.showBarcodeSearch();
264264
} else {
265265
final EditText input = new EditText(getContext());

0 commit comments

Comments
 (0)