Skip to content
This repository has been archived by the owner on Feb 4, 2024. It is now read-only.

Commit

Permalink
Lazy initialization (#233)
Browse files Browse the repository at this point in the history
* Update libraries to latest versions.

* Move from bintray to jitpack.

* Update travis file.

* Make RxPermissionsFragment inherit from support.

* Make RxPermissionsFragment inherit from support.

* Make RxPermissionsFragment inherit from support.

* Prepare version to 0.10.0.

* Lazy RxPermissionsFragment initialization.

* Lazy RxPermissionsFragment initialization.
  • Loading branch information
epool authored Jul 6, 2018
1 parent d31e5dd commit 10c5b53
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 17 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ allprojects {
}
dependencies {
implementation 'com.github.tbruyelle:rxpermissions:0.10.0'
implementation 'com.github.tbruyelle:rxpermissions:0.10.1'
}
```

Expand Down
53 changes: 37 additions & 16 deletions lib/src/main/java/com/tbruyelle/rxpermissions2/RxPermissions.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,30 @@

public class RxPermissions {

static final String TAG = "RxPermissions";
static final String TAG = RxPermissions.class.getSimpleName();
static final Object TRIGGER = new Object();

RxPermissionsFragment mRxPermissionsFragment;
private final Lazy<RxPermissionsFragment> mRxPermissionsFragment;

public RxPermissions(@NonNull FragmentActivity activity) {
mRxPermissionsFragment = getRxPermissionsFragment(activity);
public RxPermissions(@NonNull final FragmentActivity activity) {
mRxPermissionsFragment = getLazySingleton(activity);
}

@NonNull
private Lazy<RxPermissionsFragment> getLazySingleton(@NonNull final FragmentActivity activity) {
return new Lazy<RxPermissionsFragment>() {

private RxPermissionsFragment rxPermissionsFragment;

@Override
public synchronized RxPermissionsFragment get() {
if (rxPermissionsFragment == null) {
rxPermissionsFragment = getRxPermissionsFragment(activity);
}
return rxPermissionsFragment;
}

};
}

private RxPermissionsFragment getRxPermissionsFragment(@NonNull FragmentActivity activity) {
Expand All @@ -51,8 +68,7 @@ private RxPermissionsFragment getRxPermissionsFragment(@NonNull FragmentActivity
fragmentManager
.beginTransaction()
.add(rxPermissionsFragment, TAG)
.commitAllowingStateLoss();
fragmentManager.executePendingTransactions();
.commitNow();
}
return rxPermissionsFragment;
}
Expand All @@ -62,7 +78,7 @@ private RxPermissionsFragment findRxPermissionsFragment(@NonNull FragmentActivit
}

public void setLogging(boolean logging) {
mRxPermissionsFragment.setLogging(logging);
mRxPermissionsFragment.get().setLogging(logging);
}

/**
Expand Down Expand Up @@ -186,7 +202,7 @@ public Observable<Permission> apply(Object o) {

private Observable<?> pending(final String... permissions) {
for (String p : permissions) {
if (!mRxPermissionsFragment.containsByPermission(p)) {
if (!mRxPermissionsFragment.get().containsByPermission(p)) {
return Observable.empty();
}
}
Expand All @@ -208,7 +224,7 @@ private Observable<Permission> requestImplementation(final String... permissions
// In case of multiple permissions, we create an Observable for each of them.
// At the end, the observables are combined to have a unique response.
for (String permission : permissions) {
mRxPermissionsFragment.log("Requesting permission " + permission);
mRxPermissionsFragment.get().log("Requesting permission " + permission);
if (isGranted(permission)) {
// Already granted, or not Android M
// Return a granted Permission object.
Expand All @@ -222,12 +238,12 @@ private Observable<Permission> requestImplementation(final String... permissions
continue;
}

PublishSubject<Permission> subject = mRxPermissionsFragment.getSubjectByPermission(permission);
PublishSubject<Permission> subject = mRxPermissionsFragment.get().getSubjectByPermission(permission);
// Create a new subject if not exists
if (subject == null) {
unrequestedPermissions.add(permission);
subject = PublishSubject.create();
mRxPermissionsFragment.setSubjectForPermission(permission, subject);
mRxPermissionsFragment.get().setSubjectForPermission(permission, subject);
}

list.add(subject);
Expand Down Expand Up @@ -272,8 +288,8 @@ private boolean shouldShowRequestPermissionRationaleImplementation(final Activit

@TargetApi(Build.VERSION_CODES.M)
void requestPermissionsFromFragment(String[] permissions) {
mRxPermissionsFragment.log("requestPermissionsFromFragment " + TextUtils.join(", ", permissions));
mRxPermissionsFragment.requestPermissions(permissions);
mRxPermissionsFragment.get().log("requestPermissionsFromFragment " + TextUtils.join(", ", permissions));
mRxPermissionsFragment.get().requestPermissions(permissions);
}

/**
Expand All @@ -283,7 +299,7 @@ void requestPermissionsFromFragment(String[] permissions) {
*/
@SuppressWarnings("WeakerAccess")
public boolean isGranted(String permission) {
return !isMarshmallow() || mRxPermissionsFragment.isGranted(permission);
return !isMarshmallow() || mRxPermissionsFragment.get().isGranted(permission);
}

/**
Expand All @@ -293,15 +309,20 @@ public boolean isGranted(String permission) {
*/
@SuppressWarnings("WeakerAccess")
public boolean isRevoked(String permission) {
return isMarshmallow() && mRxPermissionsFragment.isRevoked(permission);
return isMarshmallow() && mRxPermissionsFragment.get().isRevoked(permission);
}

boolean isMarshmallow() {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.M;
}

void onRequestPermissionsResult(String permissions[], int[] grantResults) {
mRxPermissionsFragment.onRequestPermissionsResult(permissions, grantResults, new boolean[permissions.length]);
mRxPermissionsFragment.get().onRequestPermissionsResult(permissions, grantResults, new boolean[permissions.length]);
}

@FunctionalInterface
public interface Lazy<V> {
V get();
}

}

0 comments on commit 10c5b53

Please sign in to comment.