Skip to content

Commit

Permalink
Extract context menu to AbstractPostHelper
Browse files Browse the repository at this point in the history
  • Loading branch information
kean committed Nov 3, 2023
1 parent dd774d2 commit 9ef392b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -322,10 +322,8 @@ class PageListViewController: AbstractPostListViewController, UIViewControllerRe
UIContextMenuConfiguration(identifier: nil, previewProvider: nil) { [weak self] _ in
guard let self else { return nil }
let page = self.pages[indexPath.row]
let viewModel = PageMenuViewModel(page: page)
let helper = AbstractPostMenuHelper(page, viewModel: viewModel)
let cell = self.tableView.cellForRow(at: indexPath)
return helper.makeMenu(presentingView: cell?.contentView ?? UIView(), delegate: self)
return AbstractPostHelper.makeContextMenu(for: page, presentingView: cell, delegate: self)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import UIKit

extension AbstractPostHelper {
// MARK: - Swipe Actions

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

Expand Down Expand Up @@ -42,6 +44,23 @@ extension AbstractPostHelper {

return actions
}

// MARK: - Context Menu

static func makeContextMenu(for post: AbstractPost, presentingView: UIView?, delegate: InteractivePostViewDelegate) -> UIMenu {
switch post {
case let post as Post:
let viewModel = PostListItemViewModel(post: post).statusViewModel
let helper = AbstractPostMenuHelper(post, viewModel: viewModel)
return helper.makeMenu(presentingView: presentingView ?? UIView(), delegate: delegate)
case let page as Page:
let viewModel = PageMenuViewModel(page: page)
let helper = AbstractPostMenuHelper(page, viewModel: viewModel)
return helper.makeMenu(presentingView: presentingView ?? UIView(), delegate: delegate)
default:
fatalError("Unsupported post type: \(type(of: post))")
}
}
}

private enum Strings {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,10 +259,8 @@ class PostListViewController: AbstractPostListViewController, UIViewControllerRe
UIContextMenuConfiguration(identifier: nil, previewProvider: nil) { [weak self] _ in
guard let self else { return nil }
let post = self.postAtIndexPath(indexPath)
let viewModel = PostListItemViewModel(post: post).statusViewModel
let helper = AbstractPostMenuHelper(post, viewModel: viewModel)
let cell = self.tableView.cellForRow(at: indexPath)
return helper.makeMenu(presentingView: cell?.contentView ?? UIView(), delegate: self)
return AbstractPostHelper.makeContextMenu(for: post, presentingView: cell, delegate: self)
}
}

Expand Down

0 comments on commit 9ef392b

Please sign in to comment.