Search in sources :

Example 51 with NonNull

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

the class RxJavaUtils method createCompletable.

@CheckResult
@NonNull
public static <T> Completable createCompletable(@NonNull StorIOSQLite storIOSQLite, @NonNull PreparedOperation<T> operation) {
    throwExceptionIfRxJavaIsNotAvailable("asRxCompletable()");
    final Completable completable = Completable.create(OnSubscribeExecuteAsBlockingCompletable.newInstance(operation));
    return subscribeOn(storIOSQLite, completable);
}
Also used : Completable(rx.Completable) OnSubscribeExecuteAsBlockingCompletable(com.pushtorefresh.storio.operations.internal.OnSubscribeExecuteAsBlockingCompletable) CheckResult(android.support.annotation.CheckResult) NonNull(android.support.annotation.NonNull)

Example 52 with NonNull

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

the class DefaultPutResolver method performPut.

/**
     * {@inheritDoc}
     */
@NonNull
@Override
public PutResult performPut(@NonNull StorIOSQLite storIOSQLite, @NonNull T object) {
    final UpdateQuery updateQuery = mapToUpdateQuery(object);
    final StorIOSQLite.LowLevel lowLevel = storIOSQLite.lowLevel();
    // for data consistency in concurrent environment, encapsulate Put Operation into transaction
    lowLevel.beginTransaction();
    try {
        final Cursor cursor = lowLevel.query(Query.builder().table(updateQuery.table()).where(nullableString(updateQuery.where())).whereArgs((Object[]) nullableArrayOfStringsFromListOfStrings(updateQuery.whereArgs())).build());
        final PutResult putResult;
        try {
            final ContentValues contentValues = mapToContentValues(object);
            if (cursor.getCount() == 0) {
                final InsertQuery insertQuery = mapToInsertQuery(object);
                final long insertedId = lowLevel.insert(insertQuery, contentValues);
                putResult = PutResult.newInsertResult(insertedId, insertQuery.table());
            } else {
                final int numberOfRowsUpdated = lowLevel.update(updateQuery, contentValues);
                putResult = PutResult.newUpdateResult(numberOfRowsUpdated, updateQuery.table());
            }
        } finally {
            cursor.close();
        }
        // everything okay
        lowLevel.setTransactionSuccessful();
        return putResult;
    } finally {
        // in case of bad situations, db won't be affected
        lowLevel.endTransaction();
    }
}
Also used : ContentValues(android.content.ContentValues) InsertQuery(com.pushtorefresh.storio.sqlite.queries.InsertQuery) UpdateQuery(com.pushtorefresh.storio.sqlite.queries.UpdateQuery) Cursor(android.database.Cursor) StorIOSQLite(com.pushtorefresh.storio.sqlite.StorIOSQLite) NonNull(android.support.annotation.NonNull)

Example 53 with NonNull

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

the class PreparedPutCollectionOfObjects method executeAsBlocking.

/**
     * Executes Put Operation immediately in current thread.
     * <p>
     * Notice: This is blocking I/O operation that should not be executed on the Main Thread,
     * it can cause ANR (Activity Not Responding dialog), block the UI and drop animations frames.
     * So please, call this method on some background thread. See {@link WorkerThread}.
     *
     * @return non-null results of Put Operation.
     */
@SuppressWarnings("unchecked")
@WorkerThread
@NonNull
@Override
public PutResults<T> executeAsBlocking() {
    try {
        final StorIOSQLite.LowLevel lowLevel = storIOSQLite.lowLevel();
        // Nullable
        final List<SimpleImmutableEntry<T, PutResolver<T>>> objectsAndPutResolvers;
        if (explicitPutResolver != null) {
            objectsAndPutResolvers = null;
        } else {
            objectsAndPutResolvers = new ArrayList<SimpleImmutableEntry<T, PutResolver<T>>>(objects.size());
            for (final T object : objects) {
                final SQLiteTypeMapping<T> typeMapping = (SQLiteTypeMapping<T>) lowLevel.typeMapping(object.getClass());
                if (typeMapping == null) {
                    throw new IllegalStateException("One of the objects from the collection does not have type mapping: " + "object = " + object + ", object.class = " + object.getClass() + "," + "db was not affected by this operation, please add type mapping for this type");
                }
                objectsAndPutResolvers.add(new SimpleImmutableEntry<T, PutResolver<T>>(object, typeMapping.putResolver()));
            }
        }
        if (useTransaction) {
            lowLevel.beginTransaction();
        }
        final Map<T, PutResult> results = new HashMap<T, PutResult>(objects.size());
        boolean transactionSuccessful = false;
        try {
            if (explicitPutResolver != null) {
                for (final T object : objects) {
                    final PutResult putResult = explicitPutResolver.performPut(storIOSQLite, object);
                    results.put(object, putResult);
                    if (!useTransaction && (putResult.wasInserted() || putResult.wasUpdated())) {
                        lowLevel.notifyAboutChanges(Changes.newInstance(putResult.affectedTables()));
                    }
                }
            } else {
                for (final SimpleImmutableEntry<T, PutResolver<T>> objectAndPutResolver : objectsAndPutResolvers) {
                    final T object = objectAndPutResolver.getKey();
                    final PutResolver<T> putResolver = objectAndPutResolver.getValue();
                    final PutResult putResult = putResolver.performPut(storIOSQLite, object);
                    results.put(object, putResult);
                    if (!useTransaction && (putResult.wasInserted() || putResult.wasUpdated())) {
                        lowLevel.notifyAboutChanges(Changes.newInstance(putResult.affectedTables()));
                    }
                }
            }
            if (useTransaction) {
                lowLevel.setTransactionSuccessful();
                transactionSuccessful = true;
            }
        } finally {
            if (useTransaction) {
                lowLevel.endTransaction();
                // if put was in transaction and it was successful -> notify about changes
                if (transactionSuccessful) {
                    // in most cases it will be 1 table
                    final Set<String> affectedTables = new HashSet<String>(1);
                    for (final T object : results.keySet()) {
                        final PutResult putResult = results.get(object);
                        if (putResult.wasInserted() || putResult.wasUpdated()) {
                            affectedTables.addAll(putResult.affectedTables());
                        }
                    }
                    // It'll reduce number of possible deadlock situations
                    if (!affectedTables.isEmpty()) {
                        lowLevel.notifyAboutChanges(Changes.newInstance(affectedTables));
                    }
                }
            }
        }
        return PutResults.newInstance(results);
    } catch (Exception exception) {
        throw new StorIOException("Error has occurred during Put operation. objects = " + objects, exception);
    }
}
Also used : HashMap(java.util.HashMap) StorIOException(com.pushtorefresh.storio.StorIOException) StorIOException(com.pushtorefresh.storio.StorIOException) SimpleImmutableEntry(java.util.AbstractMap.SimpleImmutableEntry) StorIOSQLite(com.pushtorefresh.storio.sqlite.StorIOSQLite) SQLiteTypeMapping(com.pushtorefresh.storio.sqlite.SQLiteTypeMapping) HashSet(java.util.HashSet) WorkerThread(android.support.annotation.WorkerThread) NonNull(android.support.annotation.NonNull)

