Search in sources :

Example 31 with Observable

use of io.reactivex.Observable in project RxJava by ReactiveX.

the class RxJavaPluginsTest method observableCreate.

@SuppressWarnings("rawtypes")
@Test
public void observableCreate() {
    try {
        RxJavaPlugins.setOnObservableAssembly(new Function<Observable, Observable>() {

            @Override
            public Observable apply(Observable t) {
                return new ObservableRange(1, 2);
            }
        });
        Observable.range(10, 3).test().assertValues(1, 2).assertNoErrors().assertComplete();
    } finally {
        RxJavaPlugins.reset();
    }
    // make sure the reset worked
    Observable.range(10, 3).test().assertValues(10, 11, 12).assertNoErrors().assertComplete();
}
Also used : ObservableRange(io.reactivex.internal.operators.observable.ObservableRange) Observable(io.reactivex.Observable) ConnectableObservable(io.reactivex.observables.ConnectableObservable)

Example 32 with Observable

use of io.reactivex.Observable in project mosby by sockeqwe.

the class MainMenuPresenter method bindIntents.

@Override
protected void bindIntents() {
    Observable<List<String>> loadCategories = intent(MainMenuView::loadCategoriesIntent).doOnNext(categoryName -> Timber.d("intent: load category %s", categoryName)).flatMap(ignored -> backendApi.getAllCategories().subscribeOn(Schedulers.io()));
    Observable<String> selectCategory = intent(MainMenuView::selectCategoryIntent).doOnNext(categoryName -> Timber.d("intent: select category %s", categoryName)).startWith(MainMenuItem.HOME);
    List<Observable<?>> allIntents = new ArrayList<>(2);
    allIntents.add(loadCategories);
    allIntents.add(selectCategory);
    Observable<MenuViewState> menuViewStateObservable = Observable.combineLatest(allIntents, (Function<Object[], MenuViewState>) objects -> {
        List<String> categories = (List<String>) objects[0];
        String selectedCategory = (String) objects[1];
        List<MainMenuItem> categoriesItems = new ArrayList<MainMenuItem>(categories.size() + 1);
        categoriesItems.add(new MainMenuItem(MainMenuItem.HOME, selectedCategory.equals(MainMenuItem.HOME)));
        for (int i = 0; i < categories.size(); i++) {
            String category = categories.get(i);
            categoriesItems.add(new MainMenuItem(category, category.equals(selectedCategory)));
        }
        return new MenuViewState.DataState(categoriesItems);
    }).startWith(new MenuViewState.LoadingState()).onErrorReturn(MenuViewState.ErrorState::new).observeOn(AndroidSchedulers.mainThread());
    subscribeViewState(menuViewStateObservable, MainMenuView::render);
}
Also used : List(java.util.List) MviBasePresenter(com.hannesdorfmann.mosby3.mvi.MviBasePresenter) Function(io.reactivex.functions.Function) Observable(io.reactivex.Observable) Schedulers(io.reactivex.schedulers.Schedulers) ProductBackendApiDecorator(com.hannesdorfmann.mosby3.sample.mvi.businesslogic.http.ProductBackendApiDecorator) MainMenuItem(com.hannesdorfmann.mosby3.sample.mvi.businesslogic.model.MainMenuItem) AndroidSchedulers(io.reactivex.android.schedulers.AndroidSchedulers) Timber(timber.log.Timber) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) Observable(io.reactivex.Observable) Function(io.reactivex.functions.Function) List(java.util.List) ArrayList(java.util.ArrayList) MainMenuItem(com.hannesdorfmann.mosby3.sample.mvi.businesslogic.model.MainMenuItem)

Example 33 with Observable

use of io.reactivex.Observable in project nem2-sdk-java by nemtech.

the class AccountHttp method getAccountsInfo.

@Override
public Observable<List<AccountInfo>> getAccountsInfo(List<Address> addresses) {
    JsonObject requestBody = new JsonObject();
    requestBody.put("addresses", addresses.stream().map(address -> address.plain()).collect(Collectors.toList()));
    Observable<NetworkType> networkTypeResolve = getNetworkTypeObservable();
    return networkTypeResolve.flatMap(networkType -> this.client.postAbs(this.url.toString()).as(BodyCodec.jsonArray()).rxSendJson(requestBody).toObservable().map(HttpResponse::body).map(json -> objectMapper.<List<AccountInfoDTO>>readValue(json.toString(), new TypeReference<List<AccountInfoDTO>>() {
    })).flatMapIterable(item -> item).map(AccountInfoDTO::getAccount).map(accountDTO -> new AccountInfo(Address.createFromRawAddress(accountDTO.getAddressEncoded()), accountDTO.getAddressHeight().extractIntArray(), accountDTO.getPublicKey(), accountDTO.getPublicKeyHeight().extractIntArray(), accountDTO.getImportance().extractIntArray(), accountDTO.getImportanceHeight().extractIntArray(), accountDTO.getMosaics().stream().map(mosaicDTO -> new Mosaic(new MosaicId(mosaicDTO.getId().extractIntArray()), mosaicDTO.getAmount().extractIntArray())).collect(Collectors.toList()))).toList().toObservable());
}
Also used : Mosaic(io.nem.sdk.model.mosaic.Mosaic) HttpResponse(io.vertx.reactivex.ext.web.client.HttpResponse) MalformedURLException(java.net.MalformedURLException) AggregateTransaction(io.nem.sdk.model.transaction.AggregateTransaction) HashMap(java.util.HashMap) NetworkType(io.nem.sdk.model.blockchain.NetworkType) Collectors(java.util.stream.Collectors) JsonArray(io.vertx.core.json.JsonArray) List(java.util.List) BodyCodec(io.vertx.reactivex.ext.web.codec.BodyCodec) Function(io.reactivex.functions.Function) io.nem.sdk.model.account(io.nem.sdk.model.account) Map(java.util.Map) Optional(java.util.Optional) Transaction(io.nem.sdk.model.transaction.Transaction) Observable(io.reactivex.Observable) JsonObject(io.vertx.core.json.JsonObject) TypeReference(com.fasterxml.jackson.core.type.TypeReference) MosaicId(io.nem.sdk.model.mosaic.MosaicId) NetworkType(io.nem.sdk.model.blockchain.NetworkType) MosaicId(io.nem.sdk.model.mosaic.MosaicId) JsonObject(io.vertx.core.json.JsonObject) TypeReference(com.fasterxml.jackson.core.type.TypeReference) Mosaic(io.nem.sdk.model.mosaic.Mosaic)

