Skip to content

Commit 74f20e0

Browse files
authored
Merge pull request #18 from snabble/persistance_and_initialization
APPS-55 Persist used project + improve initialization and view behavior
2 parents 8388108 + 85d112d commit 74f20e0

File tree

97 files changed

+1824
-1926
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

97 files changed

+1824
-1926
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ buildscript {
1111
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
1212
classpath "org.jetbrains.dokka:dokka-gradle-plugin:$kotlin_version"
1313
classpath "org.jetbrains.dokka:dokka-base:$kotlin_version"
14-
classpath 'com.android.tools.build:gradle:7.0.4'
14+
classpath 'com.android.tools.build:gradle:7.1.1'
1515
classpath 'gradle.plugin.com.github.jlouns:gradle-cross-platform-exec-plugin:0.5.0'
1616
classpath 'gradle.plugin.gmazzo:sqlite-plugin:0.2'
1717
}

core/build.gradle

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ android {
1010
defaultConfig {
1111
minSdkVersion project.minSdkVersion
1212
targetSdkVersion project.targetSdkVersion
13-
versionCode project.versionCode
14-
versionName project.sdkVersion
1513
testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
1614
consumerProguardFiles 'proguard-rules.pro'
1715

@@ -59,17 +57,17 @@ dependencies {
5957
implementation 'org.apache.commons:commons-lang3:3.12.0'
6058
implementation 'org.iban4j:iban4j:3.2.1'
6159
implementation 'com.caverock:androidsvg-aar:1.4'
62-
implementation 'com.google.android.gms:play-services-wallet:18.1.3'
63-
implementation 'androidx.appcompat:appcompat:1.3.1'
64-
implementation 'androidx.biometric:biometric:1.2.0-alpha03'
60+
implementation 'com.google.android.gms:play-services-wallet:19.1.0'
61+
implementation 'androidx.appcompat:appcompat:1.4.1'
62+
implementation 'androidx.biometric:biometric:1.2.0-alpha04'
6563

6664
api "com.squareup.okhttp3:okhttp:${project.okhttpVersion}"
6765
implementation "com.squareup.okhttp3:logging-interceptor:${project.okhttpVersion}"
6866

69-
api 'com.google.code.gson:gson:2.8.8'
67+
api 'com.google.code.gson:gson:2.8.9'
7068

7169
testImplementation 'junit:junit:4.13.2'
72-
testImplementation 'org.robolectric:robolectric:4.6.1'
70+
testImplementation 'org.robolectric:robolectric:4.7.3'
7371
testImplementation "com.squareup.okhttp3:mockwebserver:${project.okhttpVersion}"
7472

7573
androidTestImplementation 'androidx.test:rules:1.4.0'

core/src/main/java/io/snabble/sdk/AcceptedLanguageInterceptor.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ class AcceptedLanguageInterceptor : Interceptor {
2828
override fun intercept(chain: Interceptor.Chain): Response {
2929
var request: Request = chain.request()
3030
val url = request.url.toString()
31-
if (url.startsWith(Snabble.getInstance().endpointBaseUrl)) {
31+
val baseUrl = Snabble.endpointBaseUrl
32+
if (url.startsWith(baseUrl)) {
3233
request = request.newBuilder()
3334
.addHeader("Accept-Language", acceptedLanguagesHeader)
3435
.build()
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
package io.snabble.sdk
2+
3+
import android.content.Context
4+
import io.snabble.sdk.utils.GsonHolder
5+
import org.apache.commons.io.IOUtils
6+
import java.io.File
7+
import java.io.FileInputStream
8+
import java.io.FileOutputStream
9+
import java.nio.charset.Charset
10+
import java.util.concurrent.TimeUnit
11+
import javax.net.ssl.SSLSocketFactory
12+
import javax.net.ssl.X509TrustManager
13+
14+
data class Config (
15+
/**
16+
* The endpoint url of the snabble backend. For example "https://api.snabble.io" for the Production environment.
17+
*/
18+
@JvmField
19+
var endpointBaseUrl: String = Environment.PRODUCTION.baseUrl,
20+
21+
/**
22+
* The project identifier, which is used in the communication with the backend.
23+
*/
24+
@JvmField
25+
var appId: String? = null,
26+
27+
/**
28+
* The secret needed for Totp token generation
29+
*/
30+
@JvmField
31+
var secret: String? = null,
32+
33+
/**
34+
* Relative path from the assets folder which points to a bundled file which contains the metadata
35+
*
36+
*
37+
* This file gets initially used to initialize the sdk before network requests are made,
38+
* or be able to use the sdk in the case of no network connection.
39+
*
40+
* Optional. If no file is specified every time the sdk is initialized we wait for a network response
41+
* from the backend.
42+
*
43+
* It is HIGHLY recommended to provide bundled metadata to allow the sdk to function
44+
* without having a network connection.
45+
*/
46+
@JvmField
47+
var bundledMetadataAssetPath: String? = null,
48+
49+
/**
50+
* Optional. Used to override the versionName appended to the metadata url.
51+
*
52+
* Defaults to the versionName in the app package.
53+
*
54+
* Must be in the format %d.%d
55+
*/
56+
@JvmField
57+
var versionName: String? = null,
58+
59+
/**
60+
* If set to true, creates an full text index to support searching in the product database
61+
* using findByName or searchByName.
62+
*
63+
* Note that this increases setup time of the ProductDatabase, and it may not be
64+
* immediately available offline.
65+
*/
66+
@JvmField
67+
var generateSearchIndex: Boolean = false,
68+
69+
/**
70+
* The time that the database is allowed to be out of date. After the specified time in
71+
* milliseconds the database only uses online requests for asynchronous requests.
72+
*
73+
* Successfully calling [ProductDatabase.update] resets the timer.
74+
*
75+
* The time is specified in milliseconds.
76+
*
77+
* The default value is 1 hour.
78+
*/
79+
@JvmField
80+
var maxProductDatabaseAge: Long = TimeUnit.HOURS.toMillis(1),
81+
82+
/**
83+
* The time that the shopping cart is allowed to be alive after the last modification.
84+
*
85+
* The time is specified in milliseconds.
86+
*
87+
* The default value is 4 hours.
88+
*/
89+
@JvmField
90+
var maxShoppingCartAge: Long = TimeUnit.HOURS.toMillis(4),
91+
92+
/** If set to true, disables certificate pinning */
93+
@JvmField
94+
var disableCertificatePinning: Boolean = false,
95+
96+
/** SQL queries that will get executed in order on the product database */
97+
@JvmField
98+
var initialSQL: List<String> = emptyList(),
99+
100+
/** Vibrate while adding a product to the cart, by default false */
101+
@JvmField
102+
var vibrateToConfirmCartFilled: Boolean = false,
103+
104+
/** Set to true, to load shops that are marked as pre launch
105+
* and are not part of the original metadata in the backend
106+
* (for example for testing shops in production before a go-live) */
107+
@JvmField
108+
var loadActiveShops: Boolean = false,
109+
110+
/**
111+
* The radius in which the CheckInManager tries to check in a shop.
112+
*
113+
* In meters.
114+
*/
115+
@JvmField
116+
var checkInRadius: Float = 500.0f,
117+
118+
/**
119+
* The radius in which the CheckInManager tries to stay in a shop, if already in it.
120+
* If outside of this radius and the lastSeenThreshold, you will be checked out.
121+
*/
122+
@JvmField
123+
var checkOutRadius: Float = 1000.0f,
124+
125+
/**
126+
* The time in milliseconds which we keep you checked in at a shop.
127+
*
128+
* The timer will be refreshed while you are still inside the shop
129+
* and only begins to run if you are not inside the checkOutRadius anymore.
130+
*/
131+
@JvmField
132+
var lastSeenThreshold: Long = TimeUnit.MINUTES.toMillis(15),
133+
)

core/src/main/java/io/snabble/sdk/Coupons.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ class Coupons (
9797
}
9898
}
9999
project.urls["coupons"]?.let { path ->
100-
val couponsUrl = Snabble.getInstance().absoluteUrl(path) ?: return
100+
val couponsUrl = Snabble.absoluteUrl(path)
101101
isLoading.setAsap(true)
102102

103103
val request = Request.Builder()
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package io.snabble.sdk
2+
3+
enum class InitializationState {
4+
INITIALIZING,
5+
INITIALIZED,
6+
ERROR,
7+
}

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ static OkHttpClient createOkHttpClient(Application application) {
4242
logging.setLevel(HttpLoggingInterceptor.Level.BASIC);
4343
builder.addInterceptor(logging);
4444

45-
Snabble.Config config = Snabble.getInstance().getConfig();
45+
Config config = Snabble.getInstance().getConfig();
4646
builder.addInterceptor(new UserAgentInterceptor(application));
4747

4848
if (!Snabble.getInstance().getConfig().disableCertificatePinning) {
@@ -56,11 +56,7 @@ static OkHttpClient createOkHttpClient(Application application) {
5656
builder.certificatePinner(certificatePinnerBuilder.build());
5757
}
5858

59-
if (config.sslSocketFactory != null && config.x509TrustManager != null) {
60-
builder.sslSocketFactory(config.sslSocketFactory, config.x509TrustManager);
61-
} else {
62-
LetsEncryptCertHelper.addLetsEncryptCertificatesForMarshmallowOrEarlier(builder);
63-
}
59+
LetsEncryptCertHelper.addLetsEncryptCertificatesForMarshmallowOrEarlier(builder);
6460

6561
return builder.build();
6662
}

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,9 @@ public class ProductDatabase {
7474
this.productApi = new ProductApi(project);
7575

7676
if (open()) {
77-
String[] initialSQL = Snabble.getInstance().getConfig().initialSQL;
78-
if (initialSQL != null) {
79-
for (String sql : initialSQL) {
80-
exec(sql);
81-
}
77+
List<String> initialSQL = Snabble.getInstance().getConfig().initialSQL;
78+
for (String sql : initialSQL) {
79+
exec(sql);
8280
}
8381
} else {
8482
Logger.i("Product database is missing. Offline products are not available.");

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ public class Project {
6262

6363
private RoundingMode roundingMode;
6464
private BarcodeFormat[] supportedBarcodeFormats;
65+
@Nullable
6566
private Shop checkedInShop;
6667
private CustomerCardInfo[] acceptedCustomerCardInfos;
6768
private CustomerCardInfo requiredCustomerCardInfo;
@@ -95,13 +96,10 @@ public class Project {
9596
.addInterceptor(new SnabbleAuthorizationInterceptor(this))
9697
.addInterceptor(new AcceptedLanguageInterceptor())
9798
.build();
98-
9999
parse(jsonObject);
100-
101100
internalStorageDirectory = new File(snabble.getInternalStorageDirectory(), id + "/");
102101

103102
boolean generateSearchIndex = snabble.getConfig().generateSearchIndex;
104-
105103
productDatabase = new ProductDatabase(this, id + ".sqlite3", generateSearchIndex);
106104
shoppingCartStorage = new ShoppingCartStorage(this);
107105
checkout = new Checkout(this);
@@ -320,7 +318,6 @@ void parse(JsonObject jsonObject) {
320318
coupons.setInternalProjectCoupons(couponList);
321319
}
322320
coupons.update();
323-
324321
notifyUpdate();
325322
}
326323

@@ -490,12 +487,17 @@ public String getText(String key, String defaultValue) {
490487
* Sets the shop used for receiving store specific prices and identification in the
491488
* payment process.
492489
*/
493-
public void setCheckedInShop(Shop checkedInShop) {
490+
public void setCheckedInShop(@Nullable Shop checkedInShop) {
494491
String currentShopId = this.checkedInShop != null ? this.checkedInShop.getId() : "";
495492
String newShopId = checkedInShop != null ? checkedInShop.getId() : "";
496493

497494
if (!currentShopId.equals(newShopId)) {
498495
this.checkedInShop = checkedInShop;
496+
if (newShopId.equals("")) {
497+
snabble.getUserPreferences().setLastCheckedInShopId(null);
498+
} else {
499+
snabble.getUserPreferences().setLastCheckedInShopId(newShopId);
500+
}
499501
events.updateShop(checkedInShop);
500502
getShoppingCart().updatePrices(false);
501503
}

0 commit comments

Comments
 (0)