From 8614c03254204e2a9921415c2507f887a515152b Mon Sep 17 00:00:00 2001 From: Mario Zorz Date: Fri, 28 Jul 2017 15:29:37 -0300 Subject: [PATCH 1/5] refactored snackbar and publishing code to make it available to both PostListFragment and MySiteFragment --- .../android/ui/main/MySiteFragment.java | 36 ++++ .../android/ui/posts/EditPostActivity.java | 1 - .../wordpress/android/ui/posts/PostUtils.java | 2 +- .../android/ui/posts/PostsListFragment.java | 154 ++--------------- .../android/ui/uploads/UploadUtils.java | 161 ++++++++++++++++++ 5 files changed, 208 insertions(+), 146 deletions(-) diff --git a/WordPress/src/main/java/org/wordpress/android/ui/main/MySiteFragment.java b/WordPress/src/main/java/org/wordpress/android/ui/main/MySiteFragment.java index 805453d08e1f..38b3741e6999 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/main/MySiteFragment.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/main/MySiteFragment.java @@ -23,8 +23,11 @@ import org.wordpress.android.BuildConfig; import org.wordpress.android.R; import org.wordpress.android.WordPress; +import org.wordpress.android.fluxc.Dispatcher; +import org.wordpress.android.fluxc.model.PostModel; import org.wordpress.android.fluxc.model.SiteModel; import org.wordpress.android.fluxc.store.AccountStore; +import org.wordpress.android.fluxc.store.PostStore; import org.wordpress.android.fluxc.store.SiteStore.OnSiteChanged; import org.wordpress.android.ui.ActivityLauncher; import org.wordpress.android.ui.RequestCodes; @@ -33,6 +36,7 @@ import org.wordpress.android.ui.prefs.AppPrefs; import org.wordpress.android.ui.stats.service.StatsService; import org.wordpress.android.ui.themes.ThemeBrowserActivity; +import org.wordpress.android.ui.uploads.UploadUtils; import org.wordpress.android.util.AniUtils; import org.wordpress.android.util.CoreEvents; import org.wordpress.android.util.DateTimeUtils; @@ -82,6 +86,8 @@ public class MySiteFragment extends Fragment private int mBlavatarSz; @Inject AccountStore mAccountStore; + @Inject PostStore mPostStore; + @Inject Dispatcher mDispatcher; public static MySiteFragment newInstance() { return new MySiteFragment(); @@ -99,6 +105,13 @@ public static MySiteFragment newInstance() { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ((WordPress) getActivity().getApplication()).component().inject(this); + mDispatcher.register(this); + } + + @Override + public void onDestroy() { + mDispatcher.unregister(this); + super.onDestroy(); } @Override @@ -296,6 +309,19 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) { && data.getBooleanExtra(EditPostActivity.EXTRA_SAVED_AS_LOCAL_DRAFT, false)) { showAlert(getView().findViewById(R.id.postsGlowBackground)); } + final PostModel post = mPostStore. + getPostByLocalPostId(data.getIntExtra(EditPostActivity.EXTRA_POST_LOCAL_ID, 0)); + + if (post != null) { + final SiteModel site = getSelectedSite(); + UploadUtils.handleEditPostResultSnackbars(getActivity(), resultCode, data, post, site, + new View.OnClickListener() { + @Override + public void onClick(View v) { + UploadUtils.publishPost(getActivity(), post, site, mDispatcher); + } + }); + } break; } } @@ -459,4 +485,14 @@ public void onSiteChanged(OnSiteChanged event) { } refreshSelectedSiteDetails(); } + + @SuppressWarnings("unused") + @Subscribe(threadMode = ThreadMode.MAIN) + public void onPostUploaded(PostStore.OnPostUploaded event) { + final PostModel post = event.post; + SiteModel site = getSelectedSite(); + if (isAdded() && event.post != null && event.post.getLocalSiteId() == site.getId()) { + UploadUtils.onPostUploadedSnackbarHandler(getActivity(), event, site, mDispatcher); + } + } } diff --git a/WordPress/src/main/java/org/wordpress/android/ui/posts/EditPostActivity.java b/WordPress/src/main/java/org/wordpress/android/ui/posts/EditPostActivity.java index 2e23ad31481e..ad1d9967fece 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/posts/EditPostActivity.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/posts/EditPostActivity.java @@ -9,7 +9,6 @@ import android.content.Intent; import android.content.res.Configuration; import android.graphics.Bitmap; -import android.media.ThumbnailUtils; import android.net.Uri; import android.os.AsyncTask; import android.os.Build; diff --git a/WordPress/src/main/java/org/wordpress/android/ui/posts/PostUtils.java b/WordPress/src/main/java/org/wordpress/android/ui/posts/PostUtils.java index 829da39f25ed..378f88e79906 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/posts/PostUtils.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/posts/PostUtils.java @@ -300,7 +300,7 @@ static boolean shouldPublishImmediatelyOptionBeAvailable(PostModel postModel) { return PostStatus.fromPost(postModel) == PostStatus.DRAFT; } - static void updatePublishDateIfShouldBePublishedImmediately(PostModel postModel) { + public static void updatePublishDateIfShouldBePublishedImmediately(PostModel postModel) { if (shouldPublishImmediately(postModel)) { postModel.setDateCreated(DateTimeUtils.iso8601FromDate(new Date())); } diff --git a/WordPress/src/main/java/org/wordpress/android/ui/posts/PostsListFragment.java b/WordPress/src/main/java/org/wordpress/android/ui/posts/PostsListFragment.java index a41932f5ce06..9092d618dad8 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/posts/PostsListFragment.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/posts/PostsListFragment.java @@ -1,6 +1,5 @@ package org.wordpress.android.ui.posts; -import android.app.Activity; import android.app.Fragment; import android.content.Context; import android.content.DialogInterface; @@ -30,7 +29,6 @@ import org.wordpress.android.fluxc.model.MediaModel; import org.wordpress.android.fluxc.model.PostModel; import org.wordpress.android.fluxc.model.SiteModel; -import org.wordpress.android.fluxc.model.post.PostStatus; import org.wordpress.android.fluxc.store.MediaStore; import org.wordpress.android.fluxc.store.MediaStore.OnMediaUploaded; import org.wordpress.android.fluxc.store.PostStore; @@ -48,6 +46,7 @@ import org.wordpress.android.ui.posts.adapters.PostsListAdapter.LoadMode; import org.wordpress.android.ui.uploads.PostEvents; import org.wordpress.android.ui.uploads.UploadService; +import org.wordpress.android.ui.uploads.UploadUtils; import org.wordpress.android.util.AniUtils; import org.wordpress.android.util.NetworkUtils; import org.wordpress.android.util.ToastUtils; @@ -190,107 +189,22 @@ public void onClick(View v) { } public void handleEditPostResult(int resultCode, Intent data) { - if (resultCode != Activity.RESULT_OK || data == null || !isAdded()) { - return; - } - boolean hasChanges = data.getBooleanExtra(EditPostActivity.EXTRA_HAS_CHANGES, false); - if (!hasChanges) { - // if there are no changes, we don't need to do anything - return; - } - - boolean savedLocally = data.getBooleanExtra(EditPostActivity.EXTRA_SAVED_AS_LOCAL_DRAFT, false); - if (savedLocally && !NetworkUtils.isNetworkAvailable(getActivity())) { - // The network is not available, we can't do anything - ToastUtils.showToast(getActivity(), R.string.error_publish_no_network, - ToastUtils.Duration.SHORT); + if (!isAdded()) { return; } final PostModel post = mPostStore. getPostByLocalPostId(data.getIntExtra(EditPostActivity.EXTRA_POST_LOCAL_ID, 0)); - boolean hasFailedMedia = data.getBooleanExtra(EditPostActivity.EXTRA_HAS_FAILED_MEDIA, false); - if (hasFailedMedia) { - showSnackbar(R.string.editor_post_saved_locally_failed_media, R.string.button_edit, - new View.OnClickListener() { - @Override - public void onClick(View v) { - ActivityLauncher.editPostOrPageForResult(getActivity(), mSite, post); - } - }); - return; - } - View.OnClickListener publishPostListener = new View.OnClickListener() { - @Override - public void onClick(View v) { - publishPost(post); - } - }; - boolean isScheduledPost = post != null && PostStatus.fromPost(post) == PostStatus.SCHEDULED; - if (isScheduledPost) { - // if it's a scheduled post, we only want to show a "Sync" button if it's locally saved - if (savedLocally) { - showSnackbar(R.string.editor_post_saved_locally, R.string.button_sync, publishPostListener); - } - return; - } - - boolean isPublished = post != null && PostStatus.fromPost(post) == PostStatus.PUBLISHED; - if (isPublished) { - // if it's a published post, we only want to show a "Sync" button if it's locally saved - if (savedLocally) { - showSnackbar(R.string.editor_post_saved_locally, R.string.button_sync, publishPostListener); - } else { - showSnackbar(getString(R.string.editor_uploading_post)); - } - return; - } - - boolean isDraft = post != null && PostStatus.fromPost(post) == PostStatus.DRAFT; - if (isDraft) { - if (PostUtils.isPublishable(post)) { - // if the post is publishable, we offer the PUBLISH button - if (savedLocally) { - showSnackbar(R.string.editor_draft_saved_locally, R.string.button_publish, publishPostListener); - } - else { - if (UploadService.hasPendingOrInProgressMediaUploadsForPost(post) || - UploadService.isPostUploadingOrQueued(post)) { - showSnackbar(getString(R.string.editor_uploading_post)); - } else { - showSnackbar(R.string.editor_draft_saved_online, R.string.button_publish, publishPostListener); + UploadUtils.handleEditPostResultSnackbars(getActivity(), resultCode, data, post, mSite, + new View.OnClickListener() { + @Override + public void onClick(View v) { + UploadUtils.publishPost(getActivity(), post, mSite, mDispatcher); } - } - } else { - showSnackbar(getString(R.string.editor_draft_saved_locally)); - } - } else { - if (savedLocally) { - showSnackbar(R.string.editor_post_saved_locally, R.string.button_publish, publishPostListener); - } - else { - if (UploadService.hasPendingOrInProgressMediaUploadsForPost(post) || - UploadService.isPostUploadingOrQueued(post)) { - showSnackbar(getString(R.string.editor_uploading_post)); - } else { - showSnackbar(R.string.editor_post_saved_online, R.string.button_publish, publishPostListener); - } - } - } - } - - private void showSnackbar(int messageRes, int buttonTitleRes, View.OnClickListener onClickListener) { - Snackbar.make(getActivity().findViewById(R.id.coordinator), messageRes, Snackbar.LENGTH_LONG) - .setAction(buttonTitleRes, onClickListener).show(); - } - - private void showSnackbar(String text) { - Snackbar.make(getView().findViewById(R.id.coordinator), - text, Snackbar.LENGTH_LONG).show(); + }); } - private void initSwipeToRefreshHelper(View view) { mSwipeToRefreshHelper = new SwipeToRefreshHelper( getActivity(), @@ -541,7 +455,7 @@ public void onPostButtonClicked(int buttonType, PostModel postClicked) { case PostListButton.BUTTON_SYNC: case PostListButton.BUTTON_RETRY: case PostListButton.BUTTON_PUBLISH: - publishPost(post); + UploadUtils.publishPost(getActivity(), post, mSite, mDispatcher); break; case PostListButton.BUTTON_VIEW: ActivityLauncher.browsePostOrPage(getActivity(), mSite, post); @@ -574,36 +488,6 @@ public void onClick(DialogInterface dialogInterface, int i) { } } - private void publishPost(final PostModel post) { - if (!NetworkUtils.isNetworkAvailable(getActivity())) { - ToastUtils.showToast(getActivity(), R.string.error_publish_no_network, - ToastUtils.Duration.SHORT); - return; - } - - // If the post is empty, don't publish - if (!PostUtils.isPublishable(post)) { - ToastUtils.showToast(getActivity(), R.string.error_publish_empty_post, ToastUtils.Duration.SHORT); - return; - } - - PostUtils.updatePublishDateIfShouldBePublishedImmediately(post); - boolean isFirstTimePublish = PostStatus.fromPost(post) == PostStatus.DRAFT - || (PostStatus.fromPost(post) == PostStatus.PUBLISHED && post.isLocalDraft()); - post.setStatus(PostStatus.PUBLISHED.toString()); - - // save the post in the DB so the UploadService will get the latest change - mDispatcher.dispatch(PostActionBuilder.newUpdatePostAction(post)); - - if (isFirstTimePublish) { - UploadService.uploadPostAndTrackAnalytics(getActivity(), post); - } else { - UploadService.uploadPost(getActivity(), post); - } - - PostUtils.trackSavePostAnalytics(post, mSite); - } - /* * send the passed post to the trash with undo */ @@ -739,25 +623,7 @@ public void onPostUploaded(OnPostUploaded event) { final PostModel post = event.post; if (isAdded() && event.post != null && event.post.getLocalSiteId() == mSite.getId()) { loadPosts(LoadMode.FORCED); - - if (event.isError()) { - showSnackbar(getString(R.string.editor_draft_saved_locally)); - } else { - boolean isDraft = PostStatus.fromPost(post) == PostStatus.DRAFT; - if (isDraft) { - View.OnClickListener publishPostListener = new View.OnClickListener() { - @Override - public void onClick(View v) { - publishPost(post); - } - }; - showSnackbar(R.string.editor_draft_saved_online, R.string.button_publish, publishPostListener); - } else { - String message = post.isPage() ? getString(R.string.page_published) : - getString(R.string.post_published); - showSnackbar(message); - } - } + UploadUtils.onPostUploadedSnackbarHandler(getActivity(), event, mSite, mDispatcher); } } diff --git a/WordPress/src/main/java/org/wordpress/android/ui/uploads/UploadUtils.java b/WordPress/src/main/java/org/wordpress/android/ui/uploads/UploadUtils.java index 67df48d75d9b..7d375eca20f1 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/uploads/UploadUtils.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/uploads/UploadUtils.java @@ -1,14 +1,28 @@ package org.wordpress.android.ui.uploads; +import android.app.Activity; import android.content.Context; +import android.content.Intent; import android.support.annotation.NonNull; +import android.support.design.widget.Snackbar; import android.text.TextUtils; +import android.view.View; import org.wordpress.android.R; +import org.wordpress.android.fluxc.Dispatcher; +import org.wordpress.android.fluxc.generated.PostActionBuilder; import org.wordpress.android.fluxc.model.MediaModel; import org.wordpress.android.fluxc.model.PostModel; +import org.wordpress.android.fluxc.model.SiteModel; +import org.wordpress.android.fluxc.model.post.PostStatus; import org.wordpress.android.fluxc.store.MediaStore.MediaError; +import org.wordpress.android.fluxc.store.PostStore; import org.wordpress.android.fluxc.store.PostStore.PostError; +import org.wordpress.android.ui.ActivityLauncher; +import org.wordpress.android.ui.posts.EditPostActivity; +import org.wordpress.android.ui.posts.PostUtils; +import org.wordpress.android.util.NetworkUtils; +import org.wordpress.android.util.ToastUtils; import org.wordpress.android.util.WPMediaUtils; public class UploadUtils { @@ -50,4 +64,151 @@ public class UploadUtils { return errorMessage; } + + public static void handleEditPostResultSnackbars(final Activity activity, int resultCode, Intent data, + final PostModel post, final SiteModel site, View.OnClickListener publishPostListener) { + if (resultCode != Activity.RESULT_OK || data == null) { + return; + } + boolean hasChanges = data.getBooleanExtra(EditPostActivity.EXTRA_HAS_CHANGES, false); + if (!hasChanges) { + // if there are no changes, we don't need to do anything + return; + } + + boolean savedLocally = data.getBooleanExtra(EditPostActivity.EXTRA_SAVED_AS_LOCAL_DRAFT, false); + if (savedLocally && !NetworkUtils.isNetworkAvailable(activity)) { + // The network is not available, we can't do anything + ToastUtils.showToast(activity, R.string.error_publish_no_network, + ToastUtils.Duration.SHORT); + return; + } + + boolean hasFailedMedia = data.getBooleanExtra(EditPostActivity.EXTRA_HAS_FAILED_MEDIA, false); + if (hasFailedMedia) { + showSnackbar(activity, R.string.editor_post_saved_locally_failed_media, R.string.button_edit, + new View.OnClickListener() { + @Override + public void onClick(View v) { + ActivityLauncher.editPostOrPageForResult(activity, site, post); + } + }); + return; + } + + boolean isScheduledPost = post != null && PostStatus.fromPost(post) == PostStatus.SCHEDULED; + if (isScheduledPost) { + // if it's a scheduled post, we only want to show a "Sync" button if it's locally saved + if (savedLocally) { + showSnackbar(activity, R.string.editor_post_saved_locally, R.string.button_sync, publishPostListener); + } + return; + } + + boolean isPublished = post != null && PostStatus.fromPost(post) == PostStatus.PUBLISHED; + if (isPublished) { + // if it's a published post, we only want to show a "Sync" button if it's locally saved + if (savedLocally) { + showSnackbar(activity, R.string.editor_post_saved_locally, R.string.button_sync, publishPostListener); + } else { + showSnackbar(activity, activity.getString(R.string.editor_uploading_post)); + } + return; + } + + boolean isDraft = post != null && PostStatus.fromPost(post) == PostStatus.DRAFT; + if (isDraft) { + if (PostUtils.isPublishable(post)) { + // if the post is publishable, we offer the PUBLISH button + if (savedLocally) { + showSnackbar(activity, R.string.editor_draft_saved_locally, R.string.button_publish, publishPostListener); + } + else { + if (UploadService.hasPendingOrInProgressMediaUploadsForPost(post) || + UploadService.isPostUploadingOrQueued(post)) { + showSnackbar(activity, activity.getString(R.string.editor_uploading_post)); + } else { + showSnackbar(activity, R.string.editor_draft_saved_online, R.string.button_publish, publishPostListener); + } + } + } else { + showSnackbar(activity, activity.getString(R.string.editor_draft_saved_locally)); + } + } else { + if (savedLocally) { + showSnackbar(activity, R.string.editor_post_saved_locally, R.string.button_publish, publishPostListener); + } + else { + if (UploadService.hasPendingOrInProgressMediaUploadsForPost(post) || + UploadService.isPostUploadingOrQueued(post)) { + showSnackbar(activity, activity.getString(R.string.editor_uploading_post)); + } else { + showSnackbar(activity, R.string.editor_post_saved_online, R.string.button_publish, publishPostListener); + } + } + } + } + + public static void showSnackbar(Activity activity, int messageRes, int buttonTitleRes, View.OnClickListener onClickListener) { + Snackbar.make(activity.findViewById(R.id.coordinator), messageRes, Snackbar.LENGTH_LONG) + .setAction(buttonTitleRes, onClickListener).show(); + } + + public static void showSnackbar(Activity activity, String text) { + Snackbar.make(activity.findViewById(R.id.coordinator), + text, Snackbar.LENGTH_LONG).show(); + } + + public static void publishPost(Activity activity, final PostModel post, SiteModel site, Dispatcher dispatcher) { + if (!NetworkUtils.isNetworkAvailable(activity)) { + ToastUtils.showToast(activity, R.string.error_publish_no_network, + ToastUtils.Duration.SHORT); + return; + } + + // If the post is empty, don't publish + if (!PostUtils.isPublishable(post)) { + ToastUtils.showToast(activity, R.string.error_publish_empty_post, ToastUtils.Duration.SHORT); + return; + } + + PostUtils.updatePublishDateIfShouldBePublishedImmediately(post); + boolean isFirstTimePublish = PostStatus.fromPost(post) == PostStatus.DRAFT + || (PostStatus.fromPost(post) == PostStatus.PUBLISHED && post.isLocalDraft()); + post.setStatus(PostStatus.PUBLISHED.toString()); + + // save the post in the DB so the UploadService will get the latest change + dispatcher.dispatch(PostActionBuilder.newUpdatePostAction(post)); + + if (isFirstTimePublish) { + UploadService.uploadPostAndTrackAnalytics(activity, post); + } else { + UploadService.uploadPost(activity, post); + } + + PostUtils.trackSavePostAnalytics(post, site); + } + + public static void onPostUploadedSnackbarHandler(final Activity activity, PostStore.OnPostUploaded event, + final SiteModel site, final Dispatcher dispatcher) { + final PostModel post = event.post; + if (event.isError()) { + UploadUtils.showSnackbar(activity, activity.getString(R.string.editor_draft_saved_locally)); + } else { + boolean isDraft = PostStatus.fromPost(post) == PostStatus.DRAFT; + if (isDraft) { + View.OnClickListener publishPostListener = new View.OnClickListener() { + @Override + public void onClick(View v) { + UploadUtils.publishPost(activity, post, site, dispatcher); + } + }; + UploadUtils.showSnackbar(activity, R.string.editor_draft_saved_online, R.string.button_publish, publishPostListener); + } else { + String message = post.isPage() ? activity.getString(R.string.page_published) : + activity.getString(R.string.post_published); + UploadUtils.showSnackbar(activity, message); + } + } + } } From e37d98baf0c0b489ef4a492d848742160d71b582 Mon Sep 17 00:00:00 2001 From: Mario Zorz Date: Mon, 31 Jul 2017 12:25:12 -0300 Subject: [PATCH 2/5] changed method scope to private --- .../java/org/wordpress/android/ui/uploads/UploadUtils.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/WordPress/src/main/java/org/wordpress/android/ui/uploads/UploadUtils.java b/WordPress/src/main/java/org/wordpress/android/ui/uploads/UploadUtils.java index 7d375eca20f1..c9f2789308b0 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/uploads/UploadUtils.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/uploads/UploadUtils.java @@ -149,12 +149,12 @@ public void onClick(View v) { } } - public static void showSnackbar(Activity activity, int messageRes, int buttonTitleRes, View.OnClickListener onClickListener) { + private static void showSnackbar(Activity activity, int messageRes, int buttonTitleRes, View.OnClickListener onClickListener) { Snackbar.make(activity.findViewById(R.id.coordinator), messageRes, Snackbar.LENGTH_LONG) .setAction(buttonTitleRes, onClickListener).show(); } - public static void showSnackbar(Activity activity, String text) { + private static void showSnackbar(Activity activity, String text) { Snackbar.make(activity.findViewById(R.id.coordinator), text, Snackbar.LENGTH_LONG).show(); } From 06aaba24c685ebdc8ff1bcc24a188a9bceb0a710 Mon Sep 17 00:00:00 2001 From: Mario Zorz Date: Mon, 31 Jul 2017 12:39:36 -0300 Subject: [PATCH 3/5] refactore snackbar methods to use string resource ids and get a View from parameter --- .../android/ui/main/MySiteFragment.java | 31 +++++++------ .../android/ui/posts/PostsListFragment.java | 6 ++- .../android/ui/uploads/UploadUtils.java | 45 +++++++++---------- 3 files changed, 44 insertions(+), 38 deletions(-) diff --git a/WordPress/src/main/java/org/wordpress/android/ui/main/MySiteFragment.java b/WordPress/src/main/java/org/wordpress/android/ui/main/MySiteFragment.java index 38b3741e6999..2391507d0c1d 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/main/MySiteFragment.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/main/MySiteFragment.java @@ -309,18 +309,22 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) { && data.getBooleanExtra(EditPostActivity.EXTRA_SAVED_AS_LOCAL_DRAFT, false)) { showAlert(getView().findViewById(R.id.postsGlowBackground)); } - final PostModel post = mPostStore. - getPostByLocalPostId(data.getIntExtra(EditPostActivity.EXTRA_POST_LOCAL_ID, 0)); - - if (post != null) { - final SiteModel site = getSelectedSite(); - UploadUtils.handleEditPostResultSnackbars(getActivity(), resultCode, data, post, site, - new View.OnClickListener() { - @Override - public void onClick(View v) { - UploadUtils.publishPost(getActivity(), post, site, mDispatcher); - } - }); + + if (isAdded()) { + final PostModel post = mPostStore. + getPostByLocalPostId(data.getIntExtra(EditPostActivity.EXTRA_POST_LOCAL_ID, 0)); + + if (post != null) { + final SiteModel site = getSelectedSite(); + UploadUtils.handleEditPostResultSnackbars(getActivity(), + getActivity().findViewById(R.id.coordinator), resultCode, data, post, site, + new View.OnClickListener() { + @Override + public void onClick(View v) { + UploadUtils.publishPost(getActivity(), post, site, mDispatcher); + } + }); + } } break; } @@ -492,7 +496,8 @@ public void onPostUploaded(PostStore.OnPostUploaded event) { final PostModel post = event.post; SiteModel site = getSelectedSite(); if (isAdded() && event.post != null && event.post.getLocalSiteId() == site.getId()) { - UploadUtils.onPostUploadedSnackbarHandler(getActivity(), event, site, mDispatcher); + UploadUtils.onPostUploadedSnackbarHandler(getActivity(), + getActivity().findViewById(R.id.coordinator), event, site, mDispatcher); } } } diff --git a/WordPress/src/main/java/org/wordpress/android/ui/posts/PostsListFragment.java b/WordPress/src/main/java/org/wordpress/android/ui/posts/PostsListFragment.java index 9092d618dad8..f14d233facd8 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/posts/PostsListFragment.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/posts/PostsListFragment.java @@ -196,7 +196,8 @@ public void handleEditPostResult(int resultCode, Intent data) { final PostModel post = mPostStore. getPostByLocalPostId(data.getIntExtra(EditPostActivity.EXTRA_POST_LOCAL_ID, 0)); - UploadUtils.handleEditPostResultSnackbars(getActivity(), resultCode, data, post, mSite, + UploadUtils.handleEditPostResultSnackbars(getActivity(), + getActivity().findViewById(R.id.coordinator), resultCode, data, post, mSite, new View.OnClickListener() { @Override public void onClick(View v) { @@ -623,7 +624,8 @@ public void onPostUploaded(OnPostUploaded event) { final PostModel post = event.post; if (isAdded() && event.post != null && event.post.getLocalSiteId() == mSite.getId()) { loadPosts(LoadMode.FORCED); - UploadUtils.onPostUploadedSnackbarHandler(getActivity(), event, mSite, mDispatcher); + UploadUtils.onPostUploadedSnackbarHandler(getActivity(), + getActivity().findViewById(R.id.coordinator), event, mSite, mDispatcher); } } diff --git a/WordPress/src/main/java/org/wordpress/android/ui/uploads/UploadUtils.java b/WordPress/src/main/java/org/wordpress/android/ui/uploads/UploadUtils.java index c9f2789308b0..3758d693264b 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/uploads/UploadUtils.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/uploads/UploadUtils.java @@ -65,7 +65,7 @@ public class UploadUtils { return errorMessage; } - public static void handleEditPostResultSnackbars(final Activity activity, int resultCode, Intent data, + public static void handleEditPostResultSnackbars(final Activity activity, View view, int resultCode, Intent data, final PostModel post, final SiteModel site, View.OnClickListener publishPostListener) { if (resultCode != Activity.RESULT_OK || data == null) { return; @@ -86,7 +86,7 @@ public static void handleEditPostResultSnackbars(final Activity activity, int re boolean hasFailedMedia = data.getBooleanExtra(EditPostActivity.EXTRA_HAS_FAILED_MEDIA, false); if (hasFailedMedia) { - showSnackbar(activity, R.string.editor_post_saved_locally_failed_media, R.string.button_edit, + showSnackbar(view, R.string.editor_post_saved_locally_failed_media, R.string.button_edit, new View.OnClickListener() { @Override public void onClick(View v) { @@ -100,7 +100,7 @@ public void onClick(View v) { if (isScheduledPost) { // if it's a scheduled post, we only want to show a "Sync" button if it's locally saved if (savedLocally) { - showSnackbar(activity, R.string.editor_post_saved_locally, R.string.button_sync, publishPostListener); + showSnackbar(view, R.string.editor_post_saved_locally, R.string.button_sync, publishPostListener); } return; } @@ -109,9 +109,9 @@ public void onClick(View v) { if (isPublished) { // if it's a published post, we only want to show a "Sync" button if it's locally saved if (savedLocally) { - showSnackbar(activity, R.string.editor_post_saved_locally, R.string.button_sync, publishPostListener); + showSnackbar(view, R.string.editor_post_saved_locally, R.string.button_sync, publishPostListener); } else { - showSnackbar(activity, activity.getString(R.string.editor_uploading_post)); + showSnackbar(view, R.string.editor_uploading_post); } return; } @@ -121,42 +121,42 @@ public void onClick(View v) { if (PostUtils.isPublishable(post)) { // if the post is publishable, we offer the PUBLISH button if (savedLocally) { - showSnackbar(activity, R.string.editor_draft_saved_locally, R.string.button_publish, publishPostListener); + showSnackbar(view, R.string.editor_draft_saved_locally, R.string.button_publish, publishPostListener); } else { if (UploadService.hasPendingOrInProgressMediaUploadsForPost(post) || UploadService.isPostUploadingOrQueued(post)) { - showSnackbar(activity, activity.getString(R.string.editor_uploading_post)); + showSnackbar(view, R.string.editor_uploading_post); } else { - showSnackbar(activity, R.string.editor_draft_saved_online, R.string.button_publish, publishPostListener); + showSnackbar(view, R.string.editor_draft_saved_online, R.string.button_publish, publishPostListener); } } } else { - showSnackbar(activity, activity.getString(R.string.editor_draft_saved_locally)); + showSnackbar(view, R.string.editor_draft_saved_locally); } } else { if (savedLocally) { - showSnackbar(activity, R.string.editor_post_saved_locally, R.string.button_publish, publishPostListener); + showSnackbar(view, R.string.editor_post_saved_locally, R.string.button_publish, publishPostListener); } else { if (UploadService.hasPendingOrInProgressMediaUploadsForPost(post) || UploadService.isPostUploadingOrQueued(post)) { - showSnackbar(activity, activity.getString(R.string.editor_uploading_post)); + showSnackbar(view, R.string.editor_uploading_post); } else { - showSnackbar(activity, R.string.editor_post_saved_online, R.string.button_publish, publishPostListener); + showSnackbar(view, R.string.editor_post_saved_online, R.string.button_publish, publishPostListener); } } } } - private static void showSnackbar(Activity activity, int messageRes, int buttonTitleRes, View.OnClickListener onClickListener) { - Snackbar.make(activity.findViewById(R.id.coordinator), messageRes, Snackbar.LENGTH_LONG) + private static void showSnackbar(View view, int messageRes, int buttonTitleRes, View.OnClickListener onClickListener) { + Snackbar.make(view, messageRes, Snackbar.LENGTH_LONG) .setAction(buttonTitleRes, onClickListener).show(); } - private static void showSnackbar(Activity activity, String text) { - Snackbar.make(activity.findViewById(R.id.coordinator), - text, Snackbar.LENGTH_LONG).show(); + private static void showSnackbar(View view, int messageRes) { + Snackbar.make(view, + messageRes, Snackbar.LENGTH_LONG).show(); } public static void publishPost(Activity activity, final PostModel post, SiteModel site, Dispatcher dispatcher) { @@ -189,11 +189,11 @@ public static void publishPost(Activity activity, final PostModel post, SiteMode PostUtils.trackSavePostAnalytics(post, site); } - public static void onPostUploadedSnackbarHandler(final Activity activity, PostStore.OnPostUploaded event, + public static void onPostUploadedSnackbarHandler(final Activity activity, View snackbarAttachView, PostStore.OnPostUploaded event, final SiteModel site, final Dispatcher dispatcher) { final PostModel post = event.post; if (event.isError()) { - UploadUtils.showSnackbar(activity, activity.getString(R.string.editor_draft_saved_locally)); + UploadUtils.showSnackbar(snackbarAttachView, R.string.editor_draft_saved_locally); } else { boolean isDraft = PostStatus.fromPost(post) == PostStatus.DRAFT; if (isDraft) { @@ -203,11 +203,10 @@ public void onClick(View v) { UploadUtils.publishPost(activity, post, site, dispatcher); } }; - UploadUtils.showSnackbar(activity, R.string.editor_draft_saved_online, R.string.button_publish, publishPostListener); + UploadUtils.showSnackbar(snackbarAttachView, R.string.editor_draft_saved_online, R.string.button_publish, publishPostListener); } else { - String message = post.isPage() ? activity.getString(R.string.page_published) : - activity.getString(R.string.post_published); - UploadUtils.showSnackbar(activity, message); + int messageRes = post.isPage() ? R.string.page_published : R.string.post_published; + UploadUtils.showSnackbar(snackbarAttachView, messageRes); } } } From 935de7271b567309b8c2804fe161cfd6f5d9582f Mon Sep 17 00:00:00 2001 From: Mario Zorz Date: Mon, 31 Jul 2017 12:44:13 -0300 Subject: [PATCH 4/5] changed var name to better represent parameter snackbarAttachView, and added some CR to keep 120 chars width down --- .../android/ui/uploads/UploadUtils.java | 43 ++++++++++++------- 1 file changed, 27 insertions(+), 16 deletions(-) diff --git a/WordPress/src/main/java/org/wordpress/android/ui/uploads/UploadUtils.java b/WordPress/src/main/java/org/wordpress/android/ui/uploads/UploadUtils.java index 3758d693264b..6fdf3257124f 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/uploads/UploadUtils.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/uploads/UploadUtils.java @@ -65,8 +65,10 @@ public class UploadUtils { return errorMessage; } - public static void handleEditPostResultSnackbars(final Activity activity, View view, int resultCode, Intent data, - final PostModel post, final SiteModel site, View.OnClickListener publishPostListener) { + public static void handleEditPostResultSnackbars(final Activity activity, View snackbarAttachView, + int resultCode, Intent data, + final PostModel post, final SiteModel site, + View.OnClickListener publishPostListener) { if (resultCode != Activity.RESULT_OK || data == null) { return; } @@ -86,7 +88,7 @@ public static void handleEditPostResultSnackbars(final Activity activity, View v boolean hasFailedMedia = data.getBooleanExtra(EditPostActivity.EXTRA_HAS_FAILED_MEDIA, false); if (hasFailedMedia) { - showSnackbar(view, R.string.editor_post_saved_locally_failed_media, R.string.button_edit, + showSnackbar(snackbarAttachView, R.string.editor_post_saved_locally_failed_media, R.string.button_edit, new View.OnClickListener() { @Override public void onClick(View v) { @@ -100,7 +102,8 @@ public void onClick(View v) { if (isScheduledPost) { // if it's a scheduled post, we only want to show a "Sync" button if it's locally saved if (savedLocally) { - showSnackbar(view, R.string.editor_post_saved_locally, R.string.button_sync, publishPostListener); + showSnackbar(snackbarAttachView, R.string.editor_post_saved_locally, R.string.button_sync, + publishPostListener); } return; } @@ -109,9 +112,10 @@ public void onClick(View v) { if (isPublished) { // if it's a published post, we only want to show a "Sync" button if it's locally saved if (savedLocally) { - showSnackbar(view, R.string.editor_post_saved_locally, R.string.button_sync, publishPostListener); + showSnackbar(snackbarAttachView, R.string.editor_post_saved_locally, R.string.button_sync, + publishPostListener); } else { - showSnackbar(view, R.string.editor_uploading_post); + showSnackbar(snackbarAttachView, R.string.editor_uploading_post); } return; } @@ -121,35 +125,40 @@ public void onClick(View v) { if (PostUtils.isPublishable(post)) { // if the post is publishable, we offer the PUBLISH button if (savedLocally) { - showSnackbar(view, R.string.editor_draft_saved_locally, R.string.button_publish, publishPostListener); + showSnackbar(snackbarAttachView, R.string.editor_draft_saved_locally, R.string.button_publish, + publishPostListener); } else { if (UploadService.hasPendingOrInProgressMediaUploadsForPost(post) || UploadService.isPostUploadingOrQueued(post)) { - showSnackbar(view, R.string.editor_uploading_post); + showSnackbar(snackbarAttachView, R.string.editor_uploading_post); } else { - showSnackbar(view, R.string.editor_draft_saved_online, R.string.button_publish, publishPostListener); + showSnackbar(snackbarAttachView, R.string.editor_draft_saved_online, R.string.button_publish, + publishPostListener); } } } else { - showSnackbar(view, R.string.editor_draft_saved_locally); + showSnackbar(snackbarAttachView, R.string.editor_draft_saved_locally); } } else { if (savedLocally) { - showSnackbar(view, R.string.editor_post_saved_locally, R.string.button_publish, publishPostListener); + showSnackbar(snackbarAttachView, R.string.editor_post_saved_locally, R.string.button_publish, + publishPostListener); } else { if (UploadService.hasPendingOrInProgressMediaUploadsForPost(post) || UploadService.isPostUploadingOrQueued(post)) { - showSnackbar(view, R.string.editor_uploading_post); + showSnackbar(snackbarAttachView, R.string.editor_uploading_post); } else { - showSnackbar(view, R.string.editor_post_saved_online, R.string.button_publish, publishPostListener); + showSnackbar(snackbarAttachView, R.string.editor_post_saved_online, R.string.button_publish, + publishPostListener); } } } } - private static void showSnackbar(View view, int messageRes, int buttonTitleRes, View.OnClickListener onClickListener) { + private static void showSnackbar(View view, int messageRes, int buttonTitleRes, + View.OnClickListener onClickListener) { Snackbar.make(view, messageRes, Snackbar.LENGTH_LONG) .setAction(buttonTitleRes, onClickListener).show(); } @@ -189,7 +198,8 @@ public static void publishPost(Activity activity, final PostModel post, SiteMode PostUtils.trackSavePostAnalytics(post, site); } - public static void onPostUploadedSnackbarHandler(final Activity activity, View snackbarAttachView, PostStore.OnPostUploaded event, + public static void onPostUploadedSnackbarHandler(final Activity activity, View snackbarAttachView, + PostStore.OnPostUploaded event, final SiteModel site, final Dispatcher dispatcher) { final PostModel post = event.post; if (event.isError()) { @@ -203,7 +213,8 @@ public void onClick(View v) { UploadUtils.publishPost(activity, post, site, dispatcher); } }; - UploadUtils.showSnackbar(snackbarAttachView, R.string.editor_draft_saved_online, R.string.button_publish, publishPostListener); + UploadUtils.showSnackbar(snackbarAttachView, R.string.editor_draft_saved_online, + R.string.button_publish, publishPostListener); } else { int messageRes = post.isPage() ? R.string.page_published : R.string.post_published; UploadUtils.showSnackbar(snackbarAttachView, messageRes); From 28a9d580fd7c2cf1c73c32133019c1afcca219fa Mon Sep 17 00:00:00 2001 From: Mario Zorz Date: Mon, 31 Jul 2017 13:14:43 -0300 Subject: [PATCH 5/5] re-arranged if checks to only try getting selected site if fragment is added, then checked for site != null to avoid NPE --- .../wordpress/android/ui/main/MySiteFragment.java | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/WordPress/src/main/java/org/wordpress/android/ui/main/MySiteFragment.java b/WordPress/src/main/java/org/wordpress/android/ui/main/MySiteFragment.java index 2391507d0c1d..598cf1bb2a63 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/main/MySiteFragment.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/main/MySiteFragment.java @@ -494,10 +494,14 @@ public void onSiteChanged(OnSiteChanged event) { @Subscribe(threadMode = ThreadMode.MAIN) public void onPostUploaded(PostStore.OnPostUploaded event) { final PostModel post = event.post; - SiteModel site = getSelectedSite(); - if (isAdded() && event.post != null && event.post.getLocalSiteId() == site.getId()) { - UploadUtils.onPostUploadedSnackbarHandler(getActivity(), - getActivity().findViewById(R.id.coordinator), event, site, mDispatcher); + if (isAdded() && event.post != null) { + SiteModel site = getSelectedSite(); + if (site != null) { + if (event.post.getLocalSiteId() == site.getId()) { + UploadUtils.onPostUploadedSnackbarHandler(getActivity(), + getActivity().findViewById(R.id.coordinator), event, site, mDispatcher); + } + } } } }