Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add: 支持用户添加自定义Converter.Factory #62

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 57 additions & 4 deletions mvp/src/main/java/cn/droidlover/xdroidmvp/net/XApi.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
package cn.droidlover.xdroidmvp.net;

import android.graphics.Bitmap;
import android.support.annotation.NonNull;

import org.reactivestreams.Publisher;

import java.security.Provider;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import cn.droidlover.xdroidmvp.kit.Kits;
import cn.droidlover.xdroidmvp.mvp.VDelegateBase;
import cn.droidlover.xdroidmvp.net.progress.ProgressHelper;
import io.reactivex.Flowable;
import io.reactivex.FlowableTransformer;
Expand All @@ -16,6 +23,7 @@
import okhttp3.CookieJar;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import retrofit2.Converter;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
Expand Down Expand Up @@ -53,7 +61,9 @@ public static XApi getInstance() {


public static <S> S get(String baseUrl, Class<S> service) {
return getInstance().getRetrofit(baseUrl, true).create(service);
Config config = new Config(baseUrl)
.setUseRx(true);
return getInstance().getRetrofit(config).create(service);
}

public static void registerProvider(NetProvider provider) {
Expand All @@ -64,13 +74,21 @@ public static void registerProvider(String baseUrl, NetProvider provider) {
getInstance().providerMap.put(baseUrl, provider);
}


public Retrofit getRetrofit(String baseUrl, boolean useRx) {
return getRetrofit(baseUrl, null, useRx);
Config config = new Config(baseUrl)
.setUseRx(useRx);
return getRetrofit(config);
}


public Retrofit getRetrofit(String baseUrl, NetProvider provider, boolean useRx) {
public Retrofit getRetrofit(Config config) {

String baseUrl = config.baseUrl;
NetProvider provider = config.provider;
boolean useRx = config.useRx;

List<Converter.Factory> factories = config.converterFactories;

if (Kits.Empty.check(baseUrl)) {
throw new IllegalStateException("baseUrl can not be null");
}
Expand All @@ -88,6 +106,11 @@ public Retrofit getRetrofit(String baseUrl, NetProvider provider, boolean useRx)
.baseUrl(baseUrl)
.client(getClient(baseUrl, provider))
.addConverterFactory(GsonConverterFactory.create());

for (int i = 0, j = factories.size(); i < j; i++) {
builder.addConverterFactory(factories.get(j));
}

if (useRx) {
builder.addCallAdapterFactory(RxJava2CallAdapterFactory.create());
}
Expand Down Expand Up @@ -217,5 +240,35 @@ public Publisher<T> apply(T model) throws Exception {
};
}

public static class Config {
private String baseUrl;
private boolean useRx;
private NetProvider provider;
private final List<Converter.Factory> converterFactories = new ArrayList<>();

public Config(String baseUrl) {
this.baseUrl = baseUrl;
}

public Config setBaseUrl(@NonNull String baseUrl) {
this.baseUrl = baseUrl;
return this;
}

public Config setUseRx(boolean useRx) {
this.useRx = useRx;
return this;
}

public Config setProvider(NetProvider provider) {
this.provider = provider;
return this;
}

public Config addConverterFactory(@NonNull Converter.Factory factory) {
this.addConverterFactory(factory);
return this;
}
}

}