Skip to content

Commit

Permalink
Add initial implementation for context menu interaction delegate
Browse files Browse the repository at this point in the history
  • Loading branch information
pauljohanneskraft committed Mar 29, 2022
1 parent eb6edf7 commit 2072b66
Show file tree
Hide file tree
Showing 4 changed files with 774 additions and 698 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
//
// CoordinatorContextMenuInteractionDelegate.swift
// XCoordinator
//
// Created by Paul Kraft on 13.02.20.
//

import UIKit

@available(iOS 13.0, *)
internal class CoordinatorContextMenuInteractionDelegate<TransitionType: TransitionProtocol>: NSObject, UIContextMenuInteractionDelegate {

// MARK: Stored properties

private let identifier: NSCopying?
private let transition: () -> TransitionType
private let menu: UIMenu?

private let rootViewController: TransitionType.RootViewController
private let completion: PresentationHandler?

// MARK: Initialization

internal init(
identifier: NSCopying?,
transition: @escaping () -> TransitionType,
rootViewController: TransitionType.RootViewController,
menu: UIMenu?,
completion: PresentationHandler?
) {
self.identifier = identifier
self.transition = transition
self.menu = menu
self.rootViewController = rootViewController
self.completion = completion
}

// MARK: Methods

internal func contextMenuInteraction(
_ interaction: UIContextMenuInteraction,
configurationForMenuAtLocation location: CGPoint
) -> UIContextMenuConfiguration? {
UIContextMenuConfiguration(
identifier: identifier,
previewProvider: { [weak self] in
self?.transition().presentables.last?.viewController
},
actionProvider: { [weak self] _ in
self?.menu
}
)
}

internal func contextMenuInteraction(
_ interaction: UIContextMenuInteraction,
willDisplayMenuFor configuration: UIContextMenuConfiguration,
animator: UIContextMenuInteractionAnimating?
) {
print(#function)
}

internal func contextMenuInteraction(
_ interaction: UIContextMenuInteraction,
willPerformPreviewActionForMenuWith configuration: UIContextMenuConfiguration,
animator: UIContextMenuInteractionCommitAnimating
) {
transition().perform(on: rootViewController,
with: .default,
completion: completion)
}

internal func contextMenuInteraction(
_ interaction: UIContextMenuInteraction,
willEndFor configuration: UIContextMenuConfiguration,
animator: UIContextMenuInteractionAnimating?
) {
completion?()
}

}
27 changes: 27 additions & 0 deletions Sources/XCoordinator/Transition+Init.swift
Original file line number Diff line number Diff line change
Expand Up @@ -274,4 +274,31 @@ extension Coordinator where Self: AnyObject {
}
}

///
/// Use this method to create a UIContextMenuInteractionDelegate to generate a preview
/// from a given route and perform the route when the preview is tapped.
///
/// - Parameters:
/// - route: The route to be triggered when the preview has been selected.
/// - menu: The menu to be shown alongside the preview.
///
@available(iOS 13.0, *)
public func contextMenuInteractionDelegate(
for route: RouteType,
identifier: NSCopying? = nil,
menu: UIMenu? = nil,
completion: PresentationHandler? = nil
) -> UIContextMenuInteractionDelegate {

CoordinatorContextMenuInteractionDelegate(
identifier: identifier,
transition: { [weak self] in
self?.prepareTransition(for: route) ?? .multiple()
},
rootViewController: rootViewController,
menu: menu,
completion: completion
)
}

}
Loading

0 comments on commit 2072b66

Please sign in to comment.