use of com.meniga.sdk.models.challenges.MenigaChallenge in project mobile-sdk-android by meniga.
the class MenigaChallengesConverter method responseBodyConverter.
@Override
public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) {
Type typeOfChallenge = new TypeToken<MenigaChallenge>() {
}.getType();
Type typeOfChallenges = new TypeToken<List<MenigaChallenge>>() {
}.getType();
if (typeOfChallenge.equals(type)) {
return new Converter<ResponseBody, MenigaChallenge>() {
@Override
public MenigaChallenge convert(ResponseBody resBody) throws IOException {
return factory.getMenigaChallengeItem(getAsObject(resBody.byteStream()));
}
};
} else if (typeOfChallenges.equals(type)) {
return new Converter<ResponseBody, Object>() {
@Override
public Object convert(ResponseBody resBody) throws IOException {
JsonArray arr = getAsArray(resBody.byteStream());
List<MenigaChallenge> challenges = new ArrayList<>();
for (JsonElement element : arr) {
MenigaChallenge challenge = factory.getMenigaChallengeItem((JsonObject) element);
if (challenge.getTitle().equals("GlobalSpendingMeterMarker")) {
challenge.setType(ChallengeType.METER);
}
challenges.add(challenge);
}
return challenges;
}
};
}
return null;
}
use of com.meniga.sdk.models.challenges.MenigaChallenge in project mobile-sdk-android by meniga.
the class ChallengeItemFactory method getMenigaChallengeItem.
public MenigaChallenge getMenigaChallengeItem(JsonObject element) {
if (gson == null) {
gson = GsonProvider.getGsonBuilder();
}
MenigaChallenge topLevel = gson.fromJson(element, MenigaChallenge.class);
if (element.has("typeData")) {
JsonObject typeData = element.getAsJsonObject("typeData");
MenigaChallenge meta = gson.fromJson(typeData, MenigaChallenge.class);
topLevel.merge(meta);
if (typeData.has("metaData")) {
JsonElement metaStr = typeData.get("metaData");
if (metaStr != null && metaStr.isJsonPrimitive() && metaStr.getAsString().length() > 0) {
JsonObject metaData = new JsonParser().parse(metaStr.getAsString()).getAsJsonObject();
MenigaChallenge subMeta = gson.fromJson(metaData, MenigaChallenge.class);
topLevel.merge(subMeta);
}
}
}
return topLevel;
}
Aggregations