Panagiotis Triantafyllou

new coupons part2

......@@ -50,8 +50,8 @@ public class BaseFragmentActivity extends FragmentActivity implements Navigation
mBottomNavigationView = findViewById(R.id.bt_tabs);
WarplyManager.getUserCouponsWithCouponsets(mUserCouponsReceiver);
// WarplyManager.getCampaigns(mCampaignsCallback);
// WarplyManager.getUnifiedCouponsDeals(mUnifiedCallback);
WarplyManager.getCampaigns(mCampaignsCallback);
WarplyManager.getUnifiedCouponsDeals(mUnifiedCallback);
}
@Override
......
......@@ -96,6 +96,7 @@ public class Coupon implements Parcelable, Serializable {
private String short_description = "";
private String terms = "";
private Couponset couponsetDetails = new Couponset(true);
private Merchant merchantDetails = new Merchant(true);
public Coupon() {
this.barcode = "";
......@@ -133,6 +134,7 @@ public class Coupon implements Parcelable, Serializable {
this.merchantUuid = "";
this.redeemDate = new Date();
this.couponsetDetails = new Couponset(isUniversal);
this.merchantDetails = new Merchant(isUniversal);
}
/**
......@@ -218,6 +220,10 @@ public class Coupon implements Parcelable, Serializable {
if (tempCouponsetDetails != null) {
this.couponsetDetails = new Couponset(tempCouponsetDetails, isUniversal);
}
JSONObject tempMerchantDetails = json.optJSONObject("merchant_details");
if (tempMerchantDetails != null) {
this.merchantDetails = new Merchant(tempMerchantDetails, isUniversal);
}
// this.category = json.optString(CATEGORY);
// this.created = json.optString(CREATED);
......@@ -522,6 +528,22 @@ public class Coupon implements Parcelable, Serializable {
this.terms = terms;
}
public Couponset getCouponsetDetails() {
return couponsetDetails;
}
public void setCouponsetDetails(Couponset couponsetDetails) {
this.couponsetDetails = couponsetDetails;
}
public Merchant getMerchantDetails() {
return merchantDetails;
}
public void setMerchantDetails(Merchant merchantDetails) {
this.merchantDetails = merchantDetails;
}
@Override
public int describeContents() {
return 0;
......
......@@ -180,6 +180,19 @@ public class Merchant implements Parcelable, Serializable, ClusterItem {
coordinates = new LatLng(this.latitude, this.longitude);
}
public Merchant(boolean isUniversal) {
this.address = "";
this.city = "";
this.body = "";
this.logo = "";
this.country = "";
this.name = "";
this.postal_code = "";
this.region = "";
this.telephone = "";
this.merchant_metadata = new JSONObject();
}
/**
* Basic constructor used to create an object from a String, representing a
* JSON Object
......@@ -242,6 +255,53 @@ public class Merchant implements Parcelable, Serializable, ClusterItem {
}
}
public Merchant(JSONObject json, boolean isUniversal) {
if (json != null) {
this.address = json.isNull(ADDRESS) ? "" : json.optString(ADDRESS);
this.body = json.optString(BODY);
this.city = json.optString(CITY);
this.logo = json.optString(LOGO);
this.country = json.optString(COUNTRY);
this.name = json.isNull(NAME) ? "" : json.optString(NAME);
this.postal_code = json.optString(POSTAL_CODE);
this.region = json.optString(REGION);
this.telephone = json.isNull(TELEPHONE) ? "" : json.optString(TELEPHONE);
this.merchant_metadata = json.optJSONObject(MERCHANT_METADATA);
// this.id = json.optString(ID);
// this.store_id = json.optString(STORE_ID);
// this.website = json.optString(WEBSITE);
// this.email = json.optString(EMAIL);
// this.category = json.optString(CATEGORY);
// this.description = json.optString(DESCRIPTION);
// this.short_description = json.optString(SHORT_DESCRIPTION);
// this.latitude = json.isNull(LAT) ? 0.0 : json.optDouble(LAT);
// this.longitude = json.isNull(LON) ? 0.0 : json.optDouble(LON);
// this.image = json.optString(IMAGE);
// this.extra_fields = json.optJSONObject(EXTRA_FIELDS);
// this.active = json.optBoolean(ACTIVE);
// this.vat = json.optString(VAT);
// this.uuid = json.optString(UUID);
// this.category_uuid = json.optString(CATEGORY_UUID);
// this.created = json.optString(CREATED);
// this.parent = json.optString(PARENT);
// this.img = json.optJSONArray(IMG);
// this.img_preview = json.optString(IMG_PREVIEW);
// this.admin_name = json.optString(ADMIN_NAME);
// this.sorting = json.optInt(SORTING);
// this.owner_name = json.optString(OWNER_NAME);
// this.preview = json.optString(PREVIEW);
// this.promoted = json.optBoolean(PROMOTED);
// this.distance = json.optInt(DISTANCE);
// this.default_shown = json.optBoolean(DEFAULT_SHOWN);
// this.hidden = json.optBoolean(HIDDEN);
// this.working_hours = json.optJSONObject(WORKING_HOURS);
// this.tags = json.optJSONArray(TAGS);
// this.product = json.optJSONObject(PRODUCT);
// coordinates = new LatLng(this.latitude, this.longitude);
}
}
public Merchant(Parcel source) {
this.address = source.readString();
this.id = source.readString();
......
......@@ -1570,222 +1570,28 @@ public class WarplyManager {
ListenableFuture<CouponsetsList> futureCouponsets = getCouponsetsRetro(service);
ListenableFuture<CouponList> futureCoupons = getCouponsUniversalRetro(service);
// getMerchantsRetro(service, new Callback<ResponseBody>() {
// @Override
// public void onResponse(@NonNull Call<ResponseBody> call, @NonNull Response<ResponseBody> responseMerchants) {
// if (responseMerchants.code() == 200 && responseMerchants.body() != null) {
// JSONObject jobjMerchantsResponse = null;
// try {
// jobjMerchantsResponse = new JSONObject(responseMerchants.body().string());
// } catch (Exception e) {
// e.printStackTrace();
// }
//
// if (jobjMerchantsResponse != null && jobjMerchantsResponse.has("status") && jobjMerchantsResponse.optString("status", "2").equals("1")) {
// LoyaltySDKDynatraceEventModel dynatraceEvent = new LoyaltySDKDynatraceEventModel();
// dynatraceEvent.setEventName("custom_success_shops_loyalty");
// EventBus.getDefault().post(new WarplyEventBusManager(dynatraceEvent));
//
// JSONArray jMerchantsBody = null;
// try {
// jMerchantsBody = jobjMerchantsResponse.optJSONObject("context").optJSONObject("MAPP_SHOPS").optJSONArray("result");
// } catch (Exception e) {
// e.printStackTrace();
// }
//
// if (jMerchantsBody != null) {
// MerchantList mMerchantList = new MerchantList();
//
// final ExecutorService executorShops = Executors.newFixedThreadPool(1);
// JSONArray finalMerchantsJBody = jMerchantsBody;
// executorShops.submit(() -> {
// for (int i = 0; i < finalMerchantsJBody.length(); ++i) {
// mMerchantList.add(new Merchant(finalMerchantsJBody.optJSONObject(i)));
// }
// WarplyManagerHelper.setMerchantList(mMerchantList);
// executorShops.shutdownNow();
// });
//
// getCouponsetsRetro(service, new Callback<ResponseBody>() {
// @Override
// public void onResponse(@NonNull Call<ResponseBody> call, @NonNull Response<ResponseBody> responseCouponsets) {
// if (responseCouponsets.code() == 200 && responseCouponsets.body() != null) {
// JSONObject jobjCouponsetsResponse = null;
// try {
// jobjCouponsetsResponse = new JSONObject(responseCouponsets.body().string());
// } catch (Exception e) {
// e.printStackTrace();
// }
//
// if (jobjCouponsetsResponse != null && jobjCouponsetsResponse.has("status") && jobjCouponsetsResponse.optString("status", "2").equals("1")) {
// LoyaltySDKDynatraceEventModel dynatraceEvent = new LoyaltySDKDynatraceEventModel();
// dynatraceEvent.setEventName("custom_success_couponsets_loyalty");
// EventBus.getDefault().post(new WarplyEventBusManager(dynatraceEvent));
//
// JSONObject finalJobjCouponsetsResponse = jobjCouponsetsResponse;
// getUserCouponsRetro(service, new Callback<ResponseBody>() {
// @Override
// public void onResponse(@NonNull Call<ResponseBody> call, @NonNull Response<ResponseBody> responseCoupons) {
// if (responseCoupons.code() == 200 && responseCoupons.body() != null) {
// JSONObject jobjCouponsResponse = null;
// try {
// jobjCouponsResponse = new JSONObject(responseCoupons.body().string());
// } catch (Exception e) {
// e.printStackTrace();
// }
//
// if (jobjCouponsResponse != null && jobjCouponsResponse.has("status") && jobjCouponsResponse.optInt("status", 2) == 1) {
// LoyaltySDKDynatraceEventModel dynatraceEvent = new LoyaltySDKDynatraceEventModel();
// dynatraceEvent.setEventName("custom_success_user_coupons_loyalty");
// EventBus.getDefault().post(new WarplyEventBusManager(dynatraceEvent));
//
// JSONObject finalJobjCouponsResponse = jobjCouponsResponse;
// final ExecutorService executor = Executors.newFixedThreadPool(1);
// executor.submit(() -> {
// // COUPONS START //
// JSONArray jCouponsBody = null;
// try {
// jCouponsBody = finalJobjCouponsResponse.optJSONArray("result");
// } catch (Exception e) {
// e.printStackTrace();
// }
// // COUPONS END //
//
// // COUPONSETS START //
// JSONArray jCouponsetsBody = null;
// try {
// jCouponsetsBody = finalJobjCouponsetsResponse.optJSONObject("context").optJSONArray("MAPP_COUPON");
// } catch (Exception e) {
// e.printStackTrace();
// }
// // COUPONSETS END //
//
// if (jCouponsetsBody != null && jCouponsBody != null) {
// CouponList mCouponList = new CouponList();
// CouponList mCouponRedeemedList = new CouponList();
// CouponsetsList mCouponsetList = new CouponsetsList();
// for (int i = 0; i < jCouponsetsBody.length(); ++i) {
// Couponset tempCouponset = new Couponset(jCouponsetsBody.optJSONObject(i));
// mCouponsetList.add(tempCouponset);
// for (int j = 0; j < jCouponsBody.length(); ++j) {
// Coupon tempCoupon = new Coupon(jCouponsBody.optJSONObject(j));
// if (tempCoupon.getCouponsetUuid().equals(tempCouponset.getUuid())) {
// tempCoupon.setDescription(tempCouponset.getShortDescription());
// tempCoupon.setImage(tempCouponset.getImgPreview());
// tempCoupon.setName(tempCouponset.getName());
// tempCoupon.setMerchantUuid(tempCouponset.getMerchantUuid());
// tempCoupon.setInnerText(tempCouponset.getInnerText());
// tempCoupon.setDiscount_type(tempCouponset.getDiscount_type());
// tempCoupon.setFinal_price(tempCouponset.getFinal_price());
// mCouponList.add(tempCoupon);
// }
// }
// }
//
// for (int j = 0; j < jCouponsBody.length(); ++j) {
// Coupon tempCoupon = new Coupon(jCouponsBody.optJSONObject(j));
// if (tempCoupon.getStatus() == 0) {
// mCouponRedeemedList.add(tempCoupon);
// }
// }
// WarplyManagerHelper.setCouponsets(mCouponsetList);
// WarplyManagerHelper.setCouponList(mCouponList);
// WarplyManagerHelper.setCouponRedeemedList(mCouponRedeemedList);
//
// CouponList mActiveCouponList = new CouponList();
// for (Coupon coupon : mCouponList) {
// if (coupon.getStatus() == 1) {
// SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm");
// Date newDate = new Date();
// try {
// newDate = simpleDateFormat.parse(coupon.getExpiration());
// } catch (
// ParseException e) {
// e.printStackTrace();
// }
// coupon.setExpirationDate(newDate);
// mActiveCouponList.add(coupon);
// }
// }
//
// Collections.sort(mActiveCouponList, (coupon1, coupon2) -> coupon1.getExpirationDate().compareTo(coupon2.getExpirationDate()));
//
// new Handler(Looper.getMainLooper()).post(() -> receiver.onSuccess(mActiveCouponList));
// executor.shutdownNow();
// }
// });
// } else {
// LoyaltySDKDynatraceEventModel dynatraceEvent = new LoyaltySDKDynatraceEventModel();
// dynatraceEvent.setEventName("custom_error_user_coupons_loyalty");
// EventBus.getDefault().post(new WarplyEventBusManager(dynatraceEvent));
// receiver.onFailure(2);
// }
// } else {
// LoyaltySDKDynatraceEventModel dynatraceEvent = new LoyaltySDKDynatraceEventModel();
// dynatraceEvent.setEventName("custom_error_user_coupons_loyalty");
// EventBus.getDefault().post(new WarplyEventBusManager(dynatraceEvent));
// receiver.onFailure(responseCoupons.code());
// }
// }
//
// @Override
// public void onFailure(@NonNull Call<ResponseBody> call, @NonNull Throwable t) {
// LoyaltySDKDynatraceEventModel dynatraceEvent = new LoyaltySDKDynatraceEventModel();
// dynatraceEvent.setEventName("custom_error_user_coupons_loyalty");
// EventBus.getDefault().post(new WarplyEventBusManager(dynatraceEvent));
// receiver.onFailure(2);
// }
// });
//
// } else {
// LoyaltySDKDynatraceEventModel dynatraceEvent = new LoyaltySDKDynatraceEventModel();
// dynatraceEvent.setEventName("custom_error_couponsets_loyalty");
// EventBus.getDefault().post(new WarplyEventBusManager(dynatraceEvent));
// receiver.onFailure(2);
// }
//
// } else {
// LoyaltySDKDynatraceEventModel dynatraceEvent = new LoyaltySDKDynatraceEventModel();
// dynatraceEvent.setEventName("custom_error_couponsets_loyalty");
// EventBus.getDefault().post(new WarplyEventBusManager(dynatraceEvent));
// receiver.onFailure(responseCouponsets.code());
// }
// }
//
// @Override
// public void onFailure(@NonNull Call<ResponseBody> call, @NonNull Throwable t) {
// LoyaltySDKDynatraceEventModel dynatraceEvent = new LoyaltySDKDynatraceEventModel();
// dynatraceEvent.setEventName("custom_error_couponsets_loyalty");
// EventBus.getDefault().post(new WarplyEventBusManager(dynatraceEvent));
// receiver.onFailure(2);
// }
// });
// }
// } else {
// LoyaltySDKDynatraceEventModel dynatraceEvent = new LoyaltySDKDynatraceEventModel();
// dynatraceEvent.setEventName("custom_error_shops_loyalty");
// EventBus.getDefault().post(new WarplyEventBusManager(dynatraceEvent));
//
// receiver.onFailure(2);
// }
// } else {
// LoyaltySDKDynatraceEventModel dynatraceEvent = new LoyaltySDKDynatraceEventModel();
// dynatraceEvent.setEventName("custom_error_shops_loyalty");
// EventBus.getDefault().post(new WarplyEventBusManager(dynatraceEvent));
//
// receiver.onFailure(responseMerchants.code());
// }
// }
//
// @Override
// public void onFailure(@NonNull Call<ResponseBody> call, @NonNull Throwable t) {
// LoyaltySDKDynatraceEventModel dynatraceEvent = new LoyaltySDKDynatraceEventModel();
// dynatraceEvent.setEventName("custom_error_shops_loyalty");
// EventBus.getDefault().post(new WarplyEventBusManager(dynatraceEvent));
//
// receiver.onFailure(2);
// }
// });
ListenableFuture<List<Object>> allResultsFuture = Futures.allAsList(futureMerchants, futureCouponsets, futureCoupons);
ListenableFuture<CouponList> mergedResultFuture = Futures.transformAsync(
allResultsFuture,
results -> {
CouponList resultCoupons = (CouponList) results.get(2);
return executorService.submit(() -> resultCoupons);
},
executorService
);
Futures.addCallback(mergedResultFuture, new FutureCallback<CouponList>() {
@Override
public void onSuccess(CouponList mergedResult) {
executorService.shutdownNow();
new Handler(Looper.getMainLooper()).post(() -> receiver.onSuccess(mergedResult));
}
@Override
public void onFailure(Throwable throwable) {
receiver.onFailure(2);
}
}, executorService);
}
public static void getCampaigns(final CallbackReceiver<ArrayList<Campaign>> receiver) {
......@@ -4538,19 +4344,40 @@ public class WarplyManager {
if (jCouponsBody != null) {
CouponList mCouponsList = new CouponList();
//
CouponList mCouponRedeemedList = new CouponList();
CouponList mActiveCouponList = new CouponList();
final ExecutorService executorCoupons = Executors.newFixedThreadPool(1);
JSONArray finalJCouponsBody = jCouponsBody;
executorCoupons.submit(() -> {
for (int i = 0; i < finalJCouponsBody.length(); ++i) {
Coupon tempCoupon = new Coupon(finalJCouponsBody.optJSONObject(i), true);
mCouponsList.add(tempCoupon);
if (tempCoupon.getStatus() == 0) {
mCouponRedeemedList.add(tempCoupon);
} else if (tempCoupon.getStatus() == 1) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm");
Date newDate = new Date();
try {
newDate = simpleDateFormat.parse(tempCoupon.getExpiration());
} catch (
ParseException e) {
e.printStackTrace();
}
tempCoupon.setExpirationDate(newDate);
mActiveCouponList.add(tempCoupon);
}
}
// WarplyManagerHelper.setCouponsets(mCouponsetList);
WarplyManagerHelper.setCouponList(mCouponsList);
WarplyManagerHelper.setCouponRedeemedList(mCouponRedeemedList);
Collections.sort(mActiveCouponList, (coupon1, coupon2) -> coupon1.getExpirationDate().compareTo(coupon2.getExpirationDate()));
executorCoupons.shutdownNow();
future.set(mCouponsList);
future.set(mActiveCouponList);
});
}
} else {
......@@ -4581,8 +4408,6 @@ public class WarplyManager {
EventBus.getDefault().post(new WarplyEventBusManager(dynatraceEvent));
future.set(new CouponList());
}
future.set(new CouponList());
}
@Override
......