Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class ArCoreInteractor extends SuperInteractor<Model3D> {
- private String sku;
- @Override
- public void loadData(@Nullable Bundle bundle, @NonNull ResultListener listener) {
- //todo load from Cache
- if (bundle != null) {
- sku = bundle.getString(ArCorePresenter.K_SKU_PRODUCT);
- if (!TextUtils.isEmpty(sku)) {
- mCompositeDisposible.add(
- Rest.call().getProduct(sku)
- .filter(productBaseResponse -> {
- if (TextUtils.isEmpty(productBaseResponse.getmData().getUrl3dModel())) {
- handleResponse(new Model3D(productBaseResponse.getmData().getImage()), listener);
- return false;
- } else
- return true;
- })
- .flatMap(this::getArchive)
- .flatMap(processResponse())
- .flatMap(unpackZip())
- .flatMap(getUriFile())
- .subscribeOn(Schedulers.io())
- .observeOn(AndroidSchedulers.mainThread())
- .subscribe(response -> handleResponse(response, listener), error -> handleError(RestUtils.ErrorMessages(error), listener)));
- } else {
- listener.onFailureResult("Error");
- }
- } else {
- listener.onFailureResult("Error");
- }
- }
- private ObservableSource<Response<ResponseBody>> getArchive(BaseResponse<Product> response) {
- return Rest.call().downloadFileByUrl(response.getmData().getUrl3dModel());
- }
- private Function<File, Observable<Model3D>> getUriFile() {
- return file -> {
- for (File f : file.listFiles()) {
- if (f.getAbsolutePath().contains(".sfb")) {
- return Observable.just(new Model3D(Uri.fromFile(f)));
- }
- }
- return Observable.just(null);
- };
- }
- @Override
- public void handleResponse(Model3D model3D, @NonNull ResultListener listener) {
- mData = model3D;
- listener.onSuccessResult();
- }
- private Function<Response<ResponseBody>, Observable<File>> processResponse() {
- return this::saveToDiskRx;
- }
- private Observable<File> saveToDiskRx(final Response<ResponseBody> response) {
- return Observable.create((ObservableEmitter<File> emitter) -> {
- try {
- //String header = response.headers().get("Content-Disposition");
- String fileName = "my_temp.zip";
- File destinationFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsoluteFile(), fileName);
- BufferedSink bufferedSink = Okio.buffer(Okio.sink(destinationFile));
- bufferedSink.writeAll(response.body().source());
- bufferedSink.close();
- emitter.onNext(destinationFile);
- emitter.onComplete();
- } catch (IOException e) {
- e.printStackTrace();
- emitter.onError(e);
- }
- });
- }
- private Function<File, Observable<File>> unpackZip() {
- return file -> {
- InputStream is;
- ZipInputStream zis;
- String parentFolder;
- String filename = "";
- String extractedFolder = null;
- try {
- parentFolder = file.getParentFile().getPath();
- is = new FileInputStream(file.getAbsolutePath());
- zis = new ZipInputStream(new BufferedInputStream(is));
- ZipEntry ze;
- byte[] buffer = new byte[1024];
- int count;
- while ((ze = zis.getNextEntry()) != null) {
- filename = ze.getName();
- if (ze.isDirectory()) {
- if (extractedFolder == null)
- extractedFolder = ze.getName(); // main extractedFolder
- File fmd = new File(parentFolder + "/" + filename);
- fmd.mkdirs();
- continue;
- }
- FileOutputStream fout = new FileOutputStream(parentFolder + "/" + filename);
- while ((count = zis.read(buffer)) != -1) {
- fout.write(buffer, 0, count);
- }
- fout.close();
- zis.closeEntry();
- }
- zis.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- StringBuilder pathExtractedFile = new StringBuilder(file.getAbsolutePath());
- int lastIndex = pathExtractedFile.lastIndexOf("/");
- pathExtractedFile.delete(lastIndex + 1, pathExtractedFile.length());
- File extractedFile = new File(pathExtractedFile.append(extractedFolder).toString());
- if (!file.delete()) Log.d("unpackZip", "Failed to deleted the zip file.");
- return Observable.just(extractedFile);
- };
- }
- @Override
- public void handleError(String error, ResultListener listener) {
- listener.onFailureResult(error);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement