From f997e22f90150957440e750d0886a4115aaf9471 Mon Sep 17 00:00:00 2001 From: Docile-Alligator <25734209+Docile-Alligator@users.noreply.github.com> Date: Fri, 20 Oct 2023 00:16:46 -0400 Subject: Start applying comment filter. --- .../infinityforreddit/comment/FetchComment.java | 16 ++++--- .../infinityforreddit/comment/ParseComment.java | 16 ++++--- .../commentfilter/FetchCommentFilter.java | 25 +++++++++++ .../fragments/ViewPostDetailFragment.java | 52 +++++++++++++++------- 4 files changed, 80 insertions(+), 29 deletions(-) create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/commentfilter/FetchCommentFilter.java (limited to 'app/src/main') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/comment/FetchComment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/comment/FetchComment.java index 022438a8..0c2b3762 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/comment/FetchComment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/comment/FetchComment.java @@ -6,11 +6,11 @@ import androidx.annotation.NonNull; import androidx.annotation.Nullable; import java.util.ArrayList; -import java.util.Locale; import java.util.concurrent.Executor; import ml.docilealligator.infinityforreddit.SortType; import ml.docilealligator.infinityforreddit.apis.RedditAPI; +import ml.docilealligator.infinityforreddit.commentfilter.CommentFilter; import ml.docilealligator.infinityforreddit.utils.APIUtils; import retrofit2.Call; import retrofit2.Callback; @@ -20,8 +20,9 @@ import retrofit2.Retrofit; public class FetchComment { public static void fetchComments(Executor executor, Handler handler, Retrofit retrofit, @Nullable String accessToken, String article, - String commentId, SortType.Type sortType, String contextNumber, boolean expandChildren, - Locale locale, FetchCommentListener fetchCommentListener) { + String commentId, SortType.Type sortType, String contextNumber, + boolean expandChildren, CommentFilter commentFilter, + FetchCommentListener fetchCommentListener) { RedditAPI api = retrofit.create(RedditAPI.class); Call comments; if (accessToken == null) { @@ -39,12 +40,13 @@ public class FetchComment { } } - comments.enqueue(new Callback() { + comments.enqueue(new Callback<>() { @Override public void onResponse(@NonNull Call call, @NonNull Response response) { if (response.isSuccessful()) { ParseComment.parseComment(executor, handler, response.body(), - expandChildren, new ParseComment.ParseCommentListener() { + expandChildren, commentFilter, + new ParseComment.ParseCommentListener() { @Override public void onParseCommentSuccess(ArrayList topLevelComments, ArrayList expandedComments, @@ -95,7 +97,7 @@ public class FetchComment { sortType, APIUtils.getOAuthHeader(accessToken)); } - moreComments.enqueue(new Callback() { + moreComments.enqueue(new Callback<>() { @Override public void onResponse(@NonNull Call call, @NonNull Response response) { if (response.isSuccessful()) { @@ -106,7 +108,7 @@ public class FetchComment { ArrayList expandedComments, String parentId, ArrayList moreChildrenIds) { fetchMoreCommentListener.onFetchMoreCommentSuccess( - topLevelComments,expandedComments, moreChildrenIds); + topLevelComments, expandedComments, moreChildrenIds); } @Override diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/comment/ParseComment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/comment/ParseComment.java index 55f9d872..01d3f95f 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/comment/ParseComment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/comment/ParseComment.java @@ -6,12 +6,10 @@ import static ml.docilealligator.infinityforreddit.comment.Comment.VOTE_TYPE_UPV import android.os.Handler; import android.text.Html; -import android.util.Log; import androidx.annotation.NonNull; import androidx.annotation.Nullable; -import org.checkerframework.checker.units.qual.A; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; @@ -20,12 +18,13 @@ import java.util.ArrayList; import java.util.List; import java.util.concurrent.Executor; +import ml.docilealligator.infinityforreddit.commentfilter.CommentFilter; import ml.docilealligator.infinityforreddit.utils.JSONUtils; import ml.docilealligator.infinityforreddit.utils.Utils; public class ParseComment { public static void parseComment(Executor executor, Handler handler, String response, - boolean expandChildren, + boolean expandChildren, CommentFilter commentFilter, ParseCommentListener parseCommentListener) { executor.execute(() -> { try { @@ -38,7 +37,7 @@ public class ParseComment { ArrayList moreChildrenIds = new ArrayList<>(); ArrayList newComments = new ArrayList<>(); - parseCommentRecursion(childrenArray, newComments, moreChildrenIds, 0); + parseCommentRecursion(childrenArray, newComments, moreChildrenIds, 0, commentFilter); expandChildren(newComments, expandedNewComments, expandChildren); ArrayList commentData; @@ -159,7 +158,8 @@ public class ParseComment { } private static void parseCommentRecursion(JSONArray comments, ArrayList newCommentData, - ArrayList moreChildrenIds, int depth) throws JSONException { + ArrayList moreChildrenIds, int depth, + CommentFilter commentFilter) throws JSONException { int actualCommentLength; if (comments.length() == 0) { @@ -189,13 +189,17 @@ public class ParseComment { for (int i = 0; i < actualCommentLength; i++) { JSONObject data = comments.getJSONObject(i).getJSONObject(JSONUtils.DATA_KEY); Comment singleComment = parseSingleComment(data, depth); + if (!CommentFilter.isCommentAllowed(singleComment, commentFilter)) { + continue; + } if (data.get(JSONUtils.REPLIES_KEY) instanceof JSONObject) { JSONArray childrenArray = data.getJSONObject(JSONUtils.REPLIES_KEY) .getJSONObject(JSONUtils.DATA_KEY).getJSONArray(JSONUtils.CHILDREN_KEY); ArrayList children = new ArrayList<>(); ArrayList nextMoreChildrenIds = new ArrayList<>(); - parseCommentRecursion(childrenArray, children, nextMoreChildrenIds, singleComment.getDepth()); + parseCommentRecursion(childrenArray, children, nextMoreChildrenIds, singleComment.getDepth(), + commentFilter); singleComment.addChildren(children); singleComment.setMoreChildrenIds(nextMoreChildrenIds); singleComment.setChildCount(getChildCount(singleComment)); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/commentfilter/FetchCommentFilter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/commentfilter/FetchCommentFilter.java new file mode 100644 index 00000000..8f62c531 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/commentfilter/FetchCommentFilter.java @@ -0,0 +1,25 @@ +package ml.docilealligator.infinityforreddit.commentfilter; + +import android.os.Handler; + +import java.util.List; +import java.util.concurrent.Executor; + +import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; + +public class FetchCommentFilter { + public static void fetchCommentFilter(Executor executor, Handler handler, + RedditDataRoomDatabase redditDataRoomDatabase, + String subreddit, FetchCommentFilterListener fetchCommentFilterListener) { + executor.execute(() -> { + List commentFilterList = redditDataRoomDatabase.commentFilterDao().getValidCommentFilters(CommentFilterUsage.SUBREDDIT_TYPE, subreddit); + CommentFilter commentFilter = CommentFilter.mergeCommentFilter(commentFilterList); + + handler.post(() -> fetchCommentFilterListener.success(commentFilter)); + }); + } + + public interface FetchCommentFilterListener { + void success(CommentFilter commentFilter); + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/ViewPostDetailFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/ViewPostDetailFragment.java index 3b7a90d1..36fecec9 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/ViewPostDetailFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/ViewPostDetailFragment.java @@ -17,6 +17,7 @@ import android.graphics.drawable.ColorDrawable; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.os.Handler; +import android.os.Looper; import android.view.HapticFeedbackConstants; import android.view.LayoutInflater; import android.view.Menu; @@ -93,6 +94,8 @@ import ml.docilealligator.infinityforreddit.comment.FetchComment; import ml.docilealligator.infinityforreddit.comment.FetchRemovedComment; import ml.docilealligator.infinityforreddit.comment.FetchRemovedCommentReveddit; import ml.docilealligator.infinityforreddit.comment.ParseComment; +import ml.docilealligator.infinityforreddit.commentfilter.CommentFilter; +import ml.docilealligator.infinityforreddit.commentfilter.FetchCommentFilter; import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.customviews.CustomToroContainer; import ml.docilealligator.infinityforreddit.customviews.LinearLayoutManagerBugFixed; @@ -216,6 +219,10 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic boolean mRespectSubredditRecommendedSortType; @State long viewPostDetailFragmentId; + @State + boolean commentFilterFetched; + @State + CommentFilter mCommentFilter; private ViewPostDetailActivity activity; private RequestManager mGlide; private Locale mLocale; @@ -629,21 +636,15 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic mRecyclerView.setAdapter(mConcatAdapter); } - if (comments == null) { - fetchCommentsRespectRecommendedSort(false); + if (commentFilterFetched) { + fetchCommentsAfterCommentFilterAvailable(); } else { - if (isRefreshing) { - isRefreshing = false; - refresh(true, true); - } else if (isFetchingComments) { - fetchCommentsRespectRecommendedSort(false); - } else { - mCommentsAdapter.addComments(comments, hasMoreChildren); - if (isLoadingMoreChildren) { - isLoadingMoreChildren = false; - fetchMoreComments(); - } - } + FetchCommentFilter.fetchCommentFilter(mExecutor, new Handler(Looper.getMainLooper()), mRedditDataRoomDatabase, mPost.getSubredditName(), + commentFilter -> { + mCommentFilter = commentFilter; + commentFilterFetched = true; + fetchCommentsAfterCommentFilterAvailable(); + }); } } @@ -654,6 +655,25 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic }); } + public void fetchCommentsAfterCommentFilterAvailable() { + if (comments == null) { + fetchCommentsRespectRecommendedSort(false); + } else { + if (isRefreshing) { + isRefreshing = false; + refresh(true, true); + } else if (isFetchingComments) { + fetchCommentsRespectRecommendedSort(false); + } else { + mCommentsAdapter.addComments(comments, hasMoreChildren); + if (isLoadingMoreChildren) { + isLoadingMoreChildren = false; + fetchMoreComments(); + } + } + } + } + private void setupMenu() { if (mMenu != null) { MenuItem saveItem = mMenu.findItem(R.id.action_save_view_post_detail_fragment); @@ -1341,7 +1361,7 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic fetchCommentsRespectRecommendedSort(false); } else { ParseComment.parseComment(mExecutor, new Handler(), response.body(), - mExpandChildren, new ParseComment.ParseCommentListener() { + mExpandChildren, mCommentFilter, new ParseComment.ParseCommentListener() { @Override public void onParseCommentSuccess(ArrayList topLevelComments, ArrayList expandedComments, String parentId, ArrayList moreChildrenIds) { ViewPostDetailFragment.this.children = moreChildrenIds; @@ -1482,7 +1502,7 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic Retrofit retrofit = mAccessToken == null ? mRetrofit : mOauthRetrofit; FetchComment.fetchComments(mExecutor, new Handler(), retrofit, mAccessToken, mPost.getId(), commentId, sortType, - mContextNumber, mExpandChildren, mLocale, new FetchComment.FetchCommentListener() { + mContextNumber, mExpandChildren, mCommentFilter, new FetchComment.FetchCommentListener() { @Override public void onFetchCommentSuccess(ArrayList expandedComments, String parentId, ArrayList children) { -- cgit v1.2.3