All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.cmonbaby.http.stream.download.DownloadFile Maven / Gradle / Ivy

There is a newer version: 2.0.2
Show newest version
package com.cmonbaby.http.stream.download;

import android.text.TextUtils;
import android.util.Log;

import com.cmonbaby.http.core.HttpCallback;
import com.cmonbaby.http.dialog.FileLoadable;
import com.cmonbaby.http.schedulers.AndroidSchedulers;
import com.cmonbaby.http.stream.listener.ProgressListener;
import com.cmonbaby.http.utils.RetrofitUtils;

import java.math.BigDecimal;

import okhttp3.ResponseBody;
import retrofit2.Response;
import rx.Observable;
import rx.Observer;
import rx.Subscription;
import rx.functions.Action0;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

/**
 * 

Author: Simon *

QO: 8950764 *

Email: [email protected] *

WebSize: https://www.cmonbaby.com *

Version: 1.0.0 *

Date: 2020/12/28 *

Description: 下载文件 */ public class DownloadFile { private final Class clazz; // Retrofit接口Class private final ProgressListener progressListener; // 下载进度监听 private final DownLoadCall responseService; // 开始请求服务 private final Action0 before; // http请求开始的时候一些界面上的处理 private final Action1> filter; // http请求完成时过滤,比如isSuccess = false,调用onError() private final FileLoadable loadable; // 对于activity和fragment loading的处理 private String dialogTitle; // loading标题 private String dialogContent; // loading内容摘要 private final HttpCallback> callback; // Http请求回调 private DownloadFile(Builder builder) { this.clazz = builder.clazz; this.progressListener = builder.progressListener; this.responseService = builder.responseService; this.before = builder.before; this.filter = builder.filter; this.loadable = builder.loadable; this.dialogTitle = builder.dialogTitle; this.dialogContent = builder.dialogContent; this.callback = builder.callback; } private Subscription startDownload() { if (TextUtils.isEmpty(dialogTitle)) dialogTitle = "文件下载"; if (TextUtils.isEmpty(dialogContent)) dialogContent = "文件下载中,请稍候……"; Observer> httpCallback = new Observer>() { @Override public void onCompleted() { if (loadable != null) loadable.dismissFileDialog(); } @Override public void onError(Throwable e) { if (loadable != null) loadable.dismissFileDialog(); callback.onError(e); } @Override public void onNext(Response response) { callback.onSuccessful(response); } }; T t = RetrofitUtils.getInstance().createResponseService(clazz, (progress, total, isFinish) -> { if (progressListener != null) progressListener.onProgress(progress, total, isFinish); if (loadable != null && total > 0 && progress > 0) { int pro = new BigDecimal((progress / 1024)).setScale(0, BigDecimal.ROUND_HALF_UP).intValue(); loadable.setDialogMaxProgress((int) (total / 1024)); loadable.setDialogProgress(pro); } }); if (responseService == null) { Log.e("初始化responseService错误", "请实现:requestService(new DownLoadCall())方法"); return null; } Observable> observable = responseService.downloadFile(t); if (observable == null) { Log.e("初始化observable错误", "请检查实现:responseService(new DownLoadCall())方法"); return null; } return observable.subscribeOn(Schedulers.io()) // 是http请求的操作是在io线程中调用,而不是UI线程中。 .observeOn(AndroidSchedulers.mainThread()) // 确保subscriber的回调,即onNext、onError和onCompleted是在Ui线程中回调 .doOnSubscribe(() -> { if (loadable != null) loadable.showProgressDialog(); // 如果不为空,就进行loading的show操作 if (loadable != null) loadable.setFileDialogTitle(dialogTitle); if (loadable != null) loadable.setFileDialogContent(dialogContent); if (before != null) before.call(); // 进行额外的界面处理,在http请求开始的时候 }) // http请求开始时,一些初始化的操作。比如loading界面的显示 .subscribeOn(AndroidSchedulers.mainThread()) // 这里切换到UI线程,确保doOnSubscribe在UI线程中进行,以便subscribe回调是在UI线程中进行。 .doOnNext(response -> { // 处理 if (filter != null) filter.call(response); // 进行额外的界面处理,在http请求开始的时候 }) .subscribe(httpCallback); } public static final class Builder { private final Class clazz; // Retrofit接口Class private ProgressListener progressListener; // 下载进度监听 private DownLoadCall responseService; // 开始请求服务 private Action0 before; // http请求开始的时候一些界面上的处理 private Action1> filter; // http请求完成时过滤,比如isSuccess = false,调用onError() private FileLoadable loadable; // 对于activity和fragment loading的处理 private String dialogTitle; // loading标题 private String dialogContent; // loading内容摘要 private HttpCallback> callback; // Http请求回调 private Builder(Class clazz) { this.clazz = clazz; } public static Builder download(Class clazz) { return new Builder<>(clazz); } public Builder progressListener(ProgressListener progressListener) { this.progressListener = progressListener; return this; } public Builder responseService(DownLoadCall responseService) { this.responseService = responseService; return this; } public Builder before(Action0 before) { this.before = before; return this; } public Builder filter(Action1> filter) { this.filter = filter; return this; } public Builder loadable(FileLoadable loadable) { this.loadable = loadable; return this; } public Builder dialogTitle(String dialogTitle) { this.dialogTitle = dialogTitle; return this; } public Builder dialogContent(String dialogContent) { this.dialogContent = dialogContent; return this; } public Builder callback(HttpCallback> callback) { this.callback = callback; return this; } public Subscription toSubscribe() { return new DownloadFile<>(this).startDownload(); } } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy