Save PostFilter during Fragment recreation in PostFragment.

This commit is contained in:
Alex Ning 2020-12-23 12:30:13 +08:00
parent 6747efbccb
commit c88ceb737b
2 changed files with 11 additions and 14 deletions

View File

@ -138,6 +138,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
private static final String READ_POST_LIST_STATE = "RPLS"; private static final String READ_POST_LIST_STATE = "RPLS";
private static final String SUBREDDIT_FILTER_LIST_STATE = "SFLS"; private static final String SUBREDDIT_FILTER_LIST_STATE = "SFLS";
private static final String HIDE_READ_POSTS_INDEX_STATE = "HRPIS"; private static final String HIDE_READ_POSTS_INDEX_STATE = "HRPIS";
private static final String POST_FILTER_STATE = "PFS";
@BindView(R.id.swipe_refresh_layout_post_fragment) @BindView(R.id.swipe_refresh_layout_post_fragment)
SwipeRefreshLayout mSwipeRefreshLayout; SwipeRefreshLayout mSwipeRefreshLayout;
@ -385,6 +386,13 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
readPosts = savedInstanceState.getParcelableArrayList(READ_POST_LIST_STATE); readPosts = savedInstanceState.getParcelableArrayList(READ_POST_LIST_STATE);
subredditFilterList = savedInstanceState.getParcelableArrayList(SUBREDDIT_FILTER_LIST_STATE); subredditFilterList = savedInstanceState.getParcelableArrayList(SUBREDDIT_FILTER_LIST_STATE);
hideReadPostsIndex = savedInstanceState.getInt(HIDE_READ_POSTS_INDEX_STATE, 0); hideReadPostsIndex = savedInstanceState.getInt(HIDE_READ_POSTS_INDEX_STATE, 0);
postFilter = savedInstanceState.getParcelable(POST_FILTER_STATE);
} else {
//TODO: Initialize PostFilter
postFilter = getArguments().getParcelable(EXTRA_FILTER);
if (postFilter == null) {
postFilter = new PostFilter();
}
} }
mPostRecyclerView.setOnTouchListener((view, motionEvent) -> { mPostRecyclerView.setOnTouchListener((view, motionEvent) -> {
@ -442,12 +450,6 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
postType = getArguments().getInt(EXTRA_POST_TYPE); postType = getArguments().getInt(EXTRA_POST_TYPE);
//TODO: Initialize PostFilter
postFilter = getArguments().getParcelable(EXTRA_FILTER);
if (postFilter == null) {
postFilter = new PostFilter();
}
String accessToken = getArguments().getString(EXTRA_ACCESS_TOKEN); String accessToken = getArguments().getString(EXTRA_ACCESS_TOKEN);
accountName = getArguments().getString(EXTRA_ACCOUNT_NAME); accountName = getArguments().getString(EXTRA_ACCOUNT_NAME);
postFilter.allowNSFW = mNsfwAndSpoilerSharedPreferences.getBoolean((accountName == null ? "" : accountName) + SharedPreferencesUtils.NSFW_BASE, false); postFilter.allowNSFW = mNsfwAndSpoilerSharedPreferences.getBoolean((accountName == null ? "" : accountName) + SharedPreferencesUtils.NSFW_BASE, false);
@ -1081,6 +1083,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
outState.putInt(RECYCLER_VIEW_POSITION_STATE, outState.putInt(RECYCLER_VIEW_POSITION_STATE,
mStaggeredGridLayoutManager.findFirstVisibleItemPositions(into)[0]); mStaggeredGridLayoutManager.findFirstVisibleItemPositions(into)[0]);
} }
outState.putParcelable(POST_FILTER_STATE, postFilter);
} }
@Override @Override
@ -1252,6 +1255,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
@Override @Override
public void changePostFilter(PostFilter postFilter) { public void changePostFilter(PostFilter postFilter) {
this.postFilter = postFilter;
if (mPostViewModel != null) { if (mPostViewModel != null) {
mPostViewModel.changePostFilter(postFilter); mPostViewModel.changePostFilter(postFilter);
} }
@ -1259,10 +1263,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
@Override @Override
public PostFilter getPostFilter() { public PostFilter getPostFilter() {
if (mPostViewModel != null) { return postFilter;
return mPostViewModel.getPostFilter();
}
return null;
} }
@Override @Override

View File

@ -200,10 +200,6 @@ public class PostViewModel extends ViewModel {
postFilterLiveData.postValue(postFilter); postFilterLiveData.postValue(postFilter);
} }
public PostFilter getPostFilter() {
return postFilterLiveData.getValue();
}
public static class Factory extends ViewModelProvider.NewInstanceFactory { public static class Factory extends ViewModelProvider.NewInstanceFactory {
private Retrofit retrofit; private Retrofit retrofit;
private String accessToken; private String accessToken;