Search in sources :

Example 1 with Subject

use of io.reactivex.rxjava3.subjects.Subject in project a-medic-log by rh-id.

the class LogPage method createView.

@Override
protected View createView(Activity activity, ViewGroup container) {
    View view = activity.getLayoutInflater().inflate(R.layout.page_log, container, false);
    ViewGroup rootLayout = view.findViewById(R.id.root_layout);
    ViewGroup containerAppBar = view.findViewById(R.id.container_app_bar);
    mAppBarSV.setTitle(activity.getString(R.string.title_log_file));
    containerAppBar.addView(mAppBarSV.buildView(activity, rootLayout));
    ProgressBar progressBar = view.findViewById(R.id.progress_circular);
    View noRecord = view.findViewById(R.id.no_record);
    ScrollView scrollView = view.findViewById(R.id.scroll_view);
    TextView textView = view.findViewById(R.id.text_content);
    Provider provider = BaseApplication.of(activity).getProvider();
    if (mSvProvider != null) {
        mSvProvider.dispose();
    }
    mSvProvider = Provider.createProvider(activity.getApplicationContext(), new RxProviderModule());
    FileHelper fileHelper = provider.get(FileHelper.class);
    File logFile = fileHelper.getLogFile();
    FloatingActionButton fabClear = view.findViewById(R.id.fab_clear);
    FloatingActionButton fabShare = view.findViewById(R.id.fab_share);
    fabShare.setOnClickListener(v -> {
        try {
            UiUtils.shareFile(activity, logFile, activity.getString(R.string.share_log_file));
        } catch (Throwable e) {
            provider.get(ILogger.class).e(TAG, activity.getString(R.string.error_sharing_log_file), e);
        }
    });
    BehaviorSubject<File> subject = BehaviorSubject.createDefault(logFile);
    fabClear.setOnClickListener(view1 -> {
        fileHelper.clearLogFile();
        provider.get(ILogger.class).i(TAG, activity.getString(R.string.log_file_deleted));
        provider.get(Handler.class).post(() -> subject.onNext(logFile));
    });
    mSvProvider.get(RxDisposer.class).add("readLogFile", subject.observeOn(Schedulers.from(BaseApplication.of(activity).getProvider().get(ExecutorService.class))).map(file -> {
        if (!file.exists()) {
            return "";
        } else {
            StringBuilder stringBuilder = new StringBuilder();
            BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
            char[] buff = new char[2048];
            int b = bufferedReader.read(buff);
            while (b != -1) {
                stringBuilder.append(buff);
                b = bufferedReader.read(buff);
            }
            return stringBuilder.toString();
        }
    }).observeOn(AndroidSchedulers.mainThread()).subscribe(s -> {
        progressBar.setVisibility(View.GONE);
        textView.setText(s);
        if (s.isEmpty()) {
            noRecord.setVisibility(View.VISIBLE);
            scrollView.setVisibility(View.GONE);
            fabShare.setVisibility(View.GONE);
            fabClear.setVisibility(View.GONE);
        } else {
            noRecord.setVisibility(View.GONE);
            scrollView.setVisibility(View.VISIBLE);
            scrollView.post(() -> scrollView.fullScroll(View.FOCUS_DOWN));
            fabShare.setVisibility(View.VISIBLE);
            fabClear.setVisibility(View.VISIBLE);
        }
    }));
    return view;
}
Also used : FileHelper(m.co.rh.id.a_medic_log.base.provider.FileHelper) ProgressBar(android.widget.ProgressBar) ILogger(m.co.rh.id.alogger.ILogger) BehaviorSubject(io.reactivex.rxjava3.subjects.BehaviorSubject) AppBarSV(m.co.rh.id.a_medic_log.app.ui.component.AppBarSV) BaseApplication(m.co.rh.id.a_medic_log.base.BaseApplication) Schedulers(io.reactivex.rxjava3.schedulers.Schedulers) NavInject(m.co.rh.id.anavigator.annotation.NavInject) UiUtils(m.co.rh.id.a_medic_log.app.util.UiUtils) Handler(android.os.Handler) FloatingActionButton(com.google.android.material.floatingactionbutton.FloatingActionButton) View(android.view.View) RxProviderModule(m.co.rh.id.a_medic_log.app.provider.RxProviderModule) ExecutorService(java.util.concurrent.ExecutorService) StatefulView(m.co.rh.id.anavigator.StatefulView) Provider(m.co.rh.id.aprovider.Provider) ViewGroup(android.view.ViewGroup) File(java.io.File) AndroidSchedulers(io.reactivex.rxjava3.android.schedulers.AndroidSchedulers) TextView(android.widget.TextView) ScrollView(android.widget.ScrollView) RxDisposer(m.co.rh.id.a_medic_log.app.rx.RxDisposer) R(m.co.rh.id.a_medic_log.R) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) Activity(android.app.Activity) ViewGroup(android.view.ViewGroup) Handler(android.os.Handler) View(android.view.View) StatefulView(m.co.rh.id.anavigator.StatefulView) TextView(android.widget.TextView) ScrollView(android.widget.ScrollView) RxDisposer(m.co.rh.id.a_medic_log.app.rx.RxDisposer) Provider(m.co.rh.id.aprovider.Provider) FileHelper(m.co.rh.id.a_medic_log.base.provider.FileHelper) ScrollView(android.widget.ScrollView) RxProviderModule(m.co.rh.id.a_medic_log.app.provider.RxProviderModule) BufferedReader(java.io.BufferedReader) FloatingActionButton(com.google.android.material.floatingactionbutton.FloatingActionButton) TextView(android.widget.TextView) ILogger(m.co.rh.id.alogger.ILogger) FileReader(java.io.FileReader) ProgressBar(android.widget.ProgressBar) File(java.io.File)

