Search in sources :

Example 71 with Action1

use of rx.functions.Action1 in project realm-java by realm.

the class RxJavaTests method findFirstAsync_emittedOnUpdate.

@Test
@RunTestInLooperThread
public void findFirstAsync_emittedOnUpdate() {
    final AtomicInteger subscriberCalled = new AtomicInteger(0);
    Realm realm = looperThread.realm;
    realm.beginTransaction();
    AllTypes obj = realm.createObject(AllTypes.class);
    realm.commitTransaction();
    subscription = realm.where(AllTypes.class).findFirstAsync().<AllTypes>asObservable().subscribe(new Action1<AllTypes>() {

        @Override
        public void call(AllTypes rxObject) {
            if (subscriberCalled.incrementAndGet() == 2) {
                looperThread.testComplete();
            }
        }
    });
    realm.beginTransaction();
    obj.setColumnLong(1);
    realm.commitTransaction();
}
Also used : Action1(rx.functions.Action1) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AllTypes(io.realm.entities.AllTypes) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) UiThreadTest(android.support.test.annotation.UiThreadTest) Test(org.junit.Test)

Example 72 with Action1

use of rx.functions.Action1 in project Rutgers-Course-Tracker by tevjef.

the class SectionInfoPresenterImpl method loadRMP.

public void loadRMP() {
    final Iterable<Instructors> professorsNotFound = new ArrayList<>(mSection.getInstructors());
    cancePreviousSubscription();
    Subscriber<Professor> subscriber = new Subscriber<Professor>() {

        @Override
        public void onCompleted() {
        }

        @Override
        public void onError(Throwable e) {
            e.printStackTrace();
        }

        @Override
        public void onNext(Professor professor) {
            if (getView() != null)
                getView().addRMPProfessor(professor);
        }
    };
    mSubscription = buildSearchParameters(mSection).flatMap(new Func1<Parameter, Observable<Professor>>() {

        @Override
        public Observable<Professor> call(Parameter parameter) {
            return rmp.getProfessor(parameter);
        }
    }).doOnNext(new Action1<Professor>() {

        @Override
        public void call(Professor professor) {
            for (final Iterator<Instructors> iterator = professorsNotFound.iterator(); iterator.hasNext(); ) {
                Instructors i = iterator.next();
                if (StringUtils.getJaroWinklerDistance(i.getLastName(), professor.getLastName()) > .70 || StringUtils.getJaroWinklerDistance(i.getLastName(), professor.getFirstName()) > .70) {
                    iterator.remove();
                }
            }
        }
    }).subscribeOn(mBackgroundThread).observeOn(mMainThread).doOnTerminate(new Action0() {

        @Override
        public void call() {
            if (getView() != null) {
                getView().showRatingsLayout();
                getView().hideRatingsLoading();
                for (Instructors i : professorsNotFound) {
                    getView().addErrorProfessor(i.getName());
                }
            }
        }
    }).subscribe(subscriber);
}
Also used : Professor(com.tevinjeffrey.rmp.common.Professor) Action0(rx.functions.Action0) Action1(rx.functions.Action1) ArrayList(java.util.ArrayList) Observable(rx.Observable) Instructors(com.tevinjeffrey.rutgersct.rutgersapi.model.Course.Section.Instructors) Subscriber(rx.Subscriber) Parameter(com.tevinjeffrey.rmp.common.Parameter)

Example 73 with Action1

use of rx.functions.Action1 in project sqlbrite by square.

the class ItemsFragment method onResume.

@Override
public void onResume() {
    super.onResume();
    String listId = String.valueOf(getListId());
    subscriptions = new CompositeSubscription();
    Observable<Integer> itemCount = //
    db.createQuery(TodoItem.TABLE, COUNT_QUERY, listId).map(new Func1<Query, Integer>() {

        @Override
        public Integer call(Query query) {
            Cursor cursor = query.run();
            try {
                if (!cursor.moveToNext()) {
                    throw new AssertionError("No rows");
                }
                return cursor.getInt(0);
            } finally {
                cursor.close();
            }
        }
    });
    Observable<String> listName = db.createQuery(TodoList.TABLE, TITLE_QUERY, listId).map(new Func1<Query, String>() {

        @Override
        public String call(Query query) {
            Cursor cursor = query.run();
            try {
                if (!cursor.moveToNext()) {
                    throw new AssertionError("No rows");
                }
                return cursor.getString(0);
            } finally {
                cursor.close();
            }
        }
    });
    subscriptions.add(Observable.combineLatest(listName, itemCount, new Func2<String, Integer, String>() {

        @Override
        public String call(String listName, Integer itemCount) {
            return listName + " (" + itemCount + ")";
        }
    }).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<String>() {

        @Override
        public void call(String title) {
            getActivity().setTitle(title);
        }
    }));
    subscriptions.add(db.createQuery(TodoItem.TABLE, LIST_QUERY, listId).mapToList(TodoItem.MAPPER).observeOn(AndroidSchedulers.mainThread()).subscribe(adapter));
}
Also used : Action1(rx.functions.Action1) Query(com.squareup.sqlbrite.SqlBrite.Query) Cursor(android.database.Cursor) CompositeSubscription(rx.subscriptions.CompositeSubscription) Func2(rx.functions.Func2)

