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

Adds Duplicate/Copy post functionality #13521

Merged
merged 27 commits into from
Dec 8, 2020
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
3 changes: 2 additions & 1 deletion RELEASE-NOTES.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
16.4
-----
* [*] My Site: Fixes crash on rotation while editing site title [https://github.com/wordpress-mobile/WordPress-Android/pull/13505]

* [**] Posts List: Adds duplicate post functionality [https://github.com/wordpress-mobile/WordPress-Android/pull/13521]

16.3
-----
* [***] Site Creation: Adds an option to pick a home page design when creating a WordPress.com site.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import org.wordpress.android.util.ToastUtils.Duration
import org.wordpress.android.viewmodel.helpers.ToastMessageHolder
import org.wordpress.android.widgets.PostListButtonType
import org.wordpress.android.widgets.PostListButtonType.BUTTON_CANCEL_PENDING_AUTO_UPLOAD
import org.wordpress.android.widgets.PostListButtonType.BUTTON_COPY
import org.wordpress.android.widgets.PostListButtonType.BUTTON_DELETE
import org.wordpress.android.widgets.PostListButtonType.BUTTON_DELETE_PERMANENTLY
import org.wordpress.android.widgets.PostListButtonType.BUTTON_EDIT
Expand Down Expand Up @@ -67,7 +68,8 @@ class PostActionHandler(
private val checkNetworkConnection: () -> Boolean,
private val showSnackbar: (SnackbarMessageHolder) -> Unit,
private val showToast: (ToastMessageHolder) -> Unit,
private val triggerPreviewStateUpdate: (PostListRemotePreviewState, PostInfoType) -> Unit
private val triggerPreviewStateUpdate: (PostListRemotePreviewState, PostInfoType) -> Unit,
private val copyPost: (SiteModel, PostModel, Boolean) -> Unit
) {
private val criticalPostActionTracker = CriticalPostActionTracker(onStateChanged = {
invalidateList.invoke()
Expand Down Expand Up @@ -112,6 +114,7 @@ class PostActionHandler(
else -> trashPost(post)
}
}
BUTTON_COPY -> copyPost(site, post, true)
BUTTON_DELETE, BUTTON_DELETE_PERMANENTLY -> {
postListDialogHelper.showDeletePostConfirmationDialog(post)
}
Expand Down Expand Up @@ -168,6 +171,13 @@ class PostActionHandler(
triggerPostUploadAction.invoke(PublishPost(dispatcher, site, post))
}

fun resolveConflictsAndEditPost(localPostId: Int) {
val post = postStore.getPostByLocalPostId(localPostId)
if (post != null) {
performChecksAndEdit(site, post)
}
}

fun moveTrashedPostToDraft(localPostId: Int) {
val post = postStore.getPostByLocalPostId(localPostId)
if (post != null) {
Expand Down Expand Up @@ -198,7 +208,9 @@ class PostActionHandler(
showSnackbar.invoke(snackBarHolder)
}

private fun editPostButtonAction(site: SiteModel, post: PostModel) {
private fun editPostButtonAction(site: SiteModel, post: PostModel) = performChecksAndEdit(site, post)

private fun performChecksAndEdit(site: SiteModel, post: PostModel) {
// first of all, check whether this post is in Conflicted state with a more recent remote version
if (doesPostHaveUnhandledConflict.invoke(post)) {
postListDialogHelper.showConflictedPostResolutionDialog(post)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import org.wordpress.android.fluxc.model.SiteModel
import org.wordpress.android.util.analytics.AnalyticsUtils
import org.wordpress.android.widgets.PostListButtonType
import org.wordpress.android.widgets.PostListButtonType.BUTTON_CANCEL_PENDING_AUTO_UPLOAD
import org.wordpress.android.widgets.PostListButtonType.BUTTON_COPY
import org.wordpress.android.widgets.PostListButtonType.BUTTON_DELETE
import org.wordpress.android.widgets.PostListButtonType.BUTTON_DELETE_PERMANENTLY
import org.wordpress.android.widgets.PostListButtonType.BUTTON_EDIT
Expand Down Expand Up @@ -39,6 +40,7 @@ fun trackPostListAction(site: SiteModel, buttonType: PostListButtonType, postDat
BUTTON_PREVIEW -> "preview"
BUTTON_STATS -> "stats"
BUTTON_TRASH -> "trash"
BUTTON_COPY -> "copy"
BUTTON_DELETE,
BUTTON_DELETE_PERMANENTLY -> "delete"
BUTTON_PUBLISH -> "publish"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ private const val CONFIRM_TRASH_POST_WITH_UNSAVED_CHANGES_DIALOG_TAG =
private const val CONFIRM_ON_CONFLICT_LOAD_REMOTE_POST_DIALOG_TAG = "CONFIRM_ON_CONFLICT_LOAD_REMOTE_POST_DIALOG_TAG"
private const val CONFIRM_ON_AUTOSAVE_REVISION_DIALOG_TAG = "CONFIRM_ON_AUTOSAVE_REVISION_DIALOG_TAG"
private const val CONFIRM_SYNC_SCHEDULED_POST_DIALOG_TAG = "CONFIRM_SYNC_SCHEDULED_POST_DIALOG_TAG"
private const val COPY_CONFLICT_DIALOG_TAG = "COPY_CONFLICT_DIALOG_TAG"
private const val POST_TYPE = "post_type"

/**
Expand All @@ -37,6 +38,7 @@ class PostListDialogHelper(
private var localPostIdForConflictResolutionDialog: Int? = null
private var localPostIdForAutosaveRevisionResolutionDialog: Int? = null
private var localPostIdForScheduledPostSyncDialog: Int? = null
private var localPostIdForCopyConflictDialog: Int? = null

fun showMoveTrashedPostToDraftDialog(post: PostModel) {
val dialogHolder = DialogHolder(
Expand Down Expand Up @@ -137,6 +139,18 @@ class PostListDialogHelper(
showDialog.invoke(dialogHolder)
}

fun showCopyConflictDialog(post: PostModel) {
val dialogHolder = DialogHolder(
tag = COPY_CONFLICT_DIALOG_TAG,
title = UiStringRes(R.string.dialog_confirm_copy_local_title),
message = UiStringRes(R.string.dialog_confirm_copy_local_message),
positiveButton = UiStringRes(R.string.dialog_confirm_copy_local_edit_button),
negativeButton = UiStringRes(R.string.dialog_confirm_copy_local_copy_button)
)
localPostIdForCopyConflictDialog = post.id
showDialog.invoke(dialogHolder)
}

fun onPositiveClickedForBasicDialog(
instanceTag: String,
trashPostWithLocalChanges: (Int) -> Unit,
Expand All @@ -145,7 +159,8 @@ class PostListDialogHelper(
publishPost: (Int) -> Unit,
updateConflictedPostWithRemoteVersion: (Int) -> Unit,
editRestoredAutoSavePost: (Int) -> Unit,
moveTrashedPostToDraft: (Int) -> Unit
moveTrashedPostToDraft: (Int) -> Unit,
resolveConflictsAndEditPost: (Int) -> Unit
) {
when (instanceTag) {
CONFIRM_DELETE_POST_DIALOG_TAG -> localPostIdForDeleteDialog?.let {
Expand Down Expand Up @@ -181,14 +196,19 @@ class PostListDialogHelper(
UNPUBLISHED_REVISION_DIALOG_LOAD_UNPUBLISHED_VERSION_CLICKED,
mapOf(POST_TYPE to "post"))
}
COPY_CONFLICT_DIALOG_TAG -> localPostIdForCopyConflictDialog?.let {
localPostIdForCopyConflictDialog = null
resolveConflictsAndEditPost(it)
}
else -> throw IllegalArgumentException("Dialog's positive button click is not handled: $instanceTag")
}
}

fun onNegativeClickedForBasicDialog(
instanceTag: String,
updateConflictedPostWithLocalVersion: (Int) -> Unit,
editLocalPost: (Int) -> Unit
editLocalPost: (Int) -> Unit,
copyLocalPost: (Int) -> Unit
) {
when (instanceTag) {
CONFIRM_DELETE_POST_DIALOG_TAG -> localPostIdForDeleteDialog = null
Expand All @@ -207,14 +227,19 @@ class PostListDialogHelper(
)
}
CONFIRM_RESTORE_TRASHED_POST_DIALOG_TAG -> localPostIdForMoveTrashedPostToDraftDialog = null
COPY_CONFLICT_DIALOG_TAG -> localPostIdForCopyConflictDialog?.let {
localPostIdForCopyConflictDialog = null
copyLocalPost(it)
}
else -> throw IllegalArgumentException("Dialog's negative button click is not handled: $instanceTag")
}
}

fun onDismissByOutsideTouchForBasicDialog(
instanceTag: String,
updateConflictedPostWithLocalVersion: (Int) -> Unit,
editLocalPost: (Int) -> Unit
editLocalPost: (Int) -> Unit,
copyLocalPost: (Int) -> Unit
) {
// Cancel and outside touch dismiss works the same way for all, except for conflict and autosave revision
// dialogs, for which tapping outside and actively tapping the "edit local" have different meanings
Expand All @@ -223,7 +248,8 @@ class PostListDialogHelper(
onNegativeClickedForBasicDialog(
instanceTag = instanceTag,
updateConflictedPostWithLocalVersion = updateConflictedPostWithLocalVersion,
editLocalPost = editLocalPost
editLocalPost = editLocalPost,
copyLocalPost = copyLocalPost
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,28 @@ class PostListMainViewModel @Inject constructor(
checkNetworkConnection = this::checkNetworkConnection,
showSnackbar = { _snackBarMessage.postValue(it) },
showToast = { _toastMessage.postValue(it) },
triggerPreviewStateUpdate = this::updatePreviewAndDialogState
triggerPreviewStateUpdate = this::updatePreviewAndDialogState,
copyPost = this::copyPost
)
}

fun copyPost(site: SiteModel, postToCopy: PostModel, performChecks: Boolean = false) {
if (performChecks && (postConflictResolver.doesPostHaveUnhandledConflict(postToCopy) ||
postConflictResolver.hasUnhandledAutoSave(postToCopy))) {
postListDialogHelper.showCopyConflictDialog(postToCopy)
return
}
val post = postStore.instantiatePostModel(
site,
false,
postToCopy.title,
postToCopy.content,
PostStatus.DRAFT.toString(),
postToCopy.categoryIdList,
postToCopy.postFormat,
true
)
_postListAction.postValue(PostListAction.EditPost(site, post, loadAutoSaveRevision = false))
}

/**
Expand Down Expand Up @@ -437,6 +457,15 @@ class PostListMainViewModel @Inject constructor(
}
}

private fun copyLocalPost(localPostId: Int) {
val post = postStore.getPostByLocalPostId(localPostId)
if (post != null) {
copyPost(site, post)
} else {
_snackBarMessage.value = SnackbarMessageHolder(UiStringRes(R.string.error_post_does_not_exist))
}
}

// BasicFragmentDialog Events

fun onPositiveClickedForBasicDialog(instanceTag: String) {
Expand All @@ -448,23 +477,26 @@ class PostListMainViewModel @Inject constructor(
publishPost = postActionHandler::publishPost,
updateConflictedPostWithRemoteVersion = postConflictResolver::updateConflictedPostWithRemoteVersion,
editRestoredAutoSavePost = this::editRestoredAutoSavePost,
moveTrashedPostToDraft = postActionHandler::moveTrashedPostToDraft
moveTrashedPostToDraft = postActionHandler::moveTrashedPostToDraft,
resolveConflictsAndEditPost = postActionHandler::resolveConflictsAndEditPost
)
}

fun onNegativeClickedForBasicDialog(instanceTag: String) {
postListDialogHelper.onNegativeClickedForBasicDialog(
instanceTag = instanceTag,
updateConflictedPostWithLocalVersion = postConflictResolver::updateConflictedPostWithLocalVersion,
editLocalPost = this::editLocalPost
editLocalPost = this::editLocalPost,
copyLocalPost = this::copyLocalPost
)
}

fun onDismissByOutsideTouchForBasicDialog(instanceTag: String) {
postListDialogHelper.onDismissByOutsideTouchForBasicDialog(
instanceTag = instanceTag,
updateConflictedPostWithLocalVersion = postConflictResolver::updateConflictedPostWithLocalVersion,
editLocalPost = this::editLocalPost
editLocalPost = this::editLocalPost,
copyLocalPost = this::copyLocalPost
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import org.wordpress.android.viewmodel.posts.PostListItemType.PostListItemUiStat
import org.wordpress.android.viewmodel.uistate.ProgressBarUiState
import org.wordpress.android.widgets.PostListButtonType
import org.wordpress.android.widgets.PostListButtonType.BUTTON_CANCEL_PENDING_AUTO_UPLOAD
import org.wordpress.android.widgets.PostListButtonType.BUTTON_COPY
import org.wordpress.android.widgets.PostListButtonType.BUTTON_DELETE
import org.wordpress.android.widgets.PostListButtonType.BUTTON_DELETE_PERMANENTLY
import org.wordpress.android.widgets.PostListButtonType.BUTTON_EDIT
Expand Down Expand Up @@ -351,6 +352,7 @@ class PostListItemUiStateHelper @Inject constructor(
postStatus == PUBLISHED &&
!isLocalDraft &&
!isLocallyChanged
val canShowCopy = postStatus == PUBLISHED || postStatus == DRAFT
val canShowViewButton = !canRetryUpload && postStatus != PostStatus.TRASHED
val canShowPublishButton = canRetryUpload || canPublishPost
val buttonTypes = ArrayList<PostListButtonType>()
Expand Down Expand Up @@ -400,6 +402,10 @@ class PostListItemUiStateHelper @Inject constructor(
buttonTypes.add(BUTTON_STATS)
}

if (canShowCopy) {
buttonTypes.add(BUTTON_COPY)
}

return buttonTypes
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ enum class PostListButtonType constructor(
R.drawable.ic_undo_white_24dp,
R.attr.wpColorWarningDark
),
BUTTON_SHOW_MOVE_TRASHED_POST_TO_DRAFT_DIALOG(15, 0, 0, 0);
BUTTON_SHOW_MOVE_TRASHED_POST_TO_DRAFT_DIALOG(15, 0, 0, 0),
BUTTON_COPY(16, R.string.button_copy, R.drawable.ic_copy_white_24dp, R.attr.colorOnSurface);

companion object {
fun fromInt(value: Int): PostListButtonType? = values().firstOrNull { it.value == value }
Expand Down
7 changes: 7 additions & 0 deletions WordPress/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@
<string name="button_discard">Discard</string>
<string name="button_retry">Retry</string>
<string name="button_move_to_draft">Move to Draft</string>
<string name="button_copy">Duplicate</string>

<!-- post uploads -->
<string name="upload_failed_param">Upload failed for \"%s\"</string>
Expand Down Expand Up @@ -373,6 +374,12 @@
<string name="dialog_confirm_autosave_restore_button">The version from another device</string>
<string name="dialog_confirm_autosave_dont_restore_button">The version from this app</string>

<!-- post confirm copy local post dialog -->
<string name="dialog_confirm_copy_local_title">Post sync conflict</string>
<string name="dialog_confirm_copy_local_message">The post you are trying to copy has two versions that are in conflict or you recently made changes but didn\'t save them.\nEdit the post first to resolve any conflict or proceed with copying the version from this app.</string>
<string name="dialog_confirm_copy_local_edit_button">Edit the post first</string>
<string name="dialog_confirm_copy_local_copy_button">Copy the version from this app</string>

<!-- trash post with local changes dialog -->
<string name="dialog_confirm_trash_losing_local_changes_title">Local changes</string>
<string name="dialog_confirm_trash_losing_local_changes_message">Trashing this post will also discard local changes, are you sure you want to continue?</string>
Expand Down
Loading