Search in sources :

Example 1 with CheckResult

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

the class PreparedGetCursor method asRxObservable.

/**
     * Creates "Hot" {@link Observable} which will be subscribed to changes of tables from query
     * and will emit result each time change occurs.
     * <p>
     * First result will be emitted immediately after subscription,
     * other emissions will occur only if changes of tables from query will occur during lifetime of
     * the {@link Observable}.
     * <dl>
     * <dt><b>Scheduler:</b></dt>
     * <dd>Operates on {@link StorIOSQLite#defaultScheduler()} if not {@code null}.</dd>
     * </dl>
     * <p>
     * Please don't forget to unsubscribe from this {@link Observable} because
     * it's "Hot" and endless.
     *
     * @return non-null {@link Observable} which will emit non-null
     * list with mapped results and will be subscribed to changes of tables from query.
     */
@NonNull
@CheckResult
@Override
public Observable<Cursor> asRxObservable() {
    throwExceptionIfRxJavaIsNotAvailable("asRxObservable()");
    final Set<String> tables;
    if (query != null) {
        tables = new HashSet<String>(1);
        tables.add(query.table());
    } else if (rawQuery != null) {
        tables = rawQuery.observesTables();
    } else {
        throw new StorIOException("Please specify query");
    }
    final Observable<Cursor> observable;
    if (!tables.isEmpty()) {
        observable = storIOSQLite.observeChangesInTables(// each change triggers executeAsBlocking
        tables).map(MapSomethingToExecuteAsBlocking.newInstance(this)).startWith(// start stream with first query result
        Observable.create(OnSubscribeExecuteAsBlocking.newInstance(this))).onBackpressureLatest();
    } else {
        observable = Observable.create(OnSubscribeExecuteAsBlocking.newInstance(this));
    }
    return RxJavaUtils.subscribeOn(storIOSQLite, observable);
}
Also used : StorIOException(com.pushtorefresh.storio.StorIOException) Cursor(android.database.Cursor) NonNull(android.support.annotation.NonNull) CheckResult(android.support.annotation.CheckResult)

Example 2 with CheckResult

use of android.support.annotation.CheckResult 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 3 with CheckResult

use of android.support.annotation.CheckResult in project sqlbrite by square.

the class BriteContentResolver method createQuery.

/**
   * Create an observable which will notify subscribers with a {@linkplain Query query} for
   * execution. Subscribers are responsible for <b>always</b> closing {@link Cursor} instance
   * returned from the {@link Query}.
   * <p>
   * Subscribers will receive an immediate notification for initial data as well as subsequent
   * notifications for when the supplied {@code uri}'s data changes. Unsubscribe when you no longer
   * want updates to a query.
   * <p>
   * Since content resolver triggers are inherently asynchronous, items emitted from the returned
   * observable use the {@link Scheduler} supplied to {@link SqlBrite#wrapContentProvider}. For
   * consistency, the immediate notification sent on subscribe also uses this scheduler. As such,
   * calling {@link Observable#subscribeOn subscribeOn} on the returned observable has no effect.
   * <p>
   * Note: To skip the immediate notification and only receive subsequent notifications when data
   * has changed call {@code skip(1)} on the returned observable.
   * <p>
   * <b>Warning:</b> this method does not perform the query! Only by subscribing to the returned
   * {@link Observable} will the operation occur.
   *
   * @see ContentResolver#query(Uri, String[], String, String[], String)
   * @see ContentResolver#registerContentObserver(Uri, boolean, ContentObserver)
   */
@CheckResult
@NonNull
public QueryObservable createQuery(@NonNull final Uri uri, @Nullable final String[] projection, @Nullable final String selection, @Nullable final String[] selectionArgs, @Nullable final String sortOrder, final boolean notifyForDescendents) {
    final Query query = new Query() {

        @Override
        public Cursor run() {
            long startNanos = nanoTime();
            Cursor cursor = contentResolver.query(uri, projection, selection, selectionArgs, sortOrder);
            if (logging) {
                long tookMillis = NANOSECONDS.toMillis(nanoTime() - startNanos);
                log("QUERY (%sms)\n  uri: %s\n  projection: %s\n  selection: %s\n  selectionArgs: %s\n  " + "sortOrder: %s\n  notifyForDescendents: %s", tookMillis, uri, Arrays.toString(projection), selection, Arrays.toString(selectionArgs), sortOrder, notifyForDescendents);
            }
            return cursor;
        }
    };
    OnSubscribe<Query> subscribe = new OnSubscribe<Query>() {

        @Override
        public void call(final Subscriber<? super Query> subscriber) {
            final ContentObserver observer = new ContentObserver(contentObserverHandler) {

                @Override
                public void onChange(boolean selfChange) {
                    subscriber.onNext(query);
                }
            };
            contentResolver.registerContentObserver(uri, notifyForDescendents, observer);
            subscriber.add(Subscriptions.create(new Action0() {

                @Override
                public void call() {
                    contentResolver.unregisterContentObserver(observer);
                }
            }));
            // Trigger initial query.
            subscriber.onNext(query);
        }
    };
    final Observable<Query> queryObservable = //
    Observable.create(subscribe).onBackpressureLatest().observeOn(//
    scheduler).compose(// Apply the user's query transformer.
    queryTransformer).onBackpressureLatest();
    // TODO switch to .to() when non-@Experimental
    return new QueryObservable(new OnSubscribe<Query>() {

        @Override
        public void call(Subscriber<? super Query> subscriber) {
            queryObservable.unsafeSubscribe(subscriber);
        }
    });
}
Also used : Action0(rx.functions.Action0) Query(com.squareup.sqlbrite.SqlBrite.Query) Subscriber(rx.Subscriber) OnSubscribe(rx.Observable.OnSubscribe) Cursor(android.database.Cursor) ContentObserver(android.database.ContentObserver) CheckResult(android.support.annotation.CheckResult) NonNull(android.support.annotation.NonNull)

