Skip to content

Instantly share code, notes, and snippets.

@Anatoli-Petrosyants
Created July 10, 2018 16:31
Show Gist options
  • Save Anatoli-Petrosyants/38e85b49c95ee89b643567082f644a58 to your computer and use it in GitHub Desktop.
Save Anatoli-Petrosyants/38e85b49c95ee89b643567082f644a58 to your computer and use it in GitHub Desktop.
Amondo+Notification
import Foundation
import UIKit
class Notification {
// Shows a Notification from a NotificationType
public static func showNotification(type: NotificationType, completion: @escaping () -> () = {}) {
let view = NotificationView()
view.setBackgroundColor(color: type.backgroundColor)
view.setImage(image: type.image)
view.setText(text: type.text)
view.setTextColor(color: type.textColor)
view.setTimeToDisappear(delay: type.timeToDisappear)
view.setCompletionBlock(completion)
guard let window = UIApplication.shared.keyWindow else {
print("Failed to show Notification. No keywindow available.")
return
}
window.addSubview(view)
view.show()
}
}
import Foundation
import UIKit
// Protocol for defining a Notification style
protocol NotificationType {
var backgroundColor: UIColor { get }
var textColor: UIColor { get }
var image: UIImage { get }
var text: String { get }
var timeToDisappear: TimeInterval { get }
}
fileprivate struct NotificationTypeDefinition: NotificationType {
var backgroundColor: UIColor
var textColor: UIColor
var image: UIImage
var text: String
var timeToDisappear: TimeInterval
}
// Notification types
let emailIncorrect: NotificationType = NotificationTypeDefinition(backgroundColor: .red, textColor: .white,
image: UIImage(named: "icn_email_incorrect")!, text: "Email Incorrect", timeToDisappear: 2)
let emailReseted: NotificationType = NotificationTypeDefinition(backgroundColor: .green, textColor: .white,
image: UIImage(named: "icn_email_reseted")!, text: "Email Reseted", timeToDisappear: 2)
import UIKit
class NotificationView: UIView {
// MARK: UI
private lazy var imageView: UIImageView = {
let view = UIImageView()
view.contentMode = .scaleToFill
view.tintColor = .white
return view
}()
private lazy var textLabel: UILabel = {
let label = UILabel()
label.textColor = .white
label.numberOfLines = 0
return label
}()
// MARK: Init
required internal init?(coder aDecoder:NSCoder) {
fatalError("not implemented.")
}
init() {
super.init(frame: CGRect(x: 10, y: 0, width: UIScreen.main.bounds.width - 20, height: 100 /* Must be dynamic */))
self.alpha = 0.0
setupLayer()
setupSubviews()
setupConstraints()
}
// MARK: - Setup
private func setupLayer() {
layer.cornerRadius = 7
}
private func setupSubviews() {
addSubview(imageView)
addSubview(textLabel)
}
private func setupConstraints() {
imageView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([imageView.topAnchor.constraint(equalTo: imageView.superview!.topAnchor, constant: 10),
imageView.leadingAnchor.constraint(equalTo: imageView.superview!.leadingAnchor, constant: 10),
imageView.bottomAnchor.constraint(equalTo: imageView.superview!.bottomAnchor, constant: -10),
imageView.widthAnchor.constraint(equalTo: imageView.heightAnchor)
])
textLabel.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([textLabel.topAnchor.constraint(greaterThanOrEqualTo: textLabel.superview!.topAnchor, constant: 10),
textLabel.bottomAnchor.constraint(equalTo: textLabel.superview!.bottomAnchor, constant: -10),
textLabel.leadingAnchor.constraint(equalTo: imageView.trailingAnchor, constant: 10),
textLabel.trailingAnchor.constraint(equalTo: textLabel.superview!.trailingAnchor, constant: -10),
textLabel.widthAnchor.constraint(equalTo: textLabel.heightAnchor)
])
}
// MARK: Helpers
// Sets the background color of the notification
public func setBackgroundColor(color: UIColor) {
backgroundColor = color
}
// Sets the text color of the notification
public func setTextColor(color: UIColor) {
textLabel.textColor = color
}
// Sets the text of the notification
public func setText(text: String) {
textLabel.text = text
}
// Sets the image of the notification
public func setImage(image: UIImage?) {
imageView.image = image
}
// Sets the completion block of the notification for when it is dismissed
public func setCompletionBlock(_ completion: @escaping () -> ()) {
self.completion = completion
}
// Dismisses the notification with a delay > 0
public func setTimeToDisappear(delay: TimeInterval) {
if delay > 0 {
Timer.scheduledTimer(timeInterval: Double(delay), target: self, selector: #selector(dismissNotification), userInfo: nil, repeats: false)
}
}
// Show under status bar with .fadeInUp
public func show() {
UIView.animate(withDuration: 0.3, delay: 0.0, usingSpringWithDamping: 0.68, initialSpringVelocity: 0.1, options: UIViewAnimationOptions(), animations: {
self.frame.origin.y = UIApplication.shared.statusBarFrame.height + 10
self.alpha = 1.0
})
}
// Dismiss with .fadeOutDown
@objc private func dismissNotification() {
UIView.animate(withDuration: 0.25, delay: 0.0, options: UIViewAnimationOptions(), animations: {
self.center.y = -self.frame.height
}, completion: { [weak self] (complete) in
self?.completion()
self?.removeFromSuperview()
})
}
private var completion: () -> () = {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment