From c88ceb737be67e0240f5eff51a5570a038bd27c7 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Wed, 23 Dec 2020 12:30:13 +0800 Subject: [PATCH] Save PostFilter during Fragment recreation in PostFragment. --- .../fragments/PostFragment.java | 21 ++++++++++--------- .../infinityforreddit/post/PostViewModel.java | 4 ---- 2 files changed, 11 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/PostFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/PostFragment.java index 4418cc43..96b04e23 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/PostFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/PostFragment.java @@ -138,6 +138,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator { private static final String READ_POST_LIST_STATE = "RPLS"; private static final String SUBREDDIT_FILTER_LIST_STATE = "SFLS"; 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) SwipeRefreshLayout mSwipeRefreshLayout; @@ -385,6 +386,13 @@ public class PostFragment extends Fragment implements FragmentCommunicator { readPosts = savedInstanceState.getParcelableArrayList(READ_POST_LIST_STATE); subredditFilterList = savedInstanceState.getParcelableArrayList(SUBREDDIT_FILTER_LIST_STATE); 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) -> { @@ -442,12 +450,6 @@ public class PostFragment extends Fragment implements FragmentCommunicator { 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); accountName = getArguments().getString(EXTRA_ACCOUNT_NAME); 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, mStaggeredGridLayoutManager.findFirstVisibleItemPositions(into)[0]); } + outState.putParcelable(POST_FILTER_STATE, postFilter); } @Override @@ -1252,6 +1255,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator { @Override public void changePostFilter(PostFilter postFilter) { + this.postFilter = postFilter; if (mPostViewModel != null) { mPostViewModel.changePostFilter(postFilter); } @@ -1259,10 +1263,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator { @Override public PostFilter getPostFilter() { - if (mPostViewModel != null) { - return mPostViewModel.getPostFilter(); - } - return null; + return postFilter; } @Override diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/post/PostViewModel.java b/app/src/main/java/ml/docilealligator/infinityforreddit/post/PostViewModel.java index 83ffcd43..716eabaf 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/post/PostViewModel.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/post/PostViewModel.java @@ -200,10 +200,6 @@ public class PostViewModel extends ViewModel { postFilterLiveData.postValue(postFilter); } - public PostFilter getPostFilter() { - return postFilterLiveData.getValue(); - } - public static class Factory extends ViewModelProvider.NewInstanceFactory { private Retrofit retrofit; private String accessToken;