From ee0ecd2cf0c0c75ae538013678c5c63c15a277fd Mon Sep 17 00:00:00 2001 From: kean Date: Thu, 2 Nov 2023 19:06:18 -0400 Subject: [PATCH] Add swipe actions in post --- .../Post/AbstractPostHelper+Actions.swift | 6 ++-- .../Search/PostSearchViewController.swift | 28 +++++++++++++++---- 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/WordPress/Classes/ViewRelated/Post/AbstractPostHelper+Actions.swift b/WordPress/Classes/ViewRelated/Post/AbstractPostHelper+Actions.swift index 4ade9227ec80..046bed200c0f 100644 --- a/WordPress/Classes/ViewRelated/Post/AbstractPostHelper+Actions.swift +++ b/WordPress/Classes/ViewRelated/Post/AbstractPostHelper+Actions.swift @@ -1,8 +1,6 @@ import UIKit extension AbstractPostHelper { - // MARK: - Posts - static func makeLeadingContextualActions(for post: AbstractPost, delegate: InteractivePostViewDelegate) -> [UIContextualAction] { var actions: [UIContextualAction] = [] @@ -33,7 +31,7 @@ extension AbstractPostHelper { trashAction.image = UIImage(systemName: "trash") actions.append(trashAction) - if post.status == .publish && post.hasRemote() { + if post is Post, 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) @@ -50,5 +48,5 @@ 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") + static let swipeActionDeletePermanently = NSLocalizedString("postList.swipeActionDelete", value: "Delete", comment: "Swipe action title") } diff --git a/WordPress/Classes/ViewRelated/Post/Search/PostSearchViewController.swift b/WordPress/Classes/ViewRelated/Post/Search/PostSearchViewController.swift index 5829610118c4..5139b900cc1c 100644 --- a/WordPress/Classes/ViewRelated/Post/Search/PostSearchViewController.swift +++ b/WordPress/Classes/ViewRelated/Post/Search/PostSearchViewController.swift @@ -18,6 +18,10 @@ final class PostSearchViewController: UIViewController, UITableViewDelegate, UIS private var cancellables: [AnyCancellable] = [] + private var postDelegate: InteractivePostViewDelegate { + listViewController as! InteractivePostViewDelegate + } + init(viewModel: PostSearchViewModel) { self.viewModel = viewModel @@ -109,13 +113,13 @@ final class PostSearchViewController: UIViewController, UITableViewDelegate, UIS let cell = tableView.dequeueReusableCell(withIdentifier: Constants.postCellID, for: indexPath) as! PostListCell assert(listViewController is InteractivePostViewDelegate) let viewModel = PostListItemViewModel(post: post) - cell.configure(with: viewModel, delegate: listViewController as? InteractivePostViewDelegate) + cell.configure(with: viewModel, delegate: postDelegate) updateHighlights(for: [cell], searchTerm: self.viewModel.searchTerm) return cell case let page as Page: let cell = tableView.dequeueReusableCell(withIdentifier: Constants.pageCellID, for: indexPath) as! PageListCell let viewModel = PageListItemViewModel(page: page, indexPath: indexPath) - cell.configure(with: viewModel, delegate: listViewController as? InteractivePostViewDelegate) + cell.configure(with: viewModel, delegate: postDelegate) updateHighlights(for: [cell], searchTerm: self.viewModel.searchTerm) return cell default: @@ -165,12 +169,10 @@ final class PostSearchViewController: UIViewController, UITableViewDelegate, UIS switch viewModel.posts[indexPath.row].latest() { case let post as Post: guard post.status != .trash else { return } - (listViewController as! PostListViewController) - .edit(post) + postDelegate.edit(post) case let page as Page: guard page.status != .trash else { return } - (listViewController as! PageListViewController) - .edit(page) + postDelegate.edit(page) default: fatalError("Unsupported post") } @@ -184,6 +186,20 @@ final class PostSearchViewController: UIViewController, UITableViewDelegate, UIS } } + func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool { + indexPath.section == SectionID.posts.rawValue + } + + func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? { + let actions = AbstractPostHelper.makeLeadingContextualActions(for: viewModel.posts[indexPath.row], delegate: postDelegate) + return UISwipeActionsConfiguration(actions: actions) + } + + func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? { + let actions = AbstractPostHelper.makeTrailingContextualActions(for: viewModel.posts[indexPath.row], delegate: postDelegate) + return UISwipeActionsConfiguration(actions: actions) + } + // MARK: - UISearchControllerDelegate func willPresentSearchController(_ searchController: UISearchController) {