mirror of
https://github.com/nuclearfog/Shitter.git
synced 2025-02-06 22:13:21 +01:00
added function to load old tweets
This commit is contained in:
parent
0666f7d7ea
commit
24e4c663ed
@ -5,6 +5,7 @@ import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.View.OnClickListener;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.Button;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.TextView;
|
||||
|
||||
@ -29,9 +30,13 @@ import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
import static androidx.recyclerview.widget.RecyclerView.NO_ID;
|
||||
import static androidx.recyclerview.widget.RecyclerView.NO_POSITION;
|
||||
|
||||
public class TweetAdapter extends Adapter<TweetAdapter.ItemHolder> {
|
||||
public class TweetAdapter extends Adapter<ViewHolder> {
|
||||
|
||||
private static final int VIEW_TWEET = 0;
|
||||
private static final int VIEW_GAP = 1;
|
||||
|
||||
private WeakReference<TweetClickListener> itemClickListener;
|
||||
private NumberFormat formatter;
|
||||
@ -46,17 +51,26 @@ public class TweetAdapter extends Adapter<TweetAdapter.ItemHolder> {
|
||||
}
|
||||
|
||||
@MainThread
|
||||
public void add(@NonNull List<Tweet> newTweets) {
|
||||
tweets.clear();
|
||||
tweets.addAll(newTweets);
|
||||
notifyDataSetChanged();
|
||||
}
|
||||
|
||||
@MainThread
|
||||
public void addFirst(@NonNull List<Tweet> newTweets) {
|
||||
if (!newTweets.isEmpty()) {
|
||||
tweets.addAll(0, newTweets);
|
||||
notifyItemRangeInserted(0, newTweets.size());
|
||||
public void insert(@NonNull List<Tweet> data, int index) {
|
||||
int maxSize = settings.getRowLimit();
|
||||
int dataSize = data.size();
|
||||
if (!tweets.isEmpty() && index < tweets.size()) {
|
||||
if (dataSize == maxSize) {
|
||||
if (tweets.get(index) != null) {
|
||||
tweets.add(index, null);
|
||||
dataSize++;
|
||||
}
|
||||
} else if (tweets.get(index) == null) {
|
||||
tweets.remove(index);
|
||||
dataSize--;
|
||||
}
|
||||
tweets.addAll(index, data);
|
||||
notifyItemRangeInserted(index, dataSize);
|
||||
} else {
|
||||
tweets.addAll(data);
|
||||
if (dataSize == maxSize)
|
||||
tweets.add(null);
|
||||
notifyDataSetChanged();
|
||||
}
|
||||
}
|
||||
|
||||
@ -70,13 +84,15 @@ public class TweetAdapter extends Adapter<TweetAdapter.ItemHolder> {
|
||||
public void remove(long id) {
|
||||
int index = -1;
|
||||
for (int pos = 0; pos < tweets.size() && index < 0; pos++) {
|
||||
if (tweets.get(pos).getId() == id) {
|
||||
Tweet tweet = tweets.get(pos);
|
||||
if (tweet != null && tweet.getId() == id) {
|
||||
tweets.remove(pos);
|
||||
index = pos;
|
||||
}
|
||||
}
|
||||
if (index != -1)
|
||||
if (index != -1) {
|
||||
notifyItemRemoved(index);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isEmpty() {
|
||||
@ -85,7 +101,10 @@ public class TweetAdapter extends Adapter<TweetAdapter.ItemHolder> {
|
||||
|
||||
@Override
|
||||
public long getItemId(int index) {
|
||||
return tweets.get(index).getId();
|
||||
Tweet tweet = tweets.get(index);
|
||||
if (tweet != null)
|
||||
return tweet.getId();
|
||||
return NO_ID;
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -93,70 +112,108 @@ public class TweetAdapter extends Adapter<TweetAdapter.ItemHolder> {
|
||||
return tweets.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getItemViewType(int index) {
|
||||
if (tweets.get(index) == null)
|
||||
return VIEW_GAP;
|
||||
return VIEW_TWEET;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public ItemHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
|
||||
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_tweet, parent, false);
|
||||
final ItemHolder vh = new ItemHolder(v);
|
||||
FontTool.setViewFontAndColor(settings, v);
|
||||
|
||||
v.setOnClickListener(new OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
if (itemClickListener.get() != null) {
|
||||
int position = vh.getLayoutPosition();
|
||||
if (position != NO_POSITION)
|
||||
itemClickListener.get().onTweetClick(tweets.get(position));
|
||||
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
|
||||
if (viewType == VIEW_TWEET) {
|
||||
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_tweet, parent, false);
|
||||
FontTool.setViewFontAndColor(settings, v);
|
||||
final TweetHolder vh = new TweetHolder(v);
|
||||
v.setOnClickListener(new OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
if (itemClickListener.get() != null) {
|
||||
int position = vh.getLayoutPosition();
|
||||
if (position != NO_POSITION && tweets.get(position) != null) {
|
||||
itemClickListener.get().onTweetClick(tweets.get(position));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
return vh;
|
||||
});
|
||||
return vh;
|
||||
} else {
|
||||
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_placeholder, parent, false);
|
||||
final PlaceHolder vh = new PlaceHolder(v);
|
||||
vh.loadBtn.setTypeface(settings.getFontFace());
|
||||
vh.loadBtn.setTextColor(settings.getFontColor());
|
||||
vh.loadBtn.setOnClickListener(new OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
if (itemClickListener.get() != null) {
|
||||
int position = vh.getLayoutPosition();
|
||||
if (position != NO_POSITION) {
|
||||
long sinceId = 0;
|
||||
long maxId = 0;
|
||||
if (position == 0)
|
||||
sinceId = tweets.get(position + 1).getId();
|
||||
else if (position == tweets.size() - 1)
|
||||
maxId = tweets.get(position - 1).getId();
|
||||
else {
|
||||
sinceId = tweets.get(position + 1).getId();
|
||||
maxId = tweets.get(position - 1).getId();
|
||||
}
|
||||
itemClickListener.get().onHolderClick(sinceId, maxId, position);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
return vh;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBindViewHolder(@NonNull ItemHolder vh, int index) {
|
||||
public void onBindViewHolder(@NonNull ViewHolder holder, int index) {
|
||||
Tweet tweet = tweets.get(index);
|
||||
TwitterUser user = tweet.getUser();
|
||||
|
||||
if (tweet.getEmbeddedTweet() != null) {
|
||||
String retweeter = "RT " + user.getScreenname();
|
||||
vh.retweeter.setText(retweeter);
|
||||
tweet = tweet.getEmbeddedTweet();
|
||||
user = tweet.getUser();
|
||||
} else {
|
||||
vh.retweeter.setText("");
|
||||
if (holder instanceof TweetHolder && tweet != null) {
|
||||
TweetHolder vh = (TweetHolder) holder;
|
||||
TwitterUser user = tweet.getUser();
|
||||
if (tweet.getEmbeddedTweet() != null) {
|
||||
String retweeter = "RT " + user.getScreenname();
|
||||
vh.retweeter.setText(retweeter);
|
||||
tweet = tweet.getEmbeddedTweet();
|
||||
user = tweet.getUser();
|
||||
} else {
|
||||
vh.retweeter.setText("");
|
||||
}
|
||||
Spanned text = Tagger.makeTextWithLinks(tweet.getTweet(), settings.getHighlightColor());
|
||||
vh.tweet.setText(text);
|
||||
vh.username.setText(user.getUsername());
|
||||
vh.screenname.setText(user.getScreenname());
|
||||
vh.retweet.setText(formatter.format(tweet.getRetweetCount()));
|
||||
vh.favorite.setText(formatter.format(tweet.getFavorCount()));
|
||||
vh.time.setText(getTimeString(tweet.getTime()));
|
||||
if (tweet.retweeted())
|
||||
vh.retweet.setCompoundDrawablesWithIntrinsicBounds(R.drawable.retweet_enabled, 0, 0, 0);
|
||||
else
|
||||
vh.retweet.setCompoundDrawablesWithIntrinsicBounds(R.drawable.retweet, 0, 0, 0);
|
||||
if (tweet.favored())
|
||||
vh.favorite.setCompoundDrawablesWithIntrinsicBounds(R.drawable.favorite_enabled, 0, 0, 0);
|
||||
else
|
||||
vh.favorite.setCompoundDrawablesWithIntrinsicBounds(R.drawable.favorite, 0, 0, 0);
|
||||
if (user.isVerified())
|
||||
vh.username.setCompoundDrawablesWithIntrinsicBounds(R.drawable.verify, 0, 0, 0);
|
||||
else
|
||||
vh.username.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
|
||||
if (user.isLocked())
|
||||
vh.screenname.setCompoundDrawablesWithIntrinsicBounds(R.drawable.lock, 0, 0, 0);
|
||||
else
|
||||
vh.screenname.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
|
||||
if (settings.getImageLoad()) {
|
||||
String pbLink = user.getImageLink();
|
||||
if (!user.hasDefaultProfileImage())
|
||||
pbLink += "_mini";
|
||||
Picasso.get().load(pbLink).into(vh.profile);
|
||||
} else {
|
||||
vh.profile.setImageResource(0);
|
||||
}
|
||||
}
|
||||
Spanned text = Tagger.makeTextWithLinks(tweet.getTweet(), settings.getHighlightColor());
|
||||
vh.tweet.setText(text);
|
||||
vh.username.setText(user.getUsername());
|
||||
vh.screenname.setText(user.getScreenname());
|
||||
vh.retweet.setText(formatter.format(tweet.getRetweetCount()));
|
||||
vh.favorite.setText(formatter.format(tweet.getFavorCount()));
|
||||
vh.time.setText(getTimeString(tweet.getTime()));
|
||||
if (tweet.retweeted())
|
||||
vh.retweet.setCompoundDrawablesWithIntrinsicBounds(R.drawable.retweet_enabled, 0, 0, 0);
|
||||
else
|
||||
vh.retweet.setCompoundDrawablesWithIntrinsicBounds(R.drawable.retweet, 0, 0, 0);
|
||||
if (tweet.favored())
|
||||
vh.favorite.setCompoundDrawablesWithIntrinsicBounds(R.drawable.favorite_enabled, 0, 0, 0);
|
||||
else
|
||||
vh.favorite.setCompoundDrawablesWithIntrinsicBounds(R.drawable.favorite, 0, 0, 0);
|
||||
if (user.isVerified())
|
||||
vh.username.setCompoundDrawablesWithIntrinsicBounds(R.drawable.verify, 0, 0, 0);
|
||||
else
|
||||
vh.username.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
|
||||
if (user.isLocked())
|
||||
vh.screenname.setCompoundDrawablesWithIntrinsicBounds(R.drawable.lock, 0, 0, 0);
|
||||
else
|
||||
vh.screenname.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
|
||||
if (settings.getImageLoad()) {
|
||||
String pbLink = user.getImageLink();
|
||||
if (!user.hasDefaultProfileImage())
|
||||
pbLink += "_mini";
|
||||
Picasso.get().load(pbLink).into(vh.profile);
|
||||
}
|
||||
else
|
||||
vh.profile.setImageResource(0);
|
||||
}
|
||||
|
||||
private String getTimeString(long time) {
|
||||
@ -181,12 +238,12 @@ public class TweetAdapter extends Adapter<TweetAdapter.ItemHolder> {
|
||||
return seconds + " s";
|
||||
}
|
||||
|
||||
static class ItemHolder extends ViewHolder {
|
||||
class TweetHolder extends ViewHolder {
|
||||
final TextView username, screenname, tweet, retweet;
|
||||
final TextView favorite, retweeter, time;
|
||||
final ImageView profile;
|
||||
|
||||
ItemHolder(View v) {
|
||||
TweetHolder(@NonNull View v) {
|
||||
super(v);
|
||||
username = v.findViewById(R.id.username);
|
||||
screenname = v.findViewById(R.id.screenname);
|
||||
@ -199,6 +256,16 @@ public class TweetAdapter extends Adapter<TweetAdapter.ItemHolder> {
|
||||
}
|
||||
}
|
||||
|
||||
class PlaceHolder extends ViewHolder {
|
||||
|
||||
final Button loadBtn;
|
||||
|
||||
PlaceHolder(@NonNull View v) {
|
||||
super(v);
|
||||
loadBtn = v.findViewById(R.id.item_placeholder);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Listener for tweet click
|
||||
*/
|
||||
@ -210,5 +277,7 @@ public class TweetAdapter extends Adapter<TweetAdapter.ItemHolder> {
|
||||
* @param tweet clicked tweet
|
||||
*/
|
||||
void onTweetClick(Tweet tweet);
|
||||
|
||||
void onHolderClick(long sinceId, long maxId, int pos);
|
||||
}
|
||||
}
|
@ -13,7 +13,7 @@ import org.nuclearfog.twidda.fragment.TweetFragment;
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.util.List;
|
||||
|
||||
import static org.nuclearfog.twidda.fragment.TweetFragment.LIST_EMPTY;
|
||||
import static org.nuclearfog.twidda.fragment.TweetFragment.CLEAR_LIST;
|
||||
|
||||
|
||||
/**
|
||||
@ -21,7 +21,7 @@ import static org.nuclearfog.twidda.fragment.TweetFragment.LIST_EMPTY;
|
||||
*
|
||||
* @see TweetFragment
|
||||
*/
|
||||
public class TweetListLoader extends AsyncTask<Object, Void, List<Tweet>> {
|
||||
public class TweetListLoader extends AsyncTask<Long, Void, List<Tweet>> {
|
||||
|
||||
public enum Action {
|
||||
TL_HOME,
|
||||
@ -31,7 +31,8 @@ public class TweetListLoader extends AsyncTask<Object, Void, List<Tweet>> {
|
||||
TWEET_ANS,
|
||||
DB_ANS,
|
||||
TWEET_SEARCH,
|
||||
LIST
|
||||
LIST,
|
||||
NONE
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@ -39,16 +40,21 @@ public class TweetListLoader extends AsyncTask<Object, Void, List<Tweet>> {
|
||||
private WeakReference<TweetFragment> callback;
|
||||
private TwitterEngine mTwitter;
|
||||
private AppDatabase db;
|
||||
private final Action action;
|
||||
private long sinceId;
|
||||
|
||||
private Action action;
|
||||
private String search;
|
||||
private long id;
|
||||
private int pos;
|
||||
|
||||
|
||||
public TweetListLoader(TweetFragment callback, Action action) {
|
||||
public TweetListLoader(TweetFragment callback, Action action, long id, String search, int pos) {
|
||||
this.callback = new WeakReference<>(callback);
|
||||
db = new AppDatabase(callback.getContext());
|
||||
mTwitter = TwitterEngine.getInstance(callback.getContext());
|
||||
sinceId = callback.getTopId();
|
||||
this.action = action;
|
||||
this.search = search;
|
||||
this.id = id;
|
||||
this.pos = pos;
|
||||
}
|
||||
|
||||
|
||||
@ -61,113 +67,108 @@ public class TweetListLoader extends AsyncTask<Object, Void, List<Tweet>> {
|
||||
|
||||
|
||||
@Override
|
||||
protected List<Tweet> doInBackground(Object[] param) {
|
||||
protected List<Tweet> doInBackground(Long[] param) {
|
||||
List<Tweet> tweets = null;
|
||||
String search;
|
||||
int page;
|
||||
long id;
|
||||
|
||||
long sinceId = param[0];
|
||||
long maxId = param[1];
|
||||
try {
|
||||
switch (action) {
|
||||
case TL_HOME:
|
||||
page = (int) param[0];
|
||||
if (sinceId == LIST_EMPTY) {
|
||||
if (sinceId == 0 && maxId == 0) { // add tweets to the list
|
||||
tweets = db.getHomeTimeline();
|
||||
if (tweets.isEmpty()) {
|
||||
tweets = mTwitter.getHome(page, sinceId);
|
||||
tweets = mTwitter.getHome(sinceId, maxId);
|
||||
db.storeHomeTimeline(tweets);
|
||||
}
|
||||
} else {
|
||||
tweets = mTwitter.getHome(page, sinceId);
|
||||
} else if (sinceId > 0) { // add new tweets to the top
|
||||
tweets = mTwitter.getHome(sinceId, maxId);
|
||||
db.storeHomeTimeline(tweets);
|
||||
} else if (maxId > 0) { // add old tweets to the bottom
|
||||
tweets = mTwitter.getHome(sinceId, maxId);
|
||||
}
|
||||
break;
|
||||
|
||||
case TL_MENT:
|
||||
page = (int) param[0];
|
||||
if (sinceId == LIST_EMPTY) {
|
||||
if (sinceId == 0 && maxId == 0) {
|
||||
tweets = db.getMentions();
|
||||
if (tweets.isEmpty()) {
|
||||
tweets = mTwitter.getMention(page, sinceId);
|
||||
tweets = mTwitter.getMention(sinceId, maxId);
|
||||
db.storeMentions(tweets);
|
||||
}
|
||||
} else {
|
||||
tweets = mTwitter.getMention(page, sinceId);
|
||||
} else if (sinceId > 0) {
|
||||
tweets = mTwitter.getMention(sinceId, maxId);
|
||||
db.storeMentions(tweets);
|
||||
} else if (maxId > 0) {
|
||||
tweets = mTwitter.getMention(sinceId, maxId);
|
||||
}
|
||||
break;
|
||||
|
||||
case USR_TWEETS:
|
||||
page = (int) param[1];
|
||||
if (param[0] instanceof Long) { // search by user ID
|
||||
id = (long) param[0];
|
||||
if (sinceId == LIST_EMPTY) {
|
||||
if (search != null) {
|
||||
tweets = mTwitter.getUserTweets(search, sinceId, maxId);
|
||||
} else {
|
||||
if (sinceId == 0 && maxId == 0) {
|
||||
tweets = db.getUserTweets(id);
|
||||
if (tweets.isEmpty()) {
|
||||
tweets = mTwitter.getUserTweets(id, sinceId, page);
|
||||
tweets = mTwitter.getUserTweets(id, 0, maxId);
|
||||
db.storeUserTweets(tweets);
|
||||
}
|
||||
} else {
|
||||
tweets = mTwitter.getUserTweets(id, sinceId, page);
|
||||
} else if (sinceId > 0) {
|
||||
tweets = mTwitter.getUserTweets(id, sinceId, maxId);
|
||||
db.storeUserTweets(tweets);
|
||||
} else if (maxId > 0) {
|
||||
tweets = mTwitter.getUserTweets(id, sinceId, maxId);
|
||||
}
|
||||
} else if (param[0] instanceof String) { // search by username
|
||||
search = (String) param[0];
|
||||
tweets = mTwitter.getUserTweets(search, sinceId, page);
|
||||
}
|
||||
break;
|
||||
|
||||
case USR_FAVORS:
|
||||
page = (int) param[1];
|
||||
if (param[0] instanceof Long) { // search by user ID
|
||||
id = (long) param[0];
|
||||
if (sinceId == LIST_EMPTY) {
|
||||
if (search != null) {
|
||||
tweets = mTwitter.getUserFavs(search, sinceId, maxId);
|
||||
} else {
|
||||
if (sinceId == 0 && maxId == 0) {
|
||||
tweets = db.getUserFavs(id);
|
||||
if (tweets.isEmpty()) {
|
||||
tweets = mTwitter.getUserFavs(id, page);
|
||||
tweets = mTwitter.getUserFavs(id, 0, maxId);
|
||||
db.storeUserFavs(tweets, id);
|
||||
}
|
||||
} else {
|
||||
tweets = mTwitter.getUserFavs(id, page);
|
||||
} else if (sinceId > 0) {
|
||||
tweets = mTwitter.getUserFavs(id, 0, maxId);
|
||||
db.storeUserFavs(tweets, id);
|
||||
pos = CLEAR_LIST; // set flag to clear previous data
|
||||
} else if (maxId > 0) {
|
||||
tweets = mTwitter.getUserFavs(id, sinceId, maxId);
|
||||
}
|
||||
} else if (param[0] instanceof String) { // search by username
|
||||
search = (String) param[0];
|
||||
tweets = mTwitter.getUserFavs(search, page);
|
||||
}
|
||||
break;
|
||||
|
||||
case DB_ANS:
|
||||
id = (long) param[0];
|
||||
tweets = db.getAnswers(id);
|
||||
break;
|
||||
|
||||
case TWEET_ANS:
|
||||
id = (long) param[0];
|
||||
search = (String) param[1];
|
||||
if (sinceId == LIST_EMPTY) {
|
||||
if (sinceId == 0 && maxId == 0) {
|
||||
tweets = db.getAnswers(id);
|
||||
if (tweets.isEmpty()) {
|
||||
tweets = mTwitter.getAnswers(search, id, sinceId);
|
||||
tweets = mTwitter.getAnswers(search, id, sinceId, maxId);
|
||||
if (!tweets.isEmpty() && db.containStatus(id))
|
||||
db.storeReplies(tweets);
|
||||
}
|
||||
} else {
|
||||
tweets = mTwitter.getAnswers(search, id, sinceId);
|
||||
} else if (sinceId > 0) {
|
||||
tweets = mTwitter.getAnswers(search, id, sinceId, maxId);
|
||||
if (!tweets.isEmpty() && db.containStatus(id))
|
||||
db.storeReplies(tweets);
|
||||
} else if (maxId > 0) {
|
||||
tweets = mTwitter.getAnswers(search, id, sinceId, maxId);
|
||||
}
|
||||
break;
|
||||
|
||||
case TWEET_SEARCH:
|
||||
search = (String) param[0];
|
||||
tweets = mTwitter.searchTweets(search, sinceId);
|
||||
tweets = mTwitter.searchTweets(search, sinceId, maxId);
|
||||
break;
|
||||
|
||||
case LIST:
|
||||
id = (long) param[0];
|
||||
page = (int) param[1];
|
||||
tweets = mTwitter.getListTweets(id, sinceId, page);
|
||||
tweets = mTwitter.getListTweets(id, sinceId, maxId);
|
||||
break;
|
||||
}
|
||||
} catch (EngineException twException) {
|
||||
@ -184,10 +185,7 @@ public class TweetListLoader extends AsyncTask<Object, Void, List<Tweet>> {
|
||||
if (callback.get() != null) {
|
||||
callback.get().setRefresh(false);
|
||||
if (tweets != null) {
|
||||
if (action == Action.USR_FAVORS)
|
||||
callback.get().add(tweets);
|
||||
else
|
||||
callback.get().addTop(tweets);
|
||||
callback.get().setData(tweets, pos);
|
||||
}
|
||||
if (twException != null) {
|
||||
callback.get().onError(twException);
|
||||
|
@ -193,15 +193,20 @@ public class TwitterEngine {
|
||||
/**
|
||||
* Get Home Timeline
|
||||
*
|
||||
* @param page current page
|
||||
* @param lastId Tweet ID of the earliest Tweet
|
||||
* @param sinceId id of the earliest tweet
|
||||
* @param maxId ID of the oldest tweet
|
||||
* @return List of Tweets
|
||||
* @throws EngineException if access is unavailable
|
||||
*/
|
||||
public List<Tweet> getHome(int page, long lastId) throws EngineException {
|
||||
public List<Tweet> getHome(long sinceId, long maxId) throws EngineException {
|
||||
try {
|
||||
int load = settings.getRowLimit();
|
||||
List<Status> homeTweets = twitter.getHomeTimeline(new Paging(page, load, lastId));
|
||||
Paging paging = new Paging();
|
||||
paging.setCount(settings.getRowLimit());
|
||||
if (sinceId > 0)
|
||||
paging.setSinceId(sinceId);
|
||||
if (maxId > 0)
|
||||
paging.setMaxId(maxId);
|
||||
List<Status> homeTweets = twitter.getHomeTimeline(paging);
|
||||
return convertStatusList(homeTweets);
|
||||
} catch (TwitterException err) {
|
||||
throw new EngineException(err);
|
||||
@ -212,15 +217,20 @@ public class TwitterEngine {
|
||||
/**
|
||||
* Get Mention Tweets
|
||||
*
|
||||
* @param page current page
|
||||
* @param id ID of the earliest Tweet
|
||||
* @param sinceId id of the earliest tweet
|
||||
* @param maxId ID of the oldest tweet
|
||||
* @return List of Mention Tweets
|
||||
* @throws EngineException if access is unavailable
|
||||
*/
|
||||
public List<Tweet> getMention(int page, long id) throws EngineException {
|
||||
public List<Tweet> getMention(long sinceId, long maxId) throws EngineException {
|
||||
try {
|
||||
int load = settings.getRowLimit();
|
||||
List<Status> mentions = twitter.getMentionsTimeline(new Paging(page, load, id));
|
||||
Paging paging = new Paging();
|
||||
paging.setCount(settings.getRowLimit());
|
||||
if (sinceId > 0)
|
||||
paging.setSinceId(sinceId);
|
||||
if (maxId > 0)
|
||||
paging.setMaxId(maxId);
|
||||
List<Status> mentions = twitter.getMentionsTimeline(paging);
|
||||
return convertStatusList(mentions);
|
||||
} catch (TwitterException err) {
|
||||
throw new EngineException(err);
|
||||
@ -231,18 +241,20 @@ public class TwitterEngine {
|
||||
/**
|
||||
* Get Tweet search result
|
||||
*
|
||||
* @param search Search String
|
||||
* @param id Since ID
|
||||
* @param search Search String
|
||||
* @param sinceId id of the earliest tweet
|
||||
* @param maxId ID of the oldest tweet
|
||||
* @return List of Tweets
|
||||
* @throws EngineException if acces is unavailable
|
||||
* @throws EngineException if access is unavailable
|
||||
*/
|
||||
public List<Tweet> searchTweets(String search, long id) throws EngineException {
|
||||
public List<Tweet> searchTweets(String search, long sinceId, long maxId) throws EngineException {
|
||||
try {
|
||||
int load = settings.getRowLimit();
|
||||
Query q = new Query();
|
||||
q.setQuery(search + " +exclude:retweets");
|
||||
q.setCount(load);
|
||||
q.setSinceId(id);
|
||||
q.setSinceId(sinceId);
|
||||
q.setMaxId(maxId);
|
||||
QueryResult result = twitter.search(q);
|
||||
List<Status> results = result.getTweets();
|
||||
return convertStatusList(results);
|
||||
@ -312,15 +324,19 @@ public class TwitterEngine {
|
||||
* Get User Tweets
|
||||
*
|
||||
* @param userId User ID
|
||||
* @param sinceId minimum tweet ID
|
||||
* @param page current page
|
||||
* @param sinceId id of the earliest tweet
|
||||
* @param maxId ID of the oldest tweet
|
||||
* @return List of User Tweets
|
||||
* @throws EngineException if access is unavailable
|
||||
*/
|
||||
public List<Tweet> getUserTweets(long userId, long sinceId, int page) throws EngineException {
|
||||
public List<Tweet> getUserTweets(long userId, long sinceId, long maxId) throws EngineException {
|
||||
try {
|
||||
int load = settings.getRowLimit();
|
||||
Paging paging = new Paging(page, load, sinceId);
|
||||
Paging paging = new Paging();
|
||||
paging.setCount(settings.getRowLimit());
|
||||
if (sinceId > 0)
|
||||
paging.setSinceId(sinceId);
|
||||
if (maxId > 0)
|
||||
paging.setMaxId(maxId);
|
||||
return convertStatusList(twitter.getUserTimeline(userId, paging));
|
||||
} catch (TwitterException err) {
|
||||
throw new EngineException(err);
|
||||
@ -332,15 +348,19 @@ public class TwitterEngine {
|
||||
* Get User Tweets
|
||||
*
|
||||
* @param username screen name of the user
|
||||
* @param sinceId minimum tweet ID
|
||||
* @param page current page
|
||||
* @param sinceId id of the earliest tweet
|
||||
* @param maxId ID of the oldest tweet
|
||||
* @return List of User Tweets
|
||||
* @throws EngineException if access is unavailable
|
||||
*/
|
||||
public List<Tweet> getUserTweets(String username, long sinceId, int page) throws EngineException {
|
||||
public List<Tweet> getUserTweets(String username, long sinceId, long maxId) throws EngineException {
|
||||
try {
|
||||
int load = settings.getRowLimit();
|
||||
Paging paging = new Paging(page, load, sinceId);
|
||||
Paging paging = new Paging();
|
||||
paging.setCount(settings.getRowLimit());
|
||||
if (sinceId > 0)
|
||||
paging.setSinceId(sinceId);
|
||||
if (maxId > 0)
|
||||
paging.setMaxId(maxId);
|
||||
return convertStatusList(twitter.getUserTimeline(username, paging));
|
||||
} catch (TwitterException err) {
|
||||
throw new EngineException(err);
|
||||
@ -352,16 +372,20 @@ public class TwitterEngine {
|
||||
* Get User Favs
|
||||
*
|
||||
* @param userId User ID
|
||||
* @param page current page
|
||||
* @param sinceId id of the earliest tweet
|
||||
* @param maxId ID of the oldest tweet
|
||||
* @return List of User Favs
|
||||
* @throws EngineException if access is unavailable
|
||||
*/
|
||||
public List<Tweet> getUserFavs(long userId, int page) throws EngineException {
|
||||
public List<Tweet> getUserFavs(long userId, long sinceId, long maxId) throws EngineException {
|
||||
try {
|
||||
int load = settings.getRowLimit();
|
||||
Paging paging = new Paging(page, load);
|
||||
List<Status> favorits = twitter.getFavorites(userId, paging);
|
||||
return convertStatusList(favorits);
|
||||
Paging paging = new Paging();
|
||||
paging.setCount(settings.getRowLimit());
|
||||
if (sinceId > 0)
|
||||
paging.setSinceId(sinceId);
|
||||
if (maxId > 0)
|
||||
paging.setMaxId(maxId);
|
||||
return convertStatusList(twitter.getFavorites(userId, paging));
|
||||
} catch (TwitterException err) {
|
||||
throw new EngineException(err);
|
||||
}
|
||||
@ -372,14 +396,19 @@ public class TwitterEngine {
|
||||
* Get User Favs
|
||||
*
|
||||
* @param username screen name of the user
|
||||
* @param page current page
|
||||
* @param sinceId id of the earliest tweet
|
||||
* @param maxId ID of the oldest tweet
|
||||
* @return List of User Favs
|
||||
* @throws EngineException if access is unavailable
|
||||
*/
|
||||
public List<Tweet> getUserFavs(String username, int page) throws EngineException {
|
||||
public List<Tweet> getUserFavs(String username, long sinceId, long maxId) throws EngineException {
|
||||
try {
|
||||
int load = settings.getRowLimit();
|
||||
Paging paging = new Paging(page, load);
|
||||
Paging paging = new Paging();
|
||||
paging.setCount(settings.getRowLimit());
|
||||
if (sinceId > 0)
|
||||
paging.setSinceId(sinceId);
|
||||
if (maxId > 0)
|
||||
paging.setMaxId(maxId);
|
||||
List<Status> favorits = twitter.getFavorites(username, paging);
|
||||
return convertStatusList(favorits);
|
||||
} catch (TwitterException err) {
|
||||
@ -654,16 +683,20 @@ public class TwitterEngine {
|
||||
*
|
||||
* @param name screen name of receiver
|
||||
* @param tweetId tweet ID
|
||||
* @param sinceId last tweet
|
||||
* @param sinceId ID of the last tweet reply
|
||||
* @param maxId ID of the earliest tweet reply
|
||||
* @return List of Answers
|
||||
* @throws EngineException if Access is unavailable
|
||||
*/
|
||||
public List<Tweet> getAnswers(String name, long tweetId, long sinceId) throws EngineException {
|
||||
public List<Tweet> getAnswers(String name, long tweetId, long sinceId, long maxId) throws EngineException {
|
||||
try {
|
||||
int load = settings.getRowLimit();
|
||||
List<Status> answers = new LinkedList<>();
|
||||
Query query = new Query("to:" + name + " since_id:" + sinceId + " +exclude:retweets");
|
||||
Query query = new Query("to:" + name + " +exclude:retweets");
|
||||
query.setCount(load);
|
||||
query.setMaxId(maxId);
|
||||
query.setSinceId(sinceId);
|
||||
query.setResultType(Query.RECENT);
|
||||
QueryResult result = twitter.search(query);
|
||||
List<Status> stats = result.getTweets();
|
||||
for (Status reply : stats) {
|
||||
@ -971,15 +1004,19 @@ public class TwitterEngine {
|
||||
* get tweets of a lists
|
||||
*
|
||||
* @param listId ID of the list
|
||||
* @param sinceId Id of the recent tweet
|
||||
* @param page tweet page
|
||||
* @param sinceId id of the earliest tweet
|
||||
* @param maxId ID of the oldest tweet
|
||||
* @return list of tweets
|
||||
* @throws EngineException if access is unavailable
|
||||
*/
|
||||
public List<Tweet> getListTweets(long listId, long sinceId, int page) throws EngineException {
|
||||
public List<Tweet> getListTweets(long listId, long sinceId, long maxId) throws EngineException {
|
||||
try {
|
||||
int load = settings.getRowLimit();
|
||||
Paging paging = new Paging(page, load, sinceId);
|
||||
Paging paging = new Paging();
|
||||
paging.setCount(settings.getRowLimit());
|
||||
if (sinceId > 0)
|
||||
paging.setSinceId(sinceId);
|
||||
if (maxId > 0)
|
||||
paging.setMaxId(maxId);
|
||||
return convertStatusList(twitter.getUserListStatuses(listId, paging));
|
||||
} catch (TwitterException err) {
|
||||
throw new EngineException(err);
|
||||
|
@ -48,10 +48,10 @@ public class TweetFragment extends Fragment implements OnRefreshListener, TweetC
|
||||
public static final int TWEET_FRAG_ANSWER = 5;
|
||||
public static final int TWEET_FRAG_SEARCH = 6;
|
||||
public static final int TWEET_FRAG_LIST = 7;
|
||||
public static final int LIST_EMPTY = 1;
|
||||
|
||||
private static final int REQUEST_TWEET_CHANGED = 3;
|
||||
public static final int RETURN_TWEET_CHANGED = 4;
|
||||
public static final int CLEAR_LIST = -1;
|
||||
public static final int RETURN_TWEET_CHANGED = 1;
|
||||
private static final int REQUEST_TWEET_CHANGED = 2;
|
||||
|
||||
private TweetListLoader tweetTask;
|
||||
private GlobalSettings settings;
|
||||
@ -82,7 +82,7 @@ public class TweetFragment extends Fragment implements OnRefreshListener, TweetC
|
||||
public void onStart() {
|
||||
super.onStart();
|
||||
if (tweetTask == null) {
|
||||
load();
|
||||
load(0, 0, CLEAR_LIST);
|
||||
}
|
||||
}
|
||||
|
||||
@ -108,7 +108,10 @@ public class TweetFragment extends Fragment implements OnRefreshListener, TweetC
|
||||
@Override
|
||||
public void onRefresh() {
|
||||
if (tweetTask != null && tweetTask.getStatus() != RUNNING) {
|
||||
load();
|
||||
long sinceId = 0;
|
||||
if (!adapter.isEmpty())
|
||||
sinceId = adapter.getItemId(0);
|
||||
load(sinceId, 0, 0);
|
||||
}
|
||||
}
|
||||
|
||||
@ -126,13 +129,19 @@ public class TweetFragment extends Fragment implements OnRefreshListener, TweetC
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void onHolderClick(long sinceId, long maxId, int pos) {
|
||||
load(sinceId, maxId, pos);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void onReset() {
|
||||
if (reload != null && list != null && adapter != null) {
|
||||
reload.setProgressBackgroundColorSchemeColor(settings.getHighlightColor());
|
||||
list.setAdapter(adapter); // force redrawing list
|
||||
adapter.clear();
|
||||
load();
|
||||
load(0, 0, 0);
|
||||
}
|
||||
}
|
||||
|
||||
@ -145,31 +154,15 @@ public class TweetFragment extends Fragment implements OnRefreshListener, TweetC
|
||||
}
|
||||
|
||||
/**
|
||||
* get Id of the first Tweet
|
||||
* @return ID of the first tweet or {@link #LIST_EMPTY} if list is empty
|
||||
*/
|
||||
public long getTopId() {
|
||||
if (!adapter.isEmpty())
|
||||
return adapter.getItemId(0);
|
||||
return LIST_EMPTY;
|
||||
}
|
||||
|
||||
/**
|
||||
* replace all tweets of the list
|
||||
* Set Tweet data to list
|
||||
*
|
||||
* @param tweets list of new tweets
|
||||
* @param tweets List of tweets
|
||||
* @param pos position where tweets should be added
|
||||
*/
|
||||
public void add(List<Tweet> tweets) {
|
||||
adapter.add(tweets);
|
||||
}
|
||||
|
||||
/**
|
||||
* attach new tweets to the top of the list
|
||||
*
|
||||
* @param tweets list of new tweets
|
||||
*/
|
||||
public void addTop(List<Tweet> tweets) {
|
||||
adapter.addFirst(tweets);
|
||||
public void setData(List<Tweet> tweets, int pos) {
|
||||
if (pos == CLEAR_LIST)
|
||||
adapter.clear();
|
||||
adapter.insert(tweets, pos);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -202,58 +195,48 @@ public class TweetFragment extends Fragment implements OnRefreshListener, TweetC
|
||||
}
|
||||
|
||||
|
||||
private void load() {
|
||||
private void load(long sinceId, long maxId, int pos) {
|
||||
Bundle param = getArguments();
|
||||
if (param != null) {
|
||||
int mode = param.getInt(KEY_FRAG_TWEET_MODE, 0);
|
||||
long id = param.getLong(KEY_FRAG_TWEET_ID, 1);
|
||||
String search = param.getString(KEY_FRAG_TWEET_SEARCH, "");
|
||||
String search = param.getString(KEY_FRAG_TWEET_SEARCH);
|
||||
Action action = Action.NONE;
|
||||
|
||||
switch (mode) {
|
||||
case TWEET_FRAG_HOME:
|
||||
tweetTask = new TweetListLoader(this, Action.TL_HOME);
|
||||
tweetTask.execute(1);
|
||||
action = Action.TL_HOME;
|
||||
break;
|
||||
|
||||
case TWEET_FRAG_MENT:
|
||||
tweetTask = new TweetListLoader(this, Action.TL_MENT);
|
||||
tweetTask.execute(1);
|
||||
action = Action.TL_MENT;
|
||||
break;
|
||||
|
||||
case TWEET_FRAG_TWEETS:
|
||||
tweetTask = new TweetListLoader(this, Action.USR_TWEETS);
|
||||
if (param.containsKey(KEY_FRAG_TWEET_ID)) // Search with User ID
|
||||
tweetTask.execute(id, 1);
|
||||
else if (param.containsKey(KEY_FRAG_TWEET_SEARCH)) // With user screen name
|
||||
tweetTask.execute(search, 1);
|
||||
action = Action.USR_TWEETS;
|
||||
break;
|
||||
|
||||
case TWEET_FRAG_FAVORS:
|
||||
tweetTask = new TweetListLoader(this, Action.USR_FAVORS);
|
||||
if (param.containsKey(KEY_FRAG_TWEET_ID)) // Search with User ID
|
||||
tweetTask.execute(id, 1);
|
||||
else if (param.containsKey(KEY_FRAG_TWEET_SEARCH)) // With user screen name
|
||||
tweetTask.execute(search, 1);
|
||||
action = Action.USR_FAVORS;
|
||||
break;
|
||||
|
||||
case TWEET_FRAG_ANSWER:
|
||||
if (tweetTask != null || settings.getAnswerLoad())
|
||||
tweetTask = new TweetListLoader(this, Action.TWEET_ANS);
|
||||
action = Action.TWEET_ANS;
|
||||
else
|
||||
tweetTask = new TweetListLoader(this, Action.DB_ANS);
|
||||
tweetTask.execute(id, search);
|
||||
action = Action.DB_ANS;
|
||||
break;
|
||||
|
||||
case TWEET_FRAG_SEARCH:
|
||||
tweetTask = new TweetListLoader(this, Action.TWEET_SEARCH);
|
||||
tweetTask.execute(search);
|
||||
action = Action.TWEET_SEARCH;
|
||||
break;
|
||||
|
||||
case TWEET_FRAG_LIST:
|
||||
tweetTask = new TweetListLoader(this, Action.LIST);
|
||||
tweetTask.execute(id, 1);
|
||||
action = Action.LIST;
|
||||
break;
|
||||
}
|
||||
tweetTask = new TweetListLoader(this, action, id, search, pos);
|
||||
tweetTask.execute(sinceId, maxId);
|
||||
}
|
||||
}
|
||||
}
|
15
app/src/main/res/layout/item_placeholder.xml
Normal file
15
app/src/main/res/layout/item_placeholder.xml
Normal file
@ -0,0 +1,15 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
style="@style/CardViewStyle"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content">
|
||||
|
||||
<Button
|
||||
android:id="@+id/item_placeholder"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_margin="@dimen/item_placeholder_margin"
|
||||
android:background="@drawable/button"
|
||||
android:text="@string/item_load_more_tweets" />
|
||||
|
||||
</androidx.cardview.widget.CardView>
|
@ -145,4 +145,5 @@
|
||||
<string name="error_rate_limit">Zu viele Anfragen! Bitte 15 Minuten warten!</string>
|
||||
<string name="info_cant_add_video">Video konnte nicht hinzugefügt werden!</string>
|
||||
<string name="error_file_not_found">Datei nicht gefunden!</string>
|
||||
<string name="item_load_more_tweets">Tweets laden</string>
|
||||
</resources>
|
@ -66,4 +66,5 @@
|
||||
<dimen name="profile_tv_padding">2dp</dimen>
|
||||
<dimen name="settings_appinfo_text_padding">5dp</dimen>
|
||||
<dimen name="settings_info_appname_font">18sp</dimen>
|
||||
<dimen name="item_placeholder_margin">8dp</dimen>
|
||||
</resources>
|
@ -148,4 +148,5 @@
|
||||
<string name="info_cant_add_video">Can\'t add video!</string>
|
||||
<string name="badge_twitter4j" translatable="false">Twitter4J Badge</string>
|
||||
<string name="error_file_not_found">File not found!</string>
|
||||
<string name="item_load_more_tweets">Load tweets</string>
|
||||
</resources>
|
Loading…
x
Reference in New Issue
Block a user