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 com.squareup.picasso.Picasso;
import org.nuclearfog.twidda.R; 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.items.TwitterUser;
import org.nuclearfog.twidda.backend.utils.FontTool; import org.nuclearfog.twidda.backend.utils.FontTool;
import org.nuclearfog.twidda.database.GlobalSettings; import org.nuclearfog.twidda.database.GlobalSettings;
@ -59,13 +59,13 @@ public class UserAdapter extends Adapter<ViewHolder> {
@MainThread @MainThread
public void setData(@NonNull UserListHolder data) { public void setData(@NonNull TwitterUserList data) {
if (users.isEmpty() || !data.hasPrevious()) { if (users.isEmpty() || !data.hasPrevious()) {
if (!users.isEmpty()) { if (!users.isEmpty()) {
// replace previous data // replace previous data
users.clear(); users.clear();
} }
users.addAll(data.getUsers()); users.addAll(data);
if (data.hasNext()) { if (data.hasNext()) {
// add footer // add footer
users.add(null); users.add(null);
@ -80,8 +80,8 @@ public class UserAdapter extends Adapter<ViewHolder> {
} else { } else {
disableLoading(); disableLoading();
} }
users.addAll(end, data.getUsers()); users.addAll(end, data);
notifyItemRangeInserted(end, data.getSize()); notifyItemRangeInserted(end, data.size());
} }
nextCursor = data.getNext(); nextCursor = data.getNext();
loadingIndex = NO_INDEX; 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.EngineException;
import org.nuclearfog.twidda.backend.engine.TwitterEngine; 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 org.nuclearfog.twidda.fragment.UserFragment;
import java.lang.ref.WeakReference; import java.lang.ref.WeakReference;
@ -16,7 +16,7 @@ import java.lang.ref.WeakReference;
* *
* @see UserFragment * @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; public static final long NO_CURSOR = -1;
@ -33,12 +33,13 @@ public class UserListLoader extends AsyncTask<Void, Void, UserListHolder> {
@Nullable @Nullable
private EngineException twException; private EngineException twException;
private WeakReference<UserFragment> callback; private final WeakReference<UserFragment> callback;
private TwitterEngine mTwitter; private final TwitterEngine mTwitter;
private final Action action; private final Action action;
private String search; private final String search;
private long id, cursor; private final long id;
private final long cursor;
public UserListLoader(UserFragment callback, Action action, long id, long cursor, String search) { 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 @Override
protected UserListHolder doInBackground(Void[] v) { protected TwitterUserList doInBackground(Void[] v) {
try { try {
switch (action) { switch (action) {
case FOLLOWS: case FOLLOWS:
@ -97,7 +98,7 @@ public class UserListLoader extends AsyncTask<Void, Void, UserListHolder> {
@Override @Override
protected void onPostExecute(UserListHolder users) { protected void onPostExecute(TwitterUserList users) {
if (callback.get() != null) { if (callback.get() != null) {
callback.get().setRefresh(false); callback.get().setRefresh(false);
if (users != null) { if (users != null) {

View File

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

View File

@ -4,18 +4,17 @@ import androidx.annotation.NonNull;
import org.nuclearfog.twidda.backend.items.TwitterUser; 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; private final long prevCursor, nextCursor;
public UserListHolder(List<TwitterUser> users, long prevCursor, long nextCursor) { public TwitterUserList(long prevCursor, long nextCursor) {
this.users = users; super();
this.prevCursor = prevCursor; this.prevCursor = prevCursor;
this.nextCursor = nextCursor; this.nextCursor = nextCursor;
} }
@ -47,27 +46,9 @@ public class UserListHolder {
return nextCursor; 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 @Override
@NonNull @NonNull
public String toString() { 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;
import org.nuclearfog.twidda.backend.UserListLoader.Action; import org.nuclearfog.twidda.backend.UserListLoader.Action;
import org.nuclearfog.twidda.backend.engine.EngineException; 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.items.TwitterUser;
import org.nuclearfog.twidda.backend.utils.ErrorHandler; import org.nuclearfog.twidda.backend.utils.ErrorHandler;
import org.nuclearfog.twidda.database.GlobalSettings; import org.nuclearfog.twidda.database.GlobalSettings;
@ -132,7 +132,7 @@ public class UserFragment extends Fragment implements OnRefreshListener, UserCli
* *
* @param data list of twitter users * @param data list of twitter users
*/ */
public void setData(UserListHolder data) { public void setData(TwitterUserList data) {
adapter.setData(data); adapter.setData(data);
} }