From 2f94ef3f9af0e6fc05e2b243b3f181e6769d8b2e Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Wed, 14 Oct 2020 16:02:55 +0800 Subject: Fix NSFW posts showing in SearchResultActivity when NSFW is disabled. Hide NSFW subreddit results in SearchResultActivity if NSFW is disabled. --- .../Activity/SearchResultActivity.java | 1 + .../Subreddit/FetchSubredditData.java | 21 ++++++++++---------- .../Subreddit/ParseSubredditData.java | 23 ++++++++++++++-------- 3 files changed, 27 insertions(+), 18 deletions(-) (limited to 'app/src/main/java/ml') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/SearchResultActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/SearchResultActivity.java index 02c2c73a..bd6cd543 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/SearchResultActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/SearchResultActivity.java @@ -382,6 +382,7 @@ public class SearchResultActivity extends BaseActivity implements SortTypeSelect bundle.putString(PostFragment.EXTRA_QUERY, mQuery); bundle.putInt(PostFragment.EXTRA_FILTER, PostFragment.EXTRA_NO_FILTER); bundle.putString(PostFragment.EXTRA_ACCESS_TOKEN, mAccessToken); + bundle.putString(PostFragment.EXTRA_ACCOUNT_NAME, mAccountName); mFragment.setArguments(bundle); return mFragment; } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Subreddit/FetchSubredditData.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Subreddit/FetchSubredditData.java index f767167b..f75ed256 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Subreddit/FetchSubredditData.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Subreddit/FetchSubredditData.java @@ -56,16 +56,17 @@ public class FetchSubredditData { @Override public void onResponse(@NonNull Call call, @NonNull Response response) { if (response.isSuccessful()) { - ParseSubredditData.parseSubredditListingData(response.body(), new ParseSubredditData.ParseSubredditListingDataListener() { - @Override - public void onParseSubredditListingDataSuccess(ArrayList subredditData, String after) { - fetchSubredditListingDataListener.onFetchSubredditListingDataSuccess(subredditData, after); - } - - @Override - public void onParseSubredditListingDataFail() { - fetchSubredditListingDataListener.onFetchSubredditListingDataFail(); - } + ParseSubredditData.parseSubredditListingData(response.body(), nsfw, + new ParseSubredditData.ParseSubredditListingDataListener() { + @Override + public void onParseSubredditListingDataSuccess(ArrayList subredditData, String after) { + fetchSubredditListingDataListener.onFetchSubredditListingDataSuccess(subredditData, after); + } + + @Override + public void onParseSubredditListingDataFail() { + fetchSubredditListingDataListener.onFetchSubredditListingDataFail(); + } }); } else { fetchSubredditListingDataListener.onFetchSubredditListingDataFail(); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Subreddit/ParseSubredditData.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Subreddit/ParseSubredditData.java index 720e663b..4eb9aecd 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Subreddit/ParseSubredditData.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Subreddit/ParseSubredditData.java @@ -16,18 +16,21 @@ class ParseSubredditData { new ParseSubredditDataAsyncTask(response, parseSubredditDataListener).execute(); } - static void parseSubredditListingData(String response, ParseSubredditListingDataListener parseSubredditListingDataListener) { - new ParseSubredditListingDataAsyncTask(response, parseSubredditListingDataListener).execute(); + static void parseSubredditListingData(String response, boolean nsfw, ParseSubredditListingDataListener parseSubredditListingDataListener) { + new ParseSubredditListingDataAsyncTask(response, nsfw, parseSubredditListingDataListener).execute(); } - private static SubredditData parseSubredditData(JSONObject subredditDataJsonObject) throws JSONException { + private static SubredditData parseSubredditData(JSONObject subredditDataJsonObject, boolean nsfw) throws JSONException { + boolean isNSFW = !subredditDataJsonObject.isNull(JSONUtils.OVER18_KEY) && subredditDataJsonObject.getBoolean(JSONUtils.OVER18_KEY); + if (!nsfw && isNSFW) { + return null; + } String id = subredditDataJsonObject.getString(JSONUtils.NAME_KEY); String subredditFullName = subredditDataJsonObject.getString(JSONUtils.DISPLAY_NAME_KEY); String description = subredditDataJsonObject.getString(JSONUtils.PUBLIC_DESCRIPTION_KEY).trim(); String sidebarDescription = Utils.modifyMarkdown(subredditDataJsonObject.getString(JSONUtils.DESCRIPTION_KEY).trim()); long createdUTC = subredditDataJsonObject.getLong(JSONUtils.CREATED_UTC_KEY) * 1000; String suggestedCommentSort = subredditDataJsonObject.getString(JSONUtils.SUGGESTED_COMMENT_SORT_KEY); - boolean isNSFW = !subredditDataJsonObject.isNull(JSONUtils.OVER18_KEY) && subredditDataJsonObject.getBoolean(JSONUtils.OVER18_KEY); String bannerImageUrl; if (subredditDataJsonObject.isNull(JSONUtils.BANNER_BACKGROUND_IMAGE_KEY)) { @@ -93,7 +96,7 @@ class ParseSubredditData { try { JSONObject data = jsonResponse.getJSONObject(JSONUtils.DATA_KEY); mNCurrentOnlineSubscribers = data.getInt(JSONUtils.ACTIVE_USER_COUNT_KEY); - subredditData = parseSubredditData(data); + subredditData = parseSubredditData(data, true); } catch (JSONException e) { parseFailed = true; e.printStackTrace(); @@ -113,15 +116,17 @@ class ParseSubredditData { private static class ParseSubredditListingDataAsyncTask extends AsyncTask { private JSONObject jsonResponse; + private boolean nsfw; private boolean parseFailed; private ParseSubredditListingDataListener parseSubredditListingDataListener; private ArrayList subredditListingData; private String after; - ParseSubredditListingDataAsyncTask(String response, ParseSubredditListingDataListener parseSubredditListingDataListener) { + ParseSubredditListingDataAsyncTask(String response, boolean nsfw, ParseSubredditListingDataListener parseSubredditListingDataListener) { this.parseSubredditListingDataListener = parseSubredditListingDataListener; try { jsonResponse = new JSONObject(response); + this.nsfw = nsfw; parseFailed = false; subredditListingData = new ArrayList<>(); } catch (JSONException e) { @@ -138,8 +143,10 @@ class ParseSubredditData { .getJSONArray(JSONUtils.CHILDREN_KEY); for (int i = 0; i < children.length(); i++) { JSONObject data = children.getJSONObject(i).getJSONObject(JSONUtils.DATA_KEY); - SubredditData subredditData = parseSubredditData(data); - subredditListingData.add(subredditData); + SubredditData subredditData = parseSubredditData(data, nsfw); + if (subredditData != null) { + subredditListingData.add(subredditData); + } } after = jsonResponse.getJSONObject(JSONUtils.DATA_KEY).getString(JSONUtils.AFTER_KEY); } -- cgit v1.2.3