// Path: app/src/main/java/br/ufs/demos/rxmvp/playground/core/behaviours/emptystate/AssignEmptyState.java
// public class AssignEmptyState<T> implements FlowableTransformer<T, T> {
// 
//     private EmptyStateView view;
//     private Scheduler uiScheduler;
// 
//     public AssignEmptyState(EmptyStateView view, Scheduler uiScheduler) {
//         this.view = view;
//         this.uiScheduler = uiScheduler;
//     }
// 
//     @Override public Publisher<T> apply(Flowable<T> upstream) {
// 
//         HideAtStartShowAtError<T> delegate = new HideAtStartShowAtError<>(
//                 view.hideEmptyState(),
//                 view.showEmptyState(),
//                 error -> error instanceof ContentNotFoundError,
//                 uiScheduler
//         );
// 
//         return upstream.compose(delegate);
//     }
// 
// }
// 
// Path: app/src/main/java/br/ufs/demos/rxmvp/playground/core/behaviours/errorstate/AssignErrorState.java
// public class AssignErrorState<T> implements FlowableTransformer<T, T> {
// 
//     private ErrorStateView view;
//     private Scheduler uiScheduler;
// 
//     public AssignErrorState(ErrorStateView view, Scheduler uiScheduler) {
//         this.view = view;
//         this.uiScheduler = uiScheduler;
//     }
// 
//     @Override public Publisher<T> apply(Flowable<T> upstream) {
//         HideAtStartShowAtError<T> delegate = new HideAtStartShowAtError<>(
//                 view.hideErrorState(),
//                 view.showErrorState(),
//                 error -> !(error instanceof ContentNotFoundError),
//                 uiScheduler
//         );
// 
//         return upstream.compose(delegate);
//     }
// 
// }
// 
// Path: app/src/main/java/br/ufs/demos/rxmvp/playground/core/behaviours/loadingcontent/LoadingCoordination.java
// public class LoadingCoordination<T> implements FlowableTransformer<T, T> {
// 
//     private LoadingView view;
//     private Scheduler uiScheduler;
// 
//     public LoadingCoordination(LoadingView view,
//                                Scheduler uiScheduler) {
//         this.view = view;
//         this.uiScheduler = uiScheduler;
//     }
// 
//     @Override public Publisher<T> apply(Flowable<T> upstream) {
// 
//         ShowAtStartHideWhenDone<T> delegate = new ShowAtStartHideWhenDone<>(
//                 view.showLoading(),
//                 view.hideLoading(),
//                 uiScheduler
//         );
// 
//         return upstream.compose(delegate);
//     }
// 
// }
// 
// Path: app/src/main/java/br/ufs/demos/rxmvp/playground/core/behaviours/networking/NetworkingErrorFeedback.java
// public class NetworkingErrorFeedback<T> implements FlowableTransformer<T, T> {
// 
//     private NetworkingErrorView view;
//     private Scheduler uiScheduler;
// 
//     public NetworkingErrorFeedback(NetworkingErrorView view, Scheduler uiScheduler) {
//         this.view = view;
//         this.uiScheduler = uiScheduler;
//     }
// 
//     @Override public Publisher<T> apply(Flowable<T> upstream) {
//         return upstream.doOnError(this::handleIfNetworkingError);
//     }
// 
//     private Publisher<T> handleIfNetworkingError(Throwable throwable) {
// 
//         if (throwable instanceof NetworkingError) {
//             Completable.fromAction(view.reportNetworkingError())
//                     .observeOn(uiScheduler)
//                     .subscribe();
//         }
// 
//         return Flowable.error(throwable);
//     }
// 
// }
// 
// Path: app/src/main/java/br/ufs/demos/rxmvp/playground/core/behaviours/tooglerefresh/RefreshToogle.java
// public class RefreshToogle<T> implements FlowableTransformer<T, T> {
// 
//     private final ToogleRefreshView view;
//     private Scheduler targetScheduler;
// 
//     public RefreshToogle(ToogleRefreshView view, Scheduler targetScheduler) {
//         this.view = view;
//         this.targetScheduler = targetScheduler;
//     }
// 
//     @Override public Publisher<T> apply(Flowable<T> upstream) {
//         return upstream
//                 .doOnSubscribe(subscription -> fireAction(view.disableRefresh()))
//                 .doOnError(this::enableForSpecialErrorCase)
//                 .doOnComplete(() -> fireAction(view.enableRefresh()));
//     }
// 
//     private void enableForSpecialErrorCase(Throwable throwable) {
//         if (!(throwable instanceof UnexpectedResponseError)) fireAction(view.enableRefresh());
//     }
// 
//     private void fireAction(Action toPerform) {
//         Completable.fromAction(toPerform)
//                 .subscribeOn(targetScheduler)
//                 .subscribe();
//     }
// 
// }


