aboutsummaryrefslogtreecommitdiff
path: root/app/src
diff options
context:
space:
mode:
Diffstat (limited to 'app/src')
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/BestPostRecyclerViewAdapter.java8
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java2
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java180
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/VoteThing.java69
4 files changed, 243 insertions, 16 deletions
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/BestPostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/BestPostRecyclerViewAdapter.java
index 5978b407..d5d8614f 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/BestPostRecyclerViewAdapter.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/BestPostRecyclerViewAdapter.java
@@ -2,6 +2,7 @@ package ml.docilealligator.infinityforreddit;
import android.content.Context;
import android.content.Intent;
+import android.graphics.ColorFilter;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.support.annotation.NonNull;
@@ -299,6 +300,8 @@ class BestPostRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.View
@Override
public void onClick(View view) {
final boolean isDownvotedBefore = ((DataViewHolder) holder).downvoteButton.getColorFilter() != null;
+
+ final ColorFilter downvoteButtonColorFilter = ((DataViewHolder) holder).downvoteButton.getColorFilter();
((DataViewHolder) holder).downvoteButton.clearColorFilter();
if (((DataViewHolder) holder).upvoteButton.getColorFilter() == null) {
@@ -325,7 +328,7 @@ class BestPostRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.View
Toast.makeText(mContext, "Cannot upvote this post", Toast.LENGTH_SHORT).show();
((DataViewHolder) holder).upvoteButton.clearColorFilter();
((DataViewHolder) holder).scoreTextView.setText(Integer.toString(mBestPostData.get(position).getScore()));
- ((DataViewHolder) holder).downvoteButton.setColorFilter(ContextCompat.getColor(mContext, R.color.minusButtonColor), android.graphics.PorterDuff.Mode.SRC_IN);
+ ((DataViewHolder) holder).downvoteButton.setColorFilter(downvoteButtonColorFilter);
}
}, id, RedditUtils.DIR_UPVOTE, ((DataViewHolder) holder).getAdapterPosition(), 1);
} else {
@@ -357,6 +360,7 @@ class BestPostRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.View
public void onClick(View view) {
final boolean isUpvotedBefore = ((DataViewHolder) holder).upvoteButton.getColorFilter() != null;
+ final ColorFilter upvoteButtonColorFilter = ((DataViewHolder) holder).upvoteButton.getColorFilter();
((DataViewHolder) holder).upvoteButton.clearColorFilter();
if (((DataViewHolder) holder).downvoteButton.getColorFilter() == null) {
((DataViewHolder) holder).downvoteButton.setColorFilter(ContextCompat.getColor(mContext, R.color.minusButtonColor), android.graphics.PorterDuff.Mode.SRC_IN);
@@ -382,7 +386,7 @@ class BestPostRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.View
Toast.makeText(mContext, "Cannot downvote this post", Toast.LENGTH_SHORT).show();
((DataViewHolder) holder).downvoteButton.clearColorFilter();
((DataViewHolder) holder).scoreTextView.setText(Integer.toString(mBestPostData.get(position).getScore()));
- ((DataViewHolder) holder).upvoteButton.setColorFilter(ContextCompat.getColor(mContext, R.color.colorPrimary), android.graphics.PorterDuff.Mode.SRC_IN);
+ ((DataViewHolder) holder).upvoteButton.setColorFilter(upvoteButtonColorFilter);
}
}, id, RedditUtils.DIR_DOWNVOTE, holder.getAdapterPosition(), 1);
} else {
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java
index 8ca3fbc8..ca31c815 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java
@@ -224,7 +224,7 @@ public class MainActivity extends AppCompatActivity {
}, 1);
}
- if(mInsertSuccess) {
+ if(!mInsertSuccess) {
new FetchSubscribedThing(this, Volley.newRequestQueue(this), new ArrayList<SubscribedSubredditData>(),
new ArrayList<SubscribedUserData>(), new ArrayList<SubredditData>())
.fetchSubscribedSubreddits(new FetchSubscribedThing.FetchSubscribedSubredditsListener() {
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java
index 47a0ef7f..050db26c 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java
@@ -1,6 +1,7 @@
package ml.docilealligator.infinityforreddit;
import android.content.Intent;
+import android.graphics.ColorFilter;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
@@ -8,6 +9,7 @@ import android.support.annotation.Nullable;
import android.support.customtabs.CustomTabsIntent;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.Snackbar;
+import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.CardView;
import android.support.v7.widget.DividerItemDecoration;
@@ -20,6 +22,7 @@ import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
+import android.widget.Toast;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;
@@ -52,7 +55,7 @@ public class ViewPostDetailActivity extends AppCompatActivity {
private LinearLayout mNoCommentWrapperLinearLayout;
private ImageView mNoCommentImageView;
- private RequestQueue mVoteThingQueue;
+ private RequestQueue mVoteThingRequestQueue;
private RequestQueue mAcquireAccessTokenRequestQueue;
private RequestQueue mCommentQueue;
@@ -82,9 +85,9 @@ public class ViewPostDetailActivity extends AppCompatActivity {
ImageView imageView = findViewById(R.id.image_view_view_post_detail);
ImageView noPreviewLinkImageView = findViewById(R.id.image_view_no_preview_link_view_post_detail);
- ImageView plusButton = findViewById(R.id.plus_button_view_post_detail);
- TextView scoreTextView = findViewById(R.id.score_text_view_view_post_detail);
- ImageView minusButton = findViewById(R.id.minus_button_view_post_detail);
+ final ImageView upvoteButton = findViewById(R.id.plus_button_view_post_detail);
+ final TextView scoreTextView = findViewById(R.id.score_text_view_view_post_detail);
+ final ImageView downvoteButton = findViewById(R.id.minus_button_view_post_detail);
ImageView shareButton = findViewById(R.id.share_button_view_post_detail);
mCommentProgressbar = findViewById(R.id.comment_progress_bar_view_post_detail);
@@ -104,10 +107,21 @@ public class ViewPostDetailActivity extends AppCompatActivity {
Glide.with(this).load(R.drawable.subreddit_default_icon).into(subredditIconCircleImageView);
}
+ switch (mPostData.getVoteType()) {
+ case 1:
+ //Upvote
+ upvoteButton.setColorFilter(ContextCompat.getColor(this, R.color.colorPrimary), android.graphics.PorterDuff.Mode.SRC_IN);
+ break;
+ case -1:
+ //Downvote
+ downvoteButton.setColorFilter(ContextCompat.getColor(this, R.color.minusButtonColor), android.graphics.PorterDuff.Mode.SRC_IN);
+ break;
+ }
+
mRecyclerView.setNestedScrollingEnabled(false);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mRecyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
- mVoteThingQueue = Volley.newRequestQueue(this);
+ mVoteThingRequestQueue = Volley.newRequestQueue(this);
mAcquireAccessTokenRequestQueue = Volley.newRequestQueue(this);
mCommentQueue = Volley.newRequestQueue(this);
@@ -284,6 +298,160 @@ public class ViewPostDetailActivity extends AppCompatActivity {
}
}
queryComment();
+
+ /*final Observable<Integer> observable = Observable.create(
+ new ObservableOnSubscribe<Integer>() {
+ @Override
+ public void subscribe(ObservableEmitter<Integer> emitter) throws Exception {
+ emitter.onNext(mPostData.getVoteType());
+ emitter.onComplete();
+ Log.i("asdasdf", "adasdfasdf");
+ Toast.makeText(ViewPostDetailActivity.this, "observable", Toast.LENGTH_SHORT).show();
+ }
+ }
+ );
+
+ final Observer observer = new Observer() {
+ @Override
+ public void onSubscribe(Disposable d) {
+
+ }
+
+ @Override
+ public void onNext(Object o) {
+
+ }
+
+ @Override
+ public void onError(Throwable e) {
+
+ }
+
+ @Override
+ public void onComplete() {
+ Toast.makeText(ViewPostDetailActivity.this, "complete", Toast.LENGTH_SHORT).show();
+ }
+ };*/
+
+ upvoteButton.setOnClickListener(new View.OnClickListener() {
+ @Override
+ public void onClick(View view) {
+ //observable.subscribe(observer);
+ final boolean isDownvotedBefore = downvoteButton.getColorFilter() != null;
+
+ final ColorFilter downVoteButtonColorFilter = downvoteButton.getColorFilter();
+ downvoteButton.clearColorFilter();
+
+ if (upvoteButton.getColorFilter() == null) {
+ upvoteButton.setColorFilter(ContextCompat.getColor(ViewPostDetailActivity.this, R.color.colorPrimary), android.graphics.PorterDuff.Mode.SRC_IN);
+ if(isDownvotedBefore) {
+ scoreTextView.setText(Integer.toString(mPostData.getScore() + 2));
+ } else {
+ scoreTextView.setText(Integer.toString(mPostData.getScore() + 1));
+ }
+
+ new VoteThing(ViewPostDetailActivity.this, mVoteThingRequestQueue, mAcquireAccessTokenRequestQueue).votePost(new VoteThing.VoteThingWithoutPositionListener() {
+ @Override
+ public void onVoteThingSuccess() {
+ mPostData.setVoteType(1);
+ if(isDownvotedBefore) {
+ mPostData.setScore(mPostData.getScore() + 2);
+ } else {
+ mPostData.setScore(mPostData.getScore() + 1);
+ }
+ }
+
+ @Override
+ public void onVoteThingFail() {
+ Toast.makeText(ViewPostDetailActivity.this, "Cannot upvote this post", Toast.LENGTH_SHORT).show();
+ upvoteButton.clearColorFilter();
+ scoreTextView.setText(Integer.toString(mPostData.getScore()));
+ downvoteButton.setColorFilter(downVoteButtonColorFilter);
+ }
+ }, mPostData.getFullName(), RedditUtils.DIR_UPVOTE, 1);
+ } else {
+ //Upvoted before
+ upvoteButton.clearColorFilter();
+ scoreTextView.setText(Integer.toString(mPostData.getScore() - 1));
+
+ new VoteThing(ViewPostDetailActivity.this, mVoteThingRequestQueue, mAcquireAccessTokenRequestQueue).votePost(new VoteThing.VoteThingWithoutPositionListener() {
+ @Override
+ public void onVoteThingSuccess() {
+ mPostData.setVoteType(0);
+ mPostData.setScore(mPostData.getScore() - 1);
+ }
+
+ @Override
+ public void onVoteThingFail() {
+ Toast.makeText(ViewPostDetailActivity.this, "Cannot unvote this post", Toast.LENGTH_SHORT).show();
+ scoreTextView.setText(Integer.toString(mPostData.getScore() + 1));
+ upvoteButton.setColorFilter(ContextCompat.getColor(ViewPostDetailActivity.this, R.color.colorPrimary), android.graphics.PorterDuff.Mode.SRC_IN);
+ mPostData.setScore(mPostData.getScore() + 1);
+ }
+ }, mPostData.getFullName(), RedditUtils.DIR_UNVOTE, 1);
+ }
+ }
+ });
+
+ downvoteButton.setOnClickListener(new View.OnClickListener() {
+ @Override
+ public void onClick(View view) {
+ //observable.subscribe(observer);
+ final boolean isUpvotedBefore = upvoteButton.getColorFilter() != null;
+
+ final ColorFilter upvoteButtonColorFilter = upvoteButton.getColorFilter();
+ upvoteButton.clearColorFilter();
+
+ if (downvoteButton.getColorFilter() == null) {
+ downvoteButton.setColorFilter(ContextCompat.getColor(ViewPostDetailActivity.this, R.color.minusButtonColor), android.graphics.PorterDuff.Mode.SRC_IN);
+ if (isUpvotedBefore) {
+ scoreTextView.setText(Integer.toString(mPostData.getScore() - 2));
+ } else {
+ scoreTextView.setText(Integer.toString(mPostData.getScore() - 1));
+ }
+
+ new VoteThing(ViewPostDetailActivity.this, mVoteThingRequestQueue, mAcquireAccessTokenRequestQueue).votePost(new VoteThing.VoteThingWithoutPositionListener() {
+ @Override
+ public void onVoteThingSuccess() {
+ mPostData.setVoteType(-1);
+ if(isUpvotedBefore) {
+ mPostData.setScore(mPostData.getScore() - 2);
+ } else {
+ mPostData.setScore(mPostData.getScore() - 1);
+ }
+ }
+
+ @Override
+ public void onVoteThingFail() {
+ Toast.makeText(ViewPostDetailActivity.this, "Cannot downvote this post", Toast.LENGTH_SHORT).show();
+ downvoteButton.clearColorFilter();
+ scoreTextView.setText(Integer.toString(mPostData.getScore()));
+ upvoteButton.setColorFilter(upvoteButtonColorFilter);
+ }
+ }, mPostData.getFullName(), RedditUtils.DIR_DOWNVOTE, 1);
+ } else {
+ //Down voted before
+ downvoteButton.clearColorFilter();
+ scoreTextView.setText(Integer.toString(mPostData.getScore() + 1));
+
+ new VoteThing(ViewPostDetailActivity.this, mVoteThingRequestQueue, mAcquireAccessTokenRequestQueue).votePost(new VoteThing.VoteThingWithoutPositionListener() {
+ @Override
+ public void onVoteThingSuccess() {
+ mPostData.setVoteType(0);
+ mPostData.setScore(mPostData.getScore());
+ }
+
+ @Override
+ public void onVoteThingFail() {
+ Toast.makeText(ViewPostDetailActivity.this, "Cannot unvote this post", Toast.LENGTH_SHORT).show();
+ downvoteButton.setColorFilter(ContextCompat.getColor(ViewPostDetailActivity.this, R.color.minusButtonColor), android.graphics.PorterDuff.Mode.SRC_IN);
+ scoreTextView.setText(Integer.toString(mPostData.getScore()));
+ mPostData.setScore(mPostData.getScore());
+ }
+ }, mPostData.getFullName(), RedditUtils.DIR_UNVOTE, 1);
+ }
+ }
+ });
}
private void queryComment() {
@@ -299,7 +467,7 @@ public class ViewPostDetailActivity extends AppCompatActivity {
mMoreCommentCount = moreCommentCount;
if(commentData.size() > 0) {
CommentRecyclerViewAdapter adapter = new CommentRecyclerViewAdapter(
- ViewPostDetailActivity.this, commentData, mVoteThingQueue, mAcquireAccessTokenRequestQueue);
+ ViewPostDetailActivity.this, commentData, mVoteThingRequestQueue, mAcquireAccessTokenRequestQueue);
mRecyclerView.setAdapter(adapter);
mCommentCardView.setVisibility(View.VISIBLE);
} else {
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/VoteThing.java b/app/src/main/java/ml/docilealligator/infinityforreddit/VoteThing.java
index c6807134..0b6ef4e1 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/VoteThing.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/VoteThing.java
@@ -23,8 +23,12 @@ class VoteThing {
void onVoteThingFail(int position);
}
+ interface VoteThingWithoutPositionListener {
+ void onVoteThingSuccess();
+ void onVoteThingFail();
+ }
+
private Context mContext;
- private VoteThingListener mVoteThingListener;
private RequestQueue mQueue;
private RequestQueue mAcquireAccessTokenRequestQueue;
@@ -34,17 +38,68 @@ class VoteThing {
mAcquireAccessTokenRequestQueue = acquireAccessTokenRequestQueue;
}
- void votePost(VoteThingListener voteThingListener, final String fullName, final String point, final int position, final int refreshTime) {
+ void votePost(final VoteThingListener voteThingListener, final String fullName, final String point, final int position, final int refreshTime) {
+ if(mContext != null) {
+ if(refreshTime < 0) {
+ voteThingListener.onVoteThingFail(position);
+ return;
+ }
+
+ StringRequest voteRequest = new StringRequest(Request.Method.POST, RedditUtils.OAUTH_API_BASE_URI + RedditUtils.VOTE_SUFFIX, new Response.Listener<String>() {
+ @Override
+ public void onResponse(String response) {
+ voteThingListener.onVoteThingSuccess(position);
+ }
+ }, new Response.ErrorListener() {
+ @Override
+ public void onErrorResponse(VolleyError error) {
+ if (error instanceof AuthFailureError) {
+ //Access token expired
+ new AcquireAccessToken(mContext).refreshAccessToken(mAcquireAccessTokenRequestQueue,
+ new AcquireAccessToken.AcquireAccessTokenListener() {
+ @Override
+ public void onAcquireAccessTokenSuccess() {
+ votePost(voteThingListener, fullName, point, position, refreshTime - 1);
+ }
+
+ @Override
+ public void onAcquireAccessTokenFail() {}
+ });
+ } else {
+ voteThingListener.onVoteThingFail(position);
+ }
+ }
+ }) {
+ @Override
+ protected Map<String, String> getParams() {
+ HashMap<String, String> params = new HashMap<>();
+ params.put(RedditUtils.DIR_KEY, point);
+ params.put(RedditUtils.ID_KEY, fullName);
+ params.put(RedditUtils.RANK_KEY, RedditUtils.RANK);
+ return params;
+ }
+
+ @Override
+ public Map<String, String> getHeaders() {
+ String accessToken = mContext.getSharedPreferences(SharedPreferencesUtils.AUTH_CODE_FILE_KEY, Context.MODE_PRIVATE).getString(SharedPreferencesUtils.ACCESS_TOKEN_KEY, "");
+ return RedditUtils.getOAuthHeader(accessToken);
+ }
+ };
+ voteRequest.setTag(VoteThing.class);
+ mQueue.add(voteRequest);
+ }
+ }
+
+ void votePost(final VoteThingWithoutPositionListener voteThingWithoutPositionListener, final String fullName, final String point, final int refreshTime) {
if(mContext != null) {
if(refreshTime < 0) {
- mVoteThingListener.onVoteThingFail(position);
+ voteThingWithoutPositionListener.onVoteThingFail();
return;
}
- mVoteThingListener = voteThingListener;
StringRequest voteRequest = new StringRequest(Request.Method.POST, RedditUtils.OAUTH_API_BASE_URI + RedditUtils.VOTE_SUFFIX, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
- mVoteThingListener.onVoteThingSuccess(position);
+ voteThingWithoutPositionListener.onVoteThingSuccess();
}
}, new Response.ErrorListener() {
@Override
@@ -55,14 +110,14 @@ class VoteThing {
new AcquireAccessToken.AcquireAccessTokenListener() {
@Override
public void onAcquireAccessTokenSuccess() {
- votePost(mVoteThingListener, fullName, point, position, refreshTime - 1);
+ votePost(voteThingWithoutPositionListener, fullName, point, refreshTime - 1);
}
@Override
public void onAcquireAccessTokenFail() {}
});
} else {
- mVoteThingListener.onVoteThingFail(position);
+ voteThingWithoutPositionListener.onVoteThingFail();
}
}
}) {