Search in sources :

Example 1 with Tweet

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

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

use of com.pushtorefresh.storio3.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.id = tweet.id();
    holder.authorTextView.setText("@" + tweet.author());
    holder.contentTextView.setText(tweet.content());
}
Also used : Tweet(com.pushtorefresh.storio3.sample.db.entities.Tweet) SuppressLint(android.annotation.SuppressLint)

Example 4 with Tweet

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

the class RelationsTest method name.

@Test
public void name() {
    SampleApp sampleApp = (SampleApp) RuntimeEnvironment.application;
    sampleApp.appComponent().storIOSQLite().put().objects(asList(Tweet.newTweet(1L, "artem_zin", "test tweet 1"), Tweet.newTweet(2L, "artem_zin", "test tweet 2"), Tweet.newTweet(3L, "nikitin-da", "test tweet 3"), User.newUser(1L, "artem_zin"), User.newUser(2L, "nikitin-da"))).prepare().executeAsBlocking();
    Relations relations = new Relations(sampleApp.appComponent().storIOSQLite());
    List<TweetWithUser> tweetsWithUsers = relations.getTweetWithUser();
    // Same as count of tweets, not users.
    assertThat(tweetsWithUsers).hasSize(3);
    assertThat(tweetsWithUsers.get(0)).isEqualTo(new TweetWithUser(Tweet.newTweet(1L, "artem_zin", "test tweet 1"), User.newUser(1L, "artem_zin")));
    assertThat(tweetsWithUsers.get(1)).isEqualTo(new TweetWithUser(Tweet.newTweet(2L, "artem_zin", "test tweet 2"), User.newUser(1L, "artem_zin")));
    assertThat(tweetsWithUsers.get(2)).isEqualTo(new TweetWithUser(Tweet.newTweet(3L, "nikitin-da", "test tweet 3"), User.newUser(2L, "nikitin-da")));
}
Also used : TweetWithUser(com.pushtorefresh.storio3.sample.db.entities.TweetWithUser) SampleApp(com.pushtorefresh.storio3.sample.SampleApp) Test(org.junit.Test)

Example 5 with Tweet

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

the class UserWithTweetsGetResolver method mapFromCursor.

@NonNull
@Override
public UserWithTweets mapFromCursor(@NonNull StorIOSQLite storIOSQLite, @NonNull Cursor cursor) {
    // Or you can manually parse cursor (it will be sliiightly faster)
    final User user = userGetResolver.mapFromCursor(storIOSQLite, 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.storio3.sample.db.entities.User) Tweet(com.pushtorefresh.storio3.sample.db.entities.Tweet) UserWithTweets(com.pushtorefresh.storio3.sample.db.entities.UserWithTweets) NonNull(android.support.annotation.NonNull)

Aggregations

Tweet (com.pushtorefresh.storio3.sample.db.entities.Tweet)5 NonNull (android.support.annotation.NonNull)2 TweetWithUser (com.pushtorefresh.storio3.sample.db.entities.TweetWithUser)2 User (com.pushtorefresh.storio3.sample.db.entities.User)2 Disposable (io.reactivex.disposables.Disposable)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 SuppressLint (android.annotation.SuppressLint)1 SampleApp (com.pushtorefresh.storio3.sample.SampleApp)1 UserWithTweets (com.pushtorefresh.storio3.sample.db.entities.UserWithTweets)1 Test (org.junit.Test)1