aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/com/pinapelz/factory/VideoSearchResultConverterFactory.java
blob: 0ab27c48cf2292bf80417eb94be3d04ffec66fb9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package com.pinapelz.factory;

import com.pinapelz.datatypes.Paginated;
import com.pinapelz.datatypes.SimpleVideo;
import com.pinapelz.datatypes.VideoSearchResult;
import okhttp3.ResponseBody;
import retrofit2.Converter;
import retrofit2.Retrofit;

import java.lang.annotation.Annotation;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;

public class VideoSearchResultConverterFactory extends Converter.Factory {

    public static VideoSearchResultConverterFactory create() {
        return new VideoSearchResultConverterFactory();
    }

    @Override
    public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) {
        boolean isPaginated = false;
        for (Annotation annotation : annotations) {
            if (annotation instanceof Paginated) {
                isPaginated = ((Paginated) annotation).value();
            }
        }
        if (isPaginated) {
            Type listType = new ParameterizedType() {
                @Override
                public Type[] getActualTypeArguments() {
                    return new Type[]{VideoSearchResult.class};
                }

                @Override
                public Type getRawType() {
                    return List.class;
                }

                @Override
                public Type getOwnerType() {
                    return null;
                }
            };
            return retrofit.nextResponseBodyConverter(this, listType, annotations);
        } else {
            return retrofit.nextResponseBodyConverter(this, SimpleVideo.class, annotations);
        }
    }
}
send patches to the email below
yukais@pinapelz.com
include the subject [PATCH repo_name]
pinapelz.com
homepage