mvm_core_ui/MVMCoreUI/Notification/NotificationContainerView.swift
2023-07-14 22:46:22 +05:30

89 lines
2.7 KiB
Swift

//
// MVMCoreUITopAlertView+Extension.swift
// MVMCoreUI
//
// Created by Scott Pfeil on 9/11/20.
// Copyright © 2020 Verizon Wireless. All rights reserved.
//
import Foundation
import MVMCore
/// A simple container view that shows and hides a notification.
public class NotificationContainerView: UIView {
public var currentNotificationView: UIView?
lazy private var height = heightAnchor.constraint(equalToConstant: 0)
public init() {
super.init(frame: .zero)
setupView()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
setupView()
}
}
extension NotificationContainerView: NotificationTransitionDelegateProtocol {
@MainActor
public func show(notification: UIView) async {
currentNotificationView?.removeFromSuperview()
addSubview(notification)
NSLayoutConstraint.constraintPinSubview(toSuperview: notification)
currentNotificationView = notification
if let conformer = notification as? MVMCoreViewProtocol {
conformer.updateView(bounds.width)
}
superview?.layoutIfNeeded()
await withCheckedContinuation { continuation in
UIView.animate(withDuration: 0.5) {
self.height.isActive = false
self.superview?.layoutIfNeeded()
} completion: { finished in
self.superview?.layoutIfNeeded()
continuation.resume()
}
}
}
@MainActor
public func hide(notification: UIView) async {
await withCheckedContinuation { continuation in
UIView.animate(withDuration: 0.5) {
self.height.isActive = true
self.superview?.layoutIfNeeded()
} completion: { finished in
self.currentNotificationView?.removeFromSuperview()
self.currentNotificationView = nil
continuation.resume()
}
}
}
@MainActor
public func update(with model: NotificationModel, delegateObject: MVMCoreUIDelegateObject?) {
guard let molecule = currentNotificationView as? MoleculeViewProtocol else { return }
// Update molecule
molecule.reset()
molecule.set(with: model.molecule, delegateObject, nil)
(molecule as? MVMCoreViewProtocol)?.updateView(self.bounds.width)
}
}
extension NotificationContainerView: MVMCoreViewProtocol {
public func updateView(_ size: CGFloat) {
(currentNotificationView as? MVMCoreViewProtocol)?.updateView(size)
}
public func setupView() {
translatesAutoresizingMaskIntoConstraints = false
clipsToBounds = true
height.isActive = true
}
}