renamed class, minimized userlist holder

This commit is contained in:
nuclearfog 2020-10-18 11:38:21 +02:00
parent 6746d8c0ea
commit 8ab05e9c9f
No known key found for this signature in database
GPG Key ID: D5490E4A81F97B14
5 changed files with 56 additions and 75 deletions

View File

@ -18,7 +18,7 @@ import androidx.recyclerview.widget.RecyclerView.ViewHolder;
import com.squareup.picasso.Picasso;
import org.nuclearfog.twidda.R;
import org.nuclearfog.twidda.backend.holder.UserListHolder;
import org.nuclearfog.twidda.backend.holder.TwitterUserList;
import org.nuclearfog.twidda.backend.items.TwitterUser;
import org.nuclearfog.twidda.backend.utils.FontTool;
import org.nuclearfog.twidda.database.GlobalSettings;
@ -59,13 +59,13 @@ public class UserAdapter extends Adapter<ViewHolder> {
@MainThread
public void setData(@NonNull UserListHolder data) {
public void setData(@NonNull TwitterUserList data) {
if (users.isEmpty() || !data.hasPrevious()) {
if (!users.isEmpty()) {
// replace previous data
users.clear();
}
users.addAll(data.getUsers());
users.addAll(data);
if (data.hasNext()) {
// add footer
users.add(null);
@ -80,8 +80,8 @@ public class UserAdapter extends Adapter<ViewHolder> {
} else {
disableLoading();
}
users.addAll(end, data.getUsers());
notifyItemRangeInserted(end, data.getSize());
users.addAll(end, data);
notifyItemRangeInserted(end, data.size());
}
nextCursor = data.getNext();
loadingIndex = NO_INDEX;

View File

@ -6,7 +6,7 @@ import androidx.annotation.Nullable;
import org.nuclearfog.twidda.backend.engine.EngineException;
import org.nuclearfog.twidda.backend.engine.TwitterEngine;
import org.nuclearfog.twidda.backend.holder.UserListHolder;
import org.nuclearfog.twidda.backend.holder.TwitterUserList;
import org.nuclearfog.twidda.fragment.UserFragment;
import java.lang.ref.WeakReference;
@ -16,7 +16,7 @@ import java.lang.ref.WeakReference;
*
* @see UserFragment
*/
public class UserListLoader extends AsyncTask<Void, Void, UserListHolder> {
public class UserListLoader extends AsyncTask<Void, Void, TwitterUserList> {
public static final long NO_CURSOR = -1;
@ -33,12 +33,13 @@ public class UserListLoader extends AsyncTask<Void, Void, UserListHolder> {
@Nullable
private EngineException twException;
private WeakReference<UserFragment> callback;
private TwitterEngine mTwitter;
private final WeakReference<UserFragment> callback;
private final TwitterEngine mTwitter;
private final Action action;
private String search;
private long id, cursor;
private final String search;
private final long id;
private final long cursor;
public UserListLoader(UserFragment callback, Action action, long id, long cursor, String search) {
@ -61,7 +62,7 @@ public class UserListLoader extends AsyncTask<Void, Void, UserListHolder> {
@Override
protected UserListHolder doInBackground(Void[] v) {
protected TwitterUserList doInBackground(Void[] v) {
try {
switch (action) {
case FOLLOWS:
@ -97,7 +98,7 @@ public class UserListLoader extends AsyncTask<Void, Void, UserListHolder> {
@Override
protected void onPostExecute(UserListHolder users) {
protected void onPostExecute(TwitterUserList users) {
if (callback.get() != null) {
callback.get().setRefresh(false);
if (users != null) {

View File

@ -9,8 +9,8 @@ import androidx.annotation.Nullable;
import org.nuclearfog.twidda.BuildConfig;
import org.nuclearfog.twidda.backend.holder.MessageHolder;
import org.nuclearfog.twidda.backend.holder.TweetHolder;
import org.nuclearfog.twidda.backend.holder.TwitterUserList;
import org.nuclearfog.twidda.backend.holder.UserHolder;
import org.nuclearfog.twidda.backend.holder.UserListHolder;
import org.nuclearfog.twidda.backend.items.Message;
import org.nuclearfog.twidda.backend.items.TrendLocation;
import org.nuclearfog.twidda.backend.items.Tweet;
@ -50,6 +50,9 @@ import twitter4j.auth.AccessToken;
import twitter4j.auth.RequestToken;
import twitter4j.conf.ConfigurationBuilder;
/**
* Backend for twitter API.
*/
public class TwitterEngine {
private static final TwitterEngine mTwitter = new TwitterEngine();
@ -290,18 +293,19 @@ public class TwitterEngine {
* @return List of Users
* @throws EngineException if access is unavailable
*/
public UserListHolder searchUsers(String search, long cursor) throws EngineException {
public TwitterUserList searchUsers(String search, long cursor) throws EngineException {
try {
int currentPage = 1;
if (cursor > 0)
currentPage = (int) cursor;
long prevPage = currentPage - 1;
long nextPage = currentPage + 1;
List<TwitterUser> users = convertUserList(twitter.searchUsers(search, currentPage));
if (users.size() < 20)
nextPage = 0;
return new UserListHolder(users, prevPage, nextPage);
TwitterUserList result = new TwitterUserList(prevPage, nextPage);
result.addAll(users);
return result;
} catch (TwitterException err) {
throw new EngineException(err);
}
@ -587,20 +591,18 @@ public class TwitterEngine {
* @return List of Following User with cursors
* @throws EngineException if Access is unavailable
*/
public UserListHolder getFollowing(long userId, long cursor) throws EngineException {
public TwitterUserList getFollowing(long userId, long cursor) throws EngineException {
try {
int load = settings.getListSize();
IDs userIDs = twitter.getFriendsIDs(userId, cursor, load);
long[] ids = userIDs.getIDs();
long prevCursor = cursor > 0 ? cursor : 0;
long nextCursor = userIDs.getNextCursor();
List<TwitterUser> users;
TwitterUserList result = new TwitterUserList(prevCursor, nextCursor);
if (ids.length > 0) {
users = convertUserList(twitter.lookupUsers(ids));
} else {
users = new LinkedList<>();
result.addAll(convertUserList(twitter.lookupUsers(ids)));
}
return new UserListHolder(users, prevCursor, nextCursor);
return result;
} catch (TwitterException err) {
throw new EngineException(err);
}
@ -614,20 +616,18 @@ public class TwitterEngine {
* @return List of Follower with cursors attached
* @throws EngineException if Access is unavailable
*/
public UserListHolder getFollower(long userId, long cursor) throws EngineException {
public TwitterUserList getFollower(long userId, long cursor) throws EngineException {
try {
int load = settings.getListSize();
IDs userIDs = twitter.getFollowersIDs(userId, cursor, load);
long[] ids = userIDs.getIDs();
long prevCursor = cursor > 0 ? cursor : 0;
long nextCursor = userIDs.getNextCursor();
List<TwitterUser> users;
TwitterUserList result = new TwitterUserList(prevCursor, nextCursor);
if (ids.length > 0) {
users = convertUserList(twitter.lookupUsers(ids));
} else {
users = new LinkedList<>();
result.addAll(convertUserList(twitter.lookupUsers(ids)));
}
return new UserListHolder(users, prevCursor, nextCursor);
return result;
} catch (TwitterException err) {
throw new EngineException(err);
}
@ -791,24 +791,21 @@ public class TwitterEngine {
* Get User who retweeted a Tweet
*
* @param tweetID Tweet ID
*
* @return List of users or empty list if no match
* @throws EngineException if Access is unavailable
*/
public UserListHolder getRetweeter(long tweetID, long cursor) throws EngineException {
public TwitterUserList getRetweeter(long tweetID, long cursor) throws EngineException {
try {
int load = settings.getListSize();
IDs userIDs = twitter.getRetweeterIds(tweetID, load, cursor);
long[] ids = userIDs.getIDs();
long prevCursor = cursor > 0 ? cursor : 0;
long nextCursor = userIDs.getNextCursor(); // todo fix next cursor always zero
List<TwitterUser> users;
long nextCursor = userIDs.getNextCursor(); // fixme next cursor always zero
TwitterUserList result = new TwitterUserList(prevCursor, nextCursor);
if (ids.length > 0) {
users = convertUserList(twitter.lookupUsers(ids));
} else {
users = new LinkedList<>();
result.addAll(convertUserList(twitter.lookupUsers(ids)));
}
return new UserListHolder(users, prevCursor, nextCursor);
return result;
} catch (TwitterException err) {
throw new EngineException(err);
}
@ -910,7 +907,7 @@ public class TwitterEngine {
*/
public List<TwitterList> getUserList(long userId) throws EngineException {
try {
List<TwitterList> result = new LinkedList<>();
List<TwitterList> result = new LinkedList<>(); // TODO add a paging system
List<UserList> lists = twitter.getUserLists(userId);
for (UserList list : lists)
result.add(new TwitterList(list, twitterID));
@ -985,13 +982,14 @@ public class TwitterEngine {
* @return list of users following the list
* @throws EngineException if access is unavailable
*/
public UserListHolder getListFollower(long listId, long cursor) throws EngineException {
public TwitterUserList getListFollower(long listId, long cursor) throws EngineException {
try {
PagableResponseList<User> result = twitter.getUserListSubscribers(listId, cursor);
List<TwitterUser> users = convertUserList(result);
PagableResponseList<User> followerList = twitter.getUserListSubscribers(listId, cursor);
long prevCursor = cursor > 0 ? cursor : 0;
long nextCursor = result.getNextCursor();
return new UserListHolder(users, prevCursor, nextCursor);
long nextCursor = followerList.getNextCursor();
TwitterUserList result = new TwitterUserList(prevCursor, nextCursor);
result.addAll(convertUserList(followerList));
return result;
} catch (TwitterException err) {
throw new EngineException(err);
}
@ -1004,13 +1002,14 @@ public class TwitterEngine {
* @return list of users
* @throws EngineException if access is unavailable
*/
public UserListHolder getListMember(long listId, long cursor) throws EngineException {
public TwitterUserList getListMember(long listId, long cursor) throws EngineException {
try {
PagableResponseList<User> result = twitter.getUserListMembers(listId, cursor);
List<TwitterUser> users = convertUserList(result);
PagableResponseList<User> users = twitter.getUserListMembers(listId, cursor);
long prevCursor = cursor > 0 ? cursor : 0;
long nextCursor = result.getNextCursor();
return new UserListHolder(users, prevCursor, nextCursor);
long nextCursor = users.getNextCursor();
TwitterUserList result = new TwitterUserList(prevCursor, nextCursor);
result.addAll(convertUserList(users));
return result;
} catch (TwitterException err) {
throw new EngineException(err);
}

View File

@ -4,18 +4,17 @@ import androidx.annotation.NonNull;
import org.nuclearfog.twidda.backend.items.TwitterUser;
import java.util.List;
import java.util.LinkedList;
/**
* Container class for user list information
* custom twitter user list with cursors included
*/
public class UserListHolder {
public class TwitterUserList extends LinkedList<TwitterUser> {
private final List<TwitterUser> users;
private final long prevCursor, nextCursor;
public UserListHolder(List<TwitterUser> users, long prevCursor, long nextCursor) {
this.users = users;
public TwitterUserList(long prevCursor, long nextCursor) {
super();
this.prevCursor = prevCursor;
this.nextCursor = nextCursor;
}
@ -47,27 +46,9 @@ public class UserListHolder {
return nextCursor;
}
/**
* get size of the attached list
*
* @return size of the list
*/
public int getSize() {
return users.size();
}
/**
* get attached list
*
* @return list
*/
public List<TwitterUser> getUsers() {
return users;
}
@Override
@NonNull
public String toString() {
return "size=" + getSize() + " pre=" + prevCursor + " pos=" + nextCursor;
return "size=" + size() + " pre=" + prevCursor + " pos=" + nextCursor;
}
}

View File

@ -22,7 +22,7 @@ import org.nuclearfog.twidda.adapter.UserAdapter.UserClickListener;
import org.nuclearfog.twidda.backend.UserListLoader;
import org.nuclearfog.twidda.backend.UserListLoader.Action;
import org.nuclearfog.twidda.backend.engine.EngineException;
import org.nuclearfog.twidda.backend.holder.UserListHolder;
import org.nuclearfog.twidda.backend.holder.TwitterUserList;
import org.nuclearfog.twidda.backend.items.TwitterUser;
import org.nuclearfog.twidda.backend.utils.ErrorHandler;
import org.nuclearfog.twidda.database.GlobalSettings;
@ -132,7 +132,7 @@ public class UserFragment extends Fragment implements OnRefreshListener, UserCli
*
* @param data list of twitter users
*/
public void setData(UserListHolder data) {
public void setData(TwitterUserList data) {
adapter.setData(data);
}