From 1202c5e5e35ecbc211dcd967b355c0c41abb460a Mon Sep 17 00:00:00 2001
From: Allen <lygttpod@163.com>
Date: Sat, 8 Dec 2018 17:48:32 +0800
Subject: [PATCH] =?UTF-8?q?1=E3=80=81=E6=96=B0=E5=A2=9E=E8=87=AA=E5=AE=9A?=
 =?UTF-8?q?=E4=B9=89=E4=B8=8B=E8=BD=BD=E8=B7=AF=E5=BE=84=E7=9A=84=E9=85=8D?=
 =?UTF-8?q?=E7=BD=AE=E6=96=B9=E6=B3=95=EF=BC=9B2=E3=80=81=E6=96=B0?=
 =?UTF-8?q?=E5=A2=9E=E5=85=A8=E5=B1=80=E9=85=8D=E7=BD=AE=E8=87=AA=E5=AE=9A?=
 =?UTF-8?q?=E4=B9=89Retrofit=E7=9A=84Factory=E6=96=B9=E6=B3=95?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../main/java/com/allen/rxhttputils/App.java  |  7 ++
 .../com/allen/rxhttputils/MainActivity.java   |  1 +
 library/build.gradle                          |  2 +-
 .../allen/library/config/RetrofitConfig.java  | 79 +++++++++++++++++++
 .../library/download/DownloadManager.java     | 10 ++-
 .../library/download/DownloadObserver.java    | 10 ++-
 .../com/allen/library/http/GlobalRxHttp.java  | 32 ++++++++
 .../allen/library/http/RetrofitClient.java    | 29 +++++--
 8 files changed, 156 insertions(+), 14 deletions(-)
 create mode 100644 library/src/main/java/com/allen/library/config/RetrofitConfig.java

diff --git a/app/src/main/java/com/allen/rxhttputils/App.java b/app/src/main/java/com/allen/rxhttputils/App.java
index 8298bd1..2c7f243 100644
--- a/app/src/main/java/com/allen/rxhttputils/App.java
+++ b/app/src/main/java/com/allen/rxhttputils/App.java
@@ -6,11 +6,15 @@
 import com.allen.library.RxHttpUtils;
 import com.allen.library.config.OkHttpConfig;
 import com.allen.library.cookie.store.SPCookieStore;
+import com.allen.library.gson.GsonAdapter;
 
 import java.util.HashMap;
 import java.util.Map;
 
 import okhttp3.OkHttpClient;
