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