Skip to content

Commit

Permalink
Refactor pull request Ask for camera permission
Browse files Browse the repository at this point in the history
  • Loading branch information
Victor committed May 18, 2016
1 parent df0c6b1 commit e62b19a
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 22 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -29,26 +26,23 @@

public final class GrantPermissions extends UseCase<Void> {
private final TargetUi targetUi;
private String[] permissions;

@Inject public GrantPermissions(TargetUi targetUi) {
this.targetUi = targetUi;
}

@Override public Observable<Void> 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<Void> requestPermissions(String... permissionsString) {
RxPermissions permissions = RxPermissions.getInstance(targetUi.activity());
return permissions.request(permissionsString)
.flatMap(granted -> {
if (granted) return Observable.<Void>just(null);
throw new PermissionDeniedException();
});
}

public Observable<Void> reactAlsoWithCameraPermission() {
return requestPermissions(Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.CAMERA);
@Override public Observable<Void> react() {
return RxPermissions.getInstance(targetUi.activity())
.request(permissions)
.flatMap(granted -> {
if (granted) return Observable.<Void>just(null);
throw new PermissionDeniedException();
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -46,7 +47,11 @@ public final class Camera extends Worker {
}

public <T> Observable<Response<T, String>> 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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -51,7 +52,7 @@ public final class Gallery extends Worker{
}

public <T> Observable<Response<T, String>> 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())
Expand All @@ -60,7 +61,7 @@ public <T> Observable<Response<T, String>> pickImage() {
}

public <T> Observable<Response<T, List<String>>> pickImages() {
return grantPermissions.react()
return grantPermissions.with(permissions()).react()
.flatMap(granted -> pickImages.react())
.flatMapIterable(uris -> uris)
.concatMap(uri -> cropImage.with(uri).react())
Expand All @@ -69,4 +70,10 @@ public <T> Observable<Response<T, List<String>>> 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;
}

}

0 comments on commit e62b19a

Please sign in to comment.