Search in sources :

Example 1 with Tweet

use of com.pushtorefresh.storio.sample.db.entities.Tweet 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 2 with Tweet

use of com.pushtorefresh.storio.sample.db.entities.Tweet in project storio by pushtorefresh.

the class TweetsAdapter method onBindViewHolder.

@SuppressLint("SetTextI18n")
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
    final Tweet tweet = tweets.get(position);
    holder.authorTextView.setText("@" + tweet.author());
    holder.contentTextView.setText(tweet.content());
}
Also used : Tweet(com.pushtorefresh.storio.sample.db.entities.Tweet) SuppressLint(android.annotation.SuppressLint)

Example 3 with Tweet

use of com.pushtorefresh.storio.sample.db.entities.Tweet 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 4 with Tweet

use of com.pushtorefresh.storio.sample.db.entities.Tweet in project storio by pushtorefresh.

the class TweetsFragment method reloadData.

void reloadData() {
    uiStateController.setUiStateLoading();
    final Subscription subscription = storIOSQLite.get().listOfObjects(Tweet.class).withQuery(TweetsTable.QUERY_ALL).prepare().asRxObservable().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 Action1<List<Tweet>>() {

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

        @Override
        public void call(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(null);
        }
    });
    // Preventing memory leak (other Observables: Put, Delete emit result once so memory leak won't live long)
    // Because rx.Observable 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 subscriptions that you want to unsubscribe
    // In something like CompositeSubscription and unsubscribe them in appropriate moment of component lifecycle
    unsubscribeOnStop(subscription);
}
Also used : Tweet(com.pushtorefresh.storio.sample.db.entities.Tweet) ArrayList(java.util.ArrayList) List(java.util.List) Subscription(rx.Subscription)

Aggregations

Tweet (com.pushtorefresh.storio.sample.db.entities.Tweet)4 NonNull (android.support.annotation.NonNull)2 User (com.pushtorefresh.storio.sample.db.entities.User)2 SuppressLint (android.annotation.SuppressLint)1 TweetWithUser (com.pushtorefresh.storio.sample.db.entities.TweetWithUser)1 UserWithTweets (com.pushtorefresh.storio.sample.db.entities.UserWithTweets)1 StorIOSQLite (com.pushtorefresh.storio.sqlite.StorIOSQLite)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Subscription (rx.Subscription)1