Search in sources :

Example 1 with Changes

use of com.pushtorefresh.storio3.contentresolver.Changes in project storio by pushtorefresh.

the class TweetsSQLiteFragment method reloadData.

void reloadData() {
    uiStateController.setUiStateLoading();
    final Disposable disposable = storIOSQLite.get().listOfObjects(Tweet.class).withQuery(QUERY_ALL).prepare().asRxFlowable(// it will be subscribed to changes in tweets table!
    LATEST).delay(1, // for better User Experience :) Actually, StorIO is so fast that we need to delay emissions (it's a joke, or not)
    SECONDS).observeOn(mainThread()).subscribe(new Consumer<List<Tweet>>() {

        @Override
        public void accept(List<Tweet> tweets) {
            // So you just need to check if it's empty or not
            if (tweets.isEmpty()) {
                uiStateController.setUiStateEmpty();
                tweetsAdapter.setTweets(Collections.<Tweet>emptyList());
            } else {
                uiStateController.setUiStateContent();
                tweetsAdapter.setTweets(tweets);
            }
        }
    }, new Consumer<Throwable>() {

        @Override
        public void accept(Throwable throwable) {
            // In cases when you are not sure that query will be successful
            // You can prevent crash of the application via error handler
            Timber.e(throwable, "reloadData()");
            uiStateController.setUiStateError();
            tweetsAdapter.setTweets(Collections.<Tweet>emptyList());
        }
    });
    // Preventing memory leak (other rx operations: Put, Delete emit result once so memory leak won't live long)
    // Because io.reactivex.Flowable from Get Operation is endless (it watches for changes of tables from query)
    // You can easily create memory leak (in this case you'll leak the Fragment and all it's fields)
    // So please, PLEASE manage your subscriptions
    // We suggest same mechanism via storing all disposables that you want to dispose
    // In something like CompositeSubscription and dispose them in appropriate moment of component lifecycle
    disposeOnStop(disposable);
}
Also used : Disposable(io.reactivex.disposables.Disposable) Tweet(com.pushtorefresh.storio3.sample.db.entities.Tweet) ArrayList(java.util.ArrayList) List(java.util.List)

Example 2 with Changes

use of com.pushtorefresh.storio3.contentresolver.Changes in project storio by pushtorefresh.

the class TweetsContentResolverFragment method reloadData.

void reloadData() {
    uiStateController.setUiStateLoading();
    final Disposable disposable = storIOContentResolver.get().listOfObjects(Tweet.class).withQuery(QUERY_ALL).prepare().asRxFlowable(// it will be subscribed to changes in tweets table!
    LATEST).observeOn(mainThread()).subscribe(new Consumer<List<Tweet>>() {

        @Override
        public void accept(List<Tweet> tweets) {
            // So you just need to check if it's empty or not
            if (tweets.isEmpty()) {
                uiStateController.setUiStateEmpty();
                tweetsAdapter.setTweets(Collections.<Tweet>emptyList());
            } else {
                uiStateController.setUiStateContent();
                tweetsAdapter.setTweets(tweets);
            }
        }
    }, new Consumer<Throwable>() {

        @Override
        public void accept(Throwable throwable) {
            // In cases when you are not sure that query will be successful
            // You can prevent crash of the application via error handler
            Timber.e(throwable, "reloadData()");
            uiStateController.setUiStateError();
            tweetsAdapter.setTweets(Collections.<Tweet>emptyList());
        }
    });
    // Preventing memory leak (other Flowables: Put, Delete emit result once so memory leak won't live long)
    // Because io.reactivex.Flowable from Get Operation is endless (it watches for changes of tables from query)
    // You can easily create memory leak (in this case you'll leak the Fragment and all it's fields)
    // So please, PLEASE manage your subscriptions
    // We suggest same mechanism via storing all disposables that you want to dispose
    // In something like CompositeSubscription and dispose them in appropriate moment of component lifecycle
    disposeOnStop(disposable);
}
Also used : Disposable(io.reactivex.disposables.Disposable) Tweet(com.pushtorefresh.storio3.sample.db.entities.Tweet) ArrayList(java.util.ArrayList) List(java.util.List)

Example 3 with Changes

use of com.pushtorefresh.storio3.contentresolver.Changes in project storio by pushtorefresh.

the class PutOperationTest method updateContentValuesAsCompletable.

@Test
public void updateContentValuesAsCompletable() {
    TestSubscriber<Changes> changesTestSubscriber = new TestSubscriber<Changes>();
    storIOContentResolver.observeChangesOfUri(TestItem.CONTENT_URI, BackpressureStrategy.MISSING).take(2).subscribe(changesTestSubscriber);
    Uri insertedUri = contentResolver.insert(TestItem.CONTENT_URI, TestItem.create(null, "value").toContentValues());
    TestItem testItem = TestItem.create(ContentUris.parseId(insertedUri), "value2");
    storIOContentResolver.put().contentValues(testItem.toContentValues()).withPutResolver(testItemContentValuesPutResolver).prepare().asRxCompletable().blockingAwait(15, SECONDS);
    Cursor cursor = contentResolver.query(TestItem.CONTENT_URI, null, null, null, null);
    Assertions.assertThat(cursor).hasCount(1);
    cursor.moveToFirst();
    assertThat(testItem).isEqualTo(TestItem.fromCursor(cursor));
    changesTestSubscriber.awaitTerminalEvent(60, SECONDS);
    changesTestSubscriber.assertNoErrors();
    changesTestSubscriber.assertValues(Changes.newInstance(TestItem.CONTENT_URI), Changes.newInstance(TestItem.CONTENT_URI));
}
Also used : Changes(com.pushtorefresh.storio3.contentresolver.Changes) TestSubscriber(io.reactivex.subscribers.TestSubscriber) Cursor(android.database.Cursor) Uri(android.net.Uri) Test(org.junit.Test)

