From fbc47e62261d98c6034ff14f178779d14a214b9e Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Fri, 18 Dec 2020 13:15:50 +0800 Subject: [PATCH] Continue adding support for post filter. --- .../infinityforreddit/post/ParsePost.java | 29 ++++++++++--------- .../post/PostDataSource.java | 20 ++++++------- 2 files changed, 26 insertions(+), 23 deletions(-) diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/post/ParsePost.java b/app/src/main/java/ml/docilealligator/infinityforreddit/post/ParsePost.java index e4a7303c..a67f2ed8 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/post/ParsePost.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/post/ParsePost.java @@ -13,6 +13,7 @@ import java.util.HashSet; import java.util.LinkedHashSet; import java.util.List; +import ml.docilealligator.infinityforreddit.PostFilter; import ml.docilealligator.infinityforreddit.fragments.PostFragment; import ml.docilealligator.infinityforreddit.readpost.ReadPost; import ml.docilealligator.infinityforreddit.subredditfilter.SubredditFilter; @@ -24,18 +25,20 @@ import ml.docilealligator.infinityforreddit.utils.Utils; */ public class ParsePost { - public static void parsePosts(String response, int nPosts, int filter, boolean nsfw, List readPostList, + public static void parsePosts(String response, int nPosts, int filter, PostFilter postFilter, List readPostList, 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 readPostList, + public static void parsePosts(String response, int nPosts, int filter, PostFilter postFilter, List readPostList, List 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) { - 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 { @@ -491,7 +494,7 @@ public class ParsePost { private JSONArray allData; private int nPosts; private int filter; - private boolean nsfw; + private PostFilter postFilter; private List readPostList; private List subredditFilterList; private ParsePostsListingListener parsePostsListingListener; @@ -501,7 +504,7 @@ public class ParsePost { private String lastItem; private boolean parseFailed; - ParsePostDataAsyncTask(String response, int nPosts, int filter, boolean nsfw, List readPostList, + ParsePostDataAsyncTask(String response, int nPosts, int filter, PostFilter postFilter, List readPostList, ParsePostsListingListener parsePostsListingListener) { this.parsePostsListingListener = parsePostsListingListener; try { @@ -510,7 +513,7 @@ public class ParsePost { lastItem = jsonResponse.getJSONObject(JSONUtils.DATA_KEY).getString(JSONUtils.AFTER_KEY); this.nPosts = nPosts; this.filter = filter; - this.nsfw = nsfw; + this.postFilter = postFilter; this.readPostList = readPostList; newPosts = new LinkedHashSet<>(); parseFailed = false; @@ -520,18 +523,18 @@ public class ParsePost { } } - ParsePostDataAsyncTask(String response, int nPosts, int filter, boolean nsfw, List readPostList, + ParsePostDataAsyncTask(String response, int nPosts, int filter, PostFilter postFilter, List readPostList, List subredditFilterList, ParsePostsListingListener parsePostsListingListener) { - this(response, nPosts, filter, nsfw, readPostList, parsePostsListingListener); + this(response, nPosts, filter, postFilter, readPostList, parsePostsListingListener); this.subredditFilterList = subredditFilterList; } - ParsePostDataAsyncTask(String response, boolean nsfw, + ParsePostDataAsyncTask(String response, PostFilter postFilter, ParsePostListener parsePostListener) { this.parsePostListener = parsePostListener; try { allData = new JSONArray(response).getJSONObject(0).getJSONObject(JSONUtils.DATA_KEY).getJSONArray(JSONUtils.CHILDREN_KEY); - this.nsfw = nsfw; + this.postFilter = postFilter; parseFailed = false; } catch (JSONException e) { 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) { newPosts.add(post); } else if (filter == post.getPostType()) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/post/PostDataSource.java b/app/src/main/java/ml/docilealligator/infinityforreddit/post/PostDataSource.java index 8d27f6b0..c2fff1e6 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/post/PostDataSource.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/post/PostDataSource.java @@ -261,7 +261,7 @@ public class PostDataSource extends PageKeyedDataSource { @Override public void onResponse(@NonNull Call call, @NonNull retrofit2.Response response) { if (response.isSuccessful()) { - ParsePost.parsePosts(response.body(), -1, filter, postFilter.allowNSFW, readPostList, + ParsePost.parsePosts(response.body(), -1, filter, postFilter, readPostList, new ParsePost.ParsePostsListingListener() { @Override public void onParsePostsListingSuccess(LinkedHashSet newPosts, String lastItem) { @@ -324,7 +324,7 @@ public class PostDataSource extends PageKeyedDataSource { @Override public void onResponse(@NonNull Call call, @NonNull retrofit2.Response response) { if (response.isSuccessful()) { - ParsePost.parsePosts(response.body(), -1, filter, postFilter.allowNSFW, readPostList, + ParsePost.parsePosts(response.body(), -1, filter, postFilter, readPostList, new ParsePost.ParsePostsListingListener() { @Override public void onParsePostsListingSuccess(LinkedHashSet newPosts, String lastItem) { @@ -384,7 +384,7 @@ public class PostDataSource extends PageKeyedDataSource { @Override public void onResponse(@NonNull Call call, @NonNull retrofit2.Response response) { 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() { @Override public void onParsePostsListingSuccess(LinkedHashSet newPosts, String lastItem) { @@ -458,7 +458,7 @@ public class PostDataSource extends PageKeyedDataSource { @Override public void onResponse(@NonNull Call call, @NonNull retrofit2.Response response) { 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() { @Override public void onParsePostsListingSuccess(LinkedHashSet newPosts, String lastItem) { @@ -519,7 +519,7 @@ public class PostDataSource extends PageKeyedDataSource { @Override public void onResponse(@NonNull Call call, @NonNull retrofit2.Response response) { if (response.isSuccessful()) { - ParsePost.parsePosts(response.body(), -1, filter, postFilter.allowNSFW, readPostList, + ParsePost.parsePosts(response.body(), -1, filter, postFilter, readPostList, new ParsePost.ParsePostsListingListener() { @Override public void onParsePostsListingSuccess(LinkedHashSet newPosts, String lastItem) { @@ -590,7 +590,7 @@ public class PostDataSource extends PageKeyedDataSource { @Override public void onResponse(@NonNull Call call, @NonNull retrofit2.Response response) { if (response.isSuccessful()) { - ParsePost.parsePosts(response.body(), -1, filter, postFilter.allowNSFW, readPostList, + ParsePost.parsePosts(response.body(), -1, filter, postFilter, readPostList, new ParsePost.ParsePostsListingListener() { @Override public void onParsePostsListingSuccess(LinkedHashSet newPosts, String lastItem) { @@ -673,7 +673,7 @@ public class PostDataSource extends PageKeyedDataSource { @Override public void onResponse(@NonNull Call call, @NonNull retrofit2.Response response) { if (response.isSuccessful()) { - ParsePost.parsePosts(response.body(), -1, filter, postFilter.allowNSFW, readPostList, + ParsePost.parsePosts(response.body(), -1, filter, postFilter, readPostList, new ParsePost.ParsePostsListingListener() { @Override public void onParsePostsListingSuccess(LinkedHashSet newPosts, String lastItem) { @@ -764,7 +764,7 @@ public class PostDataSource extends PageKeyedDataSource { @Override public void onResponse(@NonNull Call call, @NonNull retrofit2.Response response) { if (response.isSuccessful()) { - ParsePost.parsePosts(response.body(), -1, filter, postFilter.allowNSFW, readPostList, + ParsePost.parsePosts(response.body(), -1, filter, postFilter, readPostList, new ParsePost.ParsePostsListingListener() { @Override public void onParsePostsListingSuccess(LinkedHashSet newPosts, String lastItem) { @@ -824,7 +824,7 @@ public class PostDataSource extends PageKeyedDataSource { @Override public void onResponse(@NonNull Call call, @NonNull retrofit2.Response response) { if (response.isSuccessful()) { - ParsePost.parsePosts(response.body(), -1, filter, postFilter.allowNSFW, readPostList, + ParsePost.parsePosts(response.body(), -1, filter, postFilter, readPostList, new ParsePost.ParsePostsListingListener() { @Override public void onParsePostsListingSuccess(LinkedHashSet newPosts, String lastItem) { @@ -895,7 +895,7 @@ public class PostDataSource extends PageKeyedDataSource { @Override public void onResponse(@NonNull Call call, @NonNull retrofit2.Response response) { if (response.isSuccessful()) { - ParsePost.parsePosts(response.body(), -1, filter, postFilter.allowNSFW, readPostList, + ParsePost.parsePosts(response.body(), -1, filter, postFilter, readPostList, new ParsePost.ParsePostsListingListener() { @Override public void onParsePostsListingSuccess(LinkedHashSet newPosts, String lastItem) {