import org.reactivestreams.Publisher;
import br.ufs.demos.rxmvp.playground.core.behaviours.emptystate.AssignEmptyState;
import br.ufs.demos.rxmvp.playground.core.behaviours.errorstate.AssignErrorState;
import br.ufs.demos.rxmvp.playground.core.behaviours.loadingcontent.LoadingCoordination;
import br.ufs.demos.rxmvp.playground.core.behaviours.networking.NetworkingErrorFeedback;
import br.ufs.demos.rxmvp.playground.core.behaviours.tooglerefresh.RefreshToogle;
import io.reactivex.Flowable;
import io.reactivex.FlowableTransformer;
package br.ufs.demos.rxmvp.playground.core.behaviours;



/**
 * Created by bira on 6/30/17.
 */

public class BehavioursCoordinator<T> implements FlowableTransformer<T, T> {

    private AssignEmptyState<T> dealWithEmptyState;
    private AssignErrorState<T> assignErrorState;
    private LoadingCoordination<T> loadingCoordinator;
    private NetworkingErrorFeedback<T> networkingErrorFeedback;
    private RefreshToogle<T> refreshToogle;

    public BehavioursCoordinator(AssignEmptyState<T> dealWithEmptyState,
                                 AssignErrorState<T> assignErrorState,
                                 LoadingCoordination<T> loadingCoordinator,
                                 NetworkingErrorFeedback<T> networkingErrorFeedback,
// Path: app/src/main/java/br/ufs/demos/rxmvp/playground/core/behaviours/emptystate/AssignEmptyState.java
// public class AssignEmptyState<T> implements FlowableTransformer<T, T> {
// 
//     private EmptyStateView view;
//     private Scheduler uiScheduler;
// 
//     public AssignEmptyState(EmptyStateView view, Scheduler uiScheduler) {
//         this.view = view;
//         this.uiScheduler = uiScheduler;
//     }
// 
//     @Override public Publisher<T> apply(Flowable<T> upstream) {
// 
//         HideAtStartShowAtError<T> delegate = new HideAtStartShowAtError<>(
//                 view.hideEmptyState(),
//                 view.showEmptyState(),
//                 error -> error instanceof ContentNotFoundError,
//                 uiScheduler
//         );
// 
//         return upstream.compose(delegate);
//     }
// 
// }
// 
// Path: app/src/main/java/br/ufs/demos/rxmvp/playground/core/behaviours/errorstate/AssignErrorState.java
// public class AssignErrorState<T> implements FlowableTransformer<T, T> {
// 
//     private ErrorStateView view;
//     private Scheduler uiScheduler;
// 
//     public AssignErrorState(ErrorStateView view, Scheduler uiScheduler) {
//         this.view = view;
//         this.uiScheduler = uiScheduler;
//     }
// 
//     @Override public Publisher<T> apply(Flowable<T> upstream) {
//         HideAtStartShowAtError<T> delegate = new HideAtStartShowAtError<>(
//                 view.hideErrorState(),
//                 view.showErrorState(),
//                 error -> !(error instanceof ContentNotFoundError),
//                 uiScheduler
//         );
// 
//         return upstream.compose(delegate);
//     }
// 
// }
// 
// Path: app/src/main/java/br/ufs/demos/rxmvp/playground/core/behaviours/loadingcontent/LoadingCoordination.java
// public class LoadingCoordination<T> implements FlowableTransformer<T, T> {
// 
//     private LoadingView view;
//     private Scheduler uiScheduler;
// 
//     public LoadingCoordination(LoadingView view,
//                                Scheduler uiScheduler) {
//         this.view = view;
//         this.uiScheduler = uiScheduler;
//     }
// 
//     @Override public Publisher<T> apply(Flowable<T> upstream) {
// 
//         ShowAtStartHideWhenDone<T> delegate = new ShowAtStartHideWhenDone<>(
//                 view.showLoading(),
//                 view.hideLoading(),
//                 uiScheduler
//         );
// 
//         return upstream.compose(delegate);
//     }
// 
// }
// 
// Path: app/src/main/java/br/ufs/demos/rxmvp/playground/core/behaviours/networking/NetworkingErrorFeedback.java
// public class NetworkingErrorFeedback<T> implements FlowableTransformer<T, T> {
// 
//     private NetworkingErrorView view;
//     private Scheduler uiScheduler;
// 
//     public NetworkingErrorFeedback(NetworkingErrorView view, Scheduler uiScheduler) {
//         this.view = view;
//         this.uiScheduler = uiScheduler;
//     }
// 
//     @Override public Publisher<T> apply(Flowable<T> upstream) {
//         return upstream.doOnError(this::handleIfNetworkingError);
//     }
// 
//     private Publisher<T> handleIfNetworkingError(Throwable throwable) {
// 
//         if (throwable instanceof NetworkingError) {
//             Completable.fromAction(view.reportNetworkingError())
//                     .observeOn(uiScheduler)
//                     .subscribe();
//         }
// 
//         return Flowable.error(throwable);
//     }
// 
// }
// 
// Path: app/src/main/java/br/ufs/demos/rxmvp/playground/core/behaviours/tooglerefresh/RefreshToogle.java
// public class RefreshToogle<T> implements FlowableTransformer<T, T> {
// 
//     private final ToogleRefreshView view;
//     private Scheduler targetScheduler;
// 
//     public RefreshToogle(ToogleRefreshView view, Scheduler targetScheduler) {
//         this.view = view;
//         this.targetScheduler = targetScheduler;
//     }
// 
//     @Override public Publisher<T> apply(Flowable<T> upstream) {
//         return upstream
//                 .doOnSubscribe(subscription -> fireAction(view.disableRefresh()))
//                 .doOnError(this::enableForSpecialErrorCase)
//                 .doOnComplete(() -> fireAction(view.enableRefresh()));
//     }
// 
//     private void enableForSpecialErrorCase(Throwable throwable) {
//         if (!(throwable instanceof UnexpectedResponseError)) fireAction(view.enableRefresh());
//     }
// 
//     private void fireAction(Action toPerform) {
//         Completable.fromAction(toPerform)
//                 .subscribeOn(targetScheduler)
//                 .subscribe();
//     }
// 
// }


// Path: app/src/main/java/br/ufs/demos/rxmvp/playground/core/behaviours/BehavioursCoordinator.java
import org.reactivestreams.Publisher;
import br.ufs.demos.rxmvp.playground.core.behaviours.emptystate.AssignEmptyState;
import br.ufs.demos.rxmvp.playground.core.behaviours.errorstate.AssignErrorState;
import br.ufs.demos.rxmvp.playground.core.behaviours.loadingcontent.LoadingCoordination;
import br.ufs.demos.rxmvp.playground.core.behaviours.networking.NetworkingErrorFeedback;
import br.ufs.demos.rxmvp.playground.core.behaviours.tooglerefresh.RefreshToogle;
import io.reactivex.Flowable;
import io.reactivex.FlowableTransformer;

package br.ufs.demos.rxmvp.playground.core.behaviours;



/**
 * Created by bira on 6/30/17.
 */

public class BehavioursCoordinator<T> implements FlowableTransformer<T, T> {

    private AssignEmptyState<T> dealWithEmptyState;
    private AssignErrorState<T> assignErrorState;
    private LoadingCoordination<T> loadingCoordinator;
    private NetworkingErrorFeedback<T> networkingErrorFeedback;
    private RefreshToogle<T> refreshToogle;

    public BehavioursCoordinator(AssignEmptyState<T> dealWithEmptyState,
                                 AssignErrorState<T> assignErrorState,
                                 LoadingCoordination<T> loadingCoordinator,
                                 NetworkingErrorFeedback<T> networkingErrorFeedback,
                                 RefreshToogle<T> refreshToogle) {