Search in sources :

Example 41 with NonNull

use of android.support.annotation.NonNull in project storio by pushtorefresh.

the class UserWithTweetsGetResolver method mapFromCursor.

@NonNull
@Override
public UserWithTweets mapFromCursor(@NonNull Cursor cursor) {
    final StorIOSQLite storIOSQLite = storIOSQLiteFromPerformGet.get();
    // Or you can manually parse cursor (it will be sliiightly faster)
    final User user = userGetResolver.mapFromCursor(cursor);
    // Yep, you can reuse StorIO here!
    // Or, you can do manual low level requests here
    // BTW, if you profiled your app and found that such queries are not very fast
    // You can always add some optimized version for particular queries to improve the performance
    final List<Tweet> tweetsOfTheUser = storIOSQLite.get().listOfObjects(Tweet.class).withQuery(Query.builder().table(TweetsTable.TABLE).where(TweetsTable.COLUMN_AUTHOR + "=?").whereArgs(user.nick()).build()).prepare().executeAsBlocking();
    return new UserWithTweets(user, tweetsOfTheUser);
}
Also used : User(com.pushtorefresh.storio.sample.db.entities.User) Tweet(com.pushtorefresh.storio.sample.db.entities.Tweet) UserWithTweets(com.pushtorefresh.storio.sample.db.entities.UserWithTweets) StorIOSQLite(com.pushtorefresh.storio.sqlite.StorIOSQLite) NonNull(android.support.annotation.NonNull)

Example 42 with NonNull

use of android.support.annotation.NonNull in project storio by pushtorefresh.

the class BoxedTypesMethodsConstructorIgnoreNullStorIOSQLiteGetResolver method mapFromCursor.

/**
     * {@inheritDoc}
     */
@Override
@NonNull
public BoxedTypesMethodsConstructorIgnoreNull mapFromCursor(@NonNull Cursor cursor) {
    Boolean field1 = null;
    if (!cursor.isNull(cursor.getColumnIndex("field1"))) {
        field1 = cursor.getInt(cursor.getColumnIndex("field1")) == 1;
    }
    Short field2 = null;
    if (!cursor.isNull(cursor.getColumnIndex("field2"))) {
        field2 = cursor.getShort(cursor.getColumnIndex("field2"));
    }
    Integer field3 = null;
    if (!cursor.isNull(cursor.getColumnIndex("field3"))) {
        field3 = cursor.getInt(cursor.getColumnIndex("field3"));
    }
    Long field4 = null;
    if (!cursor.isNull(cursor.getColumnIndex("field4"))) {
        field4 = cursor.getLong(cursor.getColumnIndex("field4"));
    }
    Float field5 = null;
    if (!cursor.isNull(cursor.getColumnIndex("field5"))) {
        field5 = cursor.getFloat(cursor.getColumnIndex("field5"));
    }
    Double field6 = null;
    if (!cursor.isNull(cursor.getColumnIndex("field6"))) {
        field6 = cursor.getDouble(cursor.getColumnIndex("field6"));
    }
    BoxedTypesMethodsConstructorIgnoreNull object = new BoxedTypesMethodsConstructorIgnoreNull(field1, field2, field3, field4, field5, field6);
    return object;
}
Also used : Integer(java.lang.Integer) Float(java.lang.Float) Long(java.lang.Long) Boolean(java.lang.Boolean) Double(java.lang.Double) Short(java.lang.Short) NonNull(android.support.annotation.NonNull) Override(java.lang.Override)

Example 43 with NonNull

use of android.support.annotation.NonNull in project storio by pushtorefresh.

the class DefaultDeleteResolverTest method performDelete.

@Test
public void performDelete() {
    final StorIOContentResolver storIOContentResolver = mock(StorIOContentResolver.class);
    final StorIOContentResolver.LowLevel lowLevel = mock(StorIOContentResolver.LowLevel.class);
    when(storIOContentResolver.lowLevel()).thenReturn(lowLevel);
    final int expectedNumberOfRowsDeleted = 1;
    when(lowLevel.delete(any(DeleteQuery.class))).thenReturn(expectedNumberOfRowsDeleted);
    final Uri expectedUri = mock(Uri.class);
    final DeleteQuery expectedDeleteQuery = DeleteQuery.builder().uri(expectedUri).where("test where clause").whereArgs("test").build();
    final TestItem testItem = TestItem.newInstance();
    final DefaultDeleteResolver<TestItem> defaultDeleteResolver = new DefaultDeleteResolver<TestItem>() {

        @NonNull
        @Override
        protected DeleteQuery mapToDeleteQuery(@NonNull TestItem object) {
            assertThat(object).isSameAs(testItem);
            return expectedDeleteQuery;
        }
    };
    // Performing Delete Operation
    final DeleteResult deleteResult = defaultDeleteResolver.performDelete(storIOContentResolver, testItem);
    // checks that required delete was performed
    verify(lowLevel, times(1)).delete(expectedDeleteQuery);
    // only one delete should be performed
    verify(lowLevel, times(1)).delete(any(DeleteQuery.class));
    // delete result checks
    assertThat(deleteResult.numberOfRowsDeleted()).isEqualTo(expectedNumberOfRowsDeleted);
    assertThat(deleteResult.affectedUris()).hasSize(1);
    assertThat(deleteResult.affectedUris()).contains(expectedUri);
}
Also used : NonNull(android.support.annotation.NonNull) DeleteQuery(com.pushtorefresh.storio.contentresolver.queries.DeleteQuery) StorIOContentResolver(com.pushtorefresh.storio.contentresolver.StorIOContentResolver) Uri(android.net.Uri) Test(org.junit.Test)

Example 44 with NonNull

