From 6872faa93dabeb37d5899dea7126c85976383388 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Tue, 13 Aug 2019 15:29:27 +0800 Subject: [PATCH] Set NSFW and spoiler are now available. --- app/src/main/AndroidManifest.xml | 2 + .../infinityforreddit/Post.java | 12 ++ .../infinityforreddit/PostFragment.java | 3 + .../infinityforreddit/RedditAPI.java | 16 ++ .../ViewPostDetailActivity.java | 142 +++++++++++++++++- app/src/main/res/layout/item_comment.xml | 80 ---------- .../res/menu/view_post_detail_activity.xml | 14 +- app/src/main/res/values/strings.xml | 17 ++- 8 files changed, 200 insertions(+), 86 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 3679f066..8f7d4e15 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -22,11 +22,13 @@ android:usesCleartextTraffic="true"> diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Post.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Post.java index 69e37c31..0e23d359 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Post.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Post.java @@ -252,6 +252,10 @@ class Post implements Parcelable { return flair; } + void setFlair(String flair) { + this.flair = flair; + } + void setScore(int score) { this.score = score; } @@ -296,10 +300,18 @@ class Post implements Parcelable { return spoiler; } + void setSpoiler(boolean spoiler) { + this.spoiler = spoiler; + } + boolean isNSFW() { return nsfw; } + void setNSFW(boolean nsfw) { + this.nsfw = nsfw; + } + @Override public int describeContents() { return 0; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java index 851d351a..cc1503ae 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java @@ -423,6 +423,9 @@ public class PostFragment extends Fragment implements FragmentCommunicator { post.setTitle(event.post.getTitle()); post.setVoteType(event.post.getVoteType()); post.setScore(event.post.getScore()); + post.setNSFW(event.post.isNSFW()); + post.setSpoiler(event.post.isSpoiler()); + post.setFlair(event.post.getFlair()); mAdapter.notifyItemChanged(event.positionInList); } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java b/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java index 0412b99e..4deb3abd 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java @@ -131,4 +131,20 @@ public interface RedditAPI { @FormUrlEncoded @POST("/api/editusertext") Call editPostOrComment(@HeaderMap Map headers, @FieldMap Map params); + + @FormUrlEncoded + @POST("/api/marknsfw") + Call markNSFW(@HeaderMap Map headers, @FieldMap Map params); + + @FormUrlEncoded + @POST("/api/unmarknsfw") + Call unmarkNSFW(@HeaderMap Map headers, @FieldMap Map params); + + @FormUrlEncoded + @POST("/api/spoiler") + Call markSpoiler(@HeaderMap Map headers, @FieldMap Map params); + + @FormUrlEncoded + @POST("/api/unspoiler") + Call unmarkSpoiler(@HeaderMap Map headers, @FieldMap Map params); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java index 797c1834..964e1809 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java @@ -37,7 +37,9 @@ import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.Subscribe; import java.util.ArrayList; +import java.util.HashMap; import java.util.Locale; +import java.util.Map; import javax.inject.Inject; import javax.inject.Named; @@ -223,6 +225,22 @@ public class ViewPostDetailActivity extends AppCompatActivity { mMenu.findItem(R.id.action_edit_view_post_detail_activity).setVisible(true); } mMenu.findItem(R.id.action_delete_view_post_detail_activity).setVisible(true); + + MenuItem nsfwItem = mMenu.findItem(R.id.action_nsfw_view_post_detail_activity); + nsfwItem.setVisible(true); + if(mPost.isNSFW()) { + nsfwItem.setTitle(R.string.action_unmark_nsfw); + } else { + nsfwItem.setTitle(R.string.action_mark_nsfw); + } + + MenuItem spoilerItem = mMenu.findItem(R.id.action_spoiler_view_post_detail_activity); + spoilerItem.setVisible(true); + if(mPost.isSpoiler()) { + spoilerItem.setTitle(R.string.action_unmark_spoiler); + } else { + spoilerItem.setTitle(R.string.action_mark_spoiler); + } } mAdapter = new CommentAndPostRecyclerViewAdapter(ViewPostDetailActivity.this, mRetrofit, mOauthRetrofit, mRedditDataRoomDatabase, mGlide, mAccessToken, mAccountName, mPost, @@ -459,7 +477,7 @@ public class ViewPostDetailActivity extends AppCompatActivity { @Override public void fetchPostFailed() { - showErrorMessage(R.string.refresh_post_failed); + showMessage(R.string.refresh_post_failed); isRefreshing = false; } }); @@ -474,7 +492,7 @@ public class ViewPostDetailActivity extends AppCompatActivity { mGlide.load(R.drawable.load_post_error_indicator).into(mFetchPostInfoImageView); } - private void showErrorMessage(int resId) { + private void showMessage(int resId) { if(showToast) { Toast.makeText(ViewPostDetailActivity.this, resId, Toast.LENGTH_SHORT).show(); } else { @@ -482,6 +500,94 @@ public class ViewPostDetailActivity extends AppCompatActivity { } } + private void markNSFW() { + Map params = new HashMap<>(); + params.put(RedditUtils.ID_KEY, mPost.getFullName()); + mOauthRetrofit.create(RedditAPI.class).markNSFW(RedditUtils.getOAuthHeader(mAccessToken), params) + .enqueue(new Callback() { + @Override + public void onResponse(@NonNull Call call, @NonNull Response response) { + if(response.isSuccessful()) { + refresh(true); + showMessage(R.string.mark_nsfw_success); + } else { + showMessage(R.string.mark_nsfw_failed); + } + } + + @Override + public void onFailure(@NonNull Call call, @NonNull Throwable t) { + showMessage(R.string.mark_nsfw_failed); + } + }); + } + + private void unmarkNSFW() { + Map params = new HashMap<>(); + params.put(RedditUtils.ID_KEY, mPost.getFullName()); + mOauthRetrofit.create(RedditAPI.class).unmarkNSFW(RedditUtils.getOAuthHeader(mAccessToken), params) + .enqueue(new Callback() { + @Override + public void onResponse(@NonNull Call call, @NonNull Response response) { + if(response.isSuccessful()) { + refresh(true); + showMessage(R.string.unmark_nsfw_success); + } else { + showMessage(R.string.unmark_nsfw_failed); + } + } + + @Override + public void onFailure(@NonNull Call call, @NonNull Throwable t) { + showMessage(R.string.unmark_nsfw_failed); + } + }); + } + + private void markSpoiler() { + Map params = new HashMap<>(); + params.put(RedditUtils.ID_KEY, mPost.getFullName()); + mOauthRetrofit.create(RedditAPI.class).markSpoiler(RedditUtils.getOAuthHeader(mAccessToken), params) + .enqueue(new Callback() { + @Override + public void onResponse(@NonNull Call call, @NonNull Response response) { + if(response.isSuccessful()) { + refresh(true); + showMessage(R.string.mark_spoiler_success); + } else { + showMessage(R.string.mark_spoiler_failed); + } + } + + @Override + public void onFailure(@NonNull Call call, @NonNull Throwable t) { + showMessage(R.string.mark_spoiler_failed); + } + }); + } + + private void unmarkSpoiler() { + Map params = new HashMap<>(); + params.put(RedditUtils.ID_KEY, mPost.getFullName()); + mOauthRetrofit.create(RedditAPI.class).unmarkSpoiler(RedditUtils.getOAuthHeader(mAccessToken), params) + .enqueue(new Callback() { + @Override + public void onResponse(@NonNull Call call, @NonNull Response response) { + if(response.isSuccessful()) { + refresh(true); + showMessage(R.string.unmark_spoiler_success); + } else { + showMessage(R.string.unmark_spoiler_failed); + } + } + + @Override + public void onFailure(@NonNull Call call, @NonNull Throwable t) { + showMessage(R.string.unmark_spoiler_failed); + } + }); + } + void deleteComment(String fullName, int position) { new MaterialAlertDialogBuilder(this, R.style.MaterialAlertDialogTheme) .setTitle(R.string.delete_this_comment) @@ -520,6 +626,22 @@ public class ViewPostDetailActivity extends AppCompatActivity { menu.findItem(R.id.action_edit_view_post_detail_activity).setVisible(true); } menu.findItem(R.id.action_delete_view_post_detail_activity).setVisible(true); + + MenuItem nsfwItem = menu.findItem(R.id.action_nsfw_view_post_detail_activity); + nsfwItem.setVisible(true); + if(mPost.isNSFW()) { + nsfwItem.setTitle(R.string.action_unmark_nsfw); + } else { + nsfwItem.setTitle(R.string.action_mark_nsfw); + } + + MenuItem spoilerItem = menu.findItem(R.id.action_spoiler_view_post_detail_activity); + spoilerItem.setVisible(true); + if(mPost.isSpoiler()) { + spoilerItem.setTitle(R.string.action_unmark_spoiler); + } else { + spoilerItem.setTitle(R.string.action_mark_spoiler); + } } return true; } @@ -565,12 +687,26 @@ public class ViewPostDetailActivity extends AppCompatActivity { @Override public void deleteFailed() { - showErrorMessage(R.string.delete_post_failed); + showMessage(R.string.delete_post_failed); } })) .setNegativeButton(R.string.cancel, null) .show(); return true; + case R.id.action_nsfw_view_post_detail_activity: + if(mPost.isNSFW()) { + unmarkNSFW(); + } else { + markNSFW(); + } + return true; + case R.id.action_spoiler_view_post_detail_activity: + if(mPost.isSpoiler()) { + unmarkSpoiler(); + } else { + markSpoiler(); + } + return true; case android.R.id.home: onBackPressed(); return true; diff --git a/app/src/main/res/layout/item_comment.xml b/app/src/main/res/layout/item_comment.xml index 1283aa5b..53613bc8 100644 --- a/app/src/main/res/layout/item_comment.xml +++ b/app/src/main/res/layout/item_comment.xml @@ -155,86 +155,6 @@ - - \ No newline at end of file diff --git a/app/src/main/res/menu/view_post_detail_activity.xml b/app/src/main/res/menu/view_post_detail_activity.xml index 76e2db37..3df40201 100644 --- a/app/src/main/res/menu/view_post_detail_activity.xml +++ b/app/src/main/res/menu/view_post_detail_activity.xml @@ -23,8 +23,20 @@ + + + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 9b3dd695..77d23b4c 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -11,6 +11,8 @@ Video Post Rules Subreddits + Edit Post + Edit Comment Open navigation drawer Close navigation drawer @@ -26,6 +28,10 @@ Sort Edit Post Delete Post + Mark NSFW + Unmark NSFW + Mark Spoiler + Unmark Spoiler Error occurred when parsing the JSON response Error Retrieving the token @@ -187,8 +193,15 @@ Edit Delete Cancel + Edit successful Delete successfully Delete failed - - Edit Successful + Mark NSFW successful + Mark NSFW failed + Unmark NSFW successful + Unmark NSFW failed + Mark spoiler successful + Mark spoiler failed + Unmark spoiler successful + Unmark spoiler failed