io.reactivex.android.MainThreadDisposable Java Examples

The following examples show how to use io.reactivex.android.MainThreadDisposable. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ViewClickOnSubscribe.java    From RxComboDetector with MIT License 6 votes vote down vote up
@Override
public void subscribe(final FlowableEmitter<Integer> emitter) throws Exception {
    checkUiThread();

    View.OnClickListener listener = v -> {
        if (!emitter.isCancelled()) {
            emitter.onNext(1);
        }
    };
    view.setOnClickListener(listener);

    emitter.setDisposable(new MainThreadDisposable() {
        @Override
        protected void onDispose() {
            view.setOnClickListener(null);
        }
    });
}
 
Example #2
Source File: RxCondition.java    From power-adapters with Apache License 2.0 6 votes vote down vote up
@CheckResult
@NonNull
public static Observable<Boolean> value(@NonNull final Condition condition) {
    checkNotNull(condition, "condition");
    return Observable.create(new ObservableOnSubscribe<Boolean>() {
        @Override
        public void subscribe(final ObservableEmitter<Boolean> emitter) throws Exception {
            verifyMainThread();
            emitter.onNext(condition.eval());
            final Observer dataObserver = new Observer() {
                @Override
                public void onChanged() {
                    emitter.onNext(condition.eval());
                }
            };
            condition.registerObserver(dataObserver);
            emitter.setDisposable(new MainThreadDisposable() {
                @Override
                protected void onDispose() {
                    condition.unregisterObserver(dataObserver);
                }
            });
        }
    }).distinctUntilChanged();
}
 
Example #3
Source File: RxPowerAdapter.java    From power-adapters with Apache License 2.0 6 votes vote down vote up
@CheckResult
@NonNull
public static Observable<Integer> itemCount(@NonNull final PowerAdapter adapter) {
    checkNotNull(adapter, "adapter");
    return Observable.create(new ObservableOnSubscribe<Integer>() {
        @Override
        public void subscribe(final ObservableEmitter<Integer> emitter) throws Exception {
            verifyMainThread();
            final DataObserver dataObserver = new Observer() {
                @Override
                public void onChanged() {
                    emitter.onNext(adapter.getItemCount());
                }
            };
            emitter.onNext(adapter.getItemCount());
            adapter.registerDataObserver(dataObserver);
            emitter.setDisposable(new MainThreadDisposable() {
                @Override
                protected void onDispose() {
                    adapter.unregisterDataObserver(dataObserver);
                }
            });
        }
    }).distinctUntilChanged();
}
 
Example #4
Source File: RxPowerAdapter.java    From power-adapters with Apache License 2.0 6 votes vote down vote up
@CheckResult
@NonNull
public static Observable<ChangeEvent> changes(@NonNull final PowerAdapter adapter) {
    checkNotNull(adapter, "adapter");
    return Observable.create(new ObservableOnSubscribe<ChangeEvent>() {
        @Override
        public void subscribe(final ObservableEmitter<ChangeEvent> emitter) throws Exception {
            verifyMainThread();
            final DataObserver dataObserver = new Observer() {
                @Override
                public void onItemRangeChanged(int positionStart, int itemCount, @Nullable Object payload) {
                    emitter.onNext(new ChangeEvent(positionStart, itemCount, payload));
                }
            };
            adapter.registerDataObserver(dataObserver);
            emitter.setDisposable(new MainThreadDisposable() {
                @Override
                protected void onDispose() {
                    adapter.unregisterDataObserver(dataObserver);
                }
            });
        }
    });
}
 
Example #5
Source File: RxPowerAdapter.java    From power-adapters with Apache License 2.0 6 votes vote down vote up
@CheckResult
@NonNull
public static Observable<InsertEvent> inserts(@NonNull final PowerAdapter adapter) {
    checkNotNull(adapter, "adapter");
    return Observable.create(new ObservableOnSubscribe<InsertEvent>() {
        @Override
        public void subscribe(final ObservableEmitter<InsertEvent> emitter) throws Exception {
            verifyMainThread();
            final DataObserver dataObserver = new Observer() {
                @Override
                public void onItemRangeInserted(int positionStart, int itemCount) {
                    emitter.onNext(new InsertEvent(positionStart, itemCount));
                }
            };
            adapter.registerDataObserver(dataObserver);
            emitter.setDisposable(new MainThreadDisposable() {
                @Override
                protected void onDispose() {
                    adapter.unregisterDataObserver(dataObserver);
                }
            });
        }
    });
}
 
