Skip to content

Commit

Permalink
Add swipe actions in post
Browse files Browse the repository at this point in the history
  • Loading branch information
kean committed Nov 2, 2023
1 parent 7f1edc9 commit ee0ecd2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import UIKit

extension AbstractPostHelper {
// MARK: - Posts

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

Expand Down Expand Up @@ -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)
Expand All @@ -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")
}
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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")
}
Expand All @@ -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) {
Expand Down

0 comments on commit ee0ecd2

Please sign in to comment.