Example 4 with Changes

use of com.pushtorefresh.storio3.contentresolver.Changes in project storio by pushtorefresh.

the class PutOperationTest method updateNullFieldToNotNull.

@Test
public void updateNullFieldToNotNull() {
    TestSubscriber<Changes> changesTestSubscriber = new TestSubscriber<Changes>();
    storIOContentResolver.observeChangesOfUri(TestItem.CONTENT_URI, BackpressureStrategy.MISSING).take(2).subscribe(changesTestSubscriber);
    Uri insertedUri = // firstly, optional value is null
    contentResolver.insert(TestItem.CONTENT_URI, TestItem.create(null, "value", null).toContentValues());
    // change to not null
    TestItem testItem = TestItem.create(ContentUris.parseId(insertedUri), "value", "optionalValue");
    PutResult updateResult = storIOContentResolver.put().object(testItem).prepare().executeAsBlocking();
    assertThat(updateResult.wasUpdated()).isTrue();
    Cursor cursor = contentResolver.query(TestItem.CONTENT_URI, null, null, null, null);
    Assertions.assertThat(cursor).hasCount(1);
    cursor.moveToFirst();
    assertThat(testItem).isEqualTo(TestItem.fromCursor(cursor));
    changesTestSubscriber.awaitTerminalEvent(60, SECONDS);
    changesTestSubscriber.assertNoErrors();
    changesTestSubscriber.assertValues(Changes.newInstance(TestItem.CONTENT_URI), Changes.newInstance(TestItem.CONTENT_URI));
}
Also used : Changes(com.pushtorefresh.storio3.contentresolver.Changes) TestSubscriber(io.reactivex.subscribers.TestSubscriber) Cursor(android.database.Cursor) Uri(android.net.Uri) PutResult(com.pushtorefresh.storio3.contentresolver.operations.put.PutResult) Test(org.junit.Test)

Example 5 with Changes

use of com.pushtorefresh.storio3.contentresolver.Changes in project storio by pushtorefresh.

the class PutOperationTest method insertContentValuesAsRxFlowable.

@Test
public void insertContentValuesAsRxFlowable() {
    TestSubscriber<Changes> changesTestSubscriber = new TestSubscriber<Changes>();
    storIOContentResolver.observeChangesOfUri(TestItem.CONTENT_URI, BackpressureStrategy.MISSING).take(1).subscribe(changesTestSubscriber);
    TestItem testItem = TestItem.create(null, "value");
    ContentValues cv = testItem.toContentValues();
    PutResult insertResult = storIOContentResolver.put().contentValues(cv).withPutResolver(testItemContentValuesPutResolver).prepare().asRxFlowable(BackpressureStrategy.MISSING).blockingFirst();
    assertThat(insertResult.wasInserted()).isTrue();
    Cursor cursor = contentResolver.query(TestItem.CONTENT_URI, null, null, null, null);
    Assertions.assertThat(cursor).hasCount(1);
    cursor.moveToFirst();
    assertThat(testItem.equalsWithoutId(TestItem.fromCursor(cursor))).isTrue();
    changesTestSubscriber.awaitTerminalEvent(60, SECONDS);
    changesTestSubscriber.assertNoErrors();
    changesTestSubscriber.assertValue(Changes.newInstance(TestItem.CONTENT_URI));
}
Also used : Changes(com.pushtorefresh.storio3.contentresolver.Changes) ContentValues(android.content.ContentValues) TestSubscriber(io.reactivex.subscribers.TestSubscriber) Cursor(android.database.Cursor) PutResult(com.pushtorefresh.storio3.contentresolver.operations.put.PutResult) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)56 TestSubscriber (io.reactivex.subscribers.TestSubscriber)47 Changes (com.pushtorefresh.storio3.contentresolver.Changes)37 Cursor (android.database.Cursor)33 Uri (android.net.Uri)16 Changes (com.pushtorefresh.storio3.sqlite.Changes)16 PutResult (com.pushtorefresh.storio3.contentresolver.operations.put.PutResult)15 Disposable (io.reactivex.disposables.Disposable)11 HashSet (java.util.HashSet)7 DeleteResult (com.pushtorefresh.storio3.contentresolver.operations.delete.DeleteResult)6 AbstractEmissionChecker (com.pushtorefresh.storio3.test.AbstractEmissionChecker)6 LinkedList (java.util.LinkedList)6 ContentResolver (android.content.ContentResolver)5 ContentObserver (android.database.ContentObserver)5 ArrayList (java.util.ArrayList)5 CountDownLatch (java.util.concurrent.CountDownLatch)5 Mockito.doAnswer (org.mockito.Mockito.doAnswer)5 InvocationOnMock (org.mockito.invocation.InvocationOnMock)5 Answer (org.mockito.stubbing.Answer)5 ContentValues (android.content.ContentValues)4