Example #6
Source File: RxPowerAdapter.java    From power-adapters with Apache License 2.0 6 votes vote down vote up
@CheckResult
@NonNull
public static Observable<RemoveEvent> removes(@NonNull final PowerAdapter adapter) {
    checkNotNull(adapter, "adapter");
    return Observable.create(new ObservableOnSubscribe<RemoveEvent>() {
        @Override
        public void subscribe(final ObservableEmitter<RemoveEvent> emitter) throws Exception {
            verifyMainThread();
            final DataObserver dataObserver = new Observer() {
                @Override
                public void onItemRangeRemoved(int positionStart, int itemCount) {
                    emitter.onNext(new RemoveEvent(positionStart, itemCount));
                }
            };
            adapter.registerDataObserver(dataObserver);
            emitter.setDisposable(new MainThreadDisposable() {
                @Override
                protected void onDispose() {
                    adapter.unregisterDataObserver(dataObserver);
                }
            });
        }
    });
}
 
Example #7
Source File: RxPowerAdapter.java    From power-adapters with Apache License 2.0 6 votes vote down vote up
@CheckResult
@NonNull
public static Observable<MoveEvent> moves(@NonNull final PowerAdapter adapter) {
    checkNotNull(adapter, "adapter");
    return Observable.create(new ObservableOnSubscribe<MoveEvent>() {
        @Override
        public void subscribe(final ObservableEmitter<MoveEvent> emitter) throws Exception {
            verifyMainThread();
            final DataObserver dataObserver = new Observer() {
                @Override
                public void onItemRangeMoved(int fromPosition, int toPosition, int itemCount) {
                    emitter.onNext(new MoveEvent(fromPosition, toPosition, itemCount));
                }
            };
            adapter.registerDataObserver(dataObserver);
            emitter.setDisposable(new MainThreadDisposable() {
                @Override
                protected void onDispose() {
                    adapter.unregisterDataObserver(dataObserver);
                }
            });
        }
    });
}
 
Example #8
Source File: RxData.java    From power-adapters with Apache License 2.0 6 votes vote down vote up
@CheckResult
@NonNull
public static <T> Observable<Data<T>> elements(@NonNull final Data<T> data) {
    checkNotNull(data, "data");
    return Observable.create(new ObservableOnSubscribe<Data<T>>() {
        @Override
        public void subscribe(final ObservableEmitter<Data<T>> emitter) throws Exception {
            verifyMainThread();
            emitter.onNext(data);
            final DataObserver dataObserver = new SimpleDataObserver() {
                @Override
                public void onChanged() {
                    emitter.onNext(data);
                }
            };
            data.registerDataObserver(dataObserver);
            emitter.setDisposable(new MainThreadDisposable() {
                @Override
                protected void onDispose() {
                    data.unregisterDataObserver(dataObserver);
                }
            });
        }
    });
}
 
Example #9
Source File: RxData.java    From power-adapters with Apache License 2.0 6 votes vote down vote up
@CheckResult
@NonNull
public static Observable<ChangeEvent> changes(@NonNull final Data<?> data) {
    checkNotNull(data, "data");
    return Observable.create(new ObservableOnSubscribe<ChangeEvent>() {
        @Override
        public void subscribe(final ObservableEmitter<ChangeEvent> emitter) throws Exception {
            verifyMainThread();
            final DataObserver dataObserver = new Observer() {
                @Override
                public void onItemRangeChanged(int positionStart, int itemCount, @Nullable Object payload) {
                    emitter.onNext(new ChangeEvent(positionStart, itemCount, payload));
                }
            };
            data.registerDataObserver(dataObserver);
            emitter.setDisposable(new MainThreadDisposable() {
                @Override
                protected void onDispose() {
                    data.unregisterDataObserver(dataObserver);
                }
            });
        }
    });
}
 
Example #10
Source File: RxData.java    From power-adapters with Apache License 2.0 6 votes vote down vote up
@CheckResult
@NonNull
public static Observable<InsertEvent> inserts(@NonNull final Data<?> data) {
    checkNotNull(data, "data");
    return Observable.create(new ObservableOnSubscribe<InsertEvent>() {
        @Override
        public void subscribe(final ObservableEmitter<InsertEvent> emitter) throws Exception {
            verifyMainThread();
            final DataObserver dataObserver = new Observer() {
                @Override
                public void onItemRangeInserted(int positionStart, int itemCount) {
                    emitter.onNext(new InsertEvent(positionStart, itemCount));
                }
            };
            data.registerDataObserver(dataObserver);
            emitter.setDisposable(new MainThreadDisposable() {
                @Override
                protected void onDispose() {
                    data.unregisterDataObserver(dataObserver);
                }
            });
        }
    });
}
 
