Continue adding support for post filter.

This commit is contained in:
Alex Ning 2020-12-18 13:15:50 +08:00
parent c5cedba370
commit fbc47e6226
2 changed files with 26 additions and 23 deletions

View File

@ -13,6 +13,7 @@ import java.util.HashSet;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import java.util.List; import java.util.List;
import ml.docilealligator.infinityforreddit.PostFilter;
import ml.docilealligator.infinityforreddit.fragments.PostFragment; import ml.docilealligator.infinityforreddit.fragments.PostFragment;
import ml.docilealligator.infinityforreddit.readpost.ReadPost; import ml.docilealligator.infinityforreddit.readpost.ReadPost;
import ml.docilealligator.infinityforreddit.subredditfilter.SubredditFilter; import ml.docilealligator.infinityforreddit.subredditfilter.SubredditFilter;
@ -24,18 +25,20 @@ import ml.docilealligator.infinityforreddit.utils.Utils;
*/ */
public class ParsePost { public class ParsePost {
public static void parsePosts(String response, int nPosts, int filter, boolean nsfw, List<ReadPost> readPostList, public static void parsePosts(String response, int nPosts, int filter, PostFilter postFilter, List<ReadPost> readPostList,
ParsePostsListingListener parsePostsListingListener) { ParsePostsListingListener parsePostsListingListener) {
new ParsePostDataAsyncTask(response, nPosts, filter, nsfw, readPostList, parsePostsListingListener).execute(); new ParsePostDataAsyncTask(response, nPosts, filter, postFilter, readPostList, parsePostsListingListener).execute();
} }
public static void parsePosts(String response, int nPosts, int filter, boolean nsfw, List<ReadPost> readPostList, public static void parsePosts(String response, int nPosts, int filter, PostFilter postFilter, List<ReadPost> readPostList,
List<SubredditFilter> subredditFilterList, ParsePostsListingListener parsePostsListingListener) { List<SubredditFilter> subredditFilterList, ParsePostsListingListener parsePostsListingListener) {
new ParsePostDataAsyncTask(response, nPosts, filter, nsfw, readPostList, subredditFilterList, parsePostsListingListener).execute(); new ParsePostDataAsyncTask(response, nPosts, filter, postFilter, readPostList, subredditFilterList, parsePostsListingListener).execute();
} }
public static void parsePost(String response, ParsePostListener parsePostListener) { public static void parsePost(String response, ParsePostListener parsePostListener) {
new ParsePostDataAsyncTask(response, true, parsePostListener).execute(); PostFilter postFilter = new PostFilter();
postFilter.allowNSFW = true;
new ParsePostDataAsyncTask(response, postFilter, parsePostListener).execute();
} }
private static Post parseBasicData(JSONObject data) throws JSONException { private static Post parseBasicData(JSONObject data) throws JSONException {
@ -491,7 +494,7 @@ public class ParsePost {
private JSONArray allData; private JSONArray allData;
private int nPosts; private int nPosts;
private int filter; private int filter;
private boolean nsfw; private PostFilter postFilter;
private List<ReadPost> readPostList; private List<ReadPost> readPostList;
private List<SubredditFilter> subredditFilterList; private List<SubredditFilter> subredditFilterList;
private ParsePostsListingListener parsePostsListingListener; private ParsePostsListingListener parsePostsListingListener;
@ -501,7 +504,7 @@ public class ParsePost {
private String lastItem; private String lastItem;
private boolean parseFailed; private boolean parseFailed;
ParsePostDataAsyncTask(String response, int nPosts, int filter, boolean nsfw, List<ReadPost> readPostList, ParsePostDataAsyncTask(String response, int nPosts, int filter, PostFilter postFilter, List<ReadPost> readPostList,
ParsePostsListingListener parsePostsListingListener) { ParsePostsListingListener parsePostsListingListener) {
this.parsePostsListingListener = parsePostsListingListener; this.parsePostsListingListener = parsePostsListingListener;
try { try {
@ -510,7 +513,7 @@ public class ParsePost {
lastItem = jsonResponse.getJSONObject(JSONUtils.DATA_KEY).getString(JSONUtils.AFTER_KEY); lastItem = jsonResponse.getJSONObject(JSONUtils.DATA_KEY).getString(JSONUtils.AFTER_KEY);
this.nPosts = nPosts; this.nPosts = nPosts;
this.filter = filter; this.filter = filter;
this.nsfw = nsfw; this.postFilter = postFilter;
this.readPostList = readPostList; this.readPostList = readPostList;
newPosts = new LinkedHashSet<>(); newPosts = new LinkedHashSet<>();
parseFailed = false; parseFailed = false;
@ -520,18 +523,18 @@ public class ParsePost {
} }
} }
ParsePostDataAsyncTask(String response, int nPosts, int filter, boolean nsfw, List<ReadPost> readPostList, ParsePostDataAsyncTask(String response, int nPosts, int filter, PostFilter postFilter, List<ReadPost> readPostList,
List<SubredditFilter> subredditFilterList, ParsePostsListingListener parsePostsListingListener) { List<SubredditFilter> subredditFilterList, ParsePostsListingListener parsePostsListingListener) {
this(response, nPosts, filter, nsfw, readPostList, parsePostsListingListener); this(response, nPosts, filter, postFilter, readPostList, parsePostsListingListener);
this.subredditFilterList = subredditFilterList; this.subredditFilterList = subredditFilterList;
} }
ParsePostDataAsyncTask(String response, boolean nsfw, ParsePostDataAsyncTask(String response, PostFilter postFilter,
ParsePostListener parsePostListener) { ParsePostListener parsePostListener) {
this.parsePostListener = parsePostListener; this.parsePostListener = parsePostListener;
try { try {
allData = new JSONArray(response).getJSONObject(0).getJSONObject(JSONUtils.DATA_KEY).getJSONArray(JSONUtils.CHILDREN_KEY); allData = new JSONArray(response).getJSONObject(0).getJSONObject(JSONUtils.DATA_KEY).getJSONArray(JSONUtils.CHILDREN_KEY);
this.nsfw = nsfw; this.postFilter = postFilter;
parseFailed = false; parseFailed = false;
} catch (JSONException e) { } catch (JSONException e) {
e.printStackTrace(); e.printStackTrace();
@ -589,7 +592,7 @@ public class ParsePost {
} }
} }
} }
if (availablePost && !(!nsfw && post.isNSFW())) { if (availablePost && !(!postFilter.allowNSFW && post.isNSFW())) {
if (filter == PostFragment.EXTRA_NO_FILTER) { if (filter == PostFragment.EXTRA_NO_FILTER) {
newPosts.add(post); newPosts.add(post);
} else if (filter == post.getPostType()) { } else if (filter == post.getPostType()) {

View File

@ -261,7 +261,7 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> {
@Override @Override
public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) { public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) {
if (response.isSuccessful()) { if (response.isSuccessful()) {
ParsePost.parsePosts(response.body(), -1, filter, postFilter.allowNSFW, readPostList, ParsePost.parsePosts(response.body(), -1, filter, postFilter, readPostList,
new ParsePost.ParsePostsListingListener() { new ParsePost.ParsePostsListingListener() {
@Override @Override
public void onParsePostsListingSuccess(LinkedHashSet<Post> newPosts, String lastItem) { public void onParsePostsListingSuccess(LinkedHashSet<Post> newPosts, String lastItem) {
@ -324,7 +324,7 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> {
@Override @Override
public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) { public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) {
if (response.isSuccessful()) { if (response.isSuccessful()) {
ParsePost.parsePosts(response.body(), -1, filter, postFilter.allowNSFW, readPostList, ParsePost.parsePosts(response.body(), -1, filter, postFilter, readPostList,
new ParsePost.ParsePostsListingListener() { new ParsePost.ParsePostsListingListener() {
@Override @Override
public void onParsePostsListingSuccess(LinkedHashSet<Post> newPosts, String lastItem) { public void onParsePostsListingSuccess(LinkedHashSet<Post> newPosts, String lastItem) {
@ -384,7 +384,7 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> {
@Override @Override
public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) { public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) {
if (response.isSuccessful()) { if (response.isSuccessful()) {
ParsePost.parsePosts(response.body(), -1, filter, postFilter.allowNSFW, readPostList, subredditFilterList, ParsePost.parsePosts(response.body(), -1, filter, postFilter, readPostList, subredditFilterList,
new ParsePost.ParsePostsListingListener() { new ParsePost.ParsePostsListingListener() {
@Override @Override
public void onParsePostsListingSuccess(LinkedHashSet<Post> newPosts, String lastItem) { public void onParsePostsListingSuccess(LinkedHashSet<Post> newPosts, String lastItem) {
@ -458,7 +458,7 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> {
@Override @Override
public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) { public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) {
if (response.isSuccessful()) { if (response.isSuccessful()) {
ParsePost.parsePosts(response.body(), -1, filter, postFilter.allowNSFW, readPostList, subredditFilterList, ParsePost.parsePosts(response.body(), -1, filter, postFilter, readPostList, subredditFilterList,
new ParsePost.ParsePostsListingListener() { new ParsePost.ParsePostsListingListener() {
@Override @Override
public void onParsePostsListingSuccess(LinkedHashSet<Post> newPosts, String lastItem) { public void onParsePostsListingSuccess(LinkedHashSet<Post> newPosts, String lastItem) {
@ -519,7 +519,7 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> {
@Override @Override
public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) { public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) {
if (response.isSuccessful()) { if (response.isSuccessful()) {
ParsePost.parsePosts(response.body(), -1, filter, postFilter.allowNSFW, readPostList, ParsePost.parsePosts(response.body(), -1, filter, postFilter, readPostList,
new ParsePost.ParsePostsListingListener() { new ParsePost.ParsePostsListingListener() {
@Override @Override
public void onParsePostsListingSuccess(LinkedHashSet<Post> newPosts, String lastItem) { public void onParsePostsListingSuccess(LinkedHashSet<Post> newPosts, String lastItem) {
@ -590,7 +590,7 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> {
@Override @Override
public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) { public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) {
if (response.isSuccessful()) { if (response.isSuccessful()) {
ParsePost.parsePosts(response.body(), -1, filter, postFilter.allowNSFW, readPostList, ParsePost.parsePosts(response.body(), -1, filter, postFilter, readPostList,
new ParsePost.ParsePostsListingListener() { new ParsePost.ParsePostsListingListener() {
@Override @Override
public void onParsePostsListingSuccess(LinkedHashSet<Post> newPosts, String lastItem) { public void onParsePostsListingSuccess(LinkedHashSet<Post> newPosts, String lastItem) {
@ -673,7 +673,7 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> {
@Override @Override
public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) { public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) {
if (response.isSuccessful()) { if (response.isSuccessful()) {
ParsePost.parsePosts(response.body(), -1, filter, postFilter.allowNSFW, readPostList, ParsePost.parsePosts(response.body(), -1, filter, postFilter, readPostList,
new ParsePost.ParsePostsListingListener() { new ParsePost.ParsePostsListingListener() {
@Override @Override
public void onParsePostsListingSuccess(LinkedHashSet<Post> newPosts, String lastItem) { public void onParsePostsListingSuccess(LinkedHashSet<Post> newPosts, String lastItem) {
@ -764,7 +764,7 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> {
@Override @Override
public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) { public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) {
if (response.isSuccessful()) { if (response.isSuccessful()) {
ParsePost.parsePosts(response.body(), -1, filter, postFilter.allowNSFW, readPostList, ParsePost.parsePosts(response.body(), -1, filter, postFilter, readPostList,
new ParsePost.ParsePostsListingListener() { new ParsePost.ParsePostsListingListener() {
@Override @Override
public void onParsePostsListingSuccess(LinkedHashSet<Post> newPosts, String lastItem) { public void onParsePostsListingSuccess(LinkedHashSet<Post> newPosts, String lastItem) {
@ -824,7 +824,7 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> {
@Override @Override
public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) { public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) {
if (response.isSuccessful()) { if (response.isSuccessful()) {
ParsePost.parsePosts(response.body(), -1, filter, postFilter.allowNSFW, readPostList, ParsePost.parsePosts(response.body(), -1, filter, postFilter, readPostList,
new ParsePost.ParsePostsListingListener() { new ParsePost.ParsePostsListingListener() {
@Override @Override
public void onParsePostsListingSuccess(LinkedHashSet<Post> newPosts, String lastItem) { public void onParsePostsListingSuccess(LinkedHashSet<Post> newPosts, String lastItem) {
@ -895,7 +895,7 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> {
@Override @Override
public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) { public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) {
if (response.isSuccessful()) { if (response.isSuccessful()) {
ParsePost.parsePosts(response.body(), -1, filter, postFilter.allowNSFW, readPostList, ParsePost.parsePosts(response.body(), -1, filter, postFilter, readPostList,
new ParsePost.ParsePostsListingListener() { new ParsePost.ParsePostsListingListener() {
@Override @Override
public void onParsePostsListingSuccess(LinkedHashSet<Post> newPosts, String lastItem) { public void onParsePostsListingSuccess(LinkedHashSet<Post> newPosts, String lastItem) {