Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft: UIContextMenuInteraction #159

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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