Panagiotis Triantafyllou

optimizations part3

...@@ -328,19 +328,15 @@ public class ContextualActivity extends Activity implements View.OnClickListener ...@@ -328,19 +328,15 @@ public class ContextualActivity extends Activity implements View.OnClickListener
328 private CallbackReceiver<JSONObject> mSubmitCallback = new CallbackReceiver<JSONObject>() { 328 private CallbackReceiver<JSONObject> mSubmitCallback = new CallbackReceiver<JSONObject>() {
329 @Override 329 @Override
330 public void onSuccess(JSONObject result) { 330 public void onSuccess(JSONObject result) {
331 + /* New Addition start */
332 + WarplyManager.getUserCouponsWithCouponsetsInternal(mUserCouponsReceiver);
333 + WarplyManager.getCampaignsInternal(mCampaignsCallback);
334 + /* New Addition end */
335 +
331 int status = result.optInt("status", 2); 336 int status = result.optInt("status", 2);
332 runOnUiThread(() -> { 337 runOnUiThread(() -> {
333 if (status == 1) { 338 if (status == 1) {
334 successActivatingDialog(); 339 successActivatingDialog();
335 - /* New Addition start */
336 - WarplyManager.getUserCouponsWithCouponsetsInternal(mUserCouponsReceiver);
337 -// WarplyManager.getCampaigns(mCampaignsCallback);
338 - if (mLoyalty != null) {
339 - WarplyManagerHelper.removeCampaignItem(mLoyalty);
340 - OneTimeWorkRequest mywork = new OneTimeWorkRequest.Builder(EventCampaignService.class).build();
341 - WorkManager.getInstance(ContextualActivity.this).enqueue(mywork);
342 - }
343 - /* New Addition end */
344 } else 340 } else
345 errorActivatingDialog(); 341 errorActivatingDialog();
346 }); 342 });
...@@ -367,16 +363,22 @@ public class ContextualActivity extends Activity implements View.OnClickListener ...@@ -367,16 +363,22 @@ public class ContextualActivity extends Activity implements View.OnClickListener
367 } 363 }
368 }; 364 };
369 365
370 -// private CallbackReceiver<ArrayList<Campaign>> mCampaignsCallback = new CallbackReceiver<ArrayList<Campaign>>() { 366 + private CallbackReceiver<ArrayList<Campaign>> mCampaignsCallback = new CallbackReceiver<ArrayList<Campaign>>() {
371 -// @Override 367 + @Override
372 -// public void onSuccess(ArrayList<Campaign> result) { 368 + public void onSuccess(ArrayList<Campaign> result) {
373 -// OneTimeWorkRequest mywork = new OneTimeWorkRequest.Builder(EventCampaignService.class).build(); 369 + if (mLoyalty != null) {
374 -// WorkManager.getInstance(ContextualActivity.this).enqueue(mywork); 370 + WarplyManagerHelper.removeCampaignItem(mLoyalty);
375 -// } 371 + OneTimeWorkRequest mywork = new OneTimeWorkRequest.Builder(EventCampaignService.class).build();
376 -// 372 + WorkManager.getInstance(ContextualActivity.this).enqueue(mywork);
377 -// @Override 373 + } else {
378 -// public void onFailure(int errorCode) { 374 + OneTimeWorkRequest mywork = new OneTimeWorkRequest.Builder(EventCampaignService.class).build();
379 -// 375 + WorkManager.getInstance(ContextualActivity.this).enqueue(mywork);
380 -// } 376 + }
381 -// }; 377 + }
378 +
379 + @Override
380 + public void onFailure(int errorCode) {
381 +
382 + }
383 + };
382 } 384 }
......
...@@ -267,15 +267,13 @@ public class CouponsetInfoActivity extends Activity implements View.OnClickListe ...@@ -267,15 +267,13 @@ public class CouponsetInfoActivity extends Activity implements View.OnClickListe
267 // WarplyManager.getCampaigns(mCampaignsCallback); 267 // WarplyManager.getCampaigns(mCampaignsCallback);
268 WarplyManagerHelper.addCouponItem(mCoupon); 268 WarplyManagerHelper.addCouponItem(mCoupon);
269 WarplyManagerHelper.removeCampaignItem(mLoyalty); 269 WarplyManagerHelper.removeCampaignItem(mLoyalty);
270 + WarplyManager.getCampaignsInternal(mCampaignsCallback);
270 /* New Addition end */ 271 /* New Addition end */
271 272
272 /* New Addition start */ 273 /* New Addition start */
273 WarplyCouponsChangedEventModel couponsChanged = new WarplyCouponsChangedEventModel(); 274 WarplyCouponsChangedEventModel couponsChanged = new WarplyCouponsChangedEventModel();
274 couponsChanged.setChanged(true); 275 couponsChanged.setChanged(true);
275 EventBus.getDefault().post(new WarplyEventBusManager(couponsChanged)); 276 EventBus.getDefault().post(new WarplyEventBusManager(couponsChanged));
276 -
277 - OneTimeWorkRequest mywork = new OneTimeWorkRequest.Builder(EventCampaignService.class).build();
278 - WorkManager.getInstance(CouponsetInfoActivity.this).enqueue(mywork);
279 /* New Addition end */ 277 /* New Addition end */
280 } else { 278 } else {
281 showDialog(false, status); 279 showDialog(false, status);
...@@ -409,17 +407,16 @@ public class CouponsetInfoActivity extends Activity implements View.OnClickListe ...@@ -409,17 +407,16 @@ public class CouponsetInfoActivity extends Activity implements View.OnClickListe
409 // } 407 // }
410 // }; 408 // };
411 409
412 -// private CallbackReceiver<ArrayList<Campaign>> mCampaignsCallback = new CallbackReceiver<ArrayList<Campaign>>() { 410 + private CallbackReceiver<ArrayList<Campaign>> mCampaignsCallback = new CallbackReceiver<ArrayList<Campaign>>() {
413 -// @Override 411 + @Override
414 -// public void onSuccess(ArrayList<Campaign> result) { 412 + public void onSuccess(ArrayList<Campaign> result) {
415 -// OneTimeWorkRequest mywork = new OneTimeWorkRequest.Builder(EventCampaignService.class).build(); 413 + OneTimeWorkRequest mywork = new OneTimeWorkRequest.Builder(EventCampaignService.class).build();
416 -// WorkManager.getInstance(CouponsetInfoActivity.this).enqueue(mywork); 414 + WorkManager.getInstance(CouponsetInfoActivity.this).enqueue(mywork);
417 -// } 415 + }
418 -//
419 -// @Override
420 -// public void onFailure(int errorCode) {
421 -//
422 -// }
423 -// };
424 416
417 + @Override
418 + public void onFailure(int errorCode) {
419 +
420 + }
421 + };
425 } 422 }
......
...@@ -1908,7 +1908,7 @@ public class WarplyManager { ...@@ -1908,7 +1908,7 @@ public class WarplyManager {
1908 WarpUtils.log("**************************************************"); 1908 WarpUtils.log("**************************************************");
1909 1909
1910 ApiService service = ApiClient.getRetrofitInstance().create(ApiService.class); 1910 ApiService service = ApiClient.getRetrofitInstance().create(ApiService.class);
1911 - ListeningExecutorService executorService = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(3)); 1911 + ListeningExecutorService executorService = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(1));
1912 1912
1913 SettableFuture<CouponList> futureUniversal = SettableFuture.create(); 1913 SettableFuture<CouponList> futureUniversal = SettableFuture.create();
1914 ListenableFuture<CouponList> futureCoupons = getCouponsUniversalRetro(service, 0, futureUniversal); 1914 ListenableFuture<CouponList> futureCoupons = getCouponsUniversalRetro(service, 0, futureUniversal);
...@@ -2038,6 +2038,98 @@ public class WarplyManager { ...@@ -2038,6 +2038,98 @@ public class WarplyManager {
2038 // }); 2038 // });
2039 } 2039 }
2040 2040
2041 + public static void getCampaignsInternal(final CallbackReceiver<ArrayList<Campaign>> receiver) {
2042 + WarpUtils.log("************* WARPLY Get Campaigns Request ********************");
2043 + WarpUtils.log("[WARP Trace] WARPLY Get Campaigns Request is active");
2044 + WarpUtils.log("**************************************************");
2045 +
2046 + ApiService service = ApiClient.getRetrofitInstance().create(ApiService.class);
2047 + ListeningExecutorService executorService = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(1));
2048 +
2049 + ListenableFuture<JSONObject> futureCampaignAvailability = getCampaignAvailability(service);
2050 +
2051 + ListenableFuture<List<Object>> allResultsFuture = Futures.allAsList(futureCampaignAvailability);
2052 + ListenableFuture<ArrayList<Campaign>> mergedResultFuture = Futures.transformAsync(
2053 + allResultsFuture,
2054 + results -> {
2055 + JSONObject resultCampaignsAvailability = (JSONObject) results.get(0);
2056 + return executorService.submit(() -> mergeCampaignResultsInternal(resultCampaignsAvailability));
2057 + },
2058 + executorService
2059 + );
2060 +
2061 + Futures.addCallback(mergedResultFuture, new FutureCallback<ArrayList<Campaign>>() {
2062 + @Override
2063 + public void onSuccess(ArrayList<Campaign> mergedResult) {
2064 + executorService.shutdownNow();
2065 + new Handler(Looper.getMainLooper()).post(() -> receiver.onSuccess(mergedResult));
2066 + }
2067 +
2068 + @Override
2069 + public void onFailure(Throwable throwable) {
2070 + executorService.shutdownNow();
2071 + new Handler(Looper.getMainLooper()).post(() -> receiver.onFailure(2));
2072 + }
2073 + }, executorService);
2074 + }
2075 +
2076 + private static ArrayList<Campaign> mergeCampaignResultsInternal(JSONObject resultCampaignsAvailability) {
2077 + ArrayList<Campaign> newCampaignList = new ArrayList<Campaign>();
2078 + ArrayList<Campaign> campaignLoyaltyList = new ArrayList<>();
2079 + newCampaignList.clear();
2080 + if (WarplyManagerHelper.getCampaignListAll() != null && !WarplyManagerHelper.getCampaignListAll().isEmpty())
2081 + newCampaignList.addAll(WarplyManagerHelper.getCampaignListAll());
2082 +
2083 + if (newCampaignList != null && !newCampaignList.isEmpty()) {
2084 + Collections.sort(newCampaignList, (obj1, obj2) -> Integer.compare(obj1.getSorting(), obj2.getSorting()));
2085 + campaignLoyaltyList.clear();
2086 + ArrayList<Campaign> campaignCarouselList = new ArrayList<>();
2087 + campaignCarouselList.clear();
2088 + for (Campaign camp : newCampaignList) {
2089 + if (resultCampaignsAvailability != null && resultCampaignsAvailability.length() > 0) {
2090 + JSONObject campaignSettings = WarpJSONParser.getJSONFromString(camp.getCampaignTypeSettings());
2091 + if (campaignSettings != null) {
2092 + if (campaignSettings.has("couponset")) {
2093 + String cpnUuid = campaignSettings.optString("couponset");
2094 + if (resultCampaignsAvailability.has(cpnUuid)) {
2095 + camp.setCouponAvailability(resultCampaignsAvailability.optInt(cpnUuid));
2096 + }
2097 + }
2098 + }
2099 + }
2100 +
2101 + JSONObject campMetadata = WarpJSONParser.getJSONFromString(camp.getExtraFields());
2102 + if (campMetadata != null) {
2103 + if (campMetadata.has("carousel")) {
2104 + if (camp.getCouponAvailability() == null || camp.getCouponAvailability() != 0)
2105 + campaignCarouselList.add(camp);
2106 + }
2107 + }
2108 +
2109 + try {
2110 + JSONObject extraFields = WarpJSONParser.getJSONFromString(camp.getExtraFields());
2111 + if (extraFields != null) {
2112 + if (extraFields.length() == 0 || !(extraFields.has("ccms_offer") || extraFields.has("type"))) {
2113 + if (camp.getCouponAvailability() == null || camp.getCouponAvailability() != 0)
2114 + campaignLoyaltyList.add(camp);
2115 + }
2116 + }
2117 + } catch (Exception exception) {
2118 + if (camp.getCouponAvailability() == null || camp.getCouponAvailability() != 0)
2119 + campaignLoyaltyList.add(camp);
2120 + }
2121 + }
2122 + WarplyManagerHelper.setCampaignList(newCampaignList);
2123 + WarplyManagerHelper.setCarouselList(campaignCarouselList);
2124 +
2125 + Set<Campaign> set = new LinkedHashSet<>(campaignLoyaltyList);
2126 + campaignLoyaltyList.clear();
2127 + campaignLoyaltyList.addAll(set);
2128 + }
2129 +
2130 + return campaignLoyaltyList;
2131 + }
2132 +
2041 private static /*void*/ ListenableFuture<ArrayList<Campaign>> getCampaignsRetro(ApiService service/*, final CallbackReceiver<ArrayList<Campaign>> receiver*/) { 2133 private static /*void*/ ListenableFuture<ArrayList<Campaign>> getCampaignsRetro(ApiService service/*, final CallbackReceiver<ArrayList<Campaign>> receiver*/) {
2042 SettableFuture<ArrayList<Campaign>> future = SettableFuture.create(); 2134 SettableFuture<ArrayList<Campaign>> future = SettableFuture.create();
2043 2135
......