+import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
+import retrofit2.converter.gson.GsonConverterFactory;
+import retrofit2.converter.scalars.ScalarsConverterFactory;
 
 
 /**
@@ -96,6 +100,9 @@ private void initCustomRxHttpUtils() {
                 .getInstance()
                 .init(this)
                 .config()
+                //自定义factory的用法
+                //.setCallAdapterFactory(RxJava2CallAdapterFactory.create())
+                //.setConverterFactory(ScalarsConverterFactory.create(),GsonConverterFactory.create(GsonAdapter.buildGson()))
                 //配置全局baseUrl
                 .setBaseUrl("https://api.douban.com/")
                 //开启全局配置
diff --git a/app/src/main/java/com/allen/rxhttputils/MainActivity.java b/app/src/main/java/com/allen/rxhttputils/MainActivity.java
index e049f8f..fdb4733 100644
--- a/app/src/main/java/com/allen/rxhttputils/MainActivity.java
+++ b/app/src/main/java/com/allen/rxhttputils/MainActivity.java
@@ -204,6 +204,7 @@ protected void onSuccess(Top250Bean top250Bean) {
                 final String fileName = "alipay.apk";
                 RxHttpUtils
                         .downloadFile(url)
+                        //.subscribe(new DownloadObserver(fileName,destFileDir) 其中 destFileDir是自定义下载存储路径
                         .subscribe(new DownloadObserver(fileName) {
                             //可以去下下载
                             @Override
diff --git a/library/build.gradle b/library/build.gradle
index 8881ece..262b22e 100644
--- a/library/build.gradle
+++ b/library/build.gradle
@@ -9,7 +9,7 @@ android {
         minSdkVersion 14
         targetSdkVersion 27
         versionCode 218
-        versionName "2.1.8"
+        versionName "2.1.9"
 
         testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
 
diff --git a/library/src/main/java/com/allen/library/config/RetrofitConfig.java b/library/src/main/java/com/allen/library/config/RetrofitConfig.java
new file mode 100644
index 0000000..5b1f8b8
--- /dev/null
+++ b/library/src/main/java/com/allen/library/config/RetrofitConfig.java
@@ -0,0 +1,79 @@
+package com.allen.library.config;
+
+
+import retrofit2.CallAdapter;
+import retrofit2.Converter;
+
+
+/**
+ * <pre>
+ *      @author : Allen
+ *      e-mail  : lygttpod@163.com
+ *      date    : 2018/12/08
+ *      desc    : RetrofitFactory的配置
+ *      version : 1.0
+ * </pre>
+ */
+public class RetrofitConfig {
+
+    private static RetrofitConfig instance = null;
+
+
+    private CallAdapter.Factory[] callAdapterFactory;
+
+    private Converter.Factory[] converterFactory;
+
+    private RetrofitConfig() {
+    }
+
+    public static RetrofitConfig getInstance() {
+
+        if (instance == null) {
+            synchronized (RetrofitConfig.class) {
+                if (instance == null) {
+                    instance = new RetrofitConfig();
+                }
+            }
+
+        }
+        return instance;
+    }
+
+    /**
+     * 添加CallAdapterFactory
+     *
+     * @param factories CallAdapter.Factory
+     */
+    public void addCallAdapterFactory(CallAdapter.Factory... factories) {
+        this.callAdapterFactory = factories;
+
+    }
+
+    /**
+     * 添加ConverterFactory
+     *
+     * @param factories Converter.Factory
+     */
+    public void addConverterFactory(Converter.Factory... factories) {
+        this.converterFactory = factories;
+    }
+
+    /**
+     * 获取CallAdapter.Factory
+     *
+     * @return
+     */
+    public CallAdapter.Factory[] getCallAdapterFactory() {
+        return callAdapterFactory;
+    }
+
+    /**
+     * 获取Converter.Factory
+     *
+     * @return
+     */
+    public Converter.Factory[] getConverterFactory() {
+        return converterFactory;
+    }
+
+}
diff --git a/library/src/main/java/com/allen/library/download/DownloadManager.java b/library/src/main/java/com/allen/library/download/DownloadManager.java
index 5a4fbf2..ef4add7 100644
--- a/library/src/main/java/com/allen/library/download/DownloadManager.java
+++ b/library/src/main/java/com/allen/library/download/DownloadManager.java
@@ -15,7 +15,7 @@
  * <p>
  *
  * @author Allen
