Commit fdebcb0f authored by Ivan Del Pino's avatar Ivan Del Pino Committed by Alexander Salas Bastidas
Browse files

feat(multiple): add method to download multiple files


Signed-off-by: default avatarIvan Del Pino <idelpino@teclib.com>
parent 07438494
......@@ -35,6 +35,7 @@ import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.Spinner;
import android.widget.Toast;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
......@@ -632,6 +633,7 @@ public class MainActivity extends AppCompatActivity {
public void onResponse(ResponseBody response) {
FlyveLog.i("Download file: %s", response);
updateAdapter("Success: Download file");
String pathname = getExternalFilesDir(null) + File.separator + "CHANGELOG.md";
boolean inDisk = Helpers.writeResponseBodyToDisk(response, pathname);
FlyveLog.i(inDisk + "");
......@@ -643,6 +645,22 @@ public class MainActivity extends AppCompatActivity {
updateAdapter("Error: Download file" + errorMessage);
}
});
String[] urls = {url, url, url, url, url};
glpi.handleMultipleDownload(urls, 3, new GLPI.ResponseHandle<ResponseBody[], String>() {
@Override
public void onResponse(ResponseBody[] response) {
Toast.makeText(MainActivity.this, "Success", Toast.LENGTH_SHORT).show();
for (int i = 0; i < response.length; i++) {
String pathname = getExternalFilesDir(null) + File.separator + i + "-test.md";
boolean inDisk = Helpers.writeResponseBodyToDisk(response[i], pathname);
}
}
@Override
public void onFailure(String errorMessage) {
Toast.makeText(MainActivity.this, "Error", Toast.LENGTH_SHORT).show();
}
});
glpi.getPluginPackage("fileId", new GLPI.ResponseHandle<JsonArray, String>() {
@Override
public void onResponse(JsonArray response) {
......
......@@ -57,8 +57,11 @@ dependencies {
exclude group: 'com.android.support', module: 'support-annotations'
})
implementation 'com.squareup.retrofit2:retrofit:2.3.0'
implementation 'com.squareup.retrofit2:converter-gson:2.3.0'
compile 'com.squareup.retrofit2:retrofit:2.3.0'
compile 'com.squareup.retrofit2:converter-gson:2.3.0'
compile 'com.squareup.retrofit2:adapter-rxjava2:2.2.0'
compile 'io.reactivex.rxjava2:rxandroid:2.1.0'
compile 'io.reactivex.rxjava2:rxjava:2.2.4'
}
Properties properties = new Properties()
......
......@@ -45,9 +45,16 @@ import org.glpi.api.response.InitSession;
import org.glpi.api.utils.Helpers;
import org.json.JSONObject;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.BiFunction;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
......@@ -549,6 +556,54 @@ public class GLPI extends ServiceGenerator {
});
}
public void handleMultipleDownload(String[] urls, int limit, final ResponseHandle<ResponseBody[], String> callback) {
HashMap<String, String> header = new HashMap<>();
header.put("Accept","application/octet-stream");
header.put("Content-Type","application/json");
header.put("Session-Token", sessionToken);
String[] sendUrls = Arrays.copyOfRange(urls, 0, limit);
for (int i = 0; i < limit; i++) {
Observable<ResponseBody> observableOne = interfaces.downloadFileRX(sendUrls[0], header)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread());
Observable<ResponseBody> observableTwo = interfaces.downloadFileRX(sendUrls[1], header)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread());
final Disposable subscribe = Observable.zip(observableOne, observableTwo, new BiFunction<ResponseBody, ResponseBody, ManagementFile>() {
@Override
public ManagementFile apply(ResponseBody responseBody, ResponseBody responseBody2) throws Exception {
return new ManagementFile(new ResponseBody[]{responseBody, responseBody2});
}
}).subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
new Consumer<ManagementFile>() {
@Override
public void accept(ManagementFile o) throws Exception {
callback.onResponse(o.getFile());
}
},
new Consumer<Throwable>() {
@Override
public void accept(Throwable e) throws Exception {
callback.onFailure(e.getMessage());
}
});
}
}
public class ManagementFile {
private ResponseBody[] file;
public ManagementFile(ResponseBody[] file) {
this.file = file;
}
public ResponseBody[] getFile() {
return file;
}
}
/**
* Download file specifying route.
*
......@@ -563,7 +618,7 @@ public class GLPI extends ServiceGenerator {
responseFileDownload(callback, interfaces.downloadFile(url, header));
}
private void responseFileDownload(final ResponseHandle<ResponseBody, String> callback, Call<ResponseBody> responseCall) {
private void responseFileDownload(final ResponseHandle<ResponseBody, String> callback, final Call<ResponseBody> responseCall) {
responseCall.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(@NonNull Call<ResponseBody> call, @NonNull Response<ResponseBody> response) {
......
......@@ -39,6 +39,7 @@ import org.json.JSONObject;
import java.util.Map;
import io.reactivex.Observable;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.http.Body;
......@@ -169,4 +170,8 @@ public interface Routes {
@GET("getMultipleItems")
Call<JsonArray> getMultipleItem(@HeaderMap Map<String, String> headers, @QueryMap Map<String, String> options);
@Headers("Content-Type: application/json")
@GET
Observable<ResponseBody> downloadFileRX(@Url String url, @HeaderMap Map<String, String> headers);
}
......@@ -30,6 +30,7 @@ import java.util.concurrent.TimeUnit;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
public class ServiceGenerator {
......@@ -57,6 +58,7 @@ public class ServiceGenerator {
.baseUrl(url)
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build();
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment