Fix NullPointerException in PostFilter.mergePostFilter. Minor bugs fixed in CommentsListingFragment.

This commit is contained in:
Alex Ning 2021-02-05 12:27:58 +08:00
parent 370e02c311
commit efd8d1c080
2 changed files with 98 additions and 87 deletions

View File

@ -254,80 +254,82 @@ public class CommentsListingFragment extends Fragment implements FragmentCommuni
} }
private void bindView(Resources resources) { private void bindView(Resources resources) {
mLinearLayoutManager = new LinearLayoutManager(mActivity); if (mActivity != null && !mActivity.isFinishing() && !mActivity.isDestroyed()) {
mCommentRecyclerView.setLayoutManager(mLinearLayoutManager); mLinearLayoutManager = new LinearLayoutManager(mActivity);
mCommentRecyclerView.setLayoutManager(mLinearLayoutManager);
mAdapter = new CommentsListingRecyclerViewAdapter(mActivity, mOauthRetrofit, customThemeWrapper, mAdapter = new CommentsListingRecyclerViewAdapter(mActivity, mOauthRetrofit, customThemeWrapper,
getResources().getConfiguration().locale, mSharedPreferences, getResources().getConfiguration().locale, mSharedPreferences,
getArguments().getString(EXTRA_ACCESS_TOKEN), getArguments().getString(EXTRA_ACCOUNT_NAME), getArguments().getString(EXTRA_ACCESS_TOKEN), getArguments().getString(EXTRA_ACCOUNT_NAME),
() -> mCommentViewModel.retryLoadingMore()); () -> mCommentViewModel.retryLoadingMore());
String username = getArguments().getString(EXTRA_USERNAME); String username = getArguments().getString(EXTRA_USERNAME);
String sort = mSortTypeSharedPreferences.getString(SharedPreferencesUtils.SORT_TYPE_USER_COMMENT, SortType.Type.NEW.name()); 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())) { 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()); 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())); sortType = new SortType(SortType.Type.valueOf(sort.toUpperCase()), SortType.Time.valueOf(sortTime.toUpperCase()));
} else { } else {
sortType = new SortType(SortType.Type.valueOf(sort.toUpperCase())); sortType = new SortType(SortType.Type.valueOf(sort.toUpperCase()));
} }
mCommentRecyclerView.setAdapter(mAdapter); mCommentRecyclerView.setAdapter(mAdapter);
if (mActivity instanceof RecyclerViewContentScrollingInterface) { if (mActivity instanceof RecyclerViewContentScrollingInterface) {
mCommentRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { mCommentRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override @Override
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) { public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
if (dy > 0) { if (dy > 0) {
((RecyclerViewContentScrollingInterface) mActivity).contentScrollDown(); ((RecyclerViewContentScrollingInterface) mActivity).contentScrollDown();
} else if (dy < 0) { } else if (dy < 0) {
((RecyclerViewContentScrollingInterface) mActivity).contentScrollUp(); ((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) { public void changeSortType(SortType sortType) {

View File

@ -212,10 +212,10 @@ public class PostFilter implements Parcelable {
} }
public static PostFilter mergePostFilter(List<PostFilter> postFilterList) { public static PostFilter mergePostFilter(List<PostFilter> postFilterList) {
PostFilter postFilter = new PostFilter();
if (postFilterList.size() == 1) { if (postFilterList.size() == 1) {
return postFilterList.get(0); return postFilterList.get(0);
} }
PostFilter postFilter = new PostFilter();
StringBuilder stringBuilder; StringBuilder stringBuilder;
postFilter.name = "Merged"; postFilter.name = "Merged";
for (PostFilter p : postFilterList) { for (PostFilter p : postFilterList) {
@ -229,30 +229,39 @@ public class PostFilter implements Parcelable {
postFilter.onlyNSFW = p.onlyNSFW ? p.onlyNSFW : postFilter.onlyNSFW; postFilter.onlyNSFW = p.onlyNSFW ? p.onlyNSFW : postFilter.onlyNSFW;
postFilter.onlySpoiler = p.onlySpoiler ? p.onlySpoiler : postFilter.onlySpoiler; postFilter.onlySpoiler = p.onlySpoiler ? p.onlySpoiler : postFilter.onlySpoiler;
postFilter.postTitleExcludesRegex = p.postTitleExcludesRegex.equals("") ? postFilter.postTitleExcludesRegex : p.postTitleExcludesRegex; if (p.postTitleExcludesRegex != null && !p.postTitleExcludesRegex.equals("")) {
stringBuilder = new StringBuilder(postFilter.postTitleExcludesStrings); postFilter.postTitleExcludesRegex = p.postTitleExcludesRegex;
stringBuilder.append(",").append(p.postTitleExcludesStrings); }
postFilter.postTitleExcludesStrings = stringBuilder.toString();
postFilter.excludeSubreddits = p.excludeSubreddits.equals("") ? postFilter.excludeSubreddits : p.postTitleExcludesRegex; if (p.postTitleExcludesStrings != null && !p.postTitleExcludesStrings.equals("")) {
stringBuilder = new StringBuilder(postFilter.excludeSubreddits); stringBuilder = new StringBuilder(postFilter.postTitleExcludesStrings == null ? "" : postFilter.postTitleExcludesStrings);
stringBuilder.append(",").append(p.excludeSubreddits); stringBuilder.append(",").append(p.postTitleExcludesStrings);
postFilter.excludeSubreddits = stringBuilder.toString(); postFilter.postTitleExcludesStrings = stringBuilder.toString();
}
postFilter.excludeUsers = p.excludeUsers.equals("") ? postFilter.excludeUsers : p.postTitleExcludesRegex; if (p.excludeSubreddits != null && !p.excludeSubreddits.equals("")) {
stringBuilder = new StringBuilder(postFilter.excludeUsers); stringBuilder = new StringBuilder(postFilter.excludeSubreddits == null ? "" : postFilter.excludeSubreddits);
stringBuilder.append(",").append(p.excludeUsers); stringBuilder.append(",").append(p.excludeSubreddits);
postFilter.excludeUsers = stringBuilder.toString(); postFilter.excludeSubreddits = stringBuilder.toString();
}
postFilter.containFlairs = p.containFlairs.equals("") ? postFilter.containFlairs : p.postTitleExcludesRegex; if (p.excludeUsers != null && !p.excludeUsers.equals("")) {
stringBuilder = new StringBuilder(postFilter.containFlairs); stringBuilder = new StringBuilder(postFilter.excludeUsers == null ? "" : postFilter.excludeUsers);
stringBuilder.append(",").append(p.containFlairs); stringBuilder.append(",").append(p.excludeUsers);
postFilter.containFlairs = stringBuilder.toString(); postFilter.excludeUsers = stringBuilder.toString();
}
postFilter.excludeFlairs = p.excludeFlairs.equals("") ? postFilter.excludeFlairs : p.postTitleExcludesRegex; if (p.containFlairs != null && !p.containFlairs.equals("")) {
stringBuilder = new StringBuilder(postFilter.excludeFlairs); stringBuilder = new StringBuilder(postFilter.containFlairs == null ? "" : postFilter.containFlairs);
stringBuilder.append(",").append(p.excludeFlairs); stringBuilder.append(",").append(p.containFlairs);
postFilter.excludeFlairs = stringBuilder.toString(); 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.containTextType = p.containTextType || postFilter.containTextType;
postFilter.containLinkType = p.containLinkType || postFilter.containLinkType; postFilter.containLinkType = p.containLinkType || postFilter.containLinkType;