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 @@ -45,6 +45,7 @@
import org.wordpress.android.ui.plans.PlansActivity;
import org.wordpress.android.ui.posts.AddCategoryActivity;
import org.wordpress.android.ui.posts.EditPostActivity;
import org.wordpress.android.ui.posts.EditPostPreviewFragment;
import org.wordpress.android.ui.posts.EditPostSettingsFragment;
import org.wordpress.android.ui.posts.PostPreviewActivity;
import org.wordpress.android.ui.posts.PostPreviewFragment;
Expand Down Expand Up @@ -149,6 +150,7 @@ public interface AppComponent {

void inject(EditPostActivity object);
void inject(EditPostSettingsFragment object);
void inject(EditPostPreviewFragment object);

void inject(PostPreviewActivity object);
void inject(PostPreviewFragment object);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ public static void editPostOrPageForResult(Activity activity, SiteModel site, Po

Intent intent = new Intent(activity, EditPostActivity.class);
intent.putExtra(WordPress.SITE, site);
intent.putExtra(EditPostActivity.EXTRA_POST, post);
intent.putExtra(EditPostActivity.EXTRA_POST_LOCAL_ID, post.getId());
activity.startActivityForResult(intent, RequestCodes.EDIT_POST);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@
public class EditPostActivity extends AppCompatActivity implements EditorFragmentListener, EditorDragAndDropListener,
ActivityCompat.OnRequestPermissionsResultCallback, EditorWebViewCompatibility.ReflectionFailureListener,
PhotoPickerFragment.PhotoPickerListener {
public static final String EXTRA_POST = "postModel";
public static final String EXTRA_POST_LOCAL_ID = "postModelLocalId";
Copy link
Contributor

Choose a reason for hiding this comment

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

+1 for the explicit naming EXTRA_POST_LOCAL_ID

public static final String EXTRA_IS_PAGE = "isPage";
public static final String EXTRA_IS_QUICKPRESS = "isQuickPress";
public static final String EXTRA_QUICKPRESS_BLOG_ID = "quickPressBlogId";
Expand Down Expand Up @@ -270,7 +270,7 @@ protected void onCreate(Bundle savedInstanceState) {
Bundle extras = getIntent().getExtras();
String action = getIntent().getAction();
if (savedInstanceState == null) {
if (!getIntent().hasExtra(EXTRA_POST)
if (!getIntent().hasExtra(EXTRA_POST_LOCAL_ID)
|| Intent.ACTION_SEND.equals(action)
|| Intent.ACTION_SEND_MULTIPLE.equals(action)
|| NEW_MEDIA_POST.equals(action)
Expand Down Expand Up @@ -308,15 +308,11 @@ protected void onCreate(Bundle savedInstanceState) {
mPost = mPostStore.instantiatePostModel(mSite, mIsPage, categories, postFormat);
} else if (extras != null) {
// Load post passed in extras
mPost = (PostModel) extras.getSerializable(EXTRA_POST);
mPost = mPostStore.getPostByLocalPostId(extras.getInt(EXTRA_POST_LOCAL_ID));
if (mPost != null) {
mOriginalPost = mPost.clone();
mIsPage = mPost.isPage();
}
} else {
// A postId extra must be passed to this activity
showErrorAndFinish(R.string.post_not_found);
return;
}
} else {
mDroppedMediaUris = savedInstanceState.getParcelable(STATE_KEY_DROPPED_MEDIA_URIS);
Expand Down Expand Up @@ -388,7 +384,7 @@ public void onPostSave() {
@Override
public void run() {
if (mEditPostPreviewFragment != null) {
mEditPostPreviewFragment.loadPost();
mEditPostPreviewFragment.loadPost(mPost);
}
}
});
Expand Down Expand Up @@ -1091,7 +1087,7 @@ private void saveResult(boolean saved, boolean savedLocally) {
i.putExtra(EXTRA_HAS_UNFINISHED_MEDIA, hasUnfinishedMedia());
i.putExtra(EXTRA_IS_PAGE, mIsPage);
i.putExtra(EXTRA_HAS_CHANGES, saved);
i.putExtra(EXTRA_POST, mPost);
i.putExtra(EXTRA_POST_LOCAL_ID, mPost.getId());
setResult(RESULT_OK, i);
}

Expand Down Expand Up @@ -1325,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 @@ -16,23 +16,27 @@
import org.wordpress.android.WordPress;
import org.wordpress.android.fluxc.model.PostModel;
import org.wordpress.android.fluxc.model.SiteModel;
import org.wordpress.android.fluxc.store.PostStore;
import org.wordpress.android.util.StringUtils;
import org.wordpress.android.util.ToastUtils;
import org.wordpress.android.util.WPHtml;

import javax.inject.Inject;

public class EditPostPreviewFragment extends Fragment {
@Inject PostStore mPostStore;
Copy link
Contributor

@aforcier aforcier May 9, 2017

Choose a reason for hiding this comment

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

Going to need to inject this fragment into the AppComponent, otherwise mPostStore will always be null.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was pretty sure I was missing something in that Fragment. Fixed in cd12939 thanks @aforcier!


private WebView mWebView;
private TextView mTextView;
private LoadPostPreviewTask mLoadTask;

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.putSerializable(EditPostActivity.EXTRA_POST, post);
fragment.setArguments(bundle);
return fragment;
}
Expand All @@ -41,27 +45,24 @@ public static EditPostPreviewFragment newInstance(SiteModel site, PostModel post
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putSerializable(WordPress.SITE, mSite);
outState.putSerializable(EditPostActivity.EXTRA_POST, mPost);
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
((WordPress) getActivity().getApplication()).component().inject(this);
updateSiteOrFinishActivity(savedInstanceState);
}

private void updateSiteOrFinishActivity(Bundle savedInstanceState) {
if (savedInstanceState == null) {
if (getArguments() != null) {
mSite = (SiteModel) getArguments().getSerializable(WordPress.SITE);
mPost = (PostModel) getArguments().getSerializable(EditPostActivity.EXTRA_POST);
} else {
mSite = (SiteModel) getActivity().getIntent().getSerializableExtra(WordPress.SITE);
mPost = (PostModel) getActivity().getIntent().getSerializableExtra(EditPostActivity.EXTRA_POST);
}
} else {
mSite = (SiteModel) savedInstanceState.getSerializable(WordPress.SITE);
mPost = (PostModel) savedInstanceState.getSerializable(EditPostActivity.EXTRA_POST);
}

if (mSite == null) {
Expand All @@ -80,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 @@ -94,7 +95,7 @@ public void onResume() {
super.onResume();

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

Expand All @@ -108,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
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,8 @@ public void handleEditPostResult(int resultCode, Intent data) {
return;
}

final PostModel post = (PostModel)data.getSerializableExtra(EditPostActivity.EXTRA_POST);
final PostModel post = mPostStore.
getPostByLocalPostId(data.getIntExtra(EditPostActivity.EXTRA_POST_LOCAL_ID, 0));
boolean hasUnfinishedMedia = data.getBooleanExtra(EditPostActivity.EXTRA_HAS_UNFINISHED_MEDIA, false);
if (hasUnfinishedMedia) {
showSnackbar(R.string.editor_post_saved_locally_unfinished_media, R.string.button_edit,
Expand Down