浏览代码

升级2.0.1版本,优化下载

yanyi 5 年之前
父节点
当前提交
48cbc0b5e6

+ 2 - 1
README.md

@@ -4,9 +4,10 @@
 
 module 下添加
 
-`implementation 'com.yanyi.benyanyi:okhttplib:2.0.0'` 
+`implementation 'com.yanyi.benyanyi:okhttplib:2.0.1'` 
   
 ### 更新记录
+* 2019-08-26(2.0.1) 优化下载
 * 2019-08-20(2.0.0) 升级2.x版本,重构代码,优化方法,使调用更简洁明了
 * 2019-07-19(1.1.4) 优化回调方法
 * 2019-06-14(1.1.3) 优化代码,更改包名,用于统一化开源库

+ 1 - 1
okhttplib/bintrayUpload.gradle

@@ -7,7 +7,7 @@ def siteUrl = 'http://www.yanyis.space/yanyi/baseokhttp' // 项目主页。
 def gitUrl = 'http://www.yanyis.space/yanyi/baseokhttp.git' // Git仓库的url。
 
 group = "com.yanyi.benyanyi"// 唯一包名,比如compile 'com.ansen.http:okhttpencapsulation:1.0.1'中的com.ansen.http就是这里配置的。
-version = "2.0.0" //项目引用的版本号,比如compile 'com.ansen.http:okhttpencapsulation:1.0.1'中的1.0.1就是这里配置的。
+version = "2.0.1" //项目引用的版本号,比如compile 'com.ansen.http:okhttpencapsulation:1.0.1'中的1.0.1就是这里配置的。
 install {
     repositories.mavenInstaller {
         // This generates POM.xml with proper parameters

+ 10 - 0
okhttplib/src/main/java/com/benyanyi/okhttp/download/DownloadCall.java

@@ -51,6 +51,16 @@ public class DownloadCall implements DownloadConfig {
         DownloadManager.getInstance(mContext).cancel(url);
     }
 
+    @Override
+    public boolean getDownloadUrl() {
+        return DownloadManager.getInstance(mContext).getDownloadUrl(url);
+    }
+
+    @Override
+    public DownloadInfo getDownloadInfo() {
+        return DownloadManager.getInstance(mContext).getDownloadInfo(url);
+    }
+
     public static class Builder {
 
         private Context mContext;

+ 4 - 0
okhttplib/src/main/java/com/benyanyi/okhttp/download/DownloadConfig.java

@@ -18,4 +18,8 @@ public interface DownloadConfig {
 
     void cancel();
 
+    boolean getDownloadUrl();
+
+    DownloadInfo getDownloadInfo();
+
 }

+ 16 - 19
okhttplib/src/main/java/com/benyanyi/okhttp/download/DownloadManager.java

@@ -31,6 +31,7 @@ import okhttp3.Response;
  * @overview
  */
 class DownloadManager {
+
     private static final AtomicReference<DownloadManager> INSTANCE = new AtomicReference<>();
     /**
      * 用来存放各个下载的请求
@@ -81,6 +82,16 @@ class DownloadManager {
         return downCalls.containsKey(url);
     }
 
+    /**
+     * 获取下载的downloadInfo
+     *
+     * @param url
+     * @return
+     */
+    DownloadInfo getDownloadInfo(String url) {
+        return downInfos.get(url);
+    }
+
     /**
      * 开始下载
      *
@@ -100,16 +111,18 @@ class DownloadManager {
      * @param onDownLoadObserver 用来回调的接口
      */
     void download(String url, String suffix, OnDownLoadObserver onDownLoadObserver) {
+        this.suffix = suffix;
+        this.download(url, onDownLoadObserver);
     }
 
     /**
      * 开始下载
      *
      * @param url 下载请求的网址
-     *            //     * @param onDownLoadObserver 用来回调的接口
      */
     void download(String url) {
-//        if (!downCalls.containsKey(url)) {
+        DownloadObserver observer = new DownloadObserver();
+        observer.setContext(mContext);
         Observable.just(url)
                 //call的map已经有了,就证明正在下载,则这次不下载
                 .filter(new Predicate<String>() {
@@ -143,23 +156,7 @@ class DownloadManager {
                 //在子线程执行
                 .subscribeOn(Schedulers.io())
                 //添加观察者
-                .subscribe(new DownloadObserver(mContext));
-//        } else {
-////            Call call = downCalls.get(url);
-////            if (call != null) {
-////                call.cancel();
-////            }
-//            Observable.just(url)
-//                    .flatMap(new Function<String, ObservableSource<DownloadInfo>>() {
-//                        @Override
-//                        public ObservableSource<DownloadInfo> apply(String s) throws Exception {
-//                            return Observable.create(new DownloadingSubscribe(s));
-//                        }
-//                    })
-//                    .subscribeOn(Schedulers.io())
-//                    .observeOn(AndroidSchedulers.mainThread())
-//                    .subscribe(onDownLoadObserver);
-//        }
+                .subscribe(observer);
     }
 
     void download(String url, OnDownLoadObserver onDownLoadObserver) {

+ 2 - 2
okhttplib/src/main/java/com/benyanyi/okhttp/download/DownloadObserver.java

@@ -19,8 +19,8 @@ public class DownloadObserver implements Observer<DownloadInfo> {
     public DownloadInfo downloadInfo;
     private Context mContext;
 
-    public DownloadObserver(Context context) {
-        this.mContext = context;
+    public void setContext(Context mContext) {
+        this.mContext = mContext;
     }
 
     @Override