Search in sources :

Example 16 with ObservableSource

use of io.reactivex.rxjava3.core.ObservableSource in project RxJava by ReactiveX.

the class ObservableSwitchTest method switchWhenInnerCompleteBeforeOuter.

@Test
public void switchWhenInnerCompleteBeforeOuter() {
    Observable<Observable<String>> source = Observable.unsafeCreate(new ObservableSource<Observable<String>>() {

        @Override
        public void subscribe(Observer<? super Observable<String>> outerObserver) {
            outerObserver.onSubscribe(Disposable.empty());
            publishNext(outerObserver, 10, Observable.unsafeCreate(new ObservableSource<String>() {

                @Override
                public void subscribe(Observer<? super String> innerObserver) {
                    innerObserver.onSubscribe(Disposable.empty());
                    publishNext(innerObserver, 0, "one");
                    publishNext(innerObserver, 10, "two");
                    publishCompleted(innerObserver, 20);
                }
            }));
            publishNext(outerObserver, 100, Observable.unsafeCreate(new ObservableSource<String>() {

                @Override
                public void subscribe(Observer<? super String> innerObserver) {
                    innerObserver.onSubscribe(Disposable.empty());
                    publishNext(innerObserver, 0, "three");
                    publishNext(innerObserver, 10, "four");
                    publishCompleted(innerObserver, 20);
                }
            }));
            publishCompleted(outerObserver, 200);
        }
    });
    Observable<String> sampled = Observable.switchOnNext(source);
    sampled.subscribe(observer);
    InOrder inOrder = inOrder(observer);
    scheduler.advanceTimeTo(150, TimeUnit.MILLISECONDS);
    inOrder.verify(observer, never()).onComplete();
    inOrder.verify(observer, times(1)).onNext("one");
    inOrder.verify(observer, times(1)).onNext("two");
    inOrder.verify(observer, times(1)).onNext("three");
    inOrder.verify(observer, times(1)).onNext("four");
    scheduler.advanceTimeTo(250, TimeUnit.MILLISECONDS);
    inOrder.verify(observer, never()).onNext(anyString());
    inOrder.verify(observer, times(1)).onComplete();
}
Also used : InOrder(org.mockito.InOrder) Observable(io.reactivex.rxjava3.core.Observable)

Example 17 with ObservableSource

use of io.reactivex.rxjava3.core.ObservableSource in project RxJava by ReactiveX.

the class ObservableWindowWithStartEndObservableTest method endError.

@Test
@SuppressUndeliverable
public void endError() {
    PublishSubject<Integer> source = PublishSubject.create();
    PublishSubject<Integer> start = PublishSubject.create();
    final PublishSubject<Integer> end = PublishSubject.create();
    TestObserver<Integer> to = source.window(start, new Function<Integer, ObservableSource<Integer>>() {

        @Override
        public ObservableSource<Integer> apply(Integer v) throws Exception {
            return end;
        }
    }).flatMap(Functions.<Observable<Integer>>identity()).test();
    start.onNext(1);
    end.onError(new TestException());
    to.assertFailure(TestException.class);
    assertFalse("Source has observers!", source.hasObservers());
    assertFalse("Start has observers!", start.hasObservers());
    assertFalse("End has observers!", end.hasObservers());
}
Also used : TestException(io.reactivex.rxjava3.exceptions.TestException) TestException(io.reactivex.rxjava3.exceptions.TestException) IOException(java.io.IOException) Observable(io.reactivex.rxjava3.core.Observable)

Example 18 with ObservableSource

use of io.reactivex.rxjava3.core.ObservableSource in project xxf_android by NBXXF.

the class StringFileService method writeFileString.

/**
 * 写
 *
 * @param file
 * @param content
 * @param append
 * @return
 */
default Observable<File> writeFileString(File file, String content, boolean append) {
    return Observable.defer(new Supplier<ObservableSource<? extends File>>() {

        @Override
        public ObservableSource<? extends File> get() throws Throwable {
            FileUtils.createOrExistsFile(file);
            try (FileWriter fw = new FileWriter(file, append)) {
                fw.write(content);
                fw.flush();
            }
            return Observable.just(file);
        }
    }).subscribeOn(Schedulers.io());
}
Also used : FileWriter(java.io.FileWriter) Supplier(io.reactivex.rxjava3.functions.Supplier)

