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 +- .../main/res/menu/view_post_detail_activity.xml | 9 +- app/src/main/res/values/strings.xml | 4 +- 8 files changed, 149 insertions(+), 3 deletions(-) create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/FetchRemovedPost.java (limited to 'app/src') 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 +} 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 32f83c41..29c49e1f 100644 --- a/app/src/main/res/menu/view_post_detail_activity.xml +++ b/app/src/main/res/menu/view_post_detail_activity.xml @@ -80,5 +80,12 @@ android:orderInCategory="12" android:title="@string/action_report" app:showAsAction="never" + android:visible="false"/> + + - \ 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 fa9f039b..cddcf9bf 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -61,6 +61,7 @@ Share Preview Report + See Removed Set as Wallpaper Error occurred when parsing the JSON response @@ -277,9 +278,10 @@ Edit Delete See Removed Comment - See Removed Post Fetching removed comment Could not find the removed comment + Fetching removed post + Could not find the removed post Cancel OK Edit successful -- cgit v1.2.3