Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow user to vote comments down #1444

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ public Exception(int message) {

boolean voteUp(Context context, String itemId, Callback callback);

boolean voteDown(Context context, String itemId, Callback callback);

void reply(Context context, String parentId, String text, Callback callback);

void submit(Context context, String title, String content, boolean isUrl, Callback callback);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public class UserServicesClient implements UserServices {
private static final String SUBMIT_PARAM_FNID = "fnid";
private static final String SUBMIT_PARAM_FNOP = "fnop";
private static final String VOTE_DIR_UP = "up";
private static final String VOTE_DIR_DOWN = "down";
private static final String DEFAULT_REDIRECT = "news";
private static final String CREATING_TRUE = "t";
private static final String DEFAULT_FNOP = "submit-page";
Expand Down Expand Up @@ -102,7 +103,21 @@ public boolean voteUp(Context context, String itemId, Callback callback) {
return false;
}
Toast.makeText(context, R.string.sending, Toast.LENGTH_SHORT).show();
execute(postVote(credentials.first, credentials.second, itemId))
execute(postVoteUp(credentials.first, credentials.second, itemId))
.map(response -> response.code() == HttpURLConnection.HTTP_MOVED_TEMP)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(callback::onDone, callback::onError);
return true;
}

@Override
public boolean voteDown(Context context, String itemId, Callback callback) {
Pair<String, String> credentials = AppUtils.getCredentials(context);
if (credentials == null) {
return false;
}
Toast.makeText(context, R.string.sending, Toast.LENGTH_SHORT).show();
execute(postVoteDown(credentials.first, credentials.second, itemId))
.map(response -> response.code() == HttpURLConnection.HTTP_MOVED_TEMP)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(callback::onDone, callback::onError);
Expand Down Expand Up @@ -190,7 +205,15 @@ private Request postLogin(String username, String password, boolean createAccoun
.build();
}

private Request postVote(String username, String password, String itemId) {
private Request postVoteUp(String username, String password, String itemId) {
return postVote(username, password, itemId, VOTE_DIR_UP);
}

private Request postVoteDown(String username, String password, String itemId) {
return postVote(username, password, itemId, VOTE_DIR_DOWN);
}

private Request postVote(String username, String password, String itemId, String upOrDown) {
return new Request.Builder()
.url(HttpUrl.parse(BASE_WEB_URL)
.newBuilder()
Expand All @@ -200,11 +223,12 @@ private Request postVote(String username, String password, String itemId) {
.add(LOGIN_PARAM_ACCT, username)
.add(LOGIN_PARAM_PW, password)
.add(VOTE_PARAM_ID, itemId)
.add(VOTE_PARAM_HOW, VOTE_DIR_UP)
.add(VOTE_PARAM_HOW, upOrDown)
.build())
.build();
}


private Request postReply(String parentId, String text, String username, String password) {
return new Request.Builder()
.url(HttpUrl.parse(BASE_WEB_URL)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ private void showMoreOptions(View v, final Favorite item) {
mPopupMenu.create(mContext, v, Gravity.NO_GRAVITY)
.inflate(R.menu.menu_contextual_favorite)
.setOnMenuItemClickListener(menuItem -> {
if (menuItem.getItemId() == R.id.menu_contextual_vote) {
if (menuItem.getItemId() == R.id.menu_contextual_vote_up) {
vote(item);
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,12 @@ private void bindActions(final VH holder, final Item item) {
mPopupMenu.create(mContext, holder.mMoreButton, Gravity.NO_GRAVITY)
.inflate(R.menu.menu_contextual_comment)
.setOnMenuItemClickListener(menuItem -> {
if (menuItem.getItemId() == R.id.menu_contextual_vote) {
vote(item);
if (menuItem.getItemId() == R.id.menu_contextual_vote_up) {
voteUp(item);
return true;
}
if (menuItem.getItemId() == R.id.menu_contextual_vote_down) {
voteDown(item);
return true;
}
if (menuItem.getItemId() == R.id.menu_contextual_comment) {
Expand All @@ -257,10 +261,14 @@ private void bindActions(final VH holder, final Item item) {
.show());
}

private void vote(final Item item) {
private void voteUp(final Item item) {
mUserServices.voteUp(mContext, item.getId(), new VoteCallback(this));
}

private void voteDown(final Item item) {
mUserServices.voteDown(mContext, item.getId(), new VoteCallback(this));
}

@Synthetic
void onVoted(Boolean successful) {
if (successful == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ void showMoreOptions(View v, final Item story, final ItemViewHolder holder) {
story.isFavorite() ? R.string.unsave : R.string.save)
.setMenuItemVisible(R.id.menu_contextual_save,
!mCallback.hasAction(Preferences.SwipeAction.Save))
.setMenuItemVisible(R.id.menu_contextual_vote,
.setMenuItemVisible(R.id.menu_contextual_vote_up,
!mCallback.hasAction(Preferences.SwipeAction.Vote))
.setMenuItemVisible(R.id.menu_contextual_refresh,
!mCallback.hasAction(Preferences.SwipeAction.Refresh))
Expand All @@ -451,7 +451,7 @@ void showMoreOptions(View v, final Item story, final ItemViewHolder holder) {
toggleSave(story);
return true;
}
if (item.getItemId() == R.id.menu_contextual_vote) {
if (item.getItemId() == R.id.menu_contextual_vote_up) {
vote(story, holder);
return true;
}
Expand Down
5 changes: 4 additions & 1 deletion app/src/main/res/menu/menu_contextual_comment.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@

<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@id/menu_contextual_vote"
android:id="@id/menu_contextual_vote_up"
android:title="@string/vote_up" />
<item
android:id="@id/menu_contextual_vote_down"
android:title="@string/vote_down" />
<item
android:id="@id/menu_contextual_comment"
android:title="@string/add_comment" />
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/menu/menu_contextual_favorite.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@id/menu_contextual_vote"
android:id="@id/menu_contextual_vote_up"
android:title="@string/vote_up" />
<item
android:id="@id/menu_contextual_comment"
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/menu/menu_contextual_story.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
android:id="@id/menu_contextual_save"
android:title="@string/save" />
<item
android:id="@id/menu_contextual_vote"
android:id="@id/menu_contextual_vote_up"
android:title="@string/vote_up" />
<item
android:id="@id/menu_contextual_refresh"
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-es/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@
<string name="unsave">Quitar de guardados</string>
<string name="re_enter_password">Vuelve a escribir tu contraseña</string>
<string name="vote_up">Votar arriba</string>
<string name="vote_down">Votar abajo</string>
<string name="voted">Votada correctamente</string>
<string name="vote_failed">Ha sido imposible contabilizar tu voto. Intentalo de nuevo por favor.</string>
<string name="sending">Enviando…</string>
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/res/values/ids.xml
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@
<item type="id" name="menu_sort_popular" />
<item type="id" name="menu_list" />
<item type="id" name="menu_contextual_save" />
<item type="id" name="menu_contextual_vote" />
<item type="id" name="menu_contextual_vote_up" />
<item type="id" name="menu_contextual_vote_down" />
<item type="id" name="menu_contextual_refresh" />
<item type="id" name="menu_contextual_comment" />
<item type="id" name="menu_contextual_profile" />
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@
<string name="unsave">Unsave</string>
<string name="re_enter_password">Re-enter your password</string>
<string name="vote_up">Vote up</string>
<string name="vote_down">Vote down</string>
<string name="voted">Successfully voted</string>
<string name="vote_failed">Fail to record vote. Please try again.</string>
<string name="sending" tools:ignore="TypographyEllipsis">Sending...</string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ public void testVoteItem() {
PopupMenu popupMenu = ShadowPopupMenu.getLatestPopupMenu();
Assert.assertNotNull(popupMenu);
shadowOf(popupMenu).getOnMenuItemClickListener()
.onMenuItemClick(new RoboMenuItem(R.id.menu_contextual_vote));
.onMenuItemClick(new RoboMenuItem(R.id.menu_contextual_vote_up));
verify(userServices).voteUp(any(Context.class), any(), userServicesCallback.capture());
userServicesCallback.getValue().onDone(true);
assertEquals(activity.getString(R.string.voted), ShadowToast.getTextOfLatestToast());
Expand All @@ -315,7 +315,7 @@ public void testVoteItemPromptToLogin() {
PopupMenu popupMenu = ShadowPopupMenu.getLatestPopupMenu();
Assert.assertNotNull(popupMenu);
shadowOf(popupMenu).getOnMenuItemClickListener()
.onMenuItemClick(new RoboMenuItem(R.id.menu_contextual_vote));
.onMenuItemClick(new RoboMenuItem(R.id.menu_contextual_vote_up));
verify(userServices).voteUp(any(Context.class), any(), userServicesCallback.capture());
userServicesCallback.getValue().onDone(false);
assertThat(shadowOf(activity).getNextStartedActivity())
Expand All @@ -329,7 +329,7 @@ public void testVoteItemFailed() {
PopupMenu popupMenu = ShadowPopupMenu.getLatestPopupMenu();
Assert.assertNotNull(popupMenu);
shadowOf(popupMenu).getOnMenuItemClickListener()
.onMenuItemClick(new RoboMenuItem(R.id.menu_contextual_vote));
.onMenuItemClick(new RoboMenuItem(R.id.menu_contextual_vote_up));
verify(userServices).voteUp(any(Context.class), any(), userServicesCallback.capture());
userServicesCallback.getValue().onError(new IOException());
assertEquals(activity.getString(R.string.vote_failed), ShadowToast.getTextOfLatestToast());
Expand Down