Example 19 with ObservableSource

use of io.reactivex.rxjava3.core.ObservableSource in project xxf_android by NBXXF.

the class StringFileService method readFileString.

/**
 * 读取
 *
 * @return
 */
default Observable<String> readFileString(File file) {
    return Observable.defer(new Supplier<ObservableSource<? extends String>>() {

        @Override
        public ObservableSource<? extends String> get() throws Throwable {
            if (FileUtils.isFileExists(ApplicationProvider.applicationContext, file)) {
                try (FileReader fr = new FileReader(file)) {
                    char[] bt = new char[1024];
                    StringBuffer sb = new StringBuffer();
                    while (fr.read(bt) != -1) {
                        sb.append(bt);
                        java.util.Arrays.fill(bt, (char) 0);
                    }
                    return Observable.just(sb.toString());
                }
            }
            return Observable.empty();
        }
    }).subscribeOn(Schedulers.io());
}
Also used : Supplier(io.reactivex.rxjava3.functions.Supplier) FileReader(java.io.FileReader)

Example 20 with ObservableSource

use of io.reactivex.rxjava3.core.ObservableSource in project xxf_android by NBXXF.

the class SystemUtils method selectFileUri.

/**
 * 获取文件的uri 但是不是真实的文件路径
 * 建议 UriUtils.getPath(activity, uri); 但是对于android 10 大文件拷贝较慢  2G==3s拷贝时间
 *
 * @param activity
 * @param mimeTypes
 * @return
 */
public static Observable<Uri> selectFileUri(final FragmentActivity activity, String[] mimeTypes) {
    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    intent.addCategory(Intent.CATEGORY_OPENABLE);
    intent.setType("*/*");
    intent.putExtra(Intent.EXTRA_MIME_TYPES, mimeTypes);
    return XXF.requestPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE).compose(new RxPermissionTransformer(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE)).flatMap(new Function<Boolean, ObservableSource<Uri>>() {

        @Override
        public ObservableSource<Uri> apply(Boolean aBoolean) throws Throwable {
            return XXF.startActivityForResult(activity, intent, REQUEST_CODE_DOCUMENT).flatMap(new Function<ActivityResult, ObservableSource<Uri>>() {

                @Override
                public ObservableSource<Uri> apply(ActivityResult activityResult) throws Throwable {
                    if (!activityResult.isOk()) {
                        return Observable.empty();
                    }
                    return Observable.just(activityResult.getData().getData());
                }
            }).observeOn(AndroidSchedulers.mainThread());
        }
    });
}
Also used : RxPermissionTransformer(com.xxf.permission.transformer.RxPermissionTransformer) ObservableSource(io.reactivex.rxjava3.core.ObservableSource) Intent(android.content.Intent) ActivityResult(com.xxf.activityresult.ActivityResult)

Aggregations

Test (org.junit.Test)21 Observable (io.reactivex.rxjava3.core.Observable)20 TestException (io.reactivex.rxjava3.exceptions.TestException)12 InOrder (org.mockito.InOrder)12 TestObserver (io.reactivex.rxjava3.observers.TestObserver)9 IOException (java.io.IOException)8 Observer (io.reactivex.rxjava3.core.Observer)5 RxMethod (io.reactivex.rxjava3.validators.BaseTypeParser.RxMethod)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 ObservableSource (io.reactivex.rxjava3.core.ObservableSource)4 Function (io.reactivex.rxjava3.functions.Function)4 Intent (android.content.Intent)3 Disposable (io.reactivex.rxjava3.disposables.Disposable)3 File (java.io.File)3 View (android.view.View)2 RxPermissionTransformer (com.xxf.permission.transformer.RxPermissionTransformer)2 Supplier (io.reactivex.rxjava3.functions.Supplier)2 GroupedObservable (io.reactivex.rxjava3.observables.GroupedObservable)2 ArrayList (java.util.ArrayList)2 Callable (java.util.concurrent.Callable)2