Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass post id instead of post model within intents and extras #5856

Merged
merged 9 commits into from
Jun 1, 2017
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ public void onPostSave() {
@Override
public void run() {
if (mEditPostPreviewFragment != null) {
mEditPostPreviewFragment.loadPost();
mEditPostPreviewFragment.loadPost(mPost);
}
}
});
Expand Down Expand Up @@ -1321,7 +1321,7 @@ public Fragment getItem(int position) {
case 1:
return EditPostSettingsFragment.newInstance(mSite, mPost);
default:
return EditPostPreviewFragment.newInstance(mSite, mPost);
return EditPostPreviewFragment.newInstance(mSite);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,10 @@ public class EditPostPreviewFragment extends Fragment {
private SiteModel mSite;
private PostModel mPost;

public static EditPostPreviewFragment newInstance(SiteModel site, PostModel post) {
public static EditPostPreviewFragment newInstance(SiteModel site) {
EditPostPreviewFragment fragment = new EditPostPreviewFragment();
Bundle bundle = new Bundle();
bundle.putSerializable(WordPress.SITE, site);
bundle.putInt(EditPostActivity.EXTRA_POST_LOCAL_ID, post.getId());
fragment.setArguments(bundle);
return fragment;
}
Expand All @@ -46,7 +45,6 @@ public static EditPostPreviewFragment newInstance(SiteModel site, PostModel post
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putSerializable(WordPress.SITE, mSite);
outState.putInt(EditPostActivity.EXTRA_POST_LOCAL_ID, mPost.getId());
}

@Override
Expand All @@ -60,15 +58,11 @@ private void updateSiteOrFinishActivity(Bundle savedInstanceState) {
if (savedInstanceState == null) {
if (getArguments() != null) {
mSite = (SiteModel) getArguments().getSerializable(WordPress.SITE);
mPost = mPostStore.getPostByLocalPostId(getArguments().getInt(EditPostActivity.EXTRA_POST_LOCAL_ID));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With this change this fragment no longer needs PostStore - we can drop that injection and remove EditPostPreviewFragment from the AppComponent.

} else {
mSite = (SiteModel) getActivity().getIntent().getSerializableExtra(WordPress.SITE);
mPost = mPostStore.getPostByLocalPostId(getActivity().getIntent()
.getIntExtra(EditPostActivity.EXTRA_POST_LOCAL_ID, 0));
}
} else {
mSite = (SiteModel) savedInstanceState.getSerializable(WordPress.SITE);
mPost = mPostStore.getPostByLocalPostId(savedInstanceState.getInt(EditPostActivity.EXTRA_POST_LOCAL_ID));
}

if (mSite == null) {
Expand All @@ -87,7 +81,7 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
@Override
public void onGlobalLayout() {
if (getActivity() != null) {
loadPost();
loadCurrentPost();
}
mTextView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
Expand All @@ -101,7 +95,7 @@ public void onResume() {
super.onResume();

if (getActivity() != null && !mTextView.isLayoutRequested()) {
loadPost();
loadCurrentPost();
}
}

Expand All @@ -115,7 +109,17 @@ public void onPause() {
}
}

public void loadPost() {
public void loadPost(PostModel post) {
// cancel the previous load so we can load the new post
if (mLoadTask != null) {
mLoadTask.cancel(true);
mLoadTask = null;
}
mPost = post;
loadCurrentPost();
}

private void loadCurrentPost() {
if (mLoadTask == null) {
mLoadTask = new LoadPostPreviewTask();
mLoadTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
Expand Down