Example #11
Source File: RxData.java    From power-adapters with Apache License 2.0 6 votes vote down vote up
@CheckResult
@NonNull
public static Observable<RemoveEvent> removes(@NonNull final Data<?> data) {
    checkNotNull(data, "data");
    return Observable.create(new ObservableOnSubscribe<RemoveEvent>() {
        @Override
        public void subscribe(final ObservableEmitter<RemoveEvent> emitter) throws Exception {
            verifyMainThread();
            final DataObserver dataObserver = new Observer() {
                @Override
                public void onItemRangeRemoved(int positionStart, int itemCount) {
                    emitter.onNext(new RemoveEvent(positionStart, itemCount));
                }
            };
            data.registerDataObserver(dataObserver);
            emitter.setDisposable(new MainThreadDisposable() {
                @Override
                protected void onDispose() {
                    data.unregisterDataObserver(dataObserver);
                }
            });
        }
    });
}
 
Example #12
Source File: RxData.java    From power-adapters with Apache License 2.0 6 votes vote down vote up
@CheckResult
@NonNull
public static Observable<MoveEvent> moves(@NonNull final Data<?> data) {
    checkNotNull(data, "data");
    return Observable.create(new ObservableOnSubscribe<MoveEvent>() {
        @Override
        public void subscribe(final ObservableEmitter<MoveEvent> emitter) throws Exception {
            verifyMainThread();
            final DataObserver dataObserver = new Observer() {
                @Override
                public void onItemRangeMoved(int fromPosition, int toPosition, int itemCount) {
                    emitter.onNext(new MoveEvent(fromPosition, toPosition, itemCount));
                }
            };
            data.registerDataObserver(dataObserver);
            emitter.setDisposable(new MainThreadDisposable() {
                @Override
                protected void onDispose() {
                    data.unregisterDataObserver(dataObserver);
                }
            });
        }
    });
}
 
Example #13
Source File: RxData.java    From power-adapters with Apache License 2.0 6 votes vote down vote up
@CheckResult
@NonNull
public static Observable<Boolean> loading(@NonNull final Data<?> data) {
    checkNotNull(data, "data");
    return Observable.create(new ObservableOnSubscribe<Boolean>() {
        @Override
        public void subscribe(final ObservableEmitter<Boolean> emitter) throws Exception {
            verifyMainThread();
            emitter.onNext(data.isLoading());
            final LoadingObserver loadingObserver = new LoadingObserver() {
                @Override
                public void onLoadingChange() {
                    emitter.onNext(data.isLoading());
                }
            };
            data.registerLoadingObserver(loadingObserver);
            emitter.setDisposable(new MainThreadDisposable() {
                @Override
                protected void onDispose() {
                    data.unregisterLoadingObserver(loadingObserver);
                }
            });
        }
    });
}
 
Example #14
Source File: RxData.java    From power-adapters with Apache License 2.0 6 votes vote down vote up
@CheckResult
@NonNull
public static Observable<Integer> available(@NonNull final Data<?> data) {
    checkNotNull(data, "data");
    return Observable.create(new ObservableOnSubscribe<Integer>() {
        @Override
        public void subscribe(final ObservableEmitter<Integer> emitter) throws Exception {
            emitter.onNext(data.available());
            final AvailableObserver availableObserver = new AvailableObserver() {
                @Override
                public void onAvailableChange() {
                    emitter.onNext(data.available());
                }
            };
            data.registerAvailableObserver(availableObserver);
            emitter.setDisposable(new MainThreadDisposable() {
                @Override
                protected void onDispose() {
                    data.unregisterAvailableObserver(availableObserver);
                }
            });
        }
    });
}
 
Example #15
Source File: RxData.java    From power-adapters with Apache License 2.0 6 votes vote down vote up
@CheckResult
@NonNull
public static Observable<Throwable> errors(@NonNull final Data<?> data) {
    checkNotNull(data, "data");
    return Observable.create(new ObservableOnSubscribe<Throwable>() {
        @Override
        public void subscribe(final ObservableEmitter<Throwable> emitter) throws Exception {
            final ErrorObserver errorObserver = new ErrorObserver() {
                @Override
                public void onError(@NonNull Throwable e) {
                    emitter.onNext(e);
                }
            };
            data.registerErrorObserver(errorObserver);
            emitter.setDisposable(new MainThreadDisposable() {
                @Override
                protected void onDispose() {
                    data.unregisterErrorObserver(errorObserver);
                }
            });
        }
    });
}
 
Example #16
Source File: MainActivity.java    From Reactive-Android-Programming with MIT License 5 votes vote down vote up
private void demo3() {

        Observable.create(emitter -> {
            emitter.setDisposable(new MainThreadDisposable() {
                @Override
                protected void onDispose() {
                    helloText.setOnClickListener(null);
                }
            });
            helloText.setOnClickListener(v -> emitter.onNext(v));
        })
                .subscribe();
    }