// Path: rx_paparazzo/src/main/java/com/miguelbcr/ui/rx_paparazzo2/entities/PermissionDeniedException.java
// public class PermissionDeniedException extends RuntimeException {
//   private int code;
// 
//   public PermissionDeniedException(int code) {
//     this.code = code;
//   }
// 
//   public int getCode() {
//     return code;
//   }
// }
// 
// Path: rx_paparazzo/src/main/java/com/miguelbcr/ui/rx_paparazzo2/entities/Response.java
// public class Response<T, D> {
//   private final T targetUI;
//   private final D data;
//   private final int resultCode;
// 
//   public Response(T targetUI, D data, int resultCode) {
//     this.targetUI = targetUI;
//     this.data = data;
//     this.resultCode = resultCode;
//   }
// 
//   public T targetUI() {
//     return targetUI;
//   }
// 
//   public D data() {
//     return data;
//   }
// 
//   /**
//    * @return <ul>
//    * <li>{@link Activity#RESULT_OK}</li>
//    * <li>{@link Activity#RESULT_CANCELED}</li>
//    * <li>{@link RxPaparazzo#RESULT_DENIED_PERMISSION}</li>
//    * <li>{@link RxPaparazzo#RESULT_DENIED_PERMISSION_NEVER_ASK}</li>
//    * </ul>
//    */
//   public int resultCode() {
//     return resultCode;
//   }
// }
// 
// 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 com.miguelbcr.ui.rx_paparazzo2.entities.PermissionDeniedException;
import com.miguelbcr.ui.rx_paparazzo2.entities.Response;
import com.miguelbcr.ui.rx_paparazzo2.entities.TargetUi;
import com.miguelbcr.ui.rx_paparazzo2.entities.UserCanceledException;
import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.ObservableTransformer;
import io.reactivex.exceptions.Exceptions;
import io.reactivex.functions.Function;
/*
 * Copyright 2016 Miguel Garcia
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.miguelbcr.ui.rx_paparazzo2.workers;


abstract class Worker {

  private final TargetUi targetUi;

  public Worker(TargetUi targetUi) {
    this.targetUi = targetUi;
  }

  @SuppressWarnings("unchecked") protected <T> ObservableTransformer<T, T> applyOnError() {
    return new ObservableTransformer<T, T>() {
      @Override public ObservableSource<T> apply(Observable<T> observable) {
        return observable.onErrorResumeNext(
            new Function<Throwable, ObservableSource<? extends T>>() {
              @Override public ObservableSource<? extends T> apply(Throwable throwable)
                  throws Exception {
                if (throwable instanceof UserCanceledException) {
                  return Observable.just(
                      (T) new Response(targetUi.ui(), null, Activity.RESULT_CANCELED));
                } else if (throwable instanceof PermissionDeniedException) {
// Path: rx_paparazzo/src/main/java/com/miguelbcr/ui/rx_paparazzo2/entities/PermissionDeniedException.java
// public class PermissionDeniedException extends RuntimeException {
//   private int code;
// 
//   public PermissionDeniedException(int code) {
//     this.code = code;
//   }
// 
//   public int getCode() {
//     return code;
//   }
// }
// 
// Path: rx_paparazzo/src/main/java/com/miguelbcr/ui/rx_paparazzo2/entities/Response.java
// public class Response<T, D> {
//   private final T targetUI;
//   private final D data;
//   private final int resultCode;
// 
//   public Response(T targetUI, D data, int resultCode) {
//     this.targetUI = targetUI;
//     this.data = data;
//     this.resultCode = resultCode;
//   }
// 
//   public T targetUI() {
//     return targetUI;
//   }
// 
//   public D data() {
//     return data;
//   }
// 
//   /**
//    * @return <ul>
//    * <li>{@link Activity#RESULT_OK}</li>
//    * <li>{@link Activity#RESULT_CANCELED}</li>
//    * <li>{@link RxPaparazzo#RESULT_DENIED_PERMISSION}</li>
//    * <li>{@link RxPaparazzo#RESULT_DENIED_PERMISSION_NEVER_ASK}</li>
//    * </ul>
//    */
//   public int resultCode() {
//     return resultCode;
//   }
// }
// 
// 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/workers/Worker.java
import android.app.Activity;
import com.miguelbcr.ui.rx_paparazzo2.entities.PermissionDeniedException;
import com.miguelbcr.ui.rx_paparazzo2.entities.Response;
import com.miguelbcr.ui.rx_paparazzo2.entities.TargetUi;
import com.miguelbcr.ui.rx_paparazzo2.entities.UserCanceledException;
import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.ObservableTransformer;
import io.reactivex.exceptions.Exceptions;
import io.reactivex.functions.Function;

/*
 * Copyright 2016 Miguel Garcia
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.miguelbcr.ui.rx_paparazzo2.workers;


abstract class Worker {

  private final TargetUi targetUi;

  public Worker(TargetUi targetUi) {
    this.targetUi = targetUi;
  }

  @SuppressWarnings("unchecked") protected <T> ObservableTransformer<T, T> applyOnError() {
    return new ObservableTransformer<T, T>() {
      @Override public ObservableSource<T> apply(Observable<T> observable) {
        return observable.onErrorResumeNext(
            new Function<Throwable, ObservableSource<? extends T>>() {
              @Override public ObservableSource<? extends T> apply(Throwable throwable)
                  throws Exception {
                if (throwable instanceof UserCanceledException) {
                  return Observable.just(
                      (T) new Response(targetUi.ui(), null, Activity.RESULT_CANCELED));
                } else if (throwable instanceof PermissionDeniedException) {
                  return Observable.just((T) new Response(targetUi.ui(), null,