Panagiotis Triantafyllou

optimizations part3

......@@ -328,19 +328,15 @@ public class ContextualActivity extends Activity implements View.OnClickListener
private CallbackReceiver<JSONObject> mSubmitCallback = new CallbackReceiver<JSONObject>() {
@Override
public void onSuccess(JSONObject result) {
/* New Addition start */
WarplyManager.getUserCouponsWithCouponsetsInternal(mUserCouponsReceiver);
WarplyManager.getCampaignsInternal(mCampaignsCallback);
/* New Addition end */
int status = result.optInt("status", 2);
runOnUiThread(() -> {
if (status == 1) {
successActivatingDialog();
/* New Addition start */
WarplyManager.getUserCouponsWithCouponsetsInternal(mUserCouponsReceiver);
// WarplyManager.getCampaigns(mCampaignsCallback);
if (mLoyalty != null) {
WarplyManagerHelper.removeCampaignItem(mLoyalty);
OneTimeWorkRequest mywork = new OneTimeWorkRequest.Builder(EventCampaignService.class).build();
WorkManager.getInstance(ContextualActivity.this).enqueue(mywork);
}
/* New Addition end */
} else
errorActivatingDialog();
});
......@@ -367,16 +363,22 @@ public class ContextualActivity extends Activity implements View.OnClickListener
}
};
// private CallbackReceiver<ArrayList<Campaign>> mCampaignsCallback = new CallbackReceiver<ArrayList<Campaign>>() {
// @Override
// public void onSuccess(ArrayList<Campaign> result) {
// OneTimeWorkRequest mywork = new OneTimeWorkRequest.Builder(EventCampaignService.class).build();
// WorkManager.getInstance(ContextualActivity.this).enqueue(mywork);
// }
//
// @Override
// public void onFailure(int errorCode) {
//
// }
// };
private CallbackReceiver<ArrayList<Campaign>> mCampaignsCallback = new CallbackReceiver<ArrayList<Campaign>>() {
@Override
public void onSuccess(ArrayList<Campaign> result) {
if (mLoyalty != null) {
WarplyManagerHelper.removeCampaignItem(mLoyalty);
OneTimeWorkRequest mywork = new OneTimeWorkRequest.Builder(EventCampaignService.class).build();
WorkManager.getInstance(ContextualActivity.this).enqueue(mywork);
} else {
OneTimeWorkRequest mywork = new OneTimeWorkRequest.Builder(EventCampaignService.class).build();
WorkManager.getInstance(ContextualActivity.this).enqueue(mywork);
}
}
@Override
public void onFailure(int errorCode) {
}
};
}
......
......@@ -267,15 +267,13 @@ public class CouponsetInfoActivity extends Activity implements View.OnClickListe
// WarplyManager.getCampaigns(mCampaignsCallback);
WarplyManagerHelper.addCouponItem(mCoupon);
WarplyManagerHelper.removeCampaignItem(mLoyalty);
WarplyManager.getCampaignsInternal(mCampaignsCallback);
/* New Addition end */
/* New Addition start */
WarplyCouponsChangedEventModel couponsChanged = new WarplyCouponsChangedEventModel();
couponsChanged.setChanged(true);
EventBus.getDefault().post(new WarplyEventBusManager(couponsChanged));
OneTimeWorkRequest mywork = new OneTimeWorkRequest.Builder(EventCampaignService.class).build();
WorkManager.getInstance(CouponsetInfoActivity.this).enqueue(mywork);
/* New Addition end */
} else {
showDialog(false, status);
......@@ -409,17 +407,16 @@ public class CouponsetInfoActivity extends Activity implements View.OnClickListe
// }
// };
// private CallbackReceiver<ArrayList<Campaign>> mCampaignsCallback = new CallbackReceiver<ArrayList<Campaign>>() {
// @Override
// public void onSuccess(ArrayList<Campaign> result) {
// OneTimeWorkRequest mywork = new OneTimeWorkRequest.Builder(EventCampaignService.class).build();
// WorkManager.getInstance(CouponsetInfoActivity.this).enqueue(mywork);
// }
//
// @Override
// public void onFailure(int errorCode) {
//
// }
// };
private CallbackReceiver<ArrayList<Campaign>> mCampaignsCallback = new CallbackReceiver<ArrayList<Campaign>>() {
@Override
public void onSuccess(ArrayList<Campaign> result) {
OneTimeWorkRequest mywork = new OneTimeWorkRequest.Builder(EventCampaignService.class).build();
WorkManager.getInstance(CouponsetInfoActivity.this).enqueue(mywork);
}
@Override
public void onFailure(int errorCode) {
}
};
}
......
......@@ -1908,7 +1908,7 @@ public class WarplyManager {
WarpUtils.log("**************************************************");
ApiService service = ApiClient.getRetrofitInstance().create(ApiService.class);
ListeningExecutorService executorService = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(3));
ListeningExecutorService executorService = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(1));
SettableFuture<CouponList> futureUniversal = SettableFuture.create();
ListenableFuture<CouponList> futureCoupons = getCouponsUniversalRetro(service, 0, futureUniversal);
......@@ -2038,6 +2038,98 @@ public class WarplyManager {
// });
}
public static void getCampaignsInternal(final CallbackReceiver<ArrayList<Campaign>> receiver) {
WarpUtils.log("************* WARPLY Get Campaigns Request ********************");
WarpUtils.log("[WARP Trace] WARPLY Get Campaigns Request is active");
WarpUtils.log("**************************************************");
ApiService service = ApiClient.getRetrofitInstance().create(ApiService.class);
ListeningExecutorService executorService = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(1));
ListenableFuture<JSONObject> futureCampaignAvailability = getCampaignAvailability(service);
ListenableFuture<List<Object>> allResultsFuture = Futures.allAsList(futureCampaignAvailability);
ListenableFuture<ArrayList<Campaign>> mergedResultFuture = Futures.transformAsync(
allResultsFuture,
results -> {
JSONObject resultCampaignsAvailability = (JSONObject) results.get(0);
return executorService.submit(() -> mergeCampaignResultsInternal(resultCampaignsAvailability));
},
executorService
);
Futures.addCallback(mergedResultFuture, new FutureCallback<ArrayList<Campaign>>() {
@Override
public void onSuccess(ArrayList<Campaign> mergedResult) {
executorService.shutdownNow();
new Handler(Looper.getMainLooper()).post(() -> receiver.onSuccess(mergedResult));
}
@Override
public void onFailure(Throwable throwable) {
executorService.shutdownNow();
new Handler(Looper.getMainLooper()).post(() -> receiver.onFailure(2));
}
}, executorService);
}
private static ArrayList<Campaign> mergeCampaignResultsInternal(JSONObject resultCampaignsAvailability) {
ArrayList<Campaign> newCampaignList = new ArrayList<Campaign>();
ArrayList<Campaign> campaignLoyaltyList = new ArrayList<>();
newCampaignList.clear();
if (WarplyManagerHelper.getCampaignListAll() != null && !WarplyManagerHelper.getCampaignListAll().isEmpty())
newCampaignList.addAll(WarplyManagerHelper.getCampaignListAll());
if (newCampaignList != null && !newCampaignList.isEmpty()) {
Collections.sort(newCampaignList, (obj1, obj2) -> Integer.compare(obj1.getSorting(), obj2.getSorting()));
campaignLoyaltyList.clear();
ArrayList<Campaign> campaignCarouselList = new ArrayList<>();
campaignCarouselList.clear();
for (Campaign camp : newCampaignList) {
if (resultCampaignsAvailability != null && resultCampaignsAvailability.length() > 0) {
JSONObject campaignSettings = WarpJSONParser.getJSONFromString(camp.getCampaignTypeSettings());
if (campaignSettings != null) {
if (campaignSettings.has("couponset")) {
String cpnUuid = campaignSettings.optString("couponset");
if (resultCampaignsAvailability.has(cpnUuid)) {
camp.setCouponAvailability(resultCampaignsAvailability.optInt(cpnUuid));
}
}
}
}
JSONObject campMetadata = WarpJSONParser.getJSONFromString(camp.getExtraFields());
if (campMetadata != null) {
if (campMetadata.has("carousel")) {
if (camp.getCouponAvailability() == null || camp.getCouponAvailability() != 0)
campaignCarouselList.add(camp);
}
}
try {
JSONObject extraFields = WarpJSONParser.getJSONFromString(camp.getExtraFields());
if (extraFields != null) {
if (extraFields.length() == 0 || !(extraFields.has("ccms_offer") || extraFields.has("type"))) {
if (camp.getCouponAvailability() == null || camp.getCouponAvailability() != 0)
campaignLoyaltyList.add(camp);
}
}
} catch (Exception exception) {
if (camp.getCouponAvailability() == null || camp.getCouponAvailability() != 0)
campaignLoyaltyList.add(camp);
}
}
WarplyManagerHelper.setCampaignList(newCampaignList);
WarplyManagerHelper.setCarouselList(campaignCarouselList);
Set<Campaign> set = new LinkedHashSet<>(campaignLoyaltyList);
campaignLoyaltyList.clear();
campaignLoyaltyList.addAll(set);
}
return campaignLoyaltyList;
}
private static /*void*/ ListenableFuture<ArrayList<Campaign>> getCampaignsRetro(ApiService service/*, final CallbackReceiver<ArrayList<Campaign>> receiver*/) {
SettableFuture<ArrayList<Campaign>> future = SettableFuture.create();
......