Example 4 with CheckResult

use of android.support.annotation.CheckResult in project sqlbrite by square.

the class BriteDatabase method query.

/**
   * Runs the provided SQL and returns a {@link Cursor} over the result set.
   *
   * @see SQLiteDatabase#rawQuery(String, String[])
   */
@CheckResult
@WorkerThread
public Cursor query(@NonNull String sql, @NonNull String... args) {
    long startNanos = nanoTime();
    Cursor cursor = getReadableDatabase().rawQuery(sql, args);
    long tookMillis = NANOSECONDS.toMillis(nanoTime() - startNanos);
    if (logging) {
        log("QUERY (%sms)\n  sql: %s\n  args: %s", tookMillis, indentSql(sql), Arrays.toString(args));
    }
    return cursor;
}
Also used : Cursor(android.database.Cursor) WorkerThread(android.support.annotation.WorkerThread) CheckResult(android.support.annotation.CheckResult)

Example 5 with CheckResult

use of android.support.annotation.CheckResult in project AndroidUtilLib by SiberiaDante.

the class SDToastUtil method custom.

@CheckResult
public static Toast custom(@NonNull Context context, @NonNull String message, Drawable icon, @ColorInt int textColor, @ColorInt int tintColor, int duration, boolean withIcon, boolean shouldTint) {
    if (currentToast == null) {
        currentToast = new Toast(context);
    }
    final View toastLayout = ((LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.sd_toast_layout, null);
    final ImageView toastIcon = (ImageView) toastLayout.findViewById(R.id.toast_icon);
    final TextView toastTextView = (TextView) toastLayout.findViewById(R.id.toast_text);
    Drawable drawableFrame;
    if (shouldTint) {
        drawableFrame = tint9PatchDrawableFrame(context, tintColor);
    } else {
        drawableFrame = getDrawable(R.drawable.toast_frame);
    }
    setBackground(toastLayout, drawableFrame);
    if (withIcon) {
        if (icon == null) {
            throw new IllegalArgumentException("Avoid passing 'icon' as null if 'withIcon' is set to true");
        } else {
            setBackground(toastIcon, icon);
        }
    } else {
        toastIcon.setVisibility(View.GONE);
    }
    toastTextView.setTextColor(textColor);
    toastTextView.setText(message);
    toastTextView.setTypeface(Typeface.create(TOAST_TYPEFACE, Typeface.NORMAL));
    currentToast.setView(toastLayout);
    currentToast.setDuration(duration);
    return currentToast;
}
Also used : Toast(android.widget.Toast) LayoutInflater(android.view.LayoutInflater) Drawable(android.graphics.drawable.Drawable) NinePatchDrawable(android.graphics.drawable.NinePatchDrawable) TextView(android.widget.TextView) ImageView(android.widget.ImageView) ImageView(android.widget.ImageView) View(android.view.View) TextView(android.widget.TextView) CheckResult(android.support.annotation.CheckResult)

Aggregations

CheckResult (android.support.annotation.CheckResult)27 NonNull (android.support.annotation.NonNull)9 TextView (android.widget.TextView)5 Cursor (android.database.Cursor)4 LayoutInflater (android.view.LayoutInflater)4 View (android.view.View)4 Toast (android.widget.Toast)4 Drawable (android.graphics.drawable.Drawable)3 NinePatchDrawable (android.graphics.drawable.NinePatchDrawable)3 ImageView (android.widget.ImageView)3 Resources (android.content.res.Resources)2 ContentObserver (android.database.ContentObserver)2 LinearLayout (android.widget.LinearLayout)2 Options (com.bumptech.glide.load.Options)2 StorIOException (com.pushtorefresh.storio.StorIOException)2 OnSubscribeExecuteAsBlockingCompletable (com.pushtorefresh.storio.operations.internal.OnSubscribeExecuteAsBlockingCompletable)2 CompletableOnSubscribeExecuteAsBlocking (com.pushtorefresh.storio3.operations.internal.CompletableOnSubscribeExecuteAsBlocking)2 Completable (io.reactivex.Completable)2 Completable (rx.Completable)2 SuppressLint (android.annotation.SuppressLint)1