Search in sources :

Example 1 with Tweet

use of com.birbit.android.jobqueue.examples.twitter.entities.Tweet in project android-priority-jobqueue by yigit.

the class PostTweetJob method onAdded.

@Override
public void onAdded() {
    //job has been secured to disk, add item to database
    try {
        Tweet tweet = new Tweet(localId, null, text, TwitterController.getInstance().getUserId(), null, new Date(System.currentTimeMillis()));
        TweetModel.getInstance().insertOrReplace(tweet);
        EventBus.getDefault().post(new PostingTweetEvent(tweet));
    } catch (TwitterException exception) {
    //if we cannot get user id, we won't add it locally for now.
    }
}
Also used : Tweet(com.birbit.android.jobqueue.examples.twitter.entities.Tweet) TwitterException(twitter4j.TwitterException) Date(java.util.Date) PostingTweetEvent(com.birbit.android.jobqueue.examples.twitter.events.PostingTweetEvent)

Example 2 with Tweet

use of com.birbit.android.jobqueue.examples.twitter.entities.Tweet in project android-priority-jobqueue by yigit.

the class PostTweetJob method onRun.

@Override
public void onRun() throws Throwable {
    Status status = TwitterController.getInstance().postTweet(text);
    Tweet newTweet = new Tweet(status);
    TweetModel tweetModel = TweetModel.getInstance();
    Tweet existingTweet = tweetModel.getTweetByLocalId(localId);
    if (existingTweet != null) {
        existingTweet.updateNotNull(newTweet);
        //don't set local to false. this way, next time we ask for history update, we'll send proper tweet id
        tweetModel.insertOrReplace(existingTweet);
    } else {
        //somewhat local tweet does not exist. we might have crashed before onAdded is called.
        //just insert as if it is a new tweet
        tweetModel.insertOrReplace(newTweet);
    }
    EventBus.getDefault().post(new PostedTweetEvent(newTweet, localId));
}
Also used : Status(twitter4j.Status) Tweet(com.birbit.android.jobqueue.examples.twitter.entities.Tweet) PostedTweetEvent(com.birbit.android.jobqueue.examples.twitter.events.PostedTweetEvent) TweetModel(com.birbit.android.jobqueue.examples.twitter.models.TweetModel)

Example 3 with Tweet

use of com.birbit.android.jobqueue.examples.twitter.entities.Tweet in project android-priority-jobqueue by yigit.

the class FetchTweetsJob method onRun.

@Override
public void onRun() throws Throwable {
    TweetModel tweetModel = TweetModel.getInstance();
    Tweet lastTweet = tweetModel.getLastTweet();
    List<Status> statusList = TwitterController.getInstance().loadTweets(lastTweet == null ? null : lastTweet.getServerId());
    if (statusList.size() > 0) {
        List<Tweet> tweets = new ArrayList<Tweet>(statusList.size());
        for (Status status : statusList) {
            Tweet tweet = new Tweet(status);
            tweets.add(tweet);
        }
        tweetModel.insertOrReplaceAll(tweets);
        EventBus.getDefault().post(new FetchedNewTweetsEvent());
    }
}
Also used : Status(twitter4j.Status) FetchedNewTweetsEvent(com.birbit.android.jobqueue.examples.twitter.events.FetchedNewTweetsEvent) Tweet(com.birbit.android.jobqueue.examples.twitter.entities.Tweet) ArrayList(java.util.ArrayList) TweetModel(com.birbit.android.jobqueue.examples.twitter.models.TweetModel)

Example 4 with Tweet

use of com.birbit.android.jobqueue.examples.twitter.entities.Tweet in project android-priority-jobqueue by yigit.

the class PostTweetJob method onCancel.

@Override
protected void onCancel(@CancelReason int cancelReason, @Nullable Throwable throwable) {
    //delete local tweet
    Tweet localTweet = TweetModel.getInstance().getTweetByLocalId(localId);
    if (localTweet != null) {
        TweetModel.getInstance().deleteTweetById(localId);
        EventBus.getDefault().post(new DeletedTweetEvent(localId));
    }
}
Also used : DeletedTweetEvent(com.birbit.android.jobqueue.examples.twitter.events.DeletedTweetEvent) Tweet(com.birbit.android.jobqueue.examples.twitter.entities.Tweet)

Aggregations

Tweet (com.birbit.android.jobqueue.examples.twitter.entities.Tweet)4 TweetModel (com.birbit.android.jobqueue.examples.twitter.models.TweetModel)2 Status (twitter4j.Status)2 DeletedTweetEvent (com.birbit.android.jobqueue.examples.twitter.events.DeletedTweetEvent)1 FetchedNewTweetsEvent (com.birbit.android.jobqueue.examples.twitter.events.FetchedNewTweetsEvent)1 PostedTweetEvent (com.birbit.android.jobqueue.examples.twitter.events.PostedTweetEvent)1 PostingTweetEvent (com.birbit.android.jobqueue.examples.twitter.events.PostingTweetEvent)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 TwitterException (twitter4j.TwitterException)1