Skip to content

Commit

Permalink
Merge pull request #574 from hidroh/vote
Browse files Browse the repository at this point in the history
Skip vote animation if not login
  • Loading branch information
hidroh authored Jul 24, 2016
2 parents bf60c8b + 9ef26b6 commit 3295058
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public Exception(int message) {

void login(String username, String password, boolean createAccount, Callback callback);

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,17 +92,17 @@ public void login(String username, String password, boolean createAccount, Callb
}

@Override
public void voteUp(Context context, String itemId, Callback callback) {
public boolean voteUp(Context context, String itemId, Callback callback) {
Pair<String, String> credentials = AppUtils.getCredentials(context);
if (credentials == null) {
callback.onDone(false);
return;
return false;
}
Toast.makeText(context, R.string.sending, Toast.LENGTH_SHORT).show();
execute(postVote(credentials.first, credentials.second, itemId))
.map(response -> response.code() == HttpURLConnection.HTTP_MOVED_TEMP)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(callback::onDone, callback::onError);
return true;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -412,8 +412,10 @@ private void toggleSave(final Item story) {
}

private void vote(final Item story, final RecyclerView.ViewHolder holder) {
mUserServices.voteUp(mContext, story.getId(),
new VoteCallback(this, holder.getAdapterPosition(), story));
if (!mUserServices.voteUp(mContext, story.getId(),
new VoteCallback(this, holder.getAdapterPosition(), story))) {
AppUtils.showLogin(mContext, mAlertDialogBuilder);
}
}

private void onVoted(int position, Boolean successful) {
Expand All @@ -424,8 +426,6 @@ private void onVoted(int position, Boolean successful) {
if (position < getItemCount()) {
notifyItemChanged(position);
}
} else {
AppUtils.showLogin(mContext, mAlertDialogBuilder);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import okhttp3.ResponseBody;
import rx.schedulers.Schedulers;

import static junit.framework.Assert.assertFalse;
import static junit.framework.Assert.assertTrue;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import static org.mockito.Matchers.isA;
Expand Down Expand Up @@ -84,7 +86,7 @@ public void testVoteSuccess() throws IOException {
when(call.execute()).thenReturn(responseBuilder
.code(HttpURLConnection.HTTP_MOVED_TEMP).build());
UserServices.Callback callback = mock(UserServices.Callback.class);
userServices.voteUp(RuntimeEnvironment.application, "1", callback);
assertTrue(userServices.voteUp(RuntimeEnvironment.application, "1", callback));
verify(callback).onDone(eq(true));
}

Expand All @@ -93,25 +95,24 @@ public void testVoteFailed() throws IOException {
when(call.execute()).thenReturn(responseBuilder
.code(HttpURLConnection.HTTP_OK).build());
UserServices.Callback callback = mock(UserServices.Callback.class);
userServices.voteUp(RuntimeEnvironment.application, "1", callback);
assertTrue(userServices.voteUp(RuntimeEnvironment.application, "1", callback));
verify(callback).onDone(eq(false));
}

@Test
public void testVoteError() throws IOException {
when(call.execute()).thenThrow(new IOException());
UserServices.Callback callback = mock(UserServices.Callback.class);
userServices.voteUp(RuntimeEnvironment.application, "1", callback);
assertTrue(userServices.voteUp(RuntimeEnvironment.application, "1", callback));
verify(callback).onError(any(Throwable.class));
}

@Test
public void testVoteNoMatchingAccount() throws IOException {
Preferences.setUsername(RuntimeEnvironment.application, "another");
UserServices.Callback callback = mock(UserServices.Callback.class);
userServices.voteUp(RuntimeEnvironment.application, "1", callback);
assertFalse(userServices.voteUp(RuntimeEnvironment.application, "1", callback));
verify(call, never()).enqueue(any(Callback.class));
verify(callback).onDone(eq(false));
}

@Test
Expand All @@ -137,9 +138,8 @@ public void testVoteNoAccount() throws IOException {
ShadowAccountManager.get(RuntimeEnvironment.application)
.removeAccount(account, null, null);
UserServices.Callback callback = mock(UserServices.Callback.class);
userServices.voteUp(RuntimeEnvironment.application, "1", callback);
assertFalse(userServices.voteUp(RuntimeEnvironment.application, "1", callback));
verify(call, never()).enqueue(any(Callback.class));
verify(callback).onDone(eq(false));
}

@Test
Expand Down

0 comments on commit 3295058

Please sign in to comment.