[ABANDONED] Mastodon iOS client.
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.

113 lines
3.7 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, 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
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.bottomAnchor, constant: AlertManager.alertStartPosition)
NSLayoutConstraint.activate([
blurEffectView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -20),
blurEffectView.widthAnchor.constraint(equalToConstant: 200),
blurEffectView.heightAnchor.constraint(equalToConstant: 50),
bottomLayoutConstraint!,
])
}
private func start() {
guard done else {
return
}
done = false
UIView.animate(withDuration: 1.0, animations: {
self.bottomLayoutConstraint?.constant = AlertManager.alertStartPosition
}) { _ in
self.alertView?.setAlert(self.alerts.first!)
UIView.animate(withDuration: 1.0, animations: {
self.bottomLayoutConstraint?.constant = 20
}) { _ in
_ = self.alerts.remove(at: 0)
DispatchQueue.main.asyncAfter(deadline: .now() + 5) {
UIView.animate(withDuration: 1.0, animations: {
self.bottomLayoutConstraint?.constant = AlertManager.alertStartPosition
}) { _ in
self.done = true
if self.alerts.count > 0 {
DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
self.start()
}
}
}
}
}
}
}
func show(message: String, category: AlertCategory = .normal) {
if let alert = alerts.last {
if alert.category == category {
return
}
}
alerts.append(Alert(category: category, message: message))
start()
}
}