element-ios/Riot/Modules/Room/ContextualMenu/RoomContextualMenuPresenter.swift

124 lines
4.4 KiB
Swift
Raw Normal View History

2019-05-15 21:10:07 +00:00
/*
Copyright 2019 New Vector Ltd
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import Foundation
@objcMembers
final class RoomContextualMenuPresenter: NSObject {
// MARK: - Constants
private enum Constants {
static let animationDuration: TimeInterval = 0.2
2019-05-15 21:10:07 +00:00
}
// MARK: - Properties
// MARK: Private
private weak var roomContextualMenuViewController: RoomContextualMenuViewController?
// MARK: Public
var isPresenting: Bool {
return self.roomContextualMenuViewController != nil
}
// MARK: - Public
2019-05-15 21:10:07 +00:00
func present(roomContextualMenuViewController: RoomContextualMenuViewController,
from viewController: UIViewController,
on view: UIView,
contentToReactFrame: CGRect, // Not nullable for compatibility with Obj-C
2019-05-15 21:10:07 +00:00
animated: Bool,
completion: (() -> Void)?) {
guard self.roomContextualMenuViewController == nil else {
return
}
viewController.vc_addChildViewController(viewController: roomContextualMenuViewController, onView: view)
self.roomContextualMenuViewController = roomContextualMenuViewController
roomContextualMenuViewController.contentToReactFrame = contentToReactFrame
2019-05-15 21:10:07 +00:00
roomContextualMenuViewController.hideMenuToolbar()
roomContextualMenuViewController.prepareReactionsMenuAnimations()
roomContextualMenuViewController.hideReactionsMenu()
2019-05-15 21:10:07 +00:00
roomContextualMenuViewController.view.layoutIfNeeded()
let animationInstructions: (() -> Void) = {
roomContextualMenuViewController.showMenuToolbar()
roomContextualMenuViewController.showReactionsMenu()
2019-05-15 21:10:07 +00:00
roomContextualMenuViewController.view.layoutIfNeeded()
}
if animated {
UIView.animate(withDuration: Constants.animationDuration, animations: {
animationInstructions()
}, completion: { completed in
completion?()
})
} else {
animationInstructions()
completion?()
}
}
func hideContextualMenu(animated: Bool, completion: (() -> Void)?) {
guard let roomContextualMenuViewController = self.roomContextualMenuViewController else {
completion?()
2019-05-15 21:10:07 +00:00
return
}
let animationInstructions: (() -> Void) = {
roomContextualMenuViewController.hideMenuToolbar()
roomContextualMenuViewController.hideReactionsMenu()
2019-05-15 21:10:07 +00:00
roomContextualMenuViewController.view.layoutIfNeeded()
}
let animationCompletionInstructions: (() -> Void) = {
roomContextualMenuViewController.vc_removeFromParent()
completion?()
}
if animated {
if roomContextualMenuViewController.shouldPerformTappedReactionAnimation {
UIView.animate(withDuration: 0.15, animations: {
roomContextualMenuViewController.selectedReactionAnimationsIntructionsPart1()
}, completion: { _ in
UIView.animate(withDuration: Constants.animationDuration, animations: {
roomContextualMenuViewController.selectedReactionAnimationsIntructionsPart2()
animationInstructions()
}, completion: { completed in
animationCompletionInstructions()
})
})
} else {
UIView.animate(withDuration: Constants.animationDuration, animations: {
animationInstructions()
}, completion: { completed in
animationCompletionInstructions()
})
}
2019-05-15 21:10:07 +00:00
} else {
animationInstructions()
animationCompletionInstructions()
}
}
}