|
| 1 | +import Foundation |
| 2 | +import UIKit |
| 3 | +import SwiftUI |
| 4 | + |
| 5 | +/// This struct encapsulates the content to displayed in a Permanent Notice |
| 6 | +/// |
| 7 | +struct PermanentNotice { |
| 8 | + let message: String |
| 9 | + let callToActionTitle: String |
| 10 | + let callToActionHandler: () -> Void |
| 11 | +} |
| 12 | + |
| 13 | +/// Presents a banner permanently, that is, will remain until it is called to dismiss, unlike `DefaultNoticePresenter`, which dismiss the notice after a delay. |
| 14 | +/// The design also differs with the latter to represent a more permanent air. |
| 15 | +/// |
| 16 | +final class PermanentNoticePresenter { |
| 17 | + private var hostingController: UIHostingController<PermanentNoticeView>? |
| 18 | + |
| 19 | + /// Presents the given notice into the passed view controller with an animation |
| 20 | + /// |
| 21 | + func presentNotice(notice: PermanentNotice, from viewController: UIViewController) { |
| 22 | + let permanentNoticeView = PermanentNoticeView(notice: notice) |
| 23 | + let newHostingController = ConstraintsUpdatingHostingController(rootView: permanentNoticeView) |
| 24 | + |
| 25 | + guard let hostingView = newHostingController.view else { |
| 26 | + return |
| 27 | + } |
| 28 | + |
| 29 | + viewController.addChild(newHostingController) |
| 30 | + viewController.view.addSubview(hostingView) |
| 31 | + |
| 32 | + setupConstraints(in: viewController, hostingView: hostingView) |
| 33 | + animatePresentation(of: newHostingController, in: viewController) |
| 34 | + |
| 35 | + self.hostingController = newHostingController |
| 36 | + } |
| 37 | + |
| 38 | + /// Dismiss the permanent notice presented by calling `presentNotice`. If there is no one presented this method does nothing. |
| 39 | + /// |
| 40 | + func dismiss() { |
| 41 | + animateDismiss() |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +private extension PermanentNoticePresenter { |
| 46 | + func setupConstraints(in viewController: UIViewController, hostingView: UIView) { |
| 47 | + hostingView.translatesAutoresizingMaskIntoConstraints = false |
| 48 | + |
| 49 | + NSLayoutConstraint.activate([ |
| 50 | + viewController.view.leadingAnchor.constraint(equalTo: hostingView.leadingAnchor, constant: 0), |
| 51 | + viewController.view.trailingAnchor.constraint(equalTo: hostingView.trailingAnchor, constant: 0), |
| 52 | + viewController.view.bottomAnchor.constraint(equalTo: hostingView.bottomAnchor, constant: 0), |
| 53 | + ]) |
| 54 | + } |
| 55 | + |
| 56 | + func animatePresentation(of hostingController: UIViewController, in viewController: UIViewController) { |
| 57 | + guard let hostingView = hostingController.view else { |
| 58 | + return |
| 59 | + } |
| 60 | + |
| 61 | + hostingView.alpha = 0 |
| 62 | + |
| 63 | + UIView.animate(withDuration: Animations.appearanceDuration, |
| 64 | + delay: 0, |
| 65 | + options: .transitionFlipFromLeft, animations: { |
| 66 | + hostingView.alpha = 1 |
| 67 | + }) { _ in |
| 68 | + hostingController.didMove(toParent: viewController) |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + func animateDismiss() { |
| 73 | + guard let hostingController = hostingController else { |
| 74 | + return |
| 75 | + } |
| 76 | + |
| 77 | + UIView.animate(withDuration: Animations.appearanceDuration, |
| 78 | + delay: 0, |
| 79 | + options: .transitionFlipFromLeft, animations: { |
| 80 | + hostingController.view.alpha = 0 |
| 81 | + }) { _ in |
| 82 | + hostingController.willMove(toParent: nil) |
| 83 | + hostingController.view.removeFromSuperview() |
| 84 | + } |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +private extension PermanentNoticePresenter { |
| 89 | + private enum Animations { |
| 90 | + static let appearanceDuration: TimeInterval = 0.5 |
| 91 | + } |
| 92 | +} |
0 commit comments