Swiftgram/Display/ContextMenuController.swift
2016-09-05 23:19:33 +03:00

73 lines
2.4 KiB
Swift

import Foundation
import AsyncDisplayKit
public final class ContextMenuControllerPresentationArguments {
fileprivate let sourceNodeAndRect: () -> (ASDisplayNode, CGRect)?
public init(sourceNodeAndRect: @escaping () -> (ASDisplayNode, CGRect)?) {
self.sourceNodeAndRect = sourceNodeAndRect
}
}
public final class ContextMenuController: ViewController {
private var contextMenuNode: ContextMenuNode {
return self.displayNode as! ContextMenuNode
}
private let actions: [ContextMenuAction]
private var layout: ContainerViewLayout?
public init(actions: [ContextMenuAction]) {
self.actions = actions
super.init()
}
required public init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
open override func loadDisplayNode() {
self.displayNode = ContextMenuNode(actions: self.actions, dismiss: { [weak self] in
self?.contextMenuNode.animateOut { [weak self] in
self?.presentingViewController?.dismiss(animated: false)
}
})
self.displayNodeDidLoad()
self.navigationBar.isHidden = true
}
override public func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
self.contextMenuNode.animateIn()
}
override open func containerLayoutUpdated(_ layout: ContainerViewLayout, transition: ContainedViewLayoutTransition) {
super.containerLayoutUpdated(layout, transition: transition)
if self.layout != nil && self.layout! != layout {
self.contextMenuNode.animateOut { [weak self] in
self?.presentingViewController?.dismiss(animated: false)
}
} else {
self.layout = layout
if let presentationArguments = self.presentationArguments as? ContextMenuControllerPresentationArguments, let (sourceNode, sourceRect) = presentationArguments.sourceNodeAndRect() {
self.contextMenuNode.sourceRect = sourceNode.view.convert(sourceRect, to: nil)
} else {
self.contextMenuNode.sourceRect = nil
}
self.contextMenuNode.containerLayoutUpdated(layout, transition: transition)
}
}
open override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.contextMenuNode.animateIn()
}
}