Instantly share code, notes, and snippets.
Created
November 19, 2018 12:18
-
Star
(0)
0
You must be signed in to star a gist -
Fork
(0)
0
You must be signed in to fork a gist
-
Save cliffgr/15c387465555644920be4a84ba7bac05 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.wappier.wappierSDK.loyalty.ui.mainmenu; | |
import com.wappier.wappierSDK.data.NetworkManager; | |
import com.wappier.wappierSDK.utils.Constants; | |
import com.wappier.wappierSDK.Env; | |
import com.wappier.wappierSDK.utils.Utils; | |
import com.wappier.wappierSDK.Wappier; | |
import com.wappier.wappierSDK.json.JsonParser; | |
import com.wappier.wappierSDK.utils.Logger; | |
import com.wappier.wappierSDK.loyalty.Loyalty; | |
import com.wappier.wappierSDK.loyalty.base.exception.HowToWinPointsException; | |
import com.wappier.wappierSDK.loyalty.base.exception.SpendPointsException; | |
import com.wappier.wappierSDK.loyalty.base.ui.BasePresenter; | |
import com.wappier.wappierSDK.loyalty.common.interactors.NotificationInteractor; | |
import com.wappier.wappierSDK.loyalty.model.achievement.Achievement; | |
import com.wappier.wappierSDK.loyalty.model.base.Notifications; | |
import com.wappier.wappierSDK.loyalty.model.howtowin.EarnPoints; | |
import com.wappier.wappierSDK.loyalty.model.offer.Offer; | |
import com.wappier.wappierSDK.loyalty.model.spendpoints.SpendPoints; | |
import com.wappier.wappierSDK.loyalty.model.tip.Tip; | |
import com.wappier.wappierSDK.loyalty.ui.adapter.TacticsAdapter; | |
import com.wappier.wappierSDK.model.NetworkResponse; | |
import com.wappier.wappierSDK.data.network.networkrequest.ContentType; | |
import com.wappier.wappierSDK.data.network.networkrequest.OnNetworkRequestResponseListener; | |
import com.wappier.wappierSDK.data.network.networkrequest.RequestType; | |
import org.json.JSONArray; | |
import org.json.JSONException; | |
import org.json.JSONObject; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class MainMenuPresenter extends BasePresenter<MainMenuMVP.View> implements MainMenuMVP.Presenter, OnNetworkRequestResponseListener { | |
private List<Offer> listTactics; | |
private List<EarnPoints> listHowToWinPoints; | |
private SpendPoints spendPoints; | |
private List<Achievement> listAchievements; | |
private Loyalty loyalty; | |
private Tip tip; | |
private NotificationInteractor notificationInteractor; | |
private NetworkManager mNetworkManager; | |
public MainMenuPresenter(Loyalty loyalty, NotificationInteractor notificationInteractor, NetworkManager networkManager) { | |
this.loyalty = loyalty; | |
this.mNetworkManager = networkManager; | |
this.notificationInteractor = notificationInteractor; | |
listTactics = new ArrayList<>(); | |
listHowToWinPoints = new ArrayList<>(); | |
listAchievements = new ArrayList<>(); | |
} | |
private void updateLoyaltyPoints(long points) { | |
if (loyalty != null) { | |
loyalty.setCurrentPointsWithLatestRewardCalculator(points); | |
} | |
} | |
@Override | |
public List<EarnPoints> getListHowToWinPoints() throws HowToWinPointsException { | |
if (listHowToWinPoints.isEmpty()) | |
throw new HowToWinPointsException("How to Win Points is Empty"); | |
return listHowToWinPoints; | |
} | |
@Override | |
public SpendPoints getSpendPoints() throws SpendPointsException { | |
if (spendPoints == null) | |
throw new SpendPointsException("Spend Points are Empty"); | |
return spendPoints; | |
} | |
@Override | |
public Tip getTip() { | |
if (tip == null) | |
return null; | |
return tip; | |
} | |
@Override | |
public void onItemClick(int position) { | |
if (listTactics.isEmpty()) return; | |
Offer offer = listTactics.get(position); | |
switch (offer.getType()) { | |
case Constants.STEP: | |
if (getView() != null) | |
getView().showDailyBonus(offer.getId()); | |
break; | |
case Constants.ACHIEVEMENTS: | |
for (Achievement achievement : listAchievements) { | |
if (offer.getId().equals(achievement.getOfferId())) { | |
if (getView() != null) | |
getView().showAchievements(achievement); | |
break; | |
} | |
} | |
break; | |
case Constants.GIFTPACK: | |
if (getView() != null) | |
getView().showGifts(); | |
break; | |
} | |
} | |
@Override | |
public void makeRequestLoyalty() { | |
if (isViewAttached()) | |
getView().showProgressBar(); | |
mNetworkManager.requestLoyalty(this); | |
} | |
@Override | |
public void onSuccessResponse(NetworkResponse response) { | |
if (isViewAttached()) | |
getView().hideProgressBar(); | |
JSONObject jsonResponse; | |
try { | |
jsonResponse = new JSONObject(response.getResponse()); | |
Logger.d("Loyalty Response :" + jsonResponse.toString()); | |
if (jsonResponse.has("offers")) { | |
JSONArray jsonArrayOffers = jsonResponse.getJSONArray("offers"); | |
listTactics.clear(); | |
for (int i = 0; i < jsonArrayOffers.length(); i++) { | |
JSONObject object = jsonArrayOffers.getJSONObject(i); | |
Offer offer = new Offer(); | |
listTactics.add((Offer) JsonParser.parse(offer, String.valueOf(object))); | |
} | |
} | |
if (jsonResponse.has("howToWinPoints")) { | |
JSONArray jsonArrayHowToWin = jsonResponse.getJSONArray("howToWinPoints"); | |
listHowToWinPoints.clear(); | |
for (int i = 0; i < jsonArrayHowToWin.length(); i++) { | |
JSONObject object = jsonArrayHowToWin.getJSONObject(i); | |
EarnPoints earnPoint = new EarnPoints(); | |
listHowToWinPoints.add((EarnPoints) JsonParser.parse(earnPoint, String.valueOf(object))); | |
} | |
} | |
spendPoints = new SpendPoints(); | |
spendPoints = (SpendPoints) JsonParser.parse(spendPoints, String.valueOf(jsonResponse.getJSONObject("spendPoints"))); | |
if (jsonResponse.has("loyalty")) { | |
long currentPoints = jsonResponse.getJSONObject("loyalty").getJSONObject("points").getLong("current"); | |
updateLoyaltyPoints(currentPoints); | |
} | |
if (jsonResponse.has("tip")) { | |
tip = new Tip(); | |
tip = (Tip) JsonParser.parse(tip, String.valueOf(jsonResponse.getJSONObject("tip"))); | |
} | |
if (jsonResponse.has("achievements")) { | |
JSONArray achievementsList = jsonResponse.getJSONArray("achievements"); | |
listAchievements.clear(); | |
for (int i = 0; i < achievementsList.length(); i++) { | |
JSONObject object = achievementsList.getJSONObject(i); | |
Achievement achievement = new Achievement(); | |
listAchievements.add((Achievement) JsonParser.parse(achievement, String.valueOf(object))); | |
} | |
} | |
if (jsonResponse.has("notifications")) { | |
Notifications notifications = new Notifications(); | |
notifications = (Notifications) JsonParser.parse(notifications, String.valueOf(jsonResponse.getJSONObject("notifications"))); | |
loyalty.setNotifications(notifications); | |
} | |
if (isViewAttached()) { | |
if (spendPoints.getNotifications() != null) | |
getView().showSpendPointsNotification(spendPoints.getNotifications()); | |
getView().updateAdapter(); | |
getView().updateLatestReward(); | |
getView().startPointsAnimation(); | |
getView().hideProgressBar(); | |
notificationUpdate(listTactics); | |
} | |
} catch (JSONException e) { | |
e.printStackTrace(); | |
} | |
} | |
@Override | |
public void onErrorResponse(NetworkResponse errorResponse) { | |
if (isViewAttached()) { | |
getView().hideProgressBar(); | |
if (errorResponse.getCode() == 105) { | |
getView().showToast("No internet Connection"); | |
} | |
getView().showErrorDialog(errorResponse.getCode()); | |
} | |
} | |
@Override | |
public void cancelAllRequests() { | |
Wappier.getInstance().getNetworkRequest().cancelRequest(true); | |
} | |
private void notificationUpdate(List<Offer> listTactics) { | |
for (Offer offer : listTactics) { | |
if (offer.getNotifications().getEnabled() && !offer.getType().equals(Constants.STEP)) | |
notificationInteractor.addNotification(offer.getId()); | |
} | |
notificationInteractor.send(); | |
} | |
@Override | |
public void onBindRepositoryRowViewAtPosition(int position, TacticsAdapter.TacticsViewHolder rowView) { | |
try { | |
Offer offerItem = listTactics.get(position); | |
rowView.setCardViewColor(Utils.colorStringToInt(offerItem.getAssets().getBgColor())); | |
switch (offerItem.getType()) { | |
case Constants.ACHIEVEMENTS: | |
rowView.hideTitle(true); | |
rowView.setDescription(offerItem.getMetadata().getAchievementTitle()); | |
break; | |
case Constants.GIFTPACK: | |
rowView.hideTitle(true); | |
rowView.setDescription(offerItem.getAssets().getTitle().getEn()); | |
break; | |
default: | |
if (offerItem.getActiveNow()) { | |
rowView.setTitle("Active Now"); | |
} else { | |
rowView.setTitle("Available " + offerItem.getTimeLeft()); | |
} | |
rowView.setDescription(offerItem.getAssets().getDescription().getEn()); | |
break; | |
} | |
rowView.setImageTactic(Env.WAPPIER_IMAGES_URL + offerItem.getAssets().getBanner().getEn()); | |
rowView.setNotification(offerItem.getNotifications()); | |
} catch (IndexOutOfBoundsException ex) { | |
Logger.i("Offers are empty"); | |
} | |
} | |
@Override | |
public int getRepositoriesRowsCount() { | |
return listTactics.size(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment