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

Commit

Permalink
Added interface (returning an Observable) in order to pre process dat…
Browse files Browse the repository at this point in the history
…a before finishing onActivityResult
  • Loading branch information
miguelbcr committed Oct 5, 2016
1 parent 08d73bc commit c36843e
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 22 deletions.
10 changes: 9 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,20 @@ android {

defaultConfig {
applicationId "io.victoralbertos.app"
minSdkVersion 14
minSdkVersion 16
targetSdkVersion 24
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
// productFlavors {
// production {
// minSdkVersion 16
// }
// uiTest {
// minSdkVersion 18
// }
// }
buildTypes {
release {
minifyEnabled false
Expand Down
1 change: 0 additions & 1 deletion app/src/androidTest/java/app/OnPreResultTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import org.junit.Test;
import org.junit.runner.RunWith;

import app.multi_start.MultiStartActivity;
import io.victoralbertos.app.R;

import static android.support.test.espresso.Espresso.onView;
Expand Down
13 changes: 6 additions & 7 deletions app/src/main/java/app/OnPreResultActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

import app.multi_start.FirstActivity;
import app.multi_start.SecondActivity;
import io.victoralbertos.app.R;
import rx_activity_result.OnResult;
import rx.Observable;
import rx_activity_result.RxActivityResult;

public class OnPreResultActivity extends AppCompatActivity {
Expand All @@ -29,13 +27,14 @@ protected void onCreate(Bundle savedInstanceState) {

startPreForResult.setOnClickListener(v ->
RxActivityResult.on(this)
.startIntent(new Intent(this, FirstActivity.class), (OnResult) (resultCode, data) ->
data.putExtra(EXTRA_PRE, "Do whatever you want with the data, but not with the UI"))
.startIntent(new Intent(this, FirstActivity.class), (resultCode, data) ->
Observable.just(data.getData())
.map(uri -> data.putExtra(EXTRA_PRE, "Do whatever you want with the data, but not with the UI")))
.subscribe(result -> {
result.targetUI()
.preResult.setText(result.data().getStringExtra(EXTRA_PRE));
.preResult.setText(result.data().getStringExtra(EXTRA_PRE));
result.targetUI()
.result.setText(result.data().getStringExtra(FirstActivity.EXTRA));
.result.setText(result.data().getStringExtra(FirstActivity.EXTRA));
})
);
}
Expand Down
2 changes: 1 addition & 1 deletion rx_activity_result/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ android {
buildToolsVersion "24.0.1"

defaultConfig {
minSdkVersion 14
minSdkVersion 16
targetSdkVersion 24
versionCode 1
versionName "1.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,13 @@
import android.content.IntentSender;
import android.os.Bundle;

import rx.functions.Action0;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

public class HolderActivity extends Activity {
private static Request request;
private OnResult onPreResult;
private OnPreResult onPreResult;
private OnResult onResult;
private int resultCode;
private Intent data;
Expand Down Expand Up @@ -79,10 +83,17 @@ private void startIntentSenderWithOptions(RequestIntentSender requestIntentSende
this.data = data;

if (this.onPreResult != null) {
this.onPreResult.response(resultCode, data);
this.onPreResult.response(resultCode, data)
.doOnCompleted(new Action0() {
@Override
public void call() {
finish();
}
})
.subscribe();
} else {
finish();
}

finish();
}

@Override protected void onDestroy() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package rx_activity_result;

import android.content.Intent;
import android.support.annotation.Nullable;

import rx.Observable;

public interface OnPreResult<T> {
Observable<T> response(int resultCode, @Nullable Intent data);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import android.content.Intent;
import android.support.annotation.Nullable;

import java.io.Serializable;

public interface OnResult extends Serializable {
interface OnResult {
void response(int resultCode, @Nullable Intent data);
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,18 @@

class Request {
private final Intent intent;
private OnResult onPreResult;
private OnPreResult onPreResult;
private OnResult onResult;

Request(Intent intent) {
this.intent = intent;
}

void setOnPreResult(OnResult onPreResult) {
void setOnPreResult(OnPreResult onPreResult) {
this.onPreResult = onPreResult;
}

OnResult onPreResult() {
OnPreResult onPreResult() {
return onPreResult;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ public Observable<Result<T>> startIntent(final Intent intent) {
return startIntent(intent, null);
}

public Observable<Result<T>> startIntent(final Intent intent, OnResult onPreResult) {
public Observable<Result<T>> startIntent(final Intent intent, OnPreResult onPreResult) {
return startHolderActivity(new Request(intent), onPreResult);
}

private Observable<Result<T>> startHolderActivity(Request request, OnResult onPreResult) {
private Observable<Result<T>> startHolderActivity(Request request, OnPreResult onPreResult) {

OnResult onResult = uiTargetActivity ? onResultActivity() : onResultFragment();
request.setOnResult(onResult);
Expand Down

0 comments on commit c36843e

Please sign in to comment.