Example 2 with Subject

use of io.reactivex.rxjava3.subjects.Subject in project a-news-provider by rh-id.

the class LogPage method createView.

@Override
protected View createView(Activity activity, ViewGroup container) {
    View view = activity.getLayoutInflater().inflate(R.layout.page_log, container, false);
    ViewGroup rootLayout = view.findViewById(R.id.root_layout);
    ViewGroup containerAppBar = view.findViewById(R.id.container_app_bar);
    mAppBarSV.setTitle(activity.getString(R.string.log_file));
    containerAppBar.addView(mAppBarSV.buildView(activity, rootLayout));
    ProgressBar progressBar = view.findViewById(R.id.progress_circular);
    View noRecord = view.findViewById(R.id.no_record);
    ScrollView scrollView = view.findViewById(R.id.scroll_view);
    TextView textView = view.findViewById(R.id.text_content);
    FileHelper fileHelper = mSvProvider.get(FileHelper.class);
    File logFile = fileHelper.getLogFile();
    FloatingActionButton fabClear = view.findViewById(R.id.fab_clear);
    FloatingActionButton fabShare = view.findViewById(R.id.fab_share);
    fabShare.setOnClickListener(v -> {
        try {
            UiUtils.shareFile(activity, logFile, activity.getString(R.string.share_log_file));
        } catch (Throwable e) {
            mSvProvider.get(ILogger.class).e(TAG, activity.getString(R.string.error_sharing_log_file), e);
        }
    });
    BehaviorSubject<File> subject = BehaviorSubject.createDefault(logFile);
    fabClear.setOnClickListener(view1 -> {
        fileHelper.clearLogFile();
        mSvProvider.get(ILogger.class).i(TAG, activity.getString(R.string.log_file_deleted));
        mSvProvider.get(Handler.class).post(() -> subject.onNext(logFile));
    });
    mSvProvider.get(RxDisposer.class).add("readLogFile", subject.observeOn(Schedulers.from(mSvProvider.get(ExecutorService.class))).map(file -> {
        if (!file.exists()) {
            return "";
        } else {
            StringBuilder stringBuilder = new StringBuilder();
            BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
            char[] buff = new char[2048];
            int b = bufferedReader.read(buff);
            while (b != -1) {
                stringBuilder.append(buff);
                b = bufferedReader.read(buff);
            }
            return stringBuilder.toString();
        }
    }).observeOn(AndroidSchedulers.mainThread()).subscribe(s -> {
        progressBar.setVisibility(View.GONE);
        textView.setText(s);
        if (s.isEmpty()) {
            noRecord.setVisibility(View.VISIBLE);
            scrollView.setVisibility(View.GONE);
            fabShare.setVisibility(View.GONE);
            fabClear.setVisibility(View.GONE);
        } else {
            noRecord.setVisibility(View.GONE);
            scrollView.setVisibility(View.VISIBLE);
            scrollView.post(() -> scrollView.fullScroll(View.FOCUS_DOWN));
            fabShare.setVisibility(View.VISIBLE);
            fabClear.setVisibility(View.VISIBLE);
        }
    }));
    return view;
}
Also used : UiUtils(m.co.rh.id.a_news_provider.app.util.UiUtils) ProgressBar(android.widget.ProgressBar) ILogger(m.co.rh.id.alogger.ILogger) BehaviorSubject(io.reactivex.rxjava3.subjects.BehaviorSubject) Schedulers(io.reactivex.rxjava3.schedulers.Schedulers) NavInject(m.co.rh.id.anavigator.annotation.NavInject) R(m.co.rh.id.a_news_provider.R) Handler(android.os.Handler) FloatingActionButton(com.google.android.material.floatingactionbutton.FloatingActionButton) View(android.view.View) ExecutorService(java.util.concurrent.ExecutorService) AppBarSV(m.co.rh.id.a_news_provider.app.ui.component.AppBarSV) StatefulView(m.co.rh.id.anavigator.StatefulView) Provider(m.co.rh.id.aprovider.Provider) ViewGroup(android.view.ViewGroup) File(java.io.File) AndroidSchedulers(io.reactivex.rxjava3.android.schedulers.AndroidSchedulers) RxDisposer(m.co.rh.id.a_news_provider.app.rx.RxDisposer) TextView(android.widget.TextView) RequireComponent(m.co.rh.id.anavigator.component.RequireComponent) ScrollView(android.widget.ScrollView) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) Activity(android.app.Activity) StatefulViewProvider(m.co.rh.id.a_news_provider.app.provider.StatefulViewProvider) FileHelper(m.co.rh.id.a_news_provider.base.provider.FileHelper) ViewGroup(android.view.ViewGroup) Handler(android.os.Handler) View(android.view.View) StatefulView(m.co.rh.id.anavigator.StatefulView) TextView(android.widget.TextView) ScrollView(android.widget.ScrollView) RxDisposer(m.co.rh.id.a_news_provider.app.rx.RxDisposer) FileHelper(m.co.rh.id.a_news_provider.base.provider.FileHelper) ScrollView(android.widget.ScrollView) BufferedReader(java.io.BufferedReader) FloatingActionButton(com.google.android.material.floatingactionbutton.FloatingActionButton) TextView(android.widget.TextView) ILogger(m.co.rh.id.alogger.ILogger) FileReader(java.io.FileReader) ProgressBar(android.widget.ProgressBar) File(java.io.File)