- *         保存下载的文件
+ * 保存下载的文件
  */
 
 public class DownloadManager {
@@ -26,12 +26,13 @@ public class DownloadManager {
      *
      * @param response     ResponseBody
      * @param destFileName 文件名(包括文件后缀)
+     * @param destFileDir  文件下载路径
      * @return 返回
      * @throws IOException
      */
-    public File saveFile(ResponseBody response, final String destFileName, ProgressListener progressListener) throws IOException {
+    public File saveFile(ResponseBody response, String destFileName, String destFileDir, ProgressListener progressListener) throws IOException {
 
-        String destFileDir = RxHttpUtils.getContext().getExternalFilesDir(null) + File.separator;
+        String defaultDestFileDir = RxHttpUtils.getContext().getExternalFilesDir(null) + File.separator;
 
         long contentLength = response.contentLength();
         InputStream is = null;
@@ -43,7 +44,8 @@ public File saveFile(ResponseBody response, final String destFileName, ProgressL
 
             long sum = 0;
 
-            File dir = new File(destFileDir);
+            File dir = new File(null == destFileDir ? defaultDestFileDir : destFileDir);
+
             if (!dir.exists()) {
                 dir.mkdirs();
             }
diff --git a/library/src/main/java/com/allen/library/download/DownloadObserver.java b/library/src/main/java/com/allen/library/download/DownloadObserver.java
index 67a3d7d..af154fb 100644
--- a/library/src/main/java/com/allen/library/download/DownloadObserver.java
+++ b/library/src/main/java/com/allen/library/download/DownloadObserver.java
@@ -1,10 +1,8 @@
 package com.allen.library.download;
 
 import android.annotation.SuppressLint;
-import android.app.Dialog;
 
 import com.allen.library.manage.RxHttpManager;
-import com.allen.library.observer.CommonObserver;
 
 import java.io.IOException;
 
@@ -28,11 +26,17 @@
 public abstract class DownloadObserver extends BaseDownloadObserver {
 
     private String fileName;
+    private String destFileDir;
 
     public DownloadObserver(String fileName) {
         this.fileName = fileName;
     }
 
+    public DownloadObserver(String fileName, String destFileDir) {
+        this.fileName = fileName;
+        this.destFileDir = destFileDir;
+    }
+
 
     /**
      * 失败回调
@@ -95,7 +99,7 @@ public void onSubscribe(Disposable d) {
                     @Override
                     public void onNext(ResponseBody responseBody) {
                         try {
-                            new DownloadManager().saveFile(responseBody, fileName, new ProgressListener() {
+                            new DownloadManager().saveFile(responseBody, fileName, destFileDir, new ProgressListener() {
                                 @Override
                                 public void onResponseProgress(final long bytesRead, final long contentLength, final int progress, final boolean done, final String filePath) {
                                     Observable
diff --git a/library/src/main/java/com/allen/library/http/GlobalRxHttp.java b/library/src/main/java/com/allen/library/http/GlobalRxHttp.java
index f5825a1..a72fe49 100644
--- a/library/src/main/java/com/allen/library/http/GlobalRxHttp.java
+++ b/library/src/main/java/com/allen/library/http/GlobalRxHttp.java
@@ -1,8 +1,12 @@
 package com.allen.library.http;
 
+import com.allen.library.config.RetrofitConfig;
+
 import java.util.HashMap;
 
 import okhttp3.OkHttpClient;
+import retrofit2.CallAdapter;
+import retrofit2.Converter;
 import retrofit2.Retrofit;
 
 /**
@@ -39,6 +43,34 @@ public static GlobalRxHttp getInstance() {
         return instance;
     }
 
+    /**
+     * 为Retrofit设置CallAdapterFactory
+     * 注意:需在调用SetBaseUrl之前调用
+     *
+     * @param factories callAdapterFactory
+     * @return CallAdapterFactory[]
+     */
+    public GlobalRxHttp setCallAdapterFactory(CallAdapter.Factory... factories) {
+        if (null != factories) {
+            RetrofitConfig.getInstance().addCallAdapterFactory(factories);
+        }
+        return this;
+    }
+
+    /**
+     * 为Retrofit设置ConverterFactory
+     * 注意:需在调用SetBaseUrl之前调用
+     *
+     * @param factories converterFactory
+     * @return ConverterFactory[]
+     */
+    public GlobalRxHttp setConverterFactory(Converter.Factory... factories) {
+        if (null != factories) {
+            RetrofitConfig.getInstance().addConverterFactory(factories);
+        }
+        return this;
+    }
+
     /**
      * 设置baseUrl
      *
diff --git a/library/src/main/java/com/allen/library/http/RetrofitClient.java b/library/src/main/java/com/allen/library/http/RetrofitClient.java
index 3be8fe6..7944d78 100644
--- a/library/src/main/java/com/allen/library/http/RetrofitClient.java
+++ b/library/src/main/java/com/allen/library/http/RetrofitClient.java
@@ -1,8 +1,7 @@
 package com.allen.library.http;
 
-import android.util.Log;
-
 import com.allen.library.config.OkHttpConfig;
+import com.allen.library.config.RetrofitConfig;
 import com.allen.library.gson.GsonAdapter;
 import com.allen.library.interceptor.RxHttpLogger;
 
@@ -10,6 +9,8 @@
 
 import okhttp3.OkHttpClient;
 import okhttp3.logging.HttpLoggingInterceptor;
+import retrofit2.CallAdapter;
+import retrofit2.Converter;
 import retrofit2.Retrofit;
 import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
 import retrofit2.converter.gson.GsonConverterFactory;
@@ -35,10 +36,26 @@ public RetrofitClient() {
 
         initDefaultOkHttpClient();
 
-        mRetrofitBuilder = new Retrofit.Builder()
-                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
-                .addConverterFactory(ScalarsConverterFactory.create())
-                .addConverterFactory(GsonConverterFactory.create(GsonAdapter.buildGson()));
+        mRetrofitBuilder = new Retrofit.Builder();
+
+        CallAdapter.Factory[] callAdapterFactories = RetrofitConfig.getInstance().getCallAdapterFactory();
+        Converter.Factory[] converterFactories = RetrofitConfig.getInstance().getConverterFactory();
+
+        if (null != callAdapterFactories && callAdapterFactories.length > 0) {
+            for (CallAdapter.Factory factory : callAdapterFactories) {
+                mRetrofitBuilder.addCallAdapterFactory(factory);
+            }
+        } else {
+            mRetrofitBuilder.addCallAdapterFactory(RxJava2CallAdapterFactory.create());
+        }
+        if (null != converterFactories && converterFactories.length > 0) {
+            for (Converter.Factory factory : converterFactories) {
+                mRetrofitBuilder.addConverterFactory(factory);
+            }
+        } else {
+            mRetrofitBuilder.addConverterFactory(ScalarsConverterFactory.create())
+                    .addConverterFactory(GsonConverterFactory.create(GsonAdapter.buildGson()));
+        }
     }
 
     private void initDefaultOkHttpClient() {