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

Async media: adding publishing Snackbars to MySiteFragment #6425

Merged
merged 5 commits into from
Jul 31, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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();
Expand All @@ -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
Expand Down Expand Up @@ -296,6 +309,23 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {
&& data.getBooleanExtra(EditPostActivity.EXTRA_SAVED_AS_LOCAL_DRAFT, false)) {
showAlert(getView().findViewById(R.id.postsGlowBackground));
}

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;
}
}
Expand Down Expand Up @@ -459,4 +489,19 @@ public void onSiteChanged(OnSiteChanged event) {
}
refreshSelectedSiteDetails();
}

@SuppressWarnings("unused")
@Subscribe(threadMode = ThreadMode.MAIN)
public void onPostUploaded(PostStore.OnPostUploaded event) {
final PostModel post = event.post;
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);
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -190,107 +189,23 @@ 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(),
getActivity().findViewById(R.id.coordinator), 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(),
Expand Down Expand Up @@ -541,7 +456,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);
Expand Down Expand Up @@ -574,36 +489,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
*/
Expand Down Expand Up @@ -739,25 +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);

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(),
getActivity().findViewById(R.id.coordinator), event, mSite, mDispatcher);
}
}

Expand Down
Loading