From efd8d1c080d1f69775dc8952fb521b9bd466d774 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Fri, 5 Feb 2021 12:27:58 +0800 Subject: [PATCH] Fix NullPointerException in PostFilter.mergePostFilter. Minor bugs fixed in CommentsListingFragment. --- .../fragments/CommentsListingFragment.java | 134 +++++++++--------- .../postfilter/PostFilter.java | 51 ++++--- 2 files changed, 98 insertions(+), 87 deletions(-) diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/CommentsListingFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/CommentsListingFragment.java index 3675a8ea..c730b4ba 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/CommentsListingFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/CommentsListingFragment.java @@ -254,80 +254,82 @@ public class CommentsListingFragment extends Fragment implements FragmentCommuni } private void bindView(Resources resources) { - mLinearLayoutManager = new LinearLayoutManager(mActivity); - mCommentRecyclerView.setLayoutManager(mLinearLayoutManager); + if (mActivity != null && !mActivity.isFinishing() && !mActivity.isDestroyed()) { + mLinearLayoutManager = new LinearLayoutManager(mActivity); + mCommentRecyclerView.setLayoutManager(mLinearLayoutManager); - mAdapter = new CommentsListingRecyclerViewAdapter(mActivity, mOauthRetrofit, customThemeWrapper, - getResources().getConfiguration().locale, mSharedPreferences, - getArguments().getString(EXTRA_ACCESS_TOKEN), getArguments().getString(EXTRA_ACCOUNT_NAME), - () -> mCommentViewModel.retryLoadingMore()); + mAdapter = new CommentsListingRecyclerViewAdapter(mActivity, mOauthRetrofit, customThemeWrapper, + getResources().getConfiguration().locale, mSharedPreferences, + getArguments().getString(EXTRA_ACCESS_TOKEN), getArguments().getString(EXTRA_ACCOUNT_NAME), + () -> mCommentViewModel.retryLoadingMore()); - String username = getArguments().getString(EXTRA_USERNAME); - String sort = mSortTypeSharedPreferences.getString(SharedPreferencesUtils.SORT_TYPE_USER_COMMENT, SortType.Type.NEW.name()); - if(sort.equals(SortType.Type.CONTROVERSIAL.name()) || sort.equals(SortType.Type.TOP.name())) { - String sortTime = mSortTypeSharedPreferences.getString(SharedPreferencesUtils.SORT_TIME_USER_COMMENT, SortType.Time.ALL.name()); - sortType = new SortType(SortType.Type.valueOf(sort.toUpperCase()), SortType.Time.valueOf(sortTime.toUpperCase())); - } else { - sortType = new SortType(SortType.Type.valueOf(sort.toUpperCase())); - } + String username = getArguments().getString(EXTRA_USERNAME); + String sort = mSortTypeSharedPreferences.getString(SharedPreferencesUtils.SORT_TYPE_USER_COMMENT, SortType.Type.NEW.name()); + if(sort.equals(SortType.Type.CONTROVERSIAL.name()) || sort.equals(SortType.Type.TOP.name())) { + String sortTime = mSortTypeSharedPreferences.getString(SharedPreferencesUtils.SORT_TIME_USER_COMMENT, SortType.Time.ALL.name()); + sortType = new SortType(SortType.Type.valueOf(sort.toUpperCase()), SortType.Time.valueOf(sortTime.toUpperCase())); + } else { + sortType = new SortType(SortType.Type.valueOf(sort.toUpperCase())); + } - mCommentRecyclerView.setAdapter(mAdapter); + mCommentRecyclerView.setAdapter(mAdapter); - if (mActivity instanceof RecyclerViewContentScrollingInterface) { - mCommentRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { - @Override - public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) { - if (dy > 0) { - ((RecyclerViewContentScrollingInterface) mActivity).contentScrollDown(); - } else if (dy < 0) { - ((RecyclerViewContentScrollingInterface) mActivity).contentScrollUp(); + if (mActivity instanceof RecyclerViewContentScrollingInterface) { + mCommentRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { + @Override + public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) { + if (dy > 0) { + ((RecyclerViewContentScrollingInterface) mActivity).contentScrollDown(); + } else if (dy < 0) { + ((RecyclerViewContentScrollingInterface) mActivity).contentScrollUp(); + } } + }); + } + + CommentViewModel.Factory factory; + + if (mAccessToken == null) { + factory = new CommentViewModel.Factory(mRetrofit, + resources.getConfiguration().locale, null, username, sortType, + getArguments().getBoolean(EXTRA_ARE_SAVED_COMMENTS)); + } else { + factory = new CommentViewModel.Factory(mOauthRetrofit, + resources.getConfiguration().locale, mAccessToken, username, sortType, + getArguments().getBoolean(EXTRA_ARE_SAVED_COMMENTS)); + } + + mCommentViewModel = new ViewModelProvider(this, factory).get(CommentViewModel.class); + mCommentViewModel.getComments().observe(getViewLifecycleOwner(), comments -> mAdapter.submitList(comments)); + + mCommentViewModel.hasComment().observe(getViewLifecycleOwner(), hasComment -> { + mSwipeRefreshLayout.setRefreshing(false); + if (hasComment) { + mFetchCommentInfoLinearLayout.setVisibility(View.GONE); + } else { + mFetchCommentInfoLinearLayout.setOnClickListener(view -> { + //Do nothing + }); + showErrorView(R.string.no_comments); } }); + + mCommentViewModel.getInitialLoadingState().observe(getViewLifecycleOwner(), networkState -> { + if (networkState.getStatus().equals(NetworkState.Status.SUCCESS)) { + mSwipeRefreshLayout.setRefreshing(false); + } else if (networkState.getStatus().equals(NetworkState.Status.FAILED)) { + mSwipeRefreshLayout.setRefreshing(false); + mFetchCommentInfoLinearLayout.setOnClickListener(view -> refresh()); + showErrorView(R.string.load_comments_failed); + } else { + mSwipeRefreshLayout.setRefreshing(true); + } + }); + + mCommentViewModel.getPaginationNetworkState().observe(getViewLifecycleOwner(), networkState -> mAdapter.setNetworkState(networkState)); + + mSwipeRefreshLayout.setOnRefreshListener(() -> mCommentViewModel.refresh()); } - - CommentViewModel.Factory factory; - - if (mAccessToken == null) { - factory = new CommentViewModel.Factory(mRetrofit, - resources.getConfiguration().locale, null, username, sortType, - getArguments().getBoolean(EXTRA_ARE_SAVED_COMMENTS)); - } else { - factory = new CommentViewModel.Factory(mOauthRetrofit, - resources.getConfiguration().locale, mAccessToken, username, sortType, - getArguments().getBoolean(EXTRA_ARE_SAVED_COMMENTS)); - } - - mCommentViewModel = new ViewModelProvider(this, factory).get(CommentViewModel.class); - mCommentViewModel.getComments().observe(getViewLifecycleOwner(), comments -> mAdapter.submitList(comments)); - - mCommentViewModel.hasComment().observe(getViewLifecycleOwner(), hasComment -> { - mSwipeRefreshLayout.setRefreshing(false); - if (hasComment) { - mFetchCommentInfoLinearLayout.setVisibility(View.GONE); - } else { - mFetchCommentInfoLinearLayout.setOnClickListener(view -> { - //Do nothing - }); - showErrorView(R.string.no_comments); - } - }); - - mCommentViewModel.getInitialLoadingState().observe(getViewLifecycleOwner(), networkState -> { - if (networkState.getStatus().equals(NetworkState.Status.SUCCESS)) { - mSwipeRefreshLayout.setRefreshing(false); - } else if (networkState.getStatus().equals(NetworkState.Status.FAILED)) { - mSwipeRefreshLayout.setRefreshing(false); - mFetchCommentInfoLinearLayout.setOnClickListener(view -> refresh()); - showErrorView(R.string.load_comments_failed); - } else { - mSwipeRefreshLayout.setRefreshing(true); - } - }); - - mCommentViewModel.getPaginationNetworkState().observe(getViewLifecycleOwner(), networkState -> mAdapter.setNetworkState(networkState)); - - mSwipeRefreshLayout.setOnRefreshListener(() -> mCommentViewModel.refresh()); } public void changeSortType(SortType sortType) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/PostFilter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/PostFilter.java index 30125ba8..99d40536 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/PostFilter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/PostFilter.java @@ -212,10 +212,10 @@ public class PostFilter implements Parcelable { } public static PostFilter mergePostFilter(List postFilterList) { - PostFilter postFilter = new PostFilter(); if (postFilterList.size() == 1) { return postFilterList.get(0); } + PostFilter postFilter = new PostFilter(); StringBuilder stringBuilder; postFilter.name = "Merged"; for (PostFilter p : postFilterList) { @@ -229,30 +229,39 @@ public class PostFilter implements Parcelable { postFilter.onlyNSFW = p.onlyNSFW ? p.onlyNSFW : postFilter.onlyNSFW; postFilter.onlySpoiler = p.onlySpoiler ? p.onlySpoiler : postFilter.onlySpoiler; - postFilter.postTitleExcludesRegex = p.postTitleExcludesRegex.equals("") ? postFilter.postTitleExcludesRegex : p.postTitleExcludesRegex; - stringBuilder = new StringBuilder(postFilter.postTitleExcludesStrings); - stringBuilder.append(",").append(p.postTitleExcludesStrings); - postFilter.postTitleExcludesStrings = stringBuilder.toString(); + if (p.postTitleExcludesRegex != null && !p.postTitleExcludesRegex.equals("")) { + postFilter.postTitleExcludesRegex = p.postTitleExcludesRegex; + } - postFilter.excludeSubreddits = p.excludeSubreddits.equals("") ? postFilter.excludeSubreddits : p.postTitleExcludesRegex; - stringBuilder = new StringBuilder(postFilter.excludeSubreddits); - stringBuilder.append(",").append(p.excludeSubreddits); - postFilter.excludeSubreddits = stringBuilder.toString(); + if (p.postTitleExcludesStrings != null && !p.postTitleExcludesStrings.equals("")) { + stringBuilder = new StringBuilder(postFilter.postTitleExcludesStrings == null ? "" : postFilter.postTitleExcludesStrings); + stringBuilder.append(",").append(p.postTitleExcludesStrings); + postFilter.postTitleExcludesStrings = stringBuilder.toString(); + } - postFilter.excludeUsers = p.excludeUsers.equals("") ? postFilter.excludeUsers : p.postTitleExcludesRegex; - stringBuilder = new StringBuilder(postFilter.excludeUsers); - stringBuilder.append(",").append(p.excludeUsers); - postFilter.excludeUsers = stringBuilder.toString(); + if (p.excludeSubreddits != null && !p.excludeSubreddits.equals("")) { + stringBuilder = new StringBuilder(postFilter.excludeSubreddits == null ? "" : postFilter.excludeSubreddits); + stringBuilder.append(",").append(p.excludeSubreddits); + postFilter.excludeSubreddits = stringBuilder.toString(); + } - postFilter.containFlairs = p.containFlairs.equals("") ? postFilter.containFlairs : p.postTitleExcludesRegex; - stringBuilder = new StringBuilder(postFilter.containFlairs); - stringBuilder.append(",").append(p.containFlairs); - postFilter.containFlairs = stringBuilder.toString(); + if (p.excludeUsers != null && !p.excludeUsers.equals("")) { + stringBuilder = new StringBuilder(postFilter.excludeUsers == null ? "" : postFilter.excludeUsers); + stringBuilder.append(",").append(p.excludeUsers); + postFilter.excludeUsers = stringBuilder.toString(); + } - postFilter.excludeFlairs = p.excludeFlairs.equals("") ? postFilter.excludeFlairs : p.postTitleExcludesRegex; - stringBuilder = new StringBuilder(postFilter.excludeFlairs); - stringBuilder.append(",").append(p.excludeFlairs); - postFilter.excludeFlairs = stringBuilder.toString(); + if (p.containFlairs != null && !p.containFlairs.equals("")) { + stringBuilder = new StringBuilder(postFilter.containFlairs == null ? "" : postFilter.containFlairs); + stringBuilder.append(",").append(p.containFlairs); + postFilter.containFlairs = stringBuilder.toString(); + } + + if (p.excludeFlairs != null && !p.excludeFlairs.equals("")) { + stringBuilder = new StringBuilder(postFilter.excludeFlairs == null ? "" : postFilter.excludeFlairs); + stringBuilder.append(",").append(p.excludeFlairs); + postFilter.excludeFlairs = stringBuilder.toString(); + } postFilter.containTextType = p.containTextType || postFilter.containTextType; postFilter.containLinkType = p.containLinkType || postFilter.containLinkType;