// Path: rx_paparazzo/src/main/java/com/miguelbcr/ui/rx_paparazzo2/entities/TargetUi.java
// public class TargetUi {
//   private Object ui;
// 
//   public TargetUi(Object ui) {
//     this.ui = ui;
//   }
// 
//   public FragmentActivity activity() {
//     return fragment() != null ? fragment().getActivity() : (FragmentActivity) ui;
//   }
// 
//   @Nullable
//   public Fragment fragment() {
//     if (ui instanceof Fragment) {
//       return (Fragment) ui;
//     }
//     return null;
//   }
// 
//   public Object ui() {
//     return ui;
//   }
// 
//   public void setUi(Object ui) {
//     this.ui = ui;
//   }
// 
//   public Context getContext() {
//     return fragment() == null ? activity() : fragment().getContext();
//   }
// }
// 
// Path: rx_paparazzo/src/main/java/com/miguelbcr/ui/rx_paparazzo2/entities/UserCanceledException.java
// public class UserCanceledException extends RuntimeException {
// }


import android.app.Activity;
import android.content.Intent;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import com.miguelbcr.ui.rx_paparazzo2.entities.TargetUi;
import com.miguelbcr.ui.rx_paparazzo2.entities.UserCanceledException;
import io.reactivex.Observable;
import io.reactivex.functions.Function;
import rx_activity_result2.OnPreResult;
import rx_activity_result2.Result;
import rx_activity_result2.RxActivityResult;
  StartIntent with(Intent intent, OnPreResult onPreResult) {
    this.intent = intent;
    this.onPreResult = onPreResult;
    return this;
  }

  @Override public Observable<Intent> react() {
    final Fragment fragment = targetUi.fragment();
    if (fragment != null) {
      return RxActivityResult.on(fragment)
          .startIntent(intent, onPreResult)
          .map(new Function<Result<Fragment>, Intent>() {
            @Override public Intent apply(Result<Fragment> result) throws Exception {
              return StartIntent.this.getResponse(result);
            }
          });
    } else {
      return RxActivityResult.on(targetUi.activity())
          .startIntent(intent, onPreResult)
          .map(new Function<Result<FragmentActivity>, Intent>() {
            @Override public Intent apply(Result<FragmentActivity> result) throws Exception {
              return StartIntent.this.getResponse(result);
            }
          });
    }
  }

  private Intent getResponse(Result result) {
    targetUi.setUi(result.targetUI());
    if (result.resultCode() != Activity.RESULT_OK) {
// Path: rx_paparazzo/src/main/java/com/miguelbcr/ui/rx_paparazzo2/entities/TargetUi.java
// public class TargetUi {
//   private Object ui;
// 
//   public TargetUi(Object ui) {
//     this.ui = ui;
//   }
// 
//   public FragmentActivity activity() {
//     return fragment() != null ? fragment().getActivity() : (FragmentActivity) ui;
//   }
// 
//   @Nullable
//   public Fragment fragment() {
//     if (ui instanceof Fragment) {
//       return (Fragment) ui;
//     }
//     return null;
//   }
// 
//   public Object ui() {
//     return ui;
//   }
// 
//   public void setUi(Object ui) {
//     this.ui = ui;
//   }
// 
//   public Context getContext() {
//     return fragment() == null ? activity() : fragment().getContext();
//   }
// }
// 
// Path: rx_paparazzo/src/main/java/com/miguelbcr/ui/rx_paparazzo2/entities/UserCanceledException.java
// public class UserCanceledException extends RuntimeException {
// }


// Path: rx_paparazzo/src/main/java/com/miguelbcr/ui/rx_paparazzo2/interactors/StartIntent.java
import android.app.Activity;
import android.content.Intent;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import com.miguelbcr.ui.rx_paparazzo2.entities.TargetUi;
import com.miguelbcr.ui.rx_paparazzo2.entities.UserCanceledException;
import io.reactivex.Observable;
import io.reactivex.functions.Function;
import rx_activity_result2.OnPreResult;
import rx_activity_result2.Result;
import rx_activity_result2.RxActivityResult;

  StartIntent with(Intent intent, OnPreResult onPreResult) {
    this.intent = intent;
    this.onPreResult = onPreResult;
    return this;
  }

  @Override public Observable<Intent> react() {
    final Fragment fragment = targetUi.fragment();
    if (fragment != null) {
      return RxActivityResult.on(fragment)
          .startIntent(intent, onPreResult)
          .map(new Function<Result<Fragment>, Intent>() {
            @Override public Intent apply(Result<Fragment> result) throws Exception {
              return StartIntent.this.getResponse(result);
            }
          });
    } else {
      return RxActivityResult.on(targetUi.activity())
          .startIntent(intent, onPreResult)
          .map(new Function<Result<FragmentActivity>, Intent>() {
            @Override public Intent apply(Result<FragmentActivity> result) throws Exception {
              return StartIntent.this.getResponse(result);
            }
          });
    }
  }

  private Intent getResponse(Result result) {
    targetUi.setUi(result.targetUI());
    if (result.resultCode() != Activity.RESULT_OK) {
      throw new UserCanceledException();