Search in sources :

Example 1 with MenigaCategory

use of com.meniga.sdk.models.categories.MenigaCategory in project mobile-sdk-android by meniga.

the class MenigaCategoryConverter method responseBodyConverter.

@Override
public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) {
    Type typeOfMenigaCategoryList = new TypeToken<List<MenigaCategory>>() {
    }.getType();
    Type typeOfMenigaCategory = new TypeToken<MenigaCategory>() {
    }.getType();
    Type typeOfMenigaUserCategoryList = new TypeToken<List<MenigaUserCategory>>() {
    }.getType();
    Type typeOfMenigaUserCategory = new TypeToken<MenigaUserCategory>() {
    }.getType();
    if (typeOfMenigaCategoryList.equals(type) || typeOfMenigaUserCategoryList.equals(type)) {
        return new Converter<ResponseBody, Object>() {

            @Override
            public List<MenigaCategory> convert(ResponseBody resBody) throws IOException {
                Gson gson = GsonProvider.getGsonBuilder();
                MenigaCategory[] catsRaw = gson.fromJson(getAsArray(resBody.byteStream()), MenigaCategory[].class);
                List<MenigaCategory> cats = new ArrayList<>();
                for (MenigaCategory cat : catsRaw) {
                    if (cat.getIsPublic()) {
                        cats.add(cat);
                    } else {
                        cats.add(new MenigaUserCategory(cat));
                    }
                    for (int i = 0; i < cat.getChildren().size(); i++) {
                        MenigaCategory child = cat.getChildren().get(i);
                        if (!child.getIsPublic()) {
                            cat.getChildren().set(i, new MenigaUserCategory(child));
                        }
                    }
                }
                return cats;
            }
        };
    } else if (typeOfMenigaCategory.equals(type) || typeOfMenigaUserCategory.equals(type)) {
        return new Converter<ResponseBody, Object>() {

            @Override
            public MenigaCategory convert(ResponseBody resBody) throws IOException {
                Gson gson = GsonProvider.getGsonBuilder();
                MenigaCategory catRaw = gson.fromJson(getAsObject(resBody.byteStream()), MenigaCategory.class);
                if (catRaw.getIsPublic()) {
                    return catRaw;
                } else {
                    return new MenigaUserCategory(catRaw);
                }
            }
        };
    }
    return null;
}
Also used : ArrayList(java.util.ArrayList) Gson(com.google.gson.Gson) MenigaCategory(com.meniga.sdk.models.categories.MenigaCategory) IOException(java.io.IOException) MenigaUserCategory(com.meniga.sdk.models.categories.MenigaUserCategory) ResponseBody(okhttp3.ResponseBody) Type(java.lang.reflect.Type) Converter(retrofit2.Converter) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

Gson (com.google.gson.Gson)1 MenigaCategory (com.meniga.sdk.models.categories.MenigaCategory)1 MenigaUserCategory (com.meniga.sdk.models.categories.MenigaUserCategory)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