Skip to content

Commit 327c42e

Browse files
committed
make product and shop parcelable
1 parent d9f7620 commit 327c42e

File tree

4 files changed

+165
-94
lines changed

4 files changed

+165
-94
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ the new class ProductResolver
1616
- Add support for online payment using SEPA
1717
- Add support for zebra hardware scanner devices
1818
- Add firebase barcode detector module
19+
- Product and Shop are now Parcelable
20+
- Added toShortString in Product and Shop
1921

2022
### Fixed
2123
- Properly cleanup okhttp connections on error responses

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

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,26 @@
11
package io.snabble.sdk;
22

3+
import android.os.Parcel;
4+
import android.os.Parcelable;
5+
6+
import com.google.gson.Gson;
7+
import com.google.gson.GsonBuilder;
8+
import com.google.gson.InstanceCreator;
39
import com.google.gson.annotations.SerializedName;
410

11+
import java.io.Serializable;
512
import java.math.BigDecimal;
613
import java.math.RoundingMode;
14+
import java.util.Arrays;
715
import java.util.HashMap;
816
import java.util.Map;
917

18+
import io.snabble.sdk.utils.GsonHolder;
19+
1020
/**
1121
* Class that holds all of the product information.
1222
*/
13-
public class Product {
23+
public class Product implements Serializable, Parcelable {
1424
public enum Type {
1525
/**
1626
* A basic product with price information.
@@ -107,6 +117,10 @@ public static SaleRestriction fromDatabaseField(long dbType, long value) {
107117
private Map<String, String> transmissionCodes;
108118
private boolean saleStop;
109119

120+
public Product() {
121+
122+
}
123+
110124
/**
111125
* @return The unique identifier of the product. Usually the same identifier
112126
* the retailer uses internally.
@@ -254,14 +268,68 @@ public int hashCode() {
254268
return sku.hashCode();
255269
}
256270

271+
public String toShortString() {
272+
return "Product{" +
273+
"sku='" + sku + '\'' +
274+
", name='" + name + '\'' +
275+
'}';
276+
}
277+
257278
@Override
258279
public String toString() {
259280
return "Product{" +
260281
"sku='" + sku + '\'' +
261282
", name='" + name + '\'' +
283+
", description='" + description + '\'' +
284+
", scannableCodes=" + Arrays.toString(scannableCodes) +
285+
", weighedItemIds=" + Arrays.toString(weighedItemIds) +
286+
", price=" + price +
287+
", discountedPrice=" + discountedPrice +
288+
", imageUrl='" + imageUrl + '\'' +
289+
", depositProduct=" + depositProduct +
290+
", bundleProducts=" + Arrays.toString(bundleProducts) +
291+
", type=" + type +
292+
", isDeposit=" + isDeposit +
293+
", boost=" + boost +
294+
", subtitle='" + subtitle + '\'' +
295+
", basePrice='" + basePrice + '\'' +
296+
", saleRestriction=" + saleRestriction +
297+
", transmissionCodes=" + transmissionCodes +
298+
", saleStop=" + saleStop +
262299
'}';
263300
}
264301

302+
@Override
303+
public void writeToParcel(Parcel dest, int flags) {
304+
dest.writeString(GsonHolder.get().toJson(this));
305+
}
306+
307+
protected Product(Parcel in) {
308+
InstanceCreator<Product> creator = new InstanceCreator<Product>() {
309+
public Product createInstance(java.lang.reflect.Type type) { return Product.this; }
310+
};
311+
312+
Gson gson = new GsonBuilder().registerTypeAdapter(Product.class, creator).create();
313+
gson.fromJson(in.readString(), Product.class);
314+
}
315+
316+
@Override
317+
public int describeContents() {
318+
return 0;
319+
}
320+
321+
public static final Creator<Product> CREATOR = new Creator<Product>() {
322+
@Override
323+
public Product createFromParcel(Parcel in) {
324+
return new Product(in);
325+
}
326+
327+
@Override
328+
public Product[] newArray(int size) {
329+
return new Product[size];
330+
}
331+
};
332+
265333
public static class Builder {
266334
private Product product = new Product();
267335

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

Lines changed: 90 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
11
package io.snabble.sdk;
22

3+
import android.os.Parcel;
4+
import android.os.Parcelable;
5+
6+
import com.google.gson.Gson;
7+
import com.google.gson.GsonBuilder;
8+
import com.google.gson.InstanceCreator;
39
import com.google.gson.JsonElement;
410
import com.google.gson.annotations.SerializedName;
511

612
import java.io.Serializable;
13+
import java.lang.reflect.Type;
14+
import java.util.Arrays;
715
import java.util.Map;
816

917
import io.snabble.sdk.utils.GsonHolder;
1018
import io.snabble.sdk.utils.Logger;
1119

12-
public class Shop implements Serializable {
20+
public class Shop implements Serializable, Parcelable {
1321
public enum Service {
1422
@SerializedName("euro")
1523
EURO_PAYMENT,
@@ -29,6 +37,13 @@ public enum Service {
2937

3038
public class Href {
3139
public String href;
40+
41+
@Override
42+
public String toString() {
43+
return "Href{" +
44+
"href='" + href + '\'' +
45+
'}';
46+
}
3247
}
3348

3449
public class OpeningHourSpecification {
@@ -47,6 +62,15 @@ public String getOpens() {
4762
public String getDayOfWeek() {
4863
return dayOfWeek;
4964
}
65+
66+
@Override
67+
public String toString() {
68+
return "OpeningHourSpecification{" +
69+
"closes='" + closes + '\'' +
70+
", opens='" + opens + '\'' +
71+
", dayOfWeek='" + dayOfWeek + '\'' +
72+
'}';
73+
}
5074
}
5175

5276
private String id;
@@ -68,6 +92,10 @@ public String getDayOfWeek() {
6892
private OpeningHourSpecification[] openingHoursSpecification;
6993
private JsonElement external;
7094

95+
public Shop() {
96+
97+
}
98+
7199
public String getId() {
72100
return id;
73101
}
@@ -137,107 +165,78 @@ static Shop[] fromJson(JsonElement json) {
137165
}
138166
}
139167

140-
public static class Builder {
141-
private String id;
142-
private String externalId;
143-
private String name;
144-
private Shop.Service[] services;
145-
private String street;
146-
private String zipCode;
147-
private String city;
148-
private String country;
149-
private String state;
150-
private String phone;
151-
private double latitude;
152-
private double longitude;
153-
private OpeningHourSpecification[] openingHoursSpecification;
154-
private JsonElement external;
155-
156-
public Builder id(String id) {
157-
this.id = id;
158-
return this;
159-
}
160-
161-
public Builder name(String name) {
162-
this.name = name;
163-
return this;
164-
}
165-
166-
public Builder services(Shop.Service[] services) {
167-
this.services = services;
168-
return this;
169-
}
168+
@Override
169+
public int hashCode() {
170+
return id.hashCode();
171+
}
170172

171-
public Builder street(String street) {
172-
this.street = street;
173-
return this;
174-
}
173+
@Override
174+
public boolean equals(Object o) {
175+
if (this == o) return true;
176+
if (o == null || getClass() != o.getClass()) return false;
175177

176-
public Builder zipCode(String zipCode) {
177-
this.zipCode = zipCode;
178-
return this;
179-
}
178+
Shop shop = (Shop) o;
180179

181-
public Builder city(String city) {
182-
this.city = city;
183-
return this;
184-
}
180+
return id.equals(shop.id);
181+
}
185182

186-
public Builder country(String country) {
187-
this.country = country;
188-
return this;
189-
}
183+
public String toShortString() {
184+
return "Shop{" +
185+
"id='" + id + '\'' +
186+
", name='" + name + '\'' +
187+
'}';
188+
}
190189

191-
public Builder state(String state) {
192-
this.state = state;
193-
return this;
194-
}
190+
@Override
191+
public String toString() {
192+
return "Shop{" +
193+
"id='" + id + '\'' +
194+
", externalId='" + externalId + '\'' +
195+
", name='" + name + '\'' +
196+
", services=" + Arrays.toString(services) +
197+
", street='" + street + '\'' +
198+
", zipCode='" + zipCode + '\'' +
199+
", city='" + city + '\'' +
200+
", country='" + country + '\'' +
201+
", state='" + state + '\'' +
202+
", phone='" + phone + '\'' +
203+
", links=" + links +
204+
", latitude=" + latitude +
205+
", longitude=" + longitude +
206+
", openingHoursSpecification=" + Arrays.toString(openingHoursSpecification) +
207+
", external=" + external +
208+
'}';
209+
}
195210

196-
public Builder phone(String phone) {
197-
this.phone = phone;
198-
return this;
199-
}
211+
@Override
212+
public int describeContents() {
213+
return 0;
214+
}
200215

201-
public Builder latLng(double latitude, double longitude) {
202-
this.latitude = latitude;
203-
this.longitude = longitude;
204-
return this;
205-
}
216+
@Override
217+
public void writeToParcel(Parcel dest, int flags) {
218+
String str = GsonHolder.get().toJson(this);
219+
dest.writeString(str);
220+
}
206221

207-
public Builder openingHours(OpeningHourSpecification[] openingHours) {
208-
this.openingHoursSpecification = openingHours;
209-
return this;
210-
}
222+
protected Shop(Parcel in) {
223+
InstanceCreator<Shop> creator = new InstanceCreator<Shop>() {
224+
public Shop createInstance(Type type) { return Shop.this; }
225+
};
211226

212-
public Builder externalId(String externalId) {
213-
this.externalId = externalId;
214-
return this;
215-
}
227+
Gson gson = new GsonBuilder().registerTypeAdapter(Shop.class, creator).create();
228+
gson.fromJson(in.readString(), Shop.class);
229+
}
216230

217-
public Builder external(JsonElement external) {
218-
this.external = external;
219-
return this;
231+
public static final Parcelable.Creator<Shop> CREATOR = new Parcelable.Creator<Shop>() {
232+
@Override
233+
public Shop createFromParcel(Parcel source) {
234+
return new Shop(source);
220235
}
221236

222-
public Shop create() {
223-
Shop shop = new Shop();
224-
225-
shop.id = id;
226-
shop.externalId = externalId;
227-
shop.name = name;
228-
shop.services = services;
229-
shop.street = street;
230-
shop.zipCode = zipCode;
231-
shop.city = city;
232-
shop.country = country;
233-
shop.state = state;
234-
shop.phone = phone;
235-
shop.latitude = latitude;
236-
shop.longitude = longitude;
237-
shop.openingHoursSpecification = openingHoursSpecification;
238-
shop.external = external;
239-
240-
return shop;
237+
@Override
238+
public Shop[] newArray(int size) {
239+
return new Shop[size];
241240
}
242-
}
241+
};
243242
}

sample/src/main/java/io/snabble/testapp/App.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,15 +84,17 @@ public void init(final InitCallback callback) {
8484
public void onReady() {
8585
project = snabble.getProjects().get(0);
8686

87-
// registers this sdk instance globally for use with ui components
87+
// registers this project globally for use with ui components
8888
SnabbleUI.useProject(project);
8989

9090
// select the first shop for demo purposes
9191
if (project.getShops().length > 0) {
9292
project.setCheckedInShop(project.getShops()[0]);
9393
}
9494

95-
//project.getProductDatabase().update();
95+
// you can update the local database asynchronously, you can still query
96+
// the database while this is running
97+
// project.getProductDatabase().update();
9698

9799
// optionally set a loyalty card id for identification, for demo purposes
98100
// we invent one here

0 commit comments

Comments
 (0)