Skip to content
This repository has been archived by the owner on Aug 9, 2020. It is now read-only.

Commit

Permalink
Merge pull request #38 from esunilkumare/feature/Add_RequestCode_Para…
Browse files Browse the repository at this point in the history
…meter_In_Result

Feature/add request code parameter in result
  • Loading branch information
VictorAlbertos authored Apr 13, 2017
2 parents 39975cc + ac7a397 commit 366e1fb
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 27 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ RxActivityResult.on(this).startIntent(takePhoto)
.subscribe(result -> {
Intent data = result.data();
int resultCode = result.resultCode();
// the requestCode using which the activity is started can be received here.
int requestCode = result.requestCode();

if(requestCode == YourActivity.YOUR_REQUEST_CODE)
{
// Do Something
}

if (resultCode == RESULT_OK) {
result.targetUI().showImage(data);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,20 @@
import android.content.Intent;
import android.content.IntentSender;
import android.os.Bundle;

import io.reactivex.functions.Action;

public class HolderActivity extends Activity {
private static Request request;
private OnPreResult onPreResult;
private OnResult onResult;
private int resultCode;
private int requestCode;
private Intent data;
private static int FAILED_REQUEST_CODE = -909;

@Override protected void onCreate(Bundle savedInstanceState) {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

if (request == null) {
Expand Down Expand Up @@ -59,7 +63,7 @@ private void startIntentSender(RequestIntentSender requestIntentSender) {
requestIntentSender.getFlagsValues(), requestIntentSender.getExtraFlags());
} catch (IntentSender.SendIntentException exception) {
exception.printStackTrace();
onResult.response(RESULT_CANCELED, null);
onResult.response(FAILED_REQUEST_CODE, RESULT_CANCELED, null);
}
}

Expand All @@ -70,32 +74,36 @@ private void startIntentSenderWithOptions(RequestIntentSender requestIntentSende
requestIntentSender.getFlagsValues(), requestIntentSender.getExtraFlags(), requestIntentSender.getOptions());
} catch (IntentSender.SendIntentException exception) {
exception.printStackTrace();
onResult.response(RESULT_CANCELED, null);
onResult.response(FAILED_REQUEST_CODE, RESULT_CANCELED, null);
}
}

@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) {
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
this.resultCode = resultCode;
this.requestCode = requestCode;
this.data = data;

if (this.onPreResult != null) {
this.onPreResult.response(resultCode, data)
.doOnComplete(new Action() {
@Override public void run() throws Exception {
finish();
}
})
.subscribe();
this.onPreResult.response(requestCode, resultCode, data)
.doOnComplete(new Action() {
@Override
public void run() throws Exception {
finish();
}
})
.subscribe();
} else {
finish();
}
}

@Override protected void onDestroy() {
@Override
protected void onDestroy() {
super.onDestroy();
if (onResult != null)
onResult.response(resultCode, data);
onResult.response(requestCode, resultCode, data);
}

static void setRequest(Request aRequest) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
import io.reactivex.Observable;

public interface OnPreResult<T> {
Observable<T> response(int resultCode, @Nullable Intent data);
Observable<T> response(int requestCode, int resultCode, @Nullable Intent data);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
import java.io.Serializable;

interface OnResult extends Serializable {
void response(int resultCode, @Nullable Intent data);
void response(int requestCode, int resultCode, @Nullable Intent data);
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,20 @@
public class Result<T> {
private final T targetUI;
private final int resultCode;
private final int requestCode;
private final Intent data;

public Result(T targetUI, int resultCode, Intent data) {
public Result(T targetUI, int requestCode, int resultCode, Intent data) {
this.targetUI = targetUI;
this.resultCode = resultCode;
this.requestCode = requestCode;
this.data = data;
}

public int requestCode() {
return requestCode;
}

public int resultCode() {
return resultCode;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,18 @@
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;

import java.util.List;

import io.reactivex.Observable;
import io.reactivex.functions.Consumer;
import io.reactivex.subjects.PublishSubject;
import java.util.List;


public final class RxActivityResult {
static ActivitiesLifecycleCallbacks activitiesLifecycle;

private RxActivityResult() {}
private RxActivityResult() {
}

public static void register(final Application application) {
activitiesLifecycle = new ActivitiesLifecycleCallbacks(application);
Expand Down Expand Up @@ -89,9 +91,10 @@ private Observable<Result<T>> startHolderActivity(Request request, @Nullable OnP
HolderActivity.setRequest(request);

activitiesLifecycle.getOLiveActivity().subscribe(new Consumer<Activity>() {
@Override public void accept(Activity activity) throws Exception {
@Override
public void accept(Activity activity) throws Exception {
activity.startActivity(new Intent(activity, HolderActivity.class)
.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION));
.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION));
}
});

Expand All @@ -100,7 +103,8 @@ private Observable<Result<T>> startHolderActivity(Request request, @Nullable OnP

private OnResult onResultActivity() {
return new OnResult() {
@Override public void response(int resultCode, Intent data) {
@Override
public void response(int requestCode, int resultCode, Intent data) {
if (activitiesLifecycle.getLiveActivity() == null) return;

//If true it means some other activity has been stacked as a secondary process.
Expand All @@ -110,15 +114,16 @@ private OnResult onResultActivity() {
}

T activity = (T) activitiesLifecycle.getLiveActivity();
subject.onNext(new Result<>(activity, resultCode, data));
subject.onNext(new Result<>(activity, requestCode, resultCode, data));
subject.onComplete();
}
};
}

private OnResult onResultFragment() {
return new OnResult() {
@Override public void response(int resultCode, Intent data) {
@Override
public void response(int requestCode, int resultCode, Intent data) {
if (activitiesLifecycle.getLiveActivity() == null) return;

Activity activity = activitiesLifecycle.getLiveActivity();
Expand All @@ -128,8 +133,8 @@ private OnResult onResultFragment() {

Fragment targetFragment = getTargetFragment(fragmentManager.getFragments());

if(targetFragment != null) {
subject.onNext(new Result<>((T) targetFragment, resultCode, data));
if (targetFragment != null) {
subject.onNext(new Result<>((T) targetFragment, requestCode, resultCode, data));
subject.onComplete();
}

Expand All @@ -139,11 +144,12 @@ private OnResult onResultFragment() {
};
}

@Nullable Fragment getTargetFragment(List<Fragment> fragments) {
@Nullable
Fragment getTargetFragment(List<Fragment> fragments) {
if (fragments == null) return null;

for (Fragment fragment : fragments) {
if(fragment != null && fragment.isVisible() && fragment.getClass() == clazz) {
if (fragment != null && fragment.isVisible() && fragment.getClass() == clazz) {
return fragment;
} else if (fragment != null && fragment.getChildFragmentManager() != null) {
List<Fragment> childFragments = fragment.getChildFragmentManager().getFragments();
Expand Down

0 comments on commit 366e1fb

Please sign in to comment.