bug fix, code cleanup

This commit is contained in:
nuclearfog 2022-05-22 12:04:13 +02:00
parent 69f7a4c396
commit ac5dfdb194
No known key found for this signature in database
GPG Key ID: AA0271FBE406DB98
5 changed files with 49 additions and 44 deletions

View File

@ -61,7 +61,7 @@ public class UserAdapter extends Adapter<ViewHolder> {
private GlobalSettings settings;
private Picasso picasso;
private Users data = new Users();
private Users data = new Users(0L, 0L);
private int loadingIndex = NO_LOADING;
private boolean userRemovable = false;
@ -82,24 +82,29 @@ public class UserAdapter extends Adapter<ViewHolder> {
@MainThread
public void setData(@NonNull Users newData) {
disableLoading();
// add empty list
if (newData.isEmpty()) {
// remove page footer if there isn't a next page
if (!data.isEmpty() && data.peekLast() == null) {
// remove footer
int end = data.size() - 1;
data.remove(end);
notifyItemRemoved(end);
}
} else if (data.isEmpty() || !newData.hasPrevious()) {
}
// add items to the top of the list
else if (data.isEmpty() || !newData.hasPrevious()) {
data.replace(newData);
// add page footer if there is a next page
if (newData.hasNext()) {
// add footer
data.add(null);
}
notifyDataSetChanged();
} else {
}
// add items to the end of the list
else {
int end = data.size() - 1;
// remove page footer if there isn't a next page
if (!newData.hasNext()) {
// remove footer
data.remove(end);
notifyItemRemoved(end);
}

View File

@ -64,7 +64,7 @@ public class UserlistAdapter extends Adapter<ViewHolder> {
private Resources resources;
private Picasso picasso;
private UserLists data = new UserLists();
private UserLists data = new UserLists(0L, 0L);
private int loadingIndex = NO_LOADING;
/**

View File

@ -339,9 +339,13 @@ public class Twitter implements GlobalSettings.SettingsListener {
List<String> params = new ArrayList<>(5);
params.add("list_id=" + listId);
params.add("cursor=" + cursor);
// fix API returns wrong cursor if end of the list is reached
Users result = getUsers1(USER_LIST_MEMBER, params);
result.setPrevCursor(cursor);
// fix API returns zero previous_cursor when the end of the list is reached
// override previous cursor
if (cursor == -1L)
result.setPrevCursor(0);
else
result.setPrevCursor(cursor);
return result;
}
@ -356,9 +360,13 @@ public class Twitter implements GlobalSettings.SettingsListener {
List<String> params = new ArrayList<>(5);
params.add("list_id=" + listId);
params.add("cursor=" + cursor);
// fix API returns wrong cursor if end of the list is reached
Users result = getUsers1(USER_LIST_SUBSCRIBER, params);
result.setPrevCursor(cursor);
// fix API returns zero previous_cursor when the end of the list is reached
// override previous cursor
if (cursor == -1L)
result.setPrevCursor(0);
else
result.setPrevCursor(cursor);
return result;
}
@ -580,7 +588,8 @@ public class Twitter implements GlobalSettings.SettingsListener {
nextPage = 0;
if (settings.filterResults())
filterUsers(result);
result.setCursors(currentPage - 1, nextPage);
result.setPrevCursor(currentPage - 1);
result.setNextCursor(nextPage);
return result;
}
@ -1017,9 +1026,7 @@ public class Twitter implements GlobalSettings.SettingsListener {
params.add("user_id=" + userId);
else
params.add("screen_name=" + StringTools.encode(screen_name));
UserLists result = getUserlists(USERLIST_OWNERSHIP, params);
result.setCursors(0, 0); // this endpoint doesn't support cursors
return result;
return getUserlists(USERLIST_OWNERSHIP, params);
}
/**
@ -1474,7 +1481,7 @@ public class Twitter implements GlobalSettings.SettingsListener {
Response response = get(endpoint, params);
if (response.body() != null && response.code() == 200) {
JSONObject json = new JSONObject(response.body().string());
Users users = new Users();
Users users = new Users(0L, 0L);
// check if result is not empty
if (json.has("data")) {
JSONArray array = json.getJSONArray("data");
@ -1566,7 +1573,7 @@ public class Twitter implements GlobalSettings.SettingsListener {
Response response = get(endpoint, params);
if (response.body() != null && response.code() == 200) {
JSONArray array;
UserLists result = new UserLists();
UserLists result;
String body = response.body().string();
// add cursors if available
if (body.startsWith("{")) {
@ -1574,9 +1581,10 @@ public class Twitter implements GlobalSettings.SettingsListener {
array = json.getJSONArray("lists");
long prevCursor = Long.parseLong(json.optString("previous_cursor_str", "0"));
long nextCursor = Long.parseLong(json.optString("next_cursor_str", "0"));
result.setCursors(prevCursor, nextCursor);
result = new UserLists(prevCursor, nextCursor);
} else {
array = new JSONArray(body);
result = new UserLists(0L, 0L);
}
long currentId = settings.getCurrentUserId();
for (int pos = 0; pos < array.length(); pos++) {

View File

@ -14,17 +14,10 @@ import java.util.LinkedList;
*/
public class UserLists extends LinkedList<UserList> {
private static final long serialVersionUID = -5947008315897774115L;
public static final long serialVersionUID = -5947008315897774115L;
private long prevCursor, nextCursor;
/**
* create an empty list
*/
public UserLists() {
this(0, 0);
}
/**
* @param prevCursor previous list cursor or 0 if list starts
* @param nextCursor next cursor or 0 if list ends
@ -69,12 +62,6 @@ public class UserLists extends LinkedList<UserList> {
return nextCursor;
}
public void setCursors(long prevCursor, long nextCursor) {
this.prevCursor = prevCursor;
this.nextCursor = nextCursor;
}
/**
* replace whole list including cursors
*

View File

@ -14,14 +14,10 @@ import java.util.LinkedList;
*/
public class Users extends LinkedList<User> {
private static final long serialVersionUID = -1108521824070076679L;
public static final long serialVersionUID = -1108521824070076679L;
private long prevCursor = 0;
private long nextCursor = 0;
public Users() {
super();
}
private long prevCursor;
private long nextCursor;
/**
* creates an empty list with defined cursors
@ -97,15 +93,24 @@ public class Users extends LinkedList<User> {
nextCursor = list.nextCursor;
}
public void setCursors(long prevCursor, long nextCursor) {
this.prevCursor = prevCursor;
this.nextCursor = nextCursor;
}
/**
* set previous cursor
*
* @param prevCursor cursor value
*/
public void setPrevCursor(long prevCursor) {
this.prevCursor = prevCursor;
}
/**
* set next cursor
*
* @param nextCursor cursor value
*/
public void setNextCursor(long nextCursor) {
this.nextCursor = nextCursor;
}
/**
* add a sublist at the bottom of this list including next cursor
*