Skip to content
This repository has been archived by the owner on Jan 22, 2023. It is now read-only.

Commit

Permalink
Check if list contains member before insertion, and offer to remove it
Browse files Browse the repository at this point in the history
  • Loading branch information
elvisoliveira committed Oct 18, 2021
1 parent c3b0612 commit 2db25a6
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
import fisk.chipcloud.ChipCloud;
import fisk.chipcloud.ChipCloudConfig;
import fisk.chipcloud.ChipListener;
import twitter4j.PagableResponseList;
import twitter4j.Paging;
import twitter4j.Query;
import twitter4j.QueryResult;
Expand Down Expand Up @@ -1865,7 +1866,7 @@ public int compare(UserList result1, UserList result2) {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
if (thisUser != null) {
new AddToList(lists.get(i).getId(), thisUser.getId()).execute();
new CheckList(lists.get(i), thisUser).execute();
}
}
});
Expand All @@ -1889,6 +1890,76 @@ public void onClick(DialogInterface dialogInterface, int i) {
}
}

class CheckList extends AsyncTask<String, Void, List<UserList>> {

UserList list;
User user;

public CheckList(UserList listId, User userId) {
this.list = listId;
this.user = userId;
}

protected List<UserList> doInBackground(String... urls) {
try {
Twitter twitter = Utils.getTwitter(context, settings);
List<UserList> userLists = new ArrayList<UserList>();

long cursor = -1;
PagableResponseList<UserList> pagableThisUserLists;
do {
pagableThisUserLists = twitter.getUserListMemberships(user.getScreenName(), cursor, true);
for (UserList userList : pagableThisUserLists) {
userLists.add(userList);
}
} while ((cursor = pagableThisUserLists.getNextCursor()) != 0);

return userLists;
} catch (Exception e) {
return null;
}
}

protected void onPostExecute(List<UserList> userLists) {
if(userLists != null && userLists.contains(list)) {
new AlertDialog.Builder(context)
.setMessage(R.string.remove_from_list)
.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
new RemoveFromList(list.getId(), user.getId()).execute();
}
}).show();
return;
}
new AddToList(list.getId(), user.getId()).execute();
}
}

class RemoveFromList extends AsyncTask<String, Void, Boolean> {
long listId;
long userId;

public RemoveFromList(long listId, long userId) {
this.listId = listId;
this.userId = userId;
}

protected Boolean doInBackground(String... urls) {
try {
Twitter twitter = Utils.getTwitter(context, settings);
twitter.destroyUserListMember(listId, userId);
return true;
} catch (Exception e) {
return false;
}
}

protected void onPostExecute(Boolean added) {
Toast.makeText(context, getResources().getString(added ? R.string.removed_from_list : R.string.error), Toast.LENGTH_SHORT).show();
}
}

class AddToList extends AsyncTask<String, Void, Boolean> {

long listId;
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,8 @@
<string name="finding_lists">Finding Lists...</string>
<string name="choose_list">Choose List</string>
<string name="added_to_list">Added to list</string>
<string name="remove_from_list">User already belongs to the list, would you like to remove it?</string>
<string name="removed_from_list">Removed from the list</string>
<!-- Tutorial Activity -->
<string name="part_1_tablet">\nAccess features\nby tapping items\nin the\nnavigation\ndrawer!</string>
<string name="part_1_phone">\nPull open the\nnavigation drawer\nwith a left\nside swipe!</string>
Expand Down

0 comments on commit 2db25a6

Please sign in to comment.