Example 74 with Action1

use of rx.functions.Action1 in project Reader by TheKeeperOfPie.

the class TableLink method storeLinks.

public Action1<SQLiteDatabase> storeLinks(final List<Link> links) {
    return new Action1<SQLiteDatabase>() {

        @Override
        public void call(SQLiteDatabase sqLiteDatabase) {
            List<Link> linksWithComments = new ArrayList<>();
            for (int index = links.size() - 1; index >= 0; index--) {
                if (!links.get(index).getComments().getChildren().isEmpty()) {
                    linksWithComments.add(links.remove(index));
                }
            }
            TransactionInsertOrReplace transaction = getInsertOrReplace(sqLiteDatabase, COLUMN_TITLE, COLUMN_AUTHOR, COLUMN_JSON);
            sqLiteDatabase.beginTransaction();
            for (Link link : links) {
                transaction.insertOrReplace(link.getId(), System.currentTimeMillis(), link.getCreatedUtc(), link.getTitle(), link.getAuthor(), link.getJson());
            }
            sqLiteDatabase.setTransactionSuccessful();
            sqLiteDatabase.endTransaction();
            TransactionInsertOrReplace transactionWithComments = getInsertOrReplace(sqLiteDatabase, COLUMN_TITLE, COLUMN_AUTHOR, COLUMN_JSON, COLUMN_COMMENTS);
            sqLiteDatabase.beginTransaction();
            for (Link link : linksWithComments) {
                transactionWithComments.insertOrReplace(link.getId(), System.currentTimeMillis(), link.getCreatedUtc(), link.getTitle(), link.getAuthor(), link.getJson(), link.getJsonComments());
            }
            sqLiteDatabase.setTransactionSuccessful();
            sqLiteDatabase.endTransaction();
        }
    };
}
Also used : Action1(rx.functions.Action1) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) TransactionInsertOrReplace(com.winsonchiu.reader.data.database.TransactionInsertOrReplace) ArrayList(java.util.ArrayList) Link(com.winsonchiu.reader.data.reddit.Link)

Example 75 with Action1

use of rx.functions.Action1 in project storio by pushtorefresh.

the class ObservableBehaviorCheckerTest method assertThatObservableEmitsOnceNegative.

@Test(expected = IllegalStateException.class)
public void assertThatObservableEmitsOnceNegative() {
    final Observable<Integer> testObservable = Observable.just(1, 2);
    new ObservableBehaviorChecker<Integer>().observable(testObservable).expectedNumberOfEmissions(1).testAction(new Action1<Integer>() {

        final AtomicInteger numberOfInvocations = new AtomicInteger(0);

        @Override
        public void call(Integer i) {
            if (numberOfInvocations.incrementAndGet() > 1) {
                fail("Should be called once");
            }
        }
    }).checkBehaviorOfObservable();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Action1(rx.functions.Action1) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.Test)

Aggregations

Action1 (rx.functions.Action1)106 Test (org.junit.Test)33 Action0 (rx.functions.Action0)28 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)24 UiThreadTest (android.support.test.annotation.UiThreadTest)20 Observable (rx.Observable)18 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)12 HystrixRuntimeException (com.netflix.hystrix.exception.HystrixRuntimeException)11 ArrayList (java.util.ArrayList)11 CountDownLatch (java.util.concurrent.CountDownLatch)11 TestSubscriber (rx.observers.TestSubscriber)10 AllTypes (io.realm.entities.AllTypes)9 List (java.util.List)9 TestCircuitBreaker (com.netflix.hystrix.HystrixCircuitBreakerTest.TestCircuitBreaker)7 OnSubscribe (rx.Observable.OnSubscribe)7 RunTestInLooperThread (io.realm.rule.RunTestInLooperThread)6 IOException (java.io.IOException)6 PluginTestVerifier (com.navercorp.pinpoint.bootstrap.plugin.test.PluginTestVerifier)5 Method (java.lang.reflect.Method)5 AtomicReference (java.util.concurrent.atomic.AtomicReference)5