Skip to content

Commit

Permalink
Add swipe actions for posts
Browse files Browse the repository at this point in the history
  • Loading branch information
kean committed Nov 2, 2023
1 parent ff98042 commit 7f1edc9
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ extension PageListViewController: InteractivePostViewDelegate {
publishPost(apost)
}

func trash(_ apost: AbstractPost) {
guard let page = apost as? Page else { return }
trashPage(page)
func trash(_ post: AbstractPost, completion: @escaping () -> Void) {
guard let page = post as? Page else { return }
trashPage(page, completion: completion)
}

func draft(_ apost: AbstractPost) {
Expand Down Expand Up @@ -90,7 +90,7 @@ extension PageListViewController: InteractivePostViewDelegate {
present(editorViewController, animated: false)
}

private func trashPage(_ page: Page) {
private func trashPage(_ page: Page, completion: @escaping () -> Void) {
guard ReachabilityUtils.isInternetReachable() else {
ReachabilityUtils.showNoInternetConnectionNotice(message: Strings.offlineMessage)
return
Expand All @@ -102,9 +102,12 @@ extension PageListViewController: InteractivePostViewDelegate {
let messageText = isPageTrashed ? Strings.DeletePermanently.messageText : Strings.Trash.messageText

let alertController = UIAlertController(title: titleText, message: messageText, preferredStyle: .alert)
alertController.addCancelActionWithTitle(Strings.cancelText)
alertController.addDestructiveActionWithTitle(actionText) { [weak self] action in
alertController.addCancelActionWithTitle(Strings.cancelText) { _ in
completion()
}
alertController.addDestructiveActionWithTitle(actionText) { [weak self] _ in
self?.deletePost(page)
completion()
}
alertController.presentFromRootViewController()
}
Expand Down
16 changes: 15 additions & 1 deletion WordPress/Classes/ViewRelated/Pages/PageListViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ class PageListViewController: AbstractPostListViewController, UIViewControllerRe
refreshResults()
}

// MARK: - TableView Handler Delegate Methods
// MARK: - Core Data

override func entityName() -> String {
return String(describing: Page.self)
Expand Down Expand Up @@ -333,6 +333,20 @@ class PageListViewController: AbstractPostListViewController, UIViewControllerRe
}
}

func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
indexPath.section == Section.pages.rawValue
}

func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let actions = AbstractPostHelper.makeLeadingContextualActions(for: pages[indexPath.row], delegate: self)
return UISwipeActionsConfiguration(actions: actions)
}

func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let actions = AbstractPostHelper.makeTrailingContextualActions(for: pages[indexPath.row], delegate: self)
return UISwipeActionsConfiguration(actions: actions)
}

// MARK: - UITableViewDataSource

func numberOfSections(in tableView: UITableView) -> Int {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import UIKit

extension AbstractPostHelper {
// MARK: - Posts

static func makeLeadingContextualActions(for post: AbstractPost, delegate: InteractivePostViewDelegate) -> [UIContextualAction] {
var actions: [UIContextualAction] = []

if post.status != .trash {
let viewAction = UIContextualAction(style: .normal, title: Strings.swipeActionView) { [weak delegate] _, _, completion in
delegate?.view(post)
completion(true)
}
viewAction.image = UIImage(systemName: "safari")
viewAction.backgroundColor = .systemBlue
actions.append(viewAction)
}

return actions
}

static func makeTrailingContextualActions(for post: AbstractPost, delegate: InteractivePostViewDelegate) -> [UIContextualAction] {
var actions: [UIContextualAction] = []

let trashAction = UIContextualAction(
style: .destructive,
title: post.status == .trash ? Strings.swipeActionDeletePermanently : Strings.swipeActionTrash
) { [weak delegate] _, _, completion in
delegate?.trash(post) {
completion(true)
}
}
trashAction.image = UIImage(systemName: "trash")
actions.append(trashAction)

if post.status == .publish && post.hasRemote() {
let shareAction = UIContextualAction(style: .normal, title: Strings.swipeActionShare) { [weak delegate] _, view, completion in
delegate?.share(post, fromView: view)
completion(true)
}
shareAction.image = UIImage(systemName: "square.and.arrow.up")
actions.append(shareAction)
}

return actions
}
}

private enum Strings {
static let swipeActionView = NSLocalizedString("postList.swipeActionView", value: "View", comment: "Swipe action title")
static let swipeActionShare = NSLocalizedString("postList.swipeActionShare", value: "Share", comment: "Swipe action title")
static let swipeActionTrash = NSLocalizedString("postList.swipeActionDelete", value: "Trash", comment: "Swipe action title")
static let swipeActionDeletePermanently = NSLocalizedString("postList.swipeActionDelete", value: "Delete Permanently", comment: "Swipe action title")
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ protocol InteractivePostViewDelegate: AnyObject {
func stats(for post: AbstractPost)
func duplicate(_ post: AbstractPost)
func publish(_ post: AbstractPost)
func trash(_ post: AbstractPost)
func trash(_ post: AbstractPost, completion: @escaping () -> Void)
func draft(_ post: AbstractPost)
func retry(_ post: AbstractPost)
func cancelAutoUpload(_ post: AbstractPost)
Expand All @@ -22,4 +22,8 @@ extension InteractivePostViewDelegate {
func setParent(for post: AbstractPost, at indexPath: IndexPath) {}
func setHomepage(for post: AbstractPost) {}
func setPostsPage(for post: AbstractPost) {}

func trash(_ post: AbstractPost) {
self.trash(post, completion: {})
}
}
21 changes: 19 additions & 2 deletions WordPress/Classes/ViewRelated/Post/PostListViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,20 @@ class PostListViewController: AbstractPostListViewController, UIViewControllerRe
}
}

func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
true
}

func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let actions = AbstractPostHelper.makeLeadingContextualActions(for: postAtIndexPath(indexPath), delegate: self)
return UISwipeActionsConfiguration(actions: actions)
}

