// Path: RxDbFlow-Rx2/src/main/java/au/com/roadhouse/rxdbflow/rx2/sql/observables/functions/ValueAction.java
// public interface ValueAction<T> {
//     T run();
// }
// 
// Path: RxDbFlow-Rx2/src/main/java/au/com/roadhouse/rxdbflow/rx2/sql/observables/operators/DBFlowRestartOnChange.java
// public class DBFlowRestartOnChange<T> extends Observable<T> implements HasUpstreamObservableSource<T> {
//     private final ObservableSource<T> mSource;
//     private final Class[] mSubscribedClasses;
//     private final ValueAction<T> mRestartAction;
// 
//     public DBFlowRestartOnChange(Observable<T> source, Class[] subscribedClasses, ValueAction<T> restartAction) {
//         mSource = source;
//         mSubscribedClasses = subscribedClasses;
//         mRestartAction = restartAction;
//     }
// 
//     @Override
//     protected void subscribeActual(Observer<? super T> observer) {
//         final RestartOnChangeObserver<T> disposable = new RestartOnChangeObserver<T>(observer, mSubscribedClasses, mRestartAction);
//         mSource.subscribe(disposable);
//     }
// 
//     @Override
//     public ObservableSource<T> source() {
//         return mSource;
//     }
// 
//     private class RestartOnChangeObserver<T> implements Observer<T>, Disposable, DbTransaction.OnDbTransactionListener {
//         private final Observer<? super T> mActual;
//         private final ValueAction<T> mRestartAction;
//         private List<Class> mSubscribedClasses;
//         private boolean mIsDisposed = false;
//         private FlowContentObserver mFlowContentObserver = new FlowContentObserver();
//         private boolean mIsInTransaction = false;
//         private boolean mHasPendingChange = false;
// 
// 
//         public RestartOnChangeObserver(Observer<? super T> observer, Class[] subscribedClasses, ValueAction<T> action) {
//             mActual = observer;
//             mSubscribedClasses = Arrays.asList(subscribedClasses);
//             mRestartAction = action;
//             DbTransaction.registerDbTransactionListener(mSubscribedClasses.get(0), this);
//         }
// 
//         @Override
//         public void onSubscribe(Disposable d) {
//             mActual.onSubscribe(this);
// 
//         }
// 
//         @Override
//         public void onError(Throwable e) {
//             mActual.onError(e);
//         }
// 
//         @Override
//         public void onComplete() {
//             //We capture any completes here to avoid disposing
//         }
// 
//         @Override
//         public void onNext(T o) {
//             mActual.onNext(o);
// 
//             for (int i = 0; i < mSubscribedClasses.size(); i++) {
//                 mFlowContentObserver.registerForContentChanges(FlowManager.getContext(), mSubscribedClasses.get(i));
//             }
// 
//             mFlowContentObserver.addOnTableChangedListener(
//                     new FlowContentObserver.OnTableChangedListener() {
//                         @Override
//                         public void onTableChanged(@Nullable Class<?> tableChanged, BaseModel.Action action) {
//                             if(isDisposed()){
//                                 return;
//                             }
// 
//                             if (!mIsInTransaction) {
//                                 mActual.onNext(mRestartAction.run());
//                             } else {
//                                 mHasPendingChange = true;
//                             }
//                         }
//                     });
//         }
// 
//         @Override
//         public void dispose() {
//             mFlowContentObserver.unregisterForContentChanges(FlowManager.getContext());
//             mIsDisposed = true;
//             mActual.onComplete();
//             DbTransaction.unregisterDbTransactionListener(mSubscribedClasses.get(0), this);
//         }
// 
//         @Override
//         public boolean isDisposed() {
//             return mIsDisposed;
//         }
// 
//         @Override
//         public void onTransactionStarted() {
//             mIsInTransaction = true;
//         }
// 
//         @Override
//         public void onTransactionEnded() {
//             mIsInTransaction = false;
//             if(mHasPendingChange && !isDisposed()){
//                 mHasPendingChange = false;
//                 mActual.onNext(mRestartAction.run());
//             }
//         }
//     }
// }


import au.com.roadhouse.rxdbflow.rx2.sql.observables.functions.ValueAction;
import au.com.roadhouse.rxdbflow.rx2.sql.observables.operators.DBFlowRestartOnChange;
import io.reactivex.Maybe;
import io.reactivex.Observable;
package au.com.roadhouse.rxdbflow.rx2.sql.observables;


/**
 * A base observable which provides DBFlow specific operators
 * @param <T> The observable result type
 */
public abstract class DBFlowMaybe<T> extends Maybe<T> implements ValueAction<T> {

