Skip to content

Commit

Permalink
Removed UI handling UpVotes for Stories and Comments
Browse files Browse the repository at this point in the history
  • Loading branch information
pfmaggi authored and florina-muntenescu committed Jun 20, 2019
1 parent 315d313 commit 45a14f0
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 92 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,6 @@

public class StoryActivity extends AppCompatActivity {

private static final int RC_LOGIN_UPVOTE = 7;

private View header;
private RecyclerView commentsList;
private LinearLayoutManager layoutManager;
Expand All @@ -111,7 +109,6 @@ public class StoryActivity extends AppCompatActivity {
private PinnedOffsetView toolbarBackground;
@Nullable
private View background;
private TextView upvoteStory;
private EditText enterComment;
private ImageButton postComment;
private int fabExpandDuration;
Expand Down Expand Up @@ -253,18 +250,6 @@ protected void onResume() {
draggableFrame.addListener(chromeFader);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case RC_LOGIN_UPVOTE:
if (resultCode == RESULT_OK) {
upvoteStory();
}
break;
}
}

@Override
protected void onPause() {
draggableFrame.removeListener(chromeFader);
Expand Down Expand Up @@ -488,9 +473,8 @@ private void bindDescription() {
storyComment.setVisibility(View.GONE);
}

upvoteStory = header.findViewById(R.id.story_vote_action);
storyUpvoted(story.getVoteCount());
upvoteStory.setOnClickListener(v -> upvoteStory());
TextView upvoteStory = header.findViewById(R.id.story_vote_action);
setVoteCountText(upvoteStory, story.getVoteCount());

final TextView share = header.findViewById(R.id.story_share_action);
share.setOnClickListener(v -> {
Expand Down Expand Up @@ -521,6 +505,12 @@ private void bindDescription() {
}
}

private void setVoteCountText(TextView upvoteStory, Integer voteCount) {
upvoteStory.setText(getResources().getQuantityString(
io.plaidapp.R.plurals.upvotes, voteCount,
NumberFormat.getInstance().format(voteCount)));
}

private CharSequence getStoryPosterTimeText(String userDisplayName, String userJob, Date createdAt) {
SpannableString poster = new SpannableString(userDisplayName.toLowerCase());
poster.setSpan(new TextAppearanceSpan(this, io.plaidapp.R.style
Expand Down Expand Up @@ -584,38 +574,6 @@ private void commentAdded(Comment comment) {
commentsAdapter.addComment(comment);
}

private void upvoteStory() {
if (loginRepository.isLoggedIn()) {
if (!upvoteStory.isActivated()) {
upvoteStory.setActivated(true);
viewModel.storyUpvoteRequested(story.getId(),
it -> {
if (it instanceof Result.Success) {
storyUpvoted(story.getVoteCount() + 1);
} else {
Toast.makeText(this, "Unable to upvote story", Toast.LENGTH_LONG)
.show();
upvoteStory.setActivated(false);
}
return Unit.INSTANCE;
});

} else {
upvoteStory.setActivated(false);
// TODO delete upvote. Not available in v1 API.
}

} else {
needsLogin(upvoteStory, RC_LOGIN_UPVOTE);
}
}

private void storyUpvoted(int newUpvoteCount) {
upvoteStory.setText(getResources().getQuantityString(
io.plaidapp.R.plurals.upvotes, newUpvoteCount,
NumberFormat.getInstance().format(newUpvoteCount)));
}

private void needsLogin(View triggeringView, int requestCode) {
Intent login = new Intent(StoryActivity.this,
LoginActivity.class);
Expand Down Expand Up @@ -892,52 +850,11 @@ private void replyToComment(Long commentId, String reply) {
});
}

private void handleCommentVotesClick(CommentReplyViewHolder holder,
boolean isUserLoggedIn,
Comment comment) {
if (isUserLoggedIn) {
if (!holder.getCommentVotes().isActivated()) {
viewModel.commentUpvoteRequested(story.getId(),
result -> {
if (result instanceof Result.Success) {
comment.setUpvoted(true);
// TODO fix this
// comment.vote_count++;
holder.getCommentVotes().setText(String.valueOf(comment.getUpvotesCount()));
holder.getCommentVotes().setActivated(true);
} else {
Toast.makeText(StoryActivity.this, "Unable to upvote comment",
Toast.LENGTH_LONG)
.show();
}
return Unit.INSTANCE;
});

} else {
comment.setUpvoted(false);
// TODO fix this
// comment.setVoteCount(comment.getVoteCount() - 1);
holder.getCommentVotes().setText(String.valueOf(comment.getUpvotesCount()));
holder.getCommentVotes().setActivated(false);
// TODO actually delete upvote - florina: why?
}
} else {
needsLogin(holder.getCommentVotes(), 0);
}
holder.getCommentReply().clearFocus();
}


@NonNull
private CommentReplyViewHolder createCommentReplyHolder(ViewGroup parent) {
final CommentReplyViewHolder holder = new CommentReplyViewHolder(getLayoutInflater()
.inflate(R.layout.designer_news_comment_actions, parent, false));

holder.getCommentVotes().setOnClickListener(v -> {
Comment comment = getComment(holder.getAdapterPosition());
handleCommentVotesClick(holder, loginRepository.isLoggedIn(), comment);
});

holder.getPostReply().setOnClickListener(v -> {
if (loginRepository.isLoggedIn()) {
String reply = holder.getCommentReply().getText().toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@
android:layout_weight="2"
android:drawableTop="@drawable/asl_upvote"
tools:text="33 likes"
style="@style/Widget.Plaid.InlineActionButton" />
style="@style/Widget.Plaid.InlineActionButton"
android:background="@null" />

<Space
android:layout_width="0dp"
Expand Down

0 comments on commit 45a14f0

Please sign in to comment.