From 4a1bed82d732b54608342805bc7c5ecaf36cc3b2 Mon Sep 17 00:00:00 2001 From: Hermes Junior Date: Fri, 12 Jun 2020 00:02:35 +0200 Subject: Implement option to see removed comment. --- .../infinityforreddit/API/PushshiftAPI.java | 10 +++ .../Activity/ViewPostDetailActivity.java | 29 ++++++- .../Adapter/CommentAndPostRecyclerViewAdapter.java | 6 +- .../infinityforreddit/AppModule.java | 10 +++ .../CommentMoreBottomSheetFragment.java | 14 ++++ .../infinityforreddit/FetchRemovedComment.java | 95 ++++++++++++++++++++++ .../infinityforreddit/Utils/APIUtils.java | 1 + 7 files changed, 160 insertions(+), 5 deletions(-) create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/API/PushshiftAPI.java create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/FetchRemovedComment.java (limited to 'app/src/main/java/ml') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/API/PushshiftAPI.java b/app/src/main/java/ml/docilealligator/infinityforreddit/API/PushshiftAPI.java new file mode 100644 index 00000000..66c92a02 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/API/PushshiftAPI.java @@ -0,0 +1,10 @@ +package ml.docilealligator.infinityforreddit.API; + +import retrofit2.Call; +import retrofit2.http.GET; +import retrofit2.http.Query; + +public interface PushshiftAPI { + @GET("reddit/comment/search/") + Call getRemovedComment(@Query("ids") String commentId); +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewPostDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewPostDetailActivity.java index 51a5beaa..bf01f564 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewPostDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewPostDetailActivity.java @@ -60,6 +60,8 @@ import ml.docilealligator.infinityforreddit.ActivityToolbarInterface; import ml.docilealligator.infinityforreddit.Adapter.CommentAndPostRecyclerViewAdapter; import ml.docilealligator.infinityforreddit.AsyncTask.GetCurrentAccountAsyncTask; import ml.docilealligator.infinityforreddit.AsyncTask.SwitchAccountAsyncTask; +import ml.docilealligator.infinityforreddit.BottomSheetFragment.FlairBottomSheetFragment; +import ml.docilealligator.infinityforreddit.BottomSheetFragment.PostCommentSortTypeBottomSheetFragment; import ml.docilealligator.infinityforreddit.CommentData; import ml.docilealligator.infinityforreddit.CustomTheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.CustomView.CustomToroContainer; @@ -71,9 +73,8 @@ import ml.docilealligator.infinityforreddit.Event.PostUpdateEventToDetailActivit import ml.docilealligator.infinityforreddit.Event.PostUpdateEventToPostList; import ml.docilealligator.infinityforreddit.Event.SwitchAccountEvent; import ml.docilealligator.infinityforreddit.FetchComment; +import ml.docilealligator.infinityforreddit.FetchRemovedComment; import ml.docilealligator.infinityforreddit.Flair; -import ml.docilealligator.infinityforreddit.BottomSheetFragment.FlairBottomSheetFragment; -import ml.docilealligator.infinityforreddit.BottomSheetFragment.PostCommentSortTypeBottomSheetFragment; import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.ParseComment; import ml.docilealligator.infinityforreddit.Post.FetchPost; @@ -161,6 +162,9 @@ public class ViewPostDetailActivity extends BaseActivity implements FlairBottomS @Named("no_oauth") Retrofit mRetrofit; @Inject + @Named("pushshift") + Retrofit pushshiftRetrofit; + @Inject @Named("oauth") Retrofit mOauthRetrofit; @Inject @@ -1144,6 +1148,24 @@ public class ViewPostDetailActivity extends BaseActivity implements FlairBottomS .show(); } + public void showRemovedComment(CommentData comment, int position) { + Toast.makeText(ViewPostDetailActivity.this, R.string.fetching_removed_comment, Toast.LENGTH_SHORT).show(); + FetchRemovedComment.fetchRemovedComment( + pushshiftRetrofit, + comment, + new FetchRemovedComment.FetchRemovedCommentListener() { + @Override + public void fetchSuccess(CommentData comment) { + mAdapter.editComment(comment.getAuthor(), comment.getCommentMarkdown(), position); + } + + @Override + public void fetchFailed() { + Toast.makeText(ViewPostDetailActivity.this, R.string.show_removed_comment_failed, Toast.LENGTH_SHORT).show(); + } + }); + } + public void changeToSingleThreadMode() { isSingleCommentThreadMode = false; mSingleCommentId = null; @@ -1537,7 +1559,8 @@ public class ViewPostDetailActivity extends BaseActivity implements FlairBottomS } } else if (requestCode == EDIT_COMMENT_REQUEST_CODE) { if (data != null && resultCode == RESULT_OK) { - mAdapter.editComment(data.getStringExtra(EditCommentActivity.EXTRA_EDITED_COMMENT_CONTENT), + mAdapter.editComment(null, + data.getStringExtra(EditCommentActivity.EXTRA_EDITED_COMMENT_CONTENT), data.getExtras().getInt(EditCommentActivity.EXTRA_EDITED_COMMENT_POSITION)); } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentAndPostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentAndPostRecyclerViewAdapter.java index 0510fae6..f52a6b69 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentAndPostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentAndPostRecyclerViewAdapter.java @@ -1384,8 +1384,10 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter { + dismiss(); + if (activity instanceof ViewPostDetailActivity) { + ((ViewPostDetailActivity) activity).showRemovedComment(commentData, bundle.getInt(EXTRA_POSITION)); + } + }); + } + return rootView; } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchRemovedComment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchRemovedComment.java new file mode 100644 index 00000000..4ff9e2d5 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchRemovedComment.java @@ -0,0 +1,95 @@ +package ml.docilealligator.infinityforreddit; + +import android.os.AsyncTask; + +import androidx.annotation.NonNull; + +import org.json.JSONException; +import org.json.JSONObject; + +import ml.docilealligator.infinityforreddit.API.PushshiftAPI; +import ml.docilealligator.infinityforreddit.Utils.JSONUtils; +import ml.docilealligator.infinityforreddit.Utils.Utils; +import retrofit2.Call; +import retrofit2.Callback; +import retrofit2.Response; +import retrofit2.Retrofit; + +public class FetchRemovedComment { + + public static void fetchRemovedComment(Retrofit retrofit, CommentData comment, FetchRemovedCommentListener listener) { + retrofit.create(PushshiftAPI.class).getRemovedComment(comment.getId()) + .enqueue(new Callback() { + @Override + public void onResponse(@NonNull Call call, @NonNull Response response) { + if (response.isSuccessful()) { + new ParseCommentAsyncTask(response.body(), comment, listener).execute(); + } else { + listener.fetchFailed(); + } + } + + @Override + public void onFailure(@NonNull Call call, @NonNull Throwable t) { + listener.fetchFailed(); + } + }); + } + + private static CommentData parseRemovedComment(JSONObject comment, CommentData commentData) throws JSONException { + String id = comment.getString(JSONUtils.ID_KEY); + if (id.equals(commentData.getId())) { + String author = comment.getString(JSONUtils.AUTHOR_KEY); + String commentMarkdown = ""; + if (!comment.isNull(JSONUtils.BODY_KEY)) { + commentMarkdown = Utils.modifyMarkdown(comment.getString(JSONUtils.BODY_KEY).trim()); + } + + commentData.setAuthor(author); + commentData.setCommentMarkdown(commentMarkdown); + commentData.setCommentRawText(commentMarkdown); + return commentData; + } else { + return null; + } + } + + public interface FetchRemovedCommentListener { + void fetchSuccess(CommentData comment); + + void fetchFailed(); + } + + private static class ParseCommentAsyncTask extends AsyncTask { + + private String responseBody; + private FetchRemovedCommentListener listener; + CommentData comment; + + public ParseCommentAsyncTask(String responseBody, CommentData comment, FetchRemovedCommentListener listener) { + this.responseBody = responseBody; + this.comment = comment; + this.listener = listener; + } + + @Override + protected Void doInBackground(Void... voids) { + try { + JSONObject commentJSON = new JSONObject(responseBody).getJSONArray(JSONUtils.DATA_KEY).getJSONObject(0); + comment = parseRemovedComment(commentJSON, comment); + } catch (JSONException e) { + e.printStackTrace(); + } + return null; + } + + @Override + protected void onPostExecute(Void aVoid) { + super.onPostExecute(aVoid); + if (comment != null) + listener.fetchSuccess(comment); + else + listener.fetchFailed(); + } + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/APIUtils.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/APIUtils.java index c080bdd4..7f49deee 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/APIUtils.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/APIUtils.java @@ -21,6 +21,7 @@ public class APIUtils { public static final String GFYCAT_API_BASE_URI = "https://api.gfycat.com/v1/gfycats/"; public static final String REDGIFS_API_BASE_URI = "https://api.redgifs.com/v1/gfycats/"; public static final String IMGUR_API_BASE_URI = "https://api.imgur.com/3/"; + public static final String PUSHSHIFT_API_BASE_URI = "https://api.pushshift.io/"; public static final String CLIENT_ID_KEY = "client_id"; public static final String CLIENT_ID = ""; -- cgit v1.2.3 From 99418267c44a6d3f3af54c7b24993347a863d5e4 Mon Sep 17 00:00:00 2001 From: Hermes Junior Date: Fri, 12 Jun 2020 16:28:05 +0200 Subject: Refactor menu setup. --- .../Activity/ViewPostDetailActivity.java | 260 ++++++--------------- 1 file changed, 71 insertions(+), 189 deletions(-) (limited to 'app/src/main/java/ml') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewPostDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewPostDetailActivity.java index bf01f564..8548b40e 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewPostDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewPostDetailActivity.java @@ -74,6 +74,7 @@ import ml.docilealligator.infinityforreddit.Event.PostUpdateEventToPostList; import ml.docilealligator.infinityforreddit.Event.SwitchAccountEvent; import ml.docilealligator.infinityforreddit.FetchComment; import ml.docilealligator.infinityforreddit.FetchRemovedComment; +import ml.docilealligator.infinityforreddit.FetchRemovedPost; import ml.docilealligator.infinityforreddit.Flair; import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.ParseComment; @@ -453,63 +454,7 @@ public class ViewPostDetailActivity extends BaseActivity implements FlairBottomS if (mPost == null) { fetchPostAndCommentsById(getIntent().getStringExtra(EXTRA_POST_ID)); } else { - if (mMenu != null) { - MenuItem saveItem = mMenu.findItem(R.id.action_save_view_post_detail_activity); - MenuItem hideItem = mMenu.findItem(R.id.action_hide_view_post_detail_activity); - - mMenu.findItem(R.id.action_comment_view_post_detail_activity).setVisible(true); - mMenu.findItem(R.id.action_sort_view_post_detail_activity).setVisible(true); - - if (mAccessToken != null) { - if (mPost.isSaved()) { - saveItem.setVisible(true); - saveItem.setIcon(mSavedIcon); - } else { - saveItem.setVisible(true); - saveItem.setIcon(mUnsavedIcon); - } - - if (mPost.isHidden()) { - hideItem.setVisible(true); - hideItem.setTitle(R.string.action_unhide_post); - } else { - hideItem.setVisible(true); - hideItem.setTitle(R.string.action_hide_post); - } - - mMenu.findItem(R.id.action_report_view_post_detail_activity).setVisible(true); - } else { - saveItem.setVisible(false); - hideItem.setVisible(false); - } - - if (mPost.getAuthor().equals(mAccountName)) { - if (mPost.getPostType() == Post.TEXT_TYPE) { - 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); - } - - mMenu.findItem(R.id.action_edit_flair_view_post_detail_activity).setVisible(true); - } - - mMenu.findItem(R.id.action_view_crosspost_parent_view_post_detail_activity).setVisible(mPost.getCrosspostParentId() != null); - } + setupMenu(); mAdapter = new CommentAndPostRecyclerViewAdapter(ViewPostDetailActivity.this, mCustomThemeWrapper, mRetrofit, mOauthRetrofit, mRedditDataRoomDatabase, mGlide, @@ -563,6 +508,66 @@ public class ViewPostDetailActivity extends BaseActivity implements FlairBottomS fab.setOnClickListener(view -> scrollToNextParentComment()); } + private void setupMenu() { + if (mMenu != null) { + MenuItem saveItem = mMenu.findItem(R.id.action_save_view_post_detail_activity); + MenuItem hideItem = mMenu.findItem(R.id.action_hide_view_post_detail_activity); + + mMenu.findItem(R.id.action_comment_view_post_detail_activity).setVisible(true); + mMenu.findItem(R.id.action_sort_view_post_detail_activity).setVisible(true); + + if (mAccessToken != null) { + if (mPost.isSaved()) { + saveItem.setVisible(true); + saveItem.setIcon(mSavedIcon); + } else { + saveItem.setVisible(true); + saveItem.setIcon(mUnsavedIcon); + } + + if (mPost.isHidden()) { + hideItem.setVisible(true); + hideItem.setTitle(R.string.action_unhide_post); + } else { + hideItem.setVisible(true); + hideItem.setTitle(R.string.action_hide_post); + } + + mMenu.findItem(R.id.action_report_view_post_detail_activity).setVisible(true); + } else { + saveItem.setVisible(false); + hideItem.setVisible(false); + } + + if (mPost.getAuthor().equals(mAccountName)) { + if (mPost.getPostType() == Post.TEXT_TYPE) { + 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); + } + + mMenu.findItem(R.id.action_edit_flair_view_post_detail_activity).setVisible(true); + } + + mMenu.findItem(R.id.action_view_crosspost_parent_view_post_detail_activity).setVisible(mPost.getCrosspostParentId() != null); + } + } + private Drawable getMenuItemIcon(int drawableId) { Drawable icon = getDrawable(drawableId); if (icon != null) { @@ -606,45 +611,7 @@ public class ViewPostDetailActivity extends BaseActivity implements FlairBottomS public void onParsePostSuccess(Post post) { mPost = post; - if (mMenu != null) { - MenuItem saveItem = mMenu.findItem(R.id.action_save_view_post_detail_activity); - MenuItem hideItem = mMenu.findItem(R.id.action_hide_view_post_detail_activity); - - mMenu.findItem(R.id.action_comment_view_post_detail_activity).setVisible(true); - mMenu.findItem(R.id.action_sort_view_post_detail_activity).setVisible(true); - - if (mAccessToken != null) { - if (post.isSaved()) { - saveItem.setVisible(true); - saveItem.setIcon(mSavedIcon); - } else { - saveItem.setVisible(true); - saveItem.setIcon(mUnsavedIcon); - } - - if (post.isHidden()) { - hideItem.setVisible(true); - hideItem.setTitle(R.string.action_unhide_post); - } else { - hideItem.setVisible(true); - hideItem.setTitle(R.string.action_hide_post); - } - - mMenu.findItem(R.id.action_report_view_post_detail_activity).setVisible(true); - } else { - saveItem.setVisible(false); - hideItem.setVisible(false); - } - - if (mPost.getAuthor().equals(mAccountName)) { - if (mPost.getPostType() == Post.TEXT_TYPE) { - mMenu.findItem(R.id.action_edit_view_post_detail_activity).setVisible(true); - } - mMenu.findItem(R.id.action_delete_view_post_detail_activity).setVisible(true); - } - - mMenu.findItem(R.id.action_view_crosspost_parent_view_post_detail_activity).setVisible(mPost.getCrosspostParentId() != null); - } + setupMenu(); mAdapter = new CommentAndPostRecyclerViewAdapter(ViewPostDetailActivity.this, mCustomThemeWrapper, mRetrofit, mOauthRetrofit, mRedditDataRoomDatabase, mGlide, @@ -914,38 +881,7 @@ public class ViewPostDetailActivity extends BaseActivity implements FlairBottomS mAdapter.updatePost(mPost); EventBus.getDefault().post(new PostUpdateEventToPostList(mPost, postListPosition)); isRefreshing = false; - if (mMenu != null) { - MenuItem saveItem = mMenu.findItem(R.id.action_save_view_post_detail_activity); - MenuItem hideItem = mMenu.findItem(R.id.action_hide_view_post_detail_activity); - - mMenu.findItem(R.id.action_comment_view_post_detail_activity).setVisible(true); - mMenu.findItem(R.id.action_sort_view_post_detail_activity).setVisible(true); - - if (mAccessToken != null) { - if (post.isSaved()) { - saveItem.setVisible(true); - saveItem.setIcon(mSavedIcon); - } else { - saveItem.setVisible(true); - saveItem.setIcon(mUnsavedIcon); - } - - if (post.isHidden()) { - hideItem.setVisible(true); - hideItem.setTitle(R.string.action_unhide_post); - } else { - hideItem.setVisible(true); - hideItem.setTitle(R.string.action_hide_post); - } - - mMenu.findItem(R.id.action_report_view_post_detail_activity).setVisible(true); - } else { - saveItem.setVisible(false); - hideItem.setVisible(false); - } - - mMenu.findItem(R.id.action_view_crosspost_parent_view_post_detail_activity).setVisible(mPost.getCrosspostParentId() != null); - } + setupMenu(); mSwipeRefreshLayout.setRefreshing(false); } @@ -1279,61 +1215,7 @@ public class ViewPostDetailActivity extends BaseActivity implements FlairBottomS applyMenuItemTheme(menu); mMenu = menu; if (mPost != null) { - MenuItem saveItem = mMenu.findItem(R.id.action_save_view_post_detail_activity); - MenuItem hideItem = mMenu.findItem(R.id.action_hide_view_post_detail_activity); - - mMenu.findItem(R.id.action_comment_view_post_detail_activity).setVisible(true); - mMenu.findItem(R.id.action_sort_view_post_detail_activity).setVisible(true); - - if (mAccessToken != null) { - if (mPost.isSaved()) { - saveItem.setVisible(true); - saveItem.setIcon(mSavedIcon); - } else { - saveItem.setVisible(true); - saveItem.setIcon(mUnsavedIcon); - } - - if (mPost.isHidden()) { - hideItem.setVisible(true); - hideItem.setTitle(R.string.action_unhide_post); - } else { - hideItem.setVisible(true); - hideItem.setTitle(R.string.action_hide_post); - } - - mMenu.findItem(R.id.action_report_view_post_detail_activity).setVisible(true); - } else { - saveItem.setVisible(false); - hideItem.setVisible(false); - } - - if (mPost.getAuthor().equals(mAccountName)) { - if (mPost.getPostType() == Post.TEXT_TYPE) { - 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); - } - - menu.findItem(R.id.action_edit_flair_view_post_detail_activity).setVisible(true); - } - - menu.findItem(R.id.action_view_crosspost_parent_view_post_detail_activity).setVisible(mPost.getCrosspostParentId() != null); + setupMenu(); } return true; } @@ -1472,12 +1354,12 @@ public class ViewPostDetailActivity extends BaseActivity implements FlairBottomS } return true; case R.id.action_edit_view_post_detail_activity: - Intent editPostItent = new Intent(this, EditPostActivity.class); - editPostItent.putExtra(EditPostActivity.EXTRA_ACCESS_TOKEN, mAccessToken); - editPostItent.putExtra(EditPostActivity.EXTRA_FULLNAME, mPost.getFullName()); - editPostItent.putExtra(EditPostActivity.EXTRA_TITLE, mPost.getTitle()); - editPostItent.putExtra(EditPostActivity.EXTRA_CONTENT, mPost.getSelfText()); - startActivityForResult(editPostItent, EDIT_POST_REQUEST_CODE); + Intent editPostIntent = new Intent(this, EditPostActivity.class); + editPostIntent.putExtra(EditPostActivity.EXTRA_ACCESS_TOKEN, mAccessToken); + editPostIntent.putExtra(EditPostActivity.EXTRA_FULLNAME, mPost.getFullName()); + editPostIntent.putExtra(EditPostActivity.EXTRA_TITLE, mPost.getTitle()); + editPostIntent.putExtra(EditPostActivity.EXTRA_CONTENT, mPost.getSelfText()); + startActivityForResult(editPostIntent, EDIT_POST_REQUEST_CODE); return true; case R.id.action_delete_view_post_detail_activity: new MaterialAlertDialogBuilder(this, R.style.MaterialAlertDialogTheme) -- cgit v1.2.3 From 1b972aca2eb20cd7895ba20f015ba480a7f2ade6 Mon Sep 17 00:00:00 2001 From: Hermes Junior Date: Fri, 12 Jun 2020 16:30:56 +0200 Subject: Implement option to see removed post. --- .../infinityforreddit/API/PushshiftAPI.java | 3 + .../Activity/ViewPostDetailActivity.java | 27 ++++++ .../Adapter/CommentAndPostRecyclerViewAdapter.java | 1 + .../infinityforreddit/FetchRemovedComment.java | 1 + .../infinityforreddit/FetchRemovedPost.java | 100 +++++++++++++++++++++ .../infinityforreddit/Post/Post.java | 7 +- 6 files changed, 138 insertions(+), 1 deletion(-) create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/FetchRemovedPost.java (limited to 'app/src/main/java/ml') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/API/PushshiftAPI.java b/app/src/main/java/ml/docilealligator/infinityforreddit/API/PushshiftAPI.java index 66c92a02..1dc7b411 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/API/PushshiftAPI.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/API/PushshiftAPI.java @@ -7,4 +7,7 @@ import retrofit2.http.Query; public interface PushshiftAPI { @GET("reddit/comment/search/") Call getRemovedComment(@Query("ids") String commentId); + + @GET("reddit/submission/search/") + Call getRemovedPost(@Query("ids") String postId); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewPostDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewPostDetailActivity.java index 8548b40e..9610d7c1 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewPostDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewPostDetailActivity.java @@ -565,6 +565,10 @@ public class ViewPostDetailActivity extends BaseActivity implements FlairBottomS } mMenu.findItem(R.id.action_view_crosspost_parent_view_post_detail_activity).setVisible(mPost.getCrosspostParentId() != null); + + if (mPost.getSelfText().equals("[deleted]")) { + mMenu.findItem(R.id.action_see_removed_view_post_detail_activity).setVisible(true); + } } } @@ -1220,6 +1224,25 @@ public class ViewPostDetailActivity extends BaseActivity implements FlairBottomS return true; } + public void showRemovedPost() { + Toast.makeText(ViewPostDetailActivity.this, R.string.fetching_removed_post, Toast.LENGTH_SHORT).show(); + FetchRemovedPost.fetchRemovedPost( + pushshiftRetrofit, + mPost, + new FetchRemovedPost.FetchRemovedPostListener() { + @Override + public void fetchSuccess(Post post) { + mPost = post; + mAdapter.updatePost(post); + } + + @Override + public void fetchFailed() { + Toast.makeText(ViewPostDetailActivity.this, R.string.show_removed_post_failed, Toast.LENGTH_SHORT).show(); + } + }); + } + @Override public boolean onOptionsItemSelected(@NonNull MenuItem item) { switch (item.getItemId()) { @@ -1408,6 +1431,10 @@ public class ViewPostDetailActivity extends BaseActivity implements FlairBottomS intent.putExtra(ReportActivity.EXTRA_SUBREDDIT_NAME, mPost.getSubredditName()); intent.putExtra(ReportActivity.EXTRA_THING_FULLNAME, mPost.getFullName()); startActivity(intent); + return true; + case R.id.action_see_removed_view_post_detail_activity: + showRemovedPost(); + return true; case android.R.id.home: onBackPressed(); return true; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentAndPostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentAndPostRecyclerViewAdapter.java index f52a6b69..41acea18 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentAndPostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentAndPostRecyclerViewAdapter.java @@ -1387,6 +1387,7 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter call, @NonNull Throwable t) { + t.printStackTrace(); listener.fetchFailed(); } }); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchRemovedPost.java b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchRemovedPost.java new file mode 100644 index 00000000..00a0f6a0 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchRemovedPost.java @@ -0,0 +1,100 @@ +package ml.docilealligator.infinityforreddit; + +import android.os.AsyncTask; + +import androidx.annotation.NonNull; + +import org.json.JSONException; +import org.json.JSONObject; + +import ml.docilealligator.infinityforreddit.API.PushshiftAPI; +import ml.docilealligator.infinityforreddit.Post.Post; +import ml.docilealligator.infinityforreddit.Utils.JSONUtils; +import ml.docilealligator.infinityforreddit.Utils.Utils; +import retrofit2.Call; +import retrofit2.Callback; +import retrofit2.Response; +import retrofit2.Retrofit; + +public class FetchRemovedPost { + + public static void fetchRemovedPost(Retrofit retrofit, Post post, FetchRemovedPostListener listener) { + retrofit.create(PushshiftAPI.class).getRemovedPost(post.getId()) + .enqueue(new Callback() { + @Override + public void onResponse(@NonNull Call call, @NonNull Response response) { + if (response.isSuccessful()) { + new ParsePostAsyncTask(response.body(), post, listener).execute(); + } else { + listener.fetchFailed(); + } + } + + @Override + public void onFailure(@NonNull Call call, @NonNull Throwable t) { + t.printStackTrace(); + listener.fetchFailed(); + } + }); + } + + private static Post parseRemovedPost(JSONObject postJson, Post post) throws JSONException { + String id = postJson.getString(JSONUtils.ID_KEY); + if (id.equals(post.getId())) { + String author = postJson.getString(JSONUtils.AUTHOR_KEY); + String title = postJson.getString(JSONUtils.TITLE_KEY); + String postContent = ""; + if (!postJson.isNull(JSONUtils.SELFTEXT_KEY)) { + postContent = Utils.modifyMarkdown(postJson.getString(JSONUtils.SELFTEXT_KEY).trim()); + } + + post.setAuthor(author); + post.setTitle(title); + post.setSelfText(postContent); + post.setSelfTextPlain(""); + post.setSelfTextPlainTrimmed(""); + return post; + } else { + return null; + } + } + + public interface FetchRemovedPostListener { + void fetchSuccess(Post post); + + void fetchFailed(); + } + + private static class ParsePostAsyncTask extends AsyncTask { + + private String responseBody; + private FetchRemovedPostListener listener; + Post post; + + public ParsePostAsyncTask(String responseBody, Post post, FetchRemovedPostListener listener) { + this.responseBody = responseBody; + this.post = post; + this.listener = listener; + } + + @Override + protected Void doInBackground(Void... voids) { + try { + JSONObject postJson = new JSONObject(responseBody).getJSONArray(JSONUtils.DATA_KEY).getJSONObject(0); + post = parseRemovedPost(postJson, post); + } catch (JSONException e) { + e.printStackTrace(); + } + return null; + } + + @Override + protected void onPostExecute(Void aVoid) { + super.onPostExecute(aVoid); + if (post != null) + listener.fetchSuccess(post); + else + listener.fetchFailed(); + } + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Post/Post.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Post/Post.java index a05a97f2..54e7ec7f 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Post/Post.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Post/Post.java @@ -252,6 +252,11 @@ public class Post implements Parcelable { return author; } + public void setAuthor(String author) { + this.author = author; + this.authorNamePrefixed = "u/" + author; + } + public String getAuthorNamePrefixed() { return authorNamePrefixed; } @@ -524,4 +529,4 @@ public class Post implements Parcelable { } return ((Post) obj).id.equals(id); } -} \ No newline at end of file +} -- cgit v1.2.3 From e0020e3cf21abe06ca5e6f6456451ccf7b9fd3d1 Mon Sep 17 00:00:00 2001 From: Hermes Junior Date: Fri, 12 Jun 2020 17:18:40 +0200 Subject: Fix crash for non selftext posts. --- .../infinityforreddit/Activity/ViewPostDetailActivity.java | 2 +- .../BottomSheetFragment/CommentMoreBottomSheetFragment.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'app/src/main/java/ml') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewPostDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewPostDetailActivity.java index 9610d7c1..90bae86e 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewPostDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewPostDetailActivity.java @@ -566,7 +566,7 @@ public class ViewPostDetailActivity extends BaseActivity implements FlairBottomS mMenu.findItem(R.id.action_view_crosspost_parent_view_post_detail_activity).setVisible(mPost.getCrosspostParentId() != null); - if (mPost.getSelfText().equals("[deleted]")) { + if ("[deleted]".equals(mPost.getAuthor()) || "[deleted]".equals(mPost.getSelfText())) { mMenu.findItem(R.id.action_see_removed_view_post_detail_activity).setVisible(true); } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/BottomSheetFragment/CommentMoreBottomSheetFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/BottomSheetFragment/CommentMoreBottomSheetFragment.java index bfdb0b0f..2fa1f3d7 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/BottomSheetFragment/CommentMoreBottomSheetFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/BottomSheetFragment/CommentMoreBottomSheetFragment.java @@ -134,7 +134,7 @@ public class CommentMoreBottomSheetFragment extends RoundedBottomSheetDialogFrag dismiss(); }); - if (commentData.getCommentRawText().equals("[removed]")) { + if ("[removed]".equals(commentData.getCommentRawText())) { seeRemovedTextView.setVisibility(View.VISIBLE); seeRemovedTextView.setOnClickListener(view -> { -- cgit v1.2.3 From 46a8b0d276e5e232b742e45577c94929a1f226e2 Mon Sep 17 00:00:00 2001 From: Hermes Junior Date: Fri, 12 Jun 2020 22:06:35 +0200 Subject: Implement "see removed" for other types of posts. Improve removed thing detection. --- .../Activity/ViewPostDetailActivity.java | 5 ++- .../CommentMoreBottomSheetFragment.java | 5 ++- .../infinityforreddit/FetchRemovedComment.java | 1 + .../infinityforreddit/FetchRemovedPost.java | 41 +++++++++++++++++++--- .../infinityforreddit/Post/Post.java | 4 +++ 5 files changed, 50 insertions(+), 6 deletions(-) (limited to 'app/src/main/java/ml') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewPostDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewPostDetailActivity.java index 90bae86e..81e1ba8a 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewPostDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewPostDetailActivity.java @@ -566,7 +566,10 @@ public class ViewPostDetailActivity extends BaseActivity implements FlairBottomS mMenu.findItem(R.id.action_view_crosspost_parent_view_post_detail_activity).setVisible(mPost.getCrosspostParentId() != null); - if ("[deleted]".equals(mPost.getAuthor()) || "[deleted]".equals(mPost.getSelfText())) { + if ("[deleted]".equals(mPost.getAuthor()) || + "[deleted]".equals(mPost.getSelfText()) || + "[removed]".equals(mPost.getSelfText()) + ) { mMenu.findItem(R.id.action_see_removed_view_post_detail_activity).setVisible(true); } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/BottomSheetFragment/CommentMoreBottomSheetFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/BottomSheetFragment/CommentMoreBottomSheetFragment.java index 2fa1f3d7..de4f5778 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/BottomSheetFragment/CommentMoreBottomSheetFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/BottomSheetFragment/CommentMoreBottomSheetFragment.java @@ -134,7 +134,10 @@ public class CommentMoreBottomSheetFragment extends RoundedBottomSheetDialogFrag dismiss(); }); - if ("[removed]".equals(commentData.getCommentRawText())) { + if ("[deleted]".equals(commentData.getAuthor()) || + "[deleted]".equals(commentData.getCommentRawText()) || + "[removed]".equals(commentData.getCommentRawText()) + ) { seeRemovedTextView.setVisibility(View.VISIBLE); seeRemovedTextView.setOnClickListener(view -> { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchRemovedComment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchRemovedComment.java index e2db7aed..6e18e927 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchRemovedComment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchRemovedComment.java @@ -80,6 +80,7 @@ public class FetchRemovedComment { comment = parseRemovedComment(commentJSON, comment); } catch (JSONException e) { e.printStackTrace(); + comment = null; } return null; } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchRemovedPost.java b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchRemovedPost.java index 00a0f6a0..2a8076a9 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchRemovedPost.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchRemovedPost.java @@ -1,6 +1,8 @@ package ml.docilealligator.infinityforreddit; +import android.net.Uri; import android.os.AsyncTask; +import android.text.Html; import androidx.annotation.NonNull; @@ -43,16 +45,46 @@ public class FetchRemovedPost { if (id.equals(post.getId())) { String author = postJson.getString(JSONUtils.AUTHOR_KEY); String title = postJson.getString(JSONUtils.TITLE_KEY); - String postContent = ""; - if (!postJson.isNull(JSONUtils.SELFTEXT_KEY)) { - postContent = Utils.modifyMarkdown(postJson.getString(JSONUtils.SELFTEXT_KEY).trim()); - } + String postContent = Utils.modifyMarkdown(postJson.getString(JSONUtils.SELFTEXT_KEY).trim()); post.setAuthor(author); post.setTitle(title); post.setSelfText(postContent); post.setSelfTextPlain(""); post.setSelfTextPlainTrimmed(""); + + String url = postJson.optString(JSONUtils.URL_KEY); + + if (url.endsWith("gif") || url.endsWith("mp4")) { + post.setVideoUrl(url); + post.setVideoDownloadUrl(url); + } else if (post.getPostType() == Post.VIDEO_TYPE || post.getPostType() == Post.GIF_TYPE) { + JSONObject redditVideoObject = postJson.getJSONObject("secure_media").getJSONObject(JSONUtils.REDDIT_VIDEO_KEY); + String videoUrl = Html.fromHtml(redditVideoObject.getString(JSONUtils.HLS_URL_KEY)).toString(); + String videoDownloadUrl = redditVideoObject.getString(JSONUtils.FALLBACK_URL_KEY); + + post.setVideoUrl(videoUrl); + post.setVideoDownloadUrl(videoDownloadUrl); + } else if (post.getPostType() == Post.LINK_TYPE) { + post.setUrl(url); + } + + if (post.getPostType() == Post.VIDEO_TYPE) { + try { + Uri uri = Uri.parse(url); + String authority = uri.getAuthority(); + if (authority != null && (authority.contains("gfycat.com") || authority.contains("redgifs.com"))) { + post.setPostType(Post.LINK_TYPE); + post.setUrl(url); + } + } catch (IllegalArgumentException ignore) { + } + } + + if (!postJson.isNull("thumbnail")) { + post.setThumbnailPreviewUrl(postJson.getString("thumbnail")); + } + return post; } else { return null; @@ -84,6 +116,7 @@ public class FetchRemovedPost { post = parseRemovedPost(postJson, post); } catch (JSONException e) { e.printStackTrace(); + post = null; } return null; } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Post/Post.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Post/Post.java index 54e7ec7f..5a60bbdd 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Post/Post.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Post/Post.java @@ -325,6 +325,10 @@ public class Post implements Parcelable { return thumbnailPreviewUrl; } + public void setThumbnailPreviewUrl(String thumbnailPreviewUrl) { + this.thumbnailPreviewUrl = thumbnailPreviewUrl; + } + public String getUrl() { return url; } -- cgit v1.2.3