bug fix, code cleanup
This commit is contained in:
parent
69f7a4c396
commit
ac5dfdb194
|
@ -61,7 +61,7 @@ public class UserAdapter extends Adapter<ViewHolder> {
|
||||||
private GlobalSettings settings;
|
private GlobalSettings settings;
|
||||||
private Picasso picasso;
|
private Picasso picasso;
|
||||||
|
|
||||||
private Users data = new Users();
|
private Users data = new Users(0L, 0L);
|
||||||
private int loadingIndex = NO_LOADING;
|
private int loadingIndex = NO_LOADING;
|
||||||
private boolean userRemovable = false;
|
private boolean userRemovable = false;
|
||||||
|
|
||||||
|
@ -82,24 +82,29 @@ public class UserAdapter extends Adapter<ViewHolder> {
|
||||||
@MainThread
|
@MainThread
|
||||||
public void setData(@NonNull Users newData) {
|
public void setData(@NonNull Users newData) {
|
||||||
disableLoading();
|
disableLoading();
|
||||||
|
// add empty list
|
||||||
if (newData.isEmpty()) {
|
if (newData.isEmpty()) {
|
||||||
|
// remove page footer if there isn't a next page
|
||||||
if (!data.isEmpty() && data.peekLast() == null) {
|
if (!data.isEmpty() && data.peekLast() == null) {
|
||||||
// remove footer
|
|
||||||
int end = data.size() - 1;
|
int end = data.size() - 1;
|
||||||
data.remove(end);
|
data.remove(end);
|
||||||
notifyItemRemoved(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);
|
data.replace(newData);
|
||||||
|
// add page footer if there is a next page
|
||||||
if (newData.hasNext()) {
|
if (newData.hasNext()) {
|
||||||
// add footer
|
|
||||||
data.add(null);
|
data.add(null);
|
||||||
}
|
}
|
||||||
notifyDataSetChanged();
|
notifyDataSetChanged();
|
||||||
} else {
|
}
|
||||||
|
// add items to the end of the list
|
||||||
|
else {
|
||||||
int end = data.size() - 1;
|
int end = data.size() - 1;
|
||||||
|
// remove page footer if there isn't a next page
|
||||||
if (!newData.hasNext()) {
|
if (!newData.hasNext()) {
|
||||||
// remove footer
|
|
||||||
data.remove(end);
|
data.remove(end);
|
||||||
notifyItemRemoved(end);
|
notifyItemRemoved(end);
|
||||||
}
|
}
|
||||||
|
|
|
@ -64,7 +64,7 @@ public class UserlistAdapter extends Adapter<ViewHolder> {
|
||||||
private Resources resources;
|
private Resources resources;
|
||||||
private Picasso picasso;
|
private Picasso picasso;
|
||||||
|
|
||||||
private UserLists data = new UserLists();
|
private UserLists data = new UserLists(0L, 0L);
|
||||||
private int loadingIndex = NO_LOADING;
|
private int loadingIndex = NO_LOADING;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -339,8 +339,12 @@ public class Twitter implements GlobalSettings.SettingsListener {
|
||||||
List<String> params = new ArrayList<>(5);
|
List<String> params = new ArrayList<>(5);
|
||||||
params.add("list_id=" + listId);
|
params.add("list_id=" + listId);
|
||||||
params.add("cursor=" + cursor);
|
params.add("cursor=" + cursor);
|
||||||
// fix API returns wrong cursor if end of the list is reached
|
|
||||||
Users result = getUsers1(USER_LIST_MEMBER, params);
|
Users result = getUsers1(USER_LIST_MEMBER, params);
|
||||||
|
// 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);
|
result.setPrevCursor(cursor);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
@ -356,8 +360,12 @@ public class Twitter implements GlobalSettings.SettingsListener {
|
||||||
List<String> params = new ArrayList<>(5);
|
List<String> params = new ArrayList<>(5);
|
||||||
params.add("list_id=" + listId);
|
params.add("list_id=" + listId);
|
||||||
params.add("cursor=" + cursor);
|
params.add("cursor=" + cursor);
|
||||||
// fix API returns wrong cursor if end of the list is reached
|
|
||||||
Users result = getUsers1(USER_LIST_SUBSCRIBER, params);
|
Users result = getUsers1(USER_LIST_SUBSCRIBER, params);
|
||||||
|
// 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);
|
result.setPrevCursor(cursor);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
@ -580,7 +588,8 @@ public class Twitter implements GlobalSettings.SettingsListener {
|
||||||
nextPage = 0;
|
nextPage = 0;
|
||||||
if (settings.filterResults())
|
if (settings.filterResults())
|
||||||
filterUsers(result);
|
filterUsers(result);
|
||||||
result.setCursors(currentPage - 1, nextPage);
|
result.setPrevCursor(currentPage - 1);
|
||||||
|
result.setNextCursor(nextPage);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1017,9 +1026,7 @@ public class Twitter implements GlobalSettings.SettingsListener {
|
||||||
params.add("user_id=" + userId);
|
params.add("user_id=" + userId);
|
||||||
else
|
else
|
||||||
params.add("screen_name=" + StringTools.encode(screen_name));
|
params.add("screen_name=" + StringTools.encode(screen_name));
|
||||||
UserLists result = getUserlists(USERLIST_OWNERSHIP, params);
|
return getUserlists(USERLIST_OWNERSHIP, params);
|
||||||
result.setCursors(0, 0); // this endpoint doesn't support cursors
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1474,7 +1481,7 @@ public class Twitter implements GlobalSettings.SettingsListener {
|
||||||
Response response = get(endpoint, params);
|
Response response = get(endpoint, params);
|
||||||
if (response.body() != null && response.code() == 200) {
|
if (response.body() != null && response.code() == 200) {
|
||||||
JSONObject json = new JSONObject(response.body().string());
|
JSONObject json = new JSONObject(response.body().string());
|
||||||
Users users = new Users();
|
Users users = new Users(0L, 0L);
|
||||||
// check if result is not empty
|
// check if result is not empty
|
||||||
if (json.has("data")) {
|
if (json.has("data")) {
|
||||||
JSONArray array = json.getJSONArray("data");
|
JSONArray array = json.getJSONArray("data");
|
||||||
|
@ -1566,7 +1573,7 @@ public class Twitter implements GlobalSettings.SettingsListener {
|
||||||
Response response = get(endpoint, params);
|
Response response = get(endpoint, params);
|
||||||
if (response.body() != null && response.code() == 200) {
|
if (response.body() != null && response.code() == 200) {
|
||||||
JSONArray array;
|
JSONArray array;
|
||||||
UserLists result = new UserLists();
|
UserLists result;
|
||||||
String body = response.body().string();
|
String body = response.body().string();
|
||||||
// add cursors if available
|
// add cursors if available
|
||||||
if (body.startsWith("{")) {
|
if (body.startsWith("{")) {
|
||||||
|
@ -1574,9 +1581,10 @@ public class Twitter implements GlobalSettings.SettingsListener {
|
||||||
array = json.getJSONArray("lists");
|
array = json.getJSONArray("lists");
|
||||||
long prevCursor = Long.parseLong(json.optString("previous_cursor_str", "0"));
|
long prevCursor = Long.parseLong(json.optString("previous_cursor_str", "0"));
|
||||||
long nextCursor = Long.parseLong(json.optString("next_cursor_str", "0"));
|
long nextCursor = Long.parseLong(json.optString("next_cursor_str", "0"));
|
||||||
result.setCursors(prevCursor, nextCursor);
|
result = new UserLists(prevCursor, nextCursor);
|
||||||
} else {
|
} else {
|
||||||
array = new JSONArray(body);
|
array = new JSONArray(body);
|
||||||
|
result = new UserLists(0L, 0L);
|
||||||
}
|
}
|
||||||
long currentId = settings.getCurrentUserId();
|
long currentId = settings.getCurrentUserId();
|
||||||
for (int pos = 0; pos < array.length(); pos++) {
|
for (int pos = 0; pos < array.length(); pos++) {
|
||||||
|
|
|
@ -14,17 +14,10 @@ import java.util.LinkedList;
|
||||||
*/
|
*/
|
||||||
public class UserLists extends LinkedList<UserList> {
|
public class UserLists extends LinkedList<UserList> {
|
||||||
|
|
||||||
private static final long serialVersionUID = -5947008315897774115L;
|
public static final long serialVersionUID = -5947008315897774115L;
|
||||||
|
|
||||||
private long prevCursor, nextCursor;
|
private long prevCursor, nextCursor;
|
||||||
|
|
||||||
/**
|
|
||||||
* create an empty list
|
|
||||||
*/
|
|
||||||
public UserLists() {
|
|
||||||
this(0, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param prevCursor previous list cursor or 0 if list starts
|
* @param prevCursor previous list cursor or 0 if list starts
|
||||||
* @param nextCursor next cursor or 0 if list ends
|
* @param nextCursor next cursor or 0 if list ends
|
||||||
|
@ -69,12 +62,6 @@ public class UserLists extends LinkedList<UserList> {
|
||||||
return nextCursor;
|
return nextCursor;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void setCursors(long prevCursor, long nextCursor) {
|
|
||||||
this.prevCursor = prevCursor;
|
|
||||||
this.nextCursor = nextCursor;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* replace whole list including cursors
|
* replace whole list including cursors
|
||||||
*
|
*
|
||||||
|
|
|
@ -14,14 +14,10 @@ import java.util.LinkedList;
|
||||||
*/
|
*/
|
||||||
public class Users extends LinkedList<User> {
|
public class Users extends LinkedList<User> {
|
||||||
|
|
||||||
private static final long serialVersionUID = -1108521824070076679L;
|
public static final long serialVersionUID = -1108521824070076679L;
|
||||||
|
|
||||||
private long prevCursor = 0;
|
private long prevCursor;
|
||||||
private long nextCursor = 0;
|
private long nextCursor;
|
||||||
|
|
||||||
public Users() {
|
|
||||||
super();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* creates an empty list with defined cursors
|
* creates an empty list with defined cursors
|
||||||
|
@ -97,15 +93,24 @@ public class Users extends LinkedList<User> {
|
||||||
nextCursor = list.nextCursor;
|
nextCursor = list.nextCursor;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setCursors(long prevCursor, long nextCursor) {
|
/**
|
||||||
this.prevCursor = prevCursor;
|
* set previous cursor
|
||||||
this.nextCursor = nextCursor;
|
*
|
||||||
}
|
* @param prevCursor cursor value
|
||||||
|
*/
|
||||||
public void setPrevCursor(long prevCursor) {
|
public void setPrevCursor(long prevCursor) {
|
||||||
this.prevCursor = 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
|
* add a sublist at the bottom of this list including next cursor
|
||||||
*
|
*
|
||||||
|
|
Loading…
Reference in New Issue