func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let actions = AbstractPostHelper.makeTrailingContextualActions(for: postAtIndexPath(indexPath), delegate: self)
return UISwipeActionsConfiguration(actions: actions)
}

// MARK: - Post Actions

override func createPost() {
Expand Down Expand Up @@ -359,7 +373,7 @@ class PostListViewController: AbstractPostListViewController, UIViewControllerRe
copyPostLink(post)
}

func trash(_ post: AbstractPost) {
func trash(_ post: AbstractPost, completion: @escaping () -> Void) {
guard ReachabilityUtils.isInternetReachable() else {
let offlineMessage = NSLocalizedString("Unable to trash posts while offline. Please try again later.", comment: "Message that appears when a user tries to trash a post while their device is offline.")
ReachabilityUtils.showNoInternetConnectionNotice(message: offlineMessage)
Expand All @@ -385,9 +399,12 @@ class PostListViewController: AbstractPostListViewController, UIViewControllerRe

let alertController = UIAlertController(title: titleText, message: messageText, preferredStyle: .alert)

alertController.addCancelActionWithTitle(cancelText)
alertController.addCancelActionWithTitle(cancelText) { _ in
completion()
}
alertController.addDestructiveActionWithTitle(deleteText) { [weak self] action in
self?.deletePost(post)
completion()
}
alertController.presentFromRootViewController()
}
Expand Down
6 changes: 6 additions & 0 deletions WordPress/WordPress.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,8 @@
0CF0C4232AE98C13006FFAB4 /* AbstractPostHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0CF0C4222AE98C13006FFAB4 /* AbstractPostHelper.swift */; };
0CF0C4242AE98C13006FFAB4 /* AbstractPostHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0CF0C4222AE98C13006FFAB4 /* AbstractPostHelper.swift */; };
0CF7D6C32ABB753A006D1E89 /* MediaImageServiceTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0CF7D6C22ABB753A006D1E89 /* MediaImageServiceTests.swift */; };
0CFE9AC62AF44A9F00B8F659 /* AbstractPostHelper+Actions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0CFE9AC52AF44A9F00B8F659 /* AbstractPostHelper+Actions.swift */; };
0CFE9AC72AF44A9F00B8F659 /* AbstractPostHelper+Actions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0CFE9AC52AF44A9F00B8F659 /* AbstractPostHelper+Actions.swift */; };
1702BBDC1CEDEA6B00766A33 /* BadgeLabel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1702BBDB1CEDEA6B00766A33 /* BadgeLabel.swift */; };
1702BBE01CF3034E00766A33 /* DomainsService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1702BBDF1CF3034E00766A33 /* DomainsService.swift */; };
17039225282E6D2800F602E9 /* ViewsVisitorsLineChartCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = DC772B0728201F5300664C02 /* ViewsVisitorsLineChartCell.swift */; };
Expand Down Expand Up @@ -6189,6 +6191,7 @@
0CF0C4222AE98C13006FFAB4 /* AbstractPostHelper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AbstractPostHelper.swift; sourceTree = "<group>"; };
0CF7D6C22ABB753A006D1E89 /* MediaImageServiceTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MediaImageServiceTests.swift; sourceTree = "<group>"; };
0CFD6C792A73E703003DD0A0 /* WordPress 152.xcdatamodel */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcdatamodel; path = "WordPress 152.xcdatamodel"; sourceTree = "<group>"; };
0CFE9AC52AF44A9F00B8F659 /* AbstractPostHelper+Actions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "AbstractPostHelper+Actions.swift"; sourceTree = "<group>"; };
131D0EE49695795ECEDAA446 /* Pods-WordPressTest.release-alpha.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-WordPressTest.release-alpha.xcconfig"; path = "../Pods/Target Support Files/Pods-WordPressTest/Pods-WordPressTest.release-alpha.xcconfig"; sourceTree = "<group>"; };
150B6590614A28DF9AD25491 /* Pods-Apps-Jetpack.release-alpha.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Apps-Jetpack.release-alpha.xcconfig"; path = "../Pods/Target Support Files/Pods-Apps-Jetpack/Pods-Apps-Jetpack.release-alpha.xcconfig"; sourceTree = "<group>"; };
152F25D5C232985E30F56CAC /* Pods-Apps-Jetpack.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Apps-Jetpack.debug.xcconfig"; path = "../Pods/Target Support Files/Pods-Apps-Jetpack/Pods-Apps-Jetpack.debug.xcconfig"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -12576,6 +12579,7 @@
FACF66CC2ADD645C008C3E13 /* PostListHeaderView.swift */,
FACF66CF2ADD6CD8008C3E13 /* PostListItemViewModel.swift */,
0CF0C4222AE98C13006FFAB4 /* AbstractPostHelper.swift */,
0CFE9AC52AF44A9F00B8F659 /* AbstractPostHelper+Actions.swift */,
FAD3DE802AE2965A00A3B031 /* AbstractPostMenuHelper.swift */,
FAEC116D2AEBEEA600F9DA54 /* AbstractPostMenuViewModel.swift */,
FA141F262AEC1D9E00C9A653 /* PageMenuViewModel.swift */,
Expand Down Expand Up @@ -21875,6 +21879,7 @@
85DA8C4418F3F29A0074C8A4 /* WPAnalyticsTrackerWPCom.m in Sources */,
B50C0C5E1EF42A4A00372C65 /* AztecAttachmentViewController.swift in Sources */,
43FB3F411EBD215C00FC8A62 /* LoginEpilogueBlogCell.swift in Sources */,
0CFE9AC62AF44A9F00B8F659 /* AbstractPostHelper+Actions.swift in Sources */,
57D66B9A234BB206005A2D74 /* PostServiceRemoteFactory.swift in Sources */,
3FC8D19B244F43B500495820 /* ReaderTabItemsStore.swift in Sources */,
98EB126A20D2DC2500D2D5B5 /* NoResultsViewController+Model.swift in Sources */,
Expand Down Expand Up @@ -25348,6 +25353,7 @@
FABB25312602FC2C00C8785C /* UIImageView+SiteIcon.swift in Sources */,
FABB25322602FC2C00C8785C /* ReaderSiteSearchService.swift in Sources */,
FABB25332602FC2C00C8785C /* QuickStartNavigationSettings.swift in Sources */,
0CFE9AC72AF44A9F00B8F659 /* AbstractPostHelper+Actions.swift in Sources */,
FABB25342602FC2C00C8785C /* WPImageViewController.m in Sources */,
FABB25352602FC2C00C8785C /* ReaderListTopic.swift in Sources */,
FABB25362602FC2C00C8785C /* PublicizeService.swift in Sources */,
Expand Down

0 comments on commit 7f1edc9

Please sign in to comment.