From e62b19a3f2a2713515f7704345199f4588b7fa45 Mon Sep 17 00:00:00 2001 From: Victor Date: Wed, 18 May 2016 19:06:08 +0200 Subject: [PATCH] Refactor pull request Ask for camera permission --- README.md | 2 +- .../interactors/GrantPermissions.java | 28 ++++++++----------- .../rx_paparazzo/workers/Camera.java | 7 ++++- .../rx_paparazzo/workers/Gallery.java | 13 +++++++-- 4 files changed, 28 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index 502cfbc..5ffd54e 100644 --- a/README.md +++ b/README.md @@ -34,7 +34,7 @@ allprojects { And add next dependencies in the build.gradle of the module: ```gradle dependencies { - compile "com.github.FuckBoilerplate:RxPaparazzo:0.0.8" + compile "com.github.FuckBoilerplate:RxPaparazzo:0.0.9" compile "io.reactivex:rxjava:1.1.5" } ``` diff --git a/rx_paparazzo/src/main/java/com/fuck_boilerplate/rx_paparazzo/interactors/GrantPermissions.java b/rx_paparazzo/src/main/java/com/fuck_boilerplate/rx_paparazzo/interactors/GrantPermissions.java index 90cb0d4..9ddb6d0 100644 --- a/rx_paparazzo/src/main/java/com/fuck_boilerplate/rx_paparazzo/interactors/GrantPermissions.java +++ b/rx_paparazzo/src/main/java/com/fuck_boilerplate/rx_paparazzo/interactors/GrantPermissions.java @@ -16,11 +16,8 @@ package com.fuck_boilerplate.rx_paparazzo.interactors; -import android.Manifest; - import com.fuck_boilerplate.rx_paparazzo.entities.PermissionDeniedException; import com.fuck_boilerplate.rx_paparazzo.entities.TargetUi; -import com.fuck_boilerplate.rx_paparazzo.entities.UserCanceledException; import com.tbruyelle.rxpermissions.RxPermissions; import javax.inject.Inject; @@ -29,26 +26,23 @@ public final class GrantPermissions extends UseCase { private final TargetUi targetUi; + private String[] permissions; @Inject public GrantPermissions(TargetUi targetUi) { this.targetUi = targetUi; } - @Override public Observable react() { - return requestPermissions(Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE); + public GrantPermissions with(String... permissions) { + this.permissions = permissions; + return this; } - private Observable requestPermissions(String... permissionsString) { - RxPermissions permissions = RxPermissions.getInstance(targetUi.activity()); - return permissions.request(permissionsString) - .flatMap(granted -> { - if (granted) return Observable.just(null); - throw new PermissionDeniedException(); - }); - } - - public Observable reactAlsoWithCameraPermission() { - return requestPermissions(Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE, - Manifest.permission.CAMERA); + @Override public Observable react() { + return RxPermissions.getInstance(targetUi.activity()) + .request(permissions) + .flatMap(granted -> { + if (granted) return Observable.just(null); + throw new PermissionDeniedException(); + }); } } diff --git a/rx_paparazzo/src/main/java/com/fuck_boilerplate/rx_paparazzo/workers/Camera.java b/rx_paparazzo/src/main/java/com/fuck_boilerplate/rx_paparazzo/workers/Camera.java index 4903918..9923374 100644 --- a/rx_paparazzo/src/main/java/com/fuck_boilerplate/rx_paparazzo/workers/Camera.java +++ b/rx_paparazzo/src/main/java/com/fuck_boilerplate/rx_paparazzo/workers/Camera.java @@ -16,6 +16,7 @@ package com.fuck_boilerplate.rx_paparazzo.workers; +import android.Manifest; import android.app.Activity; import com.fuck_boilerplate.rx_paparazzo.entities.Response; @@ -46,7 +47,11 @@ public final class Camera extends Worker { } public Observable> takePhoto() { - return grantPermissions.reactAlsoWithCameraPermission() + String[] permissions = {Manifest.permission.WRITE_EXTERNAL_STORAGE, + Manifest.permission.READ_EXTERNAL_STORAGE, + Manifest.permission.CAMERA}; + + return grantPermissions.with(permissions).react() .flatMap(granted -> takePhoto.react()) .flatMap(uri -> cropImage.with(uri).react()) .flatMap(uri -> saveImage.with(uri).react()) diff --git a/rx_paparazzo/src/main/java/com/fuck_boilerplate/rx_paparazzo/workers/Gallery.java b/rx_paparazzo/src/main/java/com/fuck_boilerplate/rx_paparazzo/workers/Gallery.java index 8ca011b..3ca0d76 100644 --- a/rx_paparazzo/src/main/java/com/fuck_boilerplate/rx_paparazzo/workers/Gallery.java +++ b/rx_paparazzo/src/main/java/com/fuck_boilerplate/rx_paparazzo/workers/Gallery.java @@ -16,6 +16,7 @@ package com.fuck_boilerplate.rx_paparazzo.workers; +import android.Manifest; import android.app.Activity; import com.fuck_boilerplate.rx_paparazzo.entities.Response; @@ -32,7 +33,7 @@ import rx.Observable; -public final class Gallery extends Worker{ +public final class Gallery extends Worker { private final GrantPermissions grantPermissions; private final PickImages pickImages; private final PickImage pickImage; @@ -51,7 +52,7 @@ public final class Gallery extends Worker{ } public Observable> pickImage() { - return grantPermissions.react() + return grantPermissions.with(permissions()).react() .flatMap(granted -> pickImage.react()) .flatMap(uri -> cropImage.with(uri).react()) .flatMap(uri -> saveImage.with(uri).react()) @@ -60,7 +61,7 @@ public Observable> pickImage() { } public Observable>> pickImages() { - return grantPermissions.react() + return grantPermissions.with(permissions()).react() .flatMap(granted -> pickImages.react()) .flatMapIterable(uris -> uris) .concatMap(uri -> cropImage.with(uri).react()) @@ -69,4 +70,10 @@ public Observable>> pickImages() { .map(paths -> new Response<>((T) targetUi.ui(), paths, Activity.RESULT_OK)) .compose(applyOnError()); } + + private String[] permissions() { + String[] permissions = {Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE}; + return permissions; + } + }