Implement option to see removed post.

This commit is contained in:
Hermes Junior 2020-06-12 16:30:56 +02:00 committed by OHermesJunior
parent 99418267c4
commit 1b972aca2e
8 changed files with 149 additions and 3 deletions

View File

@ -7,4 +7,7 @@ import retrofit2.http.Query;
public interface PushshiftAPI {
@GET("reddit/comment/search/")
Call<String> getRemovedComment(@Query("ids") String commentId);
@GET("reddit/submission/search/")
Call<String> getRemovedPost(@Query("ids") String postId);
}

View File

@ -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;

View File

@ -1387,6 +1387,7 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy
public void editComment(String commentAuthor, String commentContentMarkdown, int position) {
if (commentAuthor != null)
mVisibleComments.get(position).setAuthor(commentAuthor);
mVisibleComments.get(position).setCommentMarkdown(commentContentMarkdown);
if (mIsSingleCommentThreadMode) {
notifyItemChanged(position + 2);

View File

@ -31,6 +31,7 @@ public class FetchRemovedComment {
@Override
public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) {
t.printStackTrace();
listener.fetchFailed();
}
});

View File

@ -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<String>() {
@Override
public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) {
if (response.isSuccessful()) {
new ParsePostAsyncTask(response.body(), post, listener).execute();
} else {
listener.fetchFailed();
}
}
@Override
public void onFailure(@NonNull Call<String> 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<Void, Void, Void> {
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();
}
}
}

View File

@ -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);
}
}
}

View File

@ -80,5 +80,12 @@
android:orderInCategory="12"
android:title="@string/action_report"
app:showAsAction="never"
android:visible="false"/>
<item
android:id="@+id/action_see_removed_view_post_detail_activity"
android:orderInCategory="13"
android:title="@string/action_see_removed"
app:showAsAction="never"
android:visible="false" />
</menu>
</menu>

View File

@ -61,6 +61,7 @@
<string name="action_share">Share</string>
<string name="action_preview">Preview</string>
<string name="action_report">Report</string>
<string name="action_see_removed">See Removed</string>
<string name="action_set_wallpaper">Set as Wallpaper</string>
<string name="parse_json_response_error">Error occurred when parsing the JSON response</string>
@ -277,9 +278,10 @@
<string name="edit">Edit</string>
<string name="delete">Delete</string>
<string name="see_removed_comment">See Removed Comment</string>
<string name="see_removed_post">See Removed Post</string>
<string name="fetching_removed_comment">Fetching removed comment</string>
<string name="show_removed_comment_failed">Could not find the removed comment</string>
<string name="fetching_removed_post">Fetching removed post</string>
<string name="show_removed_post_failed">Could not find the removed post</string>
<string name="cancel">Cancel</string>
<string name="ok">OK</string>
<string name="edit_success">Edit successful</string>