Example 34 with Observable

use of io.reactivex.Observable in project nem2-sdk-java by nemtech.

the class TransactionHttp method getTransactionStatuses.

@Override
public Observable<List<TransactionStatus>> getTransactionStatuses(List<String> transactionHashes) {
    JsonObject requestBody = new JsonObject();
    requestBody.put("hashes", transactionHashes);
    return this.client.postAbs(this.url + "/statuses").as(BodyCodec.jsonArray()).rxSendJson(requestBody).toObservable().map(HttpResponse::body).map(json -> objectMapper.<List<TransactionStatusDTO>>readValue(json.toString(), new TypeReference<List<TransactionStatusDTO>>() {
    })).flatMapIterable(item -> item).map(transactionStatusDTO -> new TransactionStatus(transactionStatusDTO.getGroup(), transactionStatusDTO.getStatus(), transactionStatusDTO.getHash(), new Deadline(transactionStatusDTO.getDeadline().extractIntArray()), transactionStatusDTO.getHeight().extractIntArray())).toList().toObservable();
}
Also used : JsonArray(io.vertx.core.json.JsonArray) List(java.util.List) HttpResponse(io.vertx.reactivex.ext.web.client.HttpResponse) BodyCodec(io.vertx.reactivex.ext.web.codec.BodyCodec) MalformedURLException(java.net.MalformedURLException) Observable(io.reactivex.Observable) JsonObject(io.vertx.core.json.JsonObject) TypeReference(com.fasterxml.jackson.core.type.TypeReference) Collectors(java.util.stream.Collectors) io.nem.sdk.model.transaction(io.nem.sdk.model.transaction) JsonObject(io.vertx.core.json.JsonObject) TypeReference(com.fasterxml.jackson.core.type.TypeReference)

Example 35 with Observable

use of io.reactivex.Observable in project nem2-sdk-java by nemtech.

the class TransactionHttp method getTransactions.

@Override
public Observable<List<Transaction>> getTransactions(List<String> transactionHashes) {
    JsonObject requestBody = new JsonObject();
    requestBody.put("transactionIds", transactionHashes);
    return this.client.postAbs(this.url.toString()).as(BodyCodec.jsonArray()).rxSendJson(requestBody).toObservable().map(HttpResponse::body).map(json -> new JsonArray(json.toString()).stream().map(s -> (JsonObject) s).collect(Collectors.toList())).flatMapIterable(item -> item).map(new TransactionMapping()).toList().toObservable();
}
Also used : JsonArray(io.vertx.core.json.JsonArray) JsonArray(io.vertx.core.json.JsonArray) List(java.util.List) HttpResponse(io.vertx.reactivex.ext.web.client.HttpResponse) BodyCodec(io.vertx.reactivex.ext.web.codec.BodyCodec) MalformedURLException(java.net.MalformedURLException) Observable(io.reactivex.Observable) JsonObject(io.vertx.core.json.JsonObject) TypeReference(com.fasterxml.jackson.core.type.TypeReference) Collectors(java.util.stream.Collectors) io.nem.sdk.model.transaction(io.nem.sdk.model.transaction) JsonObject(io.vertx.core.json.JsonObject)

Aggregations

Observable (io.reactivex.Observable)88 InOrder (org.mockito.InOrder)27 Test (org.junit.Test)25 TestException (io.reactivex.exceptions.TestException)24 List (java.util.List)17 Observer (io.reactivex.Observer)12 Function (io.reactivex.functions.Function)11 ArrayList (java.util.ArrayList)11 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)11 AndroidSchedulers (io.reactivex.android.schedulers.AndroidSchedulers)10 TestObserver (io.reactivex.observers.TestObserver)10 Bundle (android.os.Bundle)8 Nullable (android.support.annotation.Nullable)8 TimeUnit (java.util.concurrent.TimeUnit)8 LayoutInflater (android.view.LayoutInflater)7 View (android.view.View)7 ViewGroup (android.view.ViewGroup)7 TypeReference (com.fasterxml.jackson.core.type.TypeReference)7 JsonObject (io.vertx.core.json.JsonObject)7 HttpResponse (io.vertx.reactivex.ext.web.client.HttpResponse)7