You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
120 lines
3.9 KiB
120 lines
3.9 KiB
//
|
|
// AlertManager.swift
|
|
// elpha-ios
|
|
//
|
|
// Created by Dwayne Harris on 10/30/18.
|
|
// Copyright © 2018 Elpha. All rights reserved.
|
|
//
|
|
|
|
import UIKit
|
|
|
|
enum AlertCategory {
|
|
case normal, newStatuses, favorited, boosted, noConnection, error
|
|
}
|
|
|
|
class Alert {
|
|
let category: AlertCategory
|
|
let message: String
|
|
|
|
init(category: AlertCategory, message: String) {
|
|
self.category = category
|
|
self.message = message
|
|
}
|
|
}
|
|
|
|
class AlertManager {
|
|
static let shared = AlertManager()
|
|
static let alertStartPosition: CGFloat = 100.0
|
|
static let alertEndPosition: CGFloat = -80.0
|
|
|
|
var done: Bool = true
|
|
var alertView: AlertView? = nil
|
|
var bottomLayoutConstraint: NSLayoutConstraint? = nil
|
|
var alerts: [Alert] = []
|
|
|
|
func createAlertView() {
|
|
guard alertView == nil else {
|
|
return
|
|
}
|
|
|
|
alertView = AlertView()
|
|
alertView!.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
let blurEffectView = UIVisualEffectView(effect: UIBlurEffect(style: UIBlurEffect.Style.prominent))
|
|
blurEffectView.translatesAutoresizingMaskIntoConstraints = false
|
|
blurEffectView.contentView.addSubview(alertView!)
|
|
blurEffectView.layer.cornerRadius = 10
|
|
blurEffectView.layer.masksToBounds = true
|
|
|
|
NSLayoutConstraint.activate([
|
|
alertView!.leadingAnchor.constraint(equalTo: blurEffectView.leadingAnchor),
|
|
alertView!.trailingAnchor.constraint(equalTo: blurEffectView.trailingAnchor),
|
|
alertView!.topAnchor.constraint(equalTo: blurEffectView.topAnchor),
|
|
alertView!.bottomAnchor.constraint(equalTo: blurEffectView.bottomAnchor),
|
|
])
|
|
|
|
let view = UIApplication.shared.keyWindow!
|
|
|
|
view.addSubview(blurEffectView)
|
|
|
|
bottomLayoutConstraint = blurEffectView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: AlertManager.alertStartPosition)
|
|
|
|
NSLayoutConstraint.activate([
|
|
blurEffectView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -20),
|
|
blurEffectView.widthAnchor.constraint(equalToConstant: 220),
|
|
blurEffectView.heightAnchor.constraint(equalToConstant: 50),
|
|
bottomLayoutConstraint!,
|
|
])
|
|
}
|
|
|
|
private func start() {
|
|
guard done else {
|
|
return
|
|
}
|
|
|
|
if alertView == nil {
|
|
createAlertView()
|
|
}
|
|
|
|
let view = UIApplication.shared.keyWindow!
|
|
|
|
done = false
|
|
self.alertView?.setAlert(self.alerts.first!)
|
|
|
|
view.layoutIfNeeded()
|
|
|
|
UIView.animate(withDuration: 0.2, delay: 0.0, options: .curveEaseInOut, animations: {
|
|
self.bottomLayoutConstraint?.constant = AlertManager.alertEndPosition
|
|
view.layoutIfNeeded()
|
|
}) { _ in
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
|
|
UIView.animate(withDuration: 0.2, delay: 0.0, options: .curveEaseInOut, animations: {
|
|
self.bottomLayoutConstraint?.constant = AlertManager.alertStartPosition
|
|
view.layoutIfNeeded()
|
|
}) { _ in
|
|
self.done = true
|
|
_ = self.alerts.remove(at: 0)
|
|
|
|
if self.alerts.count > 0 {
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
|
|
self.start()
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func show(message: String, category: AlertCategory = .normal) {
|
|
if let alert = alerts.last {
|
|
if alert.category == category {
|
|
return
|
|
}
|
|
}
|
|
|
|
print("ALERT: \(message)")
|
|
|
|
alerts.append(Alert(category: category, message: message))
|
|
start()
|
|
}
|
|
}
|