use of android.support.annotation.NonNull in project storio by pushtorefresh.

the class TweetWithUserGetResolver method mapFromCursor.

// We expect that cursor will contain both Tweet and User: SQL JOIN
@NonNull
@Override
public TweetWithUser mapFromCursor(@NonNull Cursor cursor) {
    final Tweet tweet = Tweet.newTweet(cursor.getLong(cursor.getColumnIndexOrThrow(Relations.QUERY_COLUMN_TWEET_ID)), cursor.getString(cursor.getColumnIndexOrThrow(Relations.QUERY_COLUMN_TWEET_AUTHOR)), cursor.getString(cursor.getColumnIndexOrThrow(Relations.QUERY_COLUMN_TWEET_CONTENT)));
    final User user = User.newUser(cursor.getLong(cursor.getColumnIndexOrThrow(Relations.QUERY_COLUMN_USER_ID)), cursor.getString(cursor.getColumnIndexOrThrow(Relations.QUERY_COLUMN_USER_NICK)));
    return new TweetWithUser(tweet, user);
}
Also used : User(com.pushtorefresh.storio.sample.db.entities.User) TweetWithUser(com.pushtorefresh.storio.sample.db.entities.TweetWithUser) TweetWithUser(com.pushtorefresh.storio.sample.db.entities.TweetWithUser) Tweet(com.pushtorefresh.storio.sample.db.entities.Tweet) NonNull(android.support.annotation.NonNull)

Example 45 with NonNull

use of android.support.annotation.NonNull in project storio by pushtorefresh.

the class DefaultPutResolverTest method shouldCloseCursorIfInsertThrowsException.

@Test
public void shouldCloseCursorIfInsertThrowsException() {
    final StorIOContentResolver storIOContentResolver = mock(StorIOContentResolver.class);
    final StorIOContentResolver.LowLevel lowLevel = mock(StorIOContentResolver.LowLevel.class);
    when(storIOContentResolver.lowLevel()).thenReturn(lowLevel);
    // item without id, should be inserted
    final TestItem testItem = new TestItem(null);
    final Query expectedQuery = Query.builder().uri(TestItem.CONTENT_URI).where(TestItem.COLUMN_ID + " = ?").whereArgs(testItem.getId()).build();
    final Cursor cursor = mock(Cursor.class);
    when(lowLevel.query(eq(expectedQuery))).thenReturn(cursor);
    when(cursor.getCount()).thenReturn(// No results -> insert should be performed
    0);
    final InsertQuery expectedInsertQuery = InsertQuery.builder().uri(TestItem.CONTENT_URI).build();
    when(lowLevel.insert(eq(expectedInsertQuery), any(ContentValues.class))).thenThrow(new IllegalStateException("Fake exception from ContentResolver"));
    final PutResolver<TestItem> putResolver = new DefaultPutResolver<TestItem>() {

        @NonNull
        @Override
        protected InsertQuery mapToInsertQuery(@NonNull TestItem object) {
            return InsertQuery.builder().uri(TestItem.CONTENT_URI).build();
        }

        @NonNull
        @Override
        protected UpdateQuery mapToUpdateQuery(@NonNull TestItem object) {
            return UpdateQuery.builder().uri(TestItem.CONTENT_URI).where(TestItem.COLUMN_ID + " = ?").whereArgs(object.getId()).build();
        }

        @NonNull
        @Override
        protected ContentValues mapToContentValues(@NonNull TestItem object) {
            return TestItem.MAP_TO_CONTENT_VALUES.call(object);
        }
    };
    try {
        putResolver.performPut(storIOContentResolver, testItem);
        failBecauseExceptionWasNotThrown(IllegalStateException.class);
    } catch (IllegalStateException expected) {
        assertThat(expected).hasMessage("Fake exception from ContentResolver");
        verify(storIOContentResolver).lowLevel();
        // Checks that it asks actual ContentResolver for results
        verify(lowLevel).query(eq(expectedQuery));
        verify(cursor).getCount();
        // Cursor must be closed in case of exception!
        verify(cursor).close();
        verify(lowLevel).insert(eq(expectedInsertQuery), any(ContentValues.class));
        verifyNoMoreInteractions(storIOContentResolver, lowLevel, cursor);
    }
}
Also used : ContentValues(android.content.ContentValues) InsertQuery(com.pushtorefresh.storio.contentresolver.queries.InsertQuery) Query(com.pushtorefresh.storio.contentresolver.queries.Query) InsertQuery(com.pushtorefresh.storio.contentresolver.queries.InsertQuery) UpdateQuery(com.pushtorefresh.storio.contentresolver.queries.UpdateQuery) NonNull(android.support.annotation.NonNull) Cursor(android.database.Cursor) StorIOContentResolver(com.pushtorefresh.storio.contentresolver.StorIOContentResolver) Test(org.junit.Test)

Aggregations

NonNull (android.support.annotation.NonNull)747 View (android.view.View)94 TextView (android.widget.TextView)90 ArrayList (java.util.ArrayList)83 Intent (android.content.Intent)53 ContentValues (android.content.ContentValues)46 Bundle (android.os.Bundle)46 Test (org.junit.Test)45 AlertDialog (android.support.v7.app.AlertDialog)41 Cursor (android.database.Cursor)38 List (java.util.List)34 IOException (java.io.IOException)32 MaterialDialog (com.afollestad.materialdialogs.MaterialDialog)31 LayoutInflater (android.view.LayoutInflater)29 RecyclerView (android.support.v7.widget.RecyclerView)28 ImageView (android.widget.ImageView)28 File (java.io.File)28 DialogAction (com.afollestad.materialdialogs.DialogAction)25 HashMap (java.util.HashMap)25 Bitmap (android.graphics.Bitmap)24