Skip to content

Instantly share code, notes, and snippets.

@jvanderwee
Created August 16, 2016 19:26
Show Gist options
  • Save jvanderwee/73bafe78382bfa8bdafb65514e64377e to your computer and use it in GitHub Desktop.
Save jvanderwee/73bafe78382bfa8bdafb65514e64377e to your computer and use it in GitHub Desktop.
Twitter Android SDK Home Timeline
package com.twitter.sdk.android.tweetui;
import com.twitter.sdk.android.core.Callback;
import com.twitter.sdk.android.core.TwitterCore;
import com.twitter.sdk.android.core.models.Tweet;
import java.util.List;
import retrofit2.Call;
public class HomeTimeline extends BaseTimeline implements Timeline<Tweet> {
private static final String SCRIBE_SECTION = "home";
private final Integer maxItemsPerRequest;
private final Boolean includeReplies;
private final Boolean includeEntities;
private HomeTimeline(TweetUi tweetUi, Integer maxItemsPerRequest, Boolean includeReplies, Boolean includeEntities) {
super(tweetUi);
this.maxItemsPerRequest = maxItemsPerRequest;
this.includeReplies = includeReplies != null && includeReplies;
this.includeEntities = includeEntities;
}
public void next(Long sinceId, Callback<TimelineResult<Tweet>> cb) {
createHomeTimelineRequest(sinceId, null).enqueue(new TweetsCallback(cb));
}
public void previous(Long maxId, Callback<TimelineResult<Tweet>> cb) {
createHomeTimelineRequest(null, decrementMaxId(maxId)).enqueue(new TweetsCallback(cb));
}
String getTimelineType() {
return SCRIBE_SECTION;
}
private Call<List<Tweet>> createHomeTimelineRequest(final Long sinceId, final Long maxId) {
return TwitterCore.getInstance().getApiClient().getStatusesService().homeTimeline(maxItemsPerRequest,
sinceId, maxId,
false, includeReplies,
null, includeEntities);
}
public static class Builder {
private final TweetUi tweetUi;
private Integer maxItemsPerRequest;
private Boolean includeReplies;
private Boolean includeEntities;
public Builder() {
this(TweetUi.getInstance());
}
public Builder(TweetUi tweetUi) {
this.maxItemsPerRequest = 30;
if (tweetUi == null) {
throw new IllegalArgumentException("TweetUi instance must not be null");
} else {
this.tweetUi = tweetUi;
}
}
public HomeTimeline.Builder maxItemsPerRequest(Integer maxItemsPerRequest) {
this.maxItemsPerRequest = maxItemsPerRequest;
return this;
}
public HomeTimeline.Builder includeReplies(Boolean includeReplies) {
this.includeReplies = includeReplies;
return this;
}
public HomeTimeline.Builder includeEntities(Boolean includeEntities) {
this.includeEntities = includeEntities;
return this;
}
public HomeTimeline build() {
return new HomeTimeline(this.tweetUi, this.maxItemsPerRequest, this.includeReplies, this.includeEntities);
}
}
}
@you55645
Copy link

hey, this looks very great! But maybe you can build a example app to show us how to hook this to the rest of the twtter api? thx.

@ManishaRana1195
Copy link

Hey, thanks for the code. I am trying to implement the same, but getting error that BaseTimeline can not be accessed outside the package, so any idea how to get around that.

@jvanderwee
Copy link
Author

This class needs to be in com.twitter.sdk.android.tweetui otherwise you cannot access BaseTimeline.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment