in app/src/main/java/com/amazonaws/postsapp/PostsAdapter.java [329:379]
private void upVotePost(final View view, final int position, final ListPostsQuery.ListPost p) {
//Execute Mutation
final UpdatePostMutation updatePostMutation = UpdatePostMutation.builder()
.input(UpdatePostInput.builder()
.id(p.id())
.title(p.title())
.author(p.author())
.content(p.content())
.ups(p.ups() + 1)
.downs(p.downs())
.build())
.build();
ClientFactory.getInstance(view.getContext()).mutate(updatePostMutation).enqueue(new GraphQLCall.Callback<UpdatePostMutation.Data>() {
@Override
public void onResponse(@Nonnull final Response<UpdatePostMutation.Data> response) {
Log.d(TAG, "Up voted successfully");
display.runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(display, "Up Voted!", Toast.LENGTH_SHORT).show();
}
});
}
@Override
public void onFailure(@Nonnull final ApolloException e) {
display.runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(display, "Failed to upvote post!", Toast.LENGTH_SHORT).show();
}
});
Log.e("", e.getMessage());
}
});
//Update listPostsQuery cache for optimistic UI
display.runOnUiThread(new Runnable() {
@Override
public void run() {
allPosts.put(p.id(), new ListPostsQuery.ListPost("Post",
p.id(), p.author(), p.title(), p.content(),
p.url(), p.ups() + 1, p.downs(), p.createdDate(),
p.aws_ds()));
updateListPostsQueryCache();
notifyDataSetChanged();
}
});
}