Example 3 with Subject

use of io.reactivex.rxjava3.subjects.Subject in project RxJava by ReactiveX.

the class SingleAmbTest method successErrorRace.

@Test
public void successErrorRace() {
    for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
        List<Throwable> errors = TestHelper.trackPluginErrors();
        try {
            final Subject<Integer> ps1 = PublishSubject.create();
            final Subject<Integer> ps2 = PublishSubject.create();
            Single.ambArray(ps1.singleOrError(), ps2.singleOrError()).test();
            final TestException ex = new TestException();
            Runnable r1 = new Runnable() {

                @Override
                public void run() {
                    ps1.onNext(1);
                    ps1.onComplete();
                }
            };
            Runnable r2 = new Runnable() {

                @Override
                public void run() {
                    ps2.onError(ex);
                }
            };
            TestHelper.race(r1, r2);
            if (!errors.isEmpty()) {
                TestHelper.assertUndeliverable(errors, 0, TestException.class);
            }
        } finally {
            RxJavaPlugins.reset();
        }
    }
}
Also used : TestException(io.reactivex.rxjava3.exceptions.TestException) Test(org.junit.Test)

Example 4 with Subject

use of io.reactivex.rxjava3.subjects.Subject in project RxJava by ReactiveX.

the class SingleAmbTest method multipleErrorRace.