Example 54 with NonNull

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

the class PreparedPutContentValuesIterable method executeAsBlocking.

/**
     * Executes Put Operation immediately in current thread.
     * <p>
     * Notice: This is blocking I/O operation that should not be executed on the Main Thread,
     * it can cause ANR (Activity Not Responding dialog), block the UI and drop animations frames.
     * So please, call this method on some background thread. See {@link WorkerThread}.
     *
     * @return non-null results of Put Operation.
     */
@WorkerThread
@NonNull
@Override
public PutResults<ContentValues> executeAsBlocking() {
    try {
        final StorIOSQLite.LowLevel lowLevel = storIOSQLite.lowLevel();
        final Map<ContentValues, PutResult> putResults = new HashMap<ContentValues, PutResult>();
        if (useTransaction) {
            lowLevel.beginTransaction();
        }
        boolean transactionSuccessful = false;
        try {
            for (ContentValues contentValues : contentValuesIterable) {
                final PutResult putResult = putResolver.performPut(storIOSQLite, contentValues);
                putResults.put(contentValues, putResult);
                if (!useTransaction && (putResult.wasInserted() || putResult.wasUpdated())) {
                    lowLevel.notifyAboutChanges(Changes.newInstance(putResult.affectedTables()));
                }
            }
            if (useTransaction) {
                lowLevel.setTransactionSuccessful();
                transactionSuccessful = true;
            }
        } finally {
            if (useTransaction) {
                lowLevel.endTransaction();
                if (transactionSuccessful) {
                    // in most cases it will be 1 table
                    final Set<String> affectedTables = new HashSet<String>(1);
                    for (final ContentValues contentValues : putResults.keySet()) {
                        final PutResult putResult = putResults.get(contentValues);
                        if (putResult.wasInserted() || putResult.wasUpdated()) {
                            affectedTables.addAll(putResult.affectedTables());
                        }
                    }
                    // It'll reduce number of possible deadlock situations
                    if (!affectedTables.isEmpty()) {
                        lowLevel.notifyAboutChanges(Changes.newInstance(affectedTables));
                    }
                }
            }
        }
        return PutResults.newInstance(putResults);
    } catch (Exception exception) {
        throw new StorIOException("Error has occurred during Put operation. contentValues = " + contentValuesIterable, exception);
    }
}
Also used : ContentValues(android.content.ContentValues) StorIOException(com.pushtorefresh.storio.StorIOException) HashMap(java.util.HashMap) StorIOSQLite(com.pushtorefresh.storio.sqlite.StorIOSQLite) StorIOException(com.pushtorefresh.storio.StorIOException) HashSet(java.util.HashSet) WorkerThread(android.support.annotation.WorkerThread) NonNull(android.support.annotation.NonNull)

Example 55 with NonNull

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

the class ToStringChecker method createObject.

@NonNull
private T createObject() {
    Constructor[] constructors = clazz.getDeclaredConstructors();
    for (Constructor constructor : constructors) {
        try {
            constructor.setAccessible(true);
            List<Object> sampleParameters = new ArrayList<Object>();
            for (Class parameterType : constructor.getParameterTypes()) {
                sampleParameters.add(createSampleValueOfType(parameterType));
            }
            //noinspection unchecked
            return (T) constructor.newInstance(sampleParameters.toArray());
        } catch (Exception uh) {
        // No luck with this constructor, let's try another one
        }
    }
    throw new IllegalStateException("Tried all declared constructors, no luck :(");
}
Also used : Constructor(java.lang.reflect.Constructor) ArrayList(java.util.ArrayList) NonNull(android.support.annotation.NonNull)

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