    /**
     * Observes changes on the current table, restarting the query on change and emits the new count
     * to any subscribers
     * @return An observable which observes any changes in the current table
     */
    public final Observable<T> restartOnChange(){
// Path: RxDbFlow-Rx2/src/main/java/au/com/roadhouse/rxdbflow/rx2/sql/observables/functions/ValueAction.java
// public interface ValueAction<T> {
//     T run();
// }
// 
// Path: RxDbFlow-Rx2/src/main/java/au/com/roadhouse/rxdbflow/rx2/sql/observables/operators/DBFlowRestartOnChange.java
// public class DBFlowRestartOnChange<T> extends Observable<T> implements HasUpstreamObservableSource<T> {
//     private final ObservableSource<T> mSource;
//     private final Class[] mSubscribedClasses;
//     private final ValueAction<T> mRestartAction;
// 
//     public DBFlowRestartOnChange(Observable<T> source, Class[] subscribedClasses, ValueAction<T> restartAction) {
//         mSource = source;
//         mSubscribedClasses = subscribedClasses;
//         mRestartAction = restartAction;
//     }
// 
//     @Override
//     protected void subscribeActual(Observer<? super T> observer) {
//         final RestartOnChangeObserver<T> disposable = new RestartOnChangeObserver<T>(observer, mSubscribedClasses, mRestartAction);
//         mSource.subscribe(disposable);
//     }
// 
//     @Override
//     public ObservableSource<T> source() {
//         return mSource;
//     }
// 
//     private class RestartOnChangeObserver<T> implements Observer<T>, Disposable, DbTransaction.OnDbTransactionListener {
//         private final Observer<? super T> mActual;
//         private final ValueAction<T> mRestartAction;
//         private List<Class> mSubscribedClasses;
//         private boolean mIsDisposed = false;
//         private FlowContentObserver mFlowContentObserver = new FlowContentObserver();
//         private boolean mIsInTransaction = false;
//         private boolean mHasPendingChange = false;
// 
// 
//         public RestartOnChangeObserver(Observer<? super T> observer, Class[] subscribedClasses, ValueAction<T> action) {
//             mActual = observer;
//             mSubscribedClasses = Arrays.asList(subscribedClasses);
//             mRestartAction = action;
//             DbTransaction.registerDbTransactionListener(mSubscribedClasses.get(0), this);
//         }
// 
//         @Override
//         public void onSubscribe(Disposable d) {
//             mActual.onSubscribe(this);
// 
//         }
// 
//         @Override
//         public void onError(Throwable e) {
//             mActual.onError(e);
//         }
// 
//         @Override
//         public void onComplete() {
//             //We capture any completes here to avoid disposing
//         }
// 
//         @Override
//         public void onNext(T o) {
//             mActual.onNext(o);
// 
//             for (int i = 0; i < mSubscribedClasses.size(); i++) {
//                 mFlowContentObserver.registerForContentChanges(FlowManager.getContext(), mSubscribedClasses.get(i));
//             }
// 
//             mFlowContentObserver.addOnTableChangedListener(
//                     new FlowContentObserver.OnTableChangedListener() {
//                         @Override
//                         public void onTableChanged(@Nullable Class<?> tableChanged, BaseModel.Action action) {
//                             if(isDisposed()){
//                                 return;
//                             }
// 
//                             if (!mIsInTransaction) {
//                                 mActual.onNext(mRestartAction.run());
//                             } else {
//                                 mHasPendingChange = true;
//                             }
//                         }
//                     });
//         }
// 
//         @Override
//         public void dispose() {
//             mFlowContentObserver.unregisterForContentChanges(FlowManager.getContext());
//             mIsDisposed = true;
//             mActual.onComplete();
//             DbTransaction.unregisterDbTransactionListener(mSubscribedClasses.get(0), this);
//         }
// 
//         @Override
//         public boolean isDisposed() {
//             return mIsDisposed;
//         }
// 
//         @Override
//         public void onTransactionStarted() {
//             mIsInTransaction = true;
//         }
// 
//         @Override
//         public void onTransactionEnded() {
//             mIsInTransaction = false;
//             if(mHasPendingChange && !isDisposed()){
//                 mHasPendingChange = false;
//                 mActual.onNext(mRestartAction.run());
//             }
//         }
//     }
// }


// Path: RxDbFlow-Rx2/src/main/java/au/com/roadhouse/rxdbflow/rx2/sql/observables/DBFlowMaybe.java
import au.com.roadhouse.rxdbflow.rx2.sql.observables.functions.ValueAction;
import au.com.roadhouse.rxdbflow.rx2.sql.observables.operators.DBFlowRestartOnChange;
import io.reactivex.Maybe;
import io.reactivex.Observable;

package au.com.roadhouse.rxdbflow.rx2.sql.observables;


/**
 * A base observable which provides DBFlow specific operators
 * @param <T> The observable result type
 */
public abstract class DBFlowMaybe<T> extends Maybe<T> implements ValueAction<T> {

    /**
     * Observes changes on the current table, restarting the query on change and emits the new count
     * to any subscribers
     * @return An observable which observes any changes in the current table
     */
    public final Observable<T> restartOnChange(){
        return new DBFlowRestartOnChange<>(this.toObservable(), new Class[]{getPrimaryModelClass()}, this);