Search in sources :

Example 1 with MenigaChallenge

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;
}
Also used : JsonObject(com.google.gson.JsonObject) IOException(java.io.IOException) ResponseBody(okhttp3.ResponseBody) JsonArray(com.google.gson.JsonArray) ChallengeType(com.meniga.sdk.models.challenges.enums.ChallengeType) Type(java.lang.reflect.Type) JsonElement(com.google.gson.JsonElement) Converter(retrofit2.Converter) ArrayList(java.util.ArrayList) List(java.util.List) JsonObject(com.google.gson.JsonObject) MenigaChallenge(com.meniga.sdk.models.challenges.MenigaChallenge)

Example 2 with MenigaChallenge

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;
}
Also used : JsonElement(com.google.gson.JsonElement) JsonObject(com.google.gson.JsonObject) MenigaChallenge(com.meniga.sdk.models.challenges.MenigaChallenge) JsonParser(com.google.gson.JsonParser)

Aggregations

JsonElement (com.google.gson.JsonElement)2 JsonObject (com.google.gson.JsonObject)2 MenigaChallenge (com.meniga.sdk.models.challenges.MenigaChallenge)2 JsonArray (com.google.gson.JsonArray)1 JsonParser (com.google.gson.JsonParser)1 ChallengeType (com.meniga.sdk.models.challenges.enums.ChallengeType)1 IOException (java.io.IOException)1 Type (java.lang.reflect.Type)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 ResponseBody (okhttp3.ResponseBody)1 Converter (retrofit2.Converter)1