@Test
public void multipleErrorRace() {
    for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
        List<Throwable> errors = TestHelper.trackPluginErrors();
        try {
            final Subject<Integer> ps1 = PublishSubject.create();
            final Subject<Integer> ps2 = PublishSubject.create();
            Single.ambArray(ps1.singleOrError(), ps2.singleOrError()).test();
            final TestException ex = new TestException();
            Runnable r1 = new Runnable() {

                @Override
                public void run() {
                    ps1.onError(ex);
                }
            };
            Runnable r2 = new Runnable() {

                @Override
                public void run() {
                    ps2.onError(ex);
                }
            };
            TestHelper.race(r1, r2);
            if (!errors.isEmpty()) {
                TestHelper.assertUndeliverable(errors, 0, TestException.class);
            }
        } finally {
            RxJavaPlugins.reset();
        }
    }
}
Also used : TestException(io.reactivex.rxjava3.exceptions.TestException) Test(org.junit.Test)

Example 5 with Subject

use of io.reactivex.rxjava3.subjects.Subject in project RxJava by ReactiveX.

the class ObservableConcatMapSchedulerTest method issue2890NoStackoverflow.

@Test
public void issue2890NoStackoverflow() throws InterruptedException, TimeoutException {
    final ExecutorService executor = Executors.newFixedThreadPool(2);
    final Scheduler sch = Schedulers.from(executor);
    Function<Integer, Observable<Integer>> func = new Function<Integer, Observable<Integer>>() {

        @Override
        public Observable<Integer> apply(Integer t) {
            Observable<Integer> flowable = Observable.just(t).subscribeOn(sch);
            Subject<Integer> processor = UnicastSubject.create();
            flowable.subscribe(processor);
            return processor;
        }
    };
    int n = 5000;
    final AtomicInteger counter = new AtomicInteger();
    Observable.range(1, n).concatMap(func, 2, ImmediateThinScheduler.INSTANCE).subscribe(new DefaultObserver<Integer>() {

        @Override
        public void onNext(Integer t) {
            // Consume after sleep for 1 ms
            try {
                Thread.sleep(1);
            } catch (InterruptedException e) {
            // ignored
            }
            if (counter.getAndIncrement() % 100 == 0) {
                System.out.print("testIssue2890NoStackoverflow -> ");
                System.out.println(counter.get());
            }
            ;
        }

        @Override
        public void onComplete() {
            executor.shutdown();
        }

        @Override
        public void onError(Throwable e) {
            executor.shutdown();
        }
    });
    long awaitTerminationTimeout = 100_000;
    if (!executor.awaitTermination(awaitTerminationTimeout, TimeUnit.MILLISECONDS)) {
        throw new TimeoutException("Completed " + counter.get() + "/" + n + " before timed out after " + awaitTerminationTimeout + " milliseconds.");
    }
    assertEquals(n, counter.get());
}
Also used : ImmediateThinScheduler(io.reactivex.rxjava3.internal.schedulers.ImmediateThinScheduler) Observable(io.reactivex.rxjava3.core.Observable) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)41 RxJavaTest (io.reactivex.rxjava3.core.RxJavaTest)26 TestException (io.reactivex.rxjava3.exceptions.TestException)26 List (java.util.List)11 Disposable (io.reactivex.rxjava3.disposables.Disposable)10 ArrayList (java.util.ArrayList)10 Arrays (java.util.Arrays)10 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)10 AmplifyException (com.amplifyframework.AmplifyException)9 Action (com.amplifyframework.core.Action)9 Consumer (com.amplifyframework.core.Consumer)9 Cancelable (com.amplifyframework.core.async.Cancelable)9 Model (com.amplifyframework.core.model.Model)9 ModelSchema (com.amplifyframework.core.model.ModelSchema)9 SchemaRegistry (com.amplifyframework.core.model.SchemaRegistry)9 SerializedModel (com.amplifyframework.core.model.SerializedModel)9 DataStoreException (com.amplifyframework.datastore.DataStoreException)9 Collections (java.util.Collections)9 ExecutorService (java.util.concurrent.ExecutorService)9 TimeUnit (java.util.concurrent.TimeUnit)9