[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.

120 lines
3.9 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
4 years ago
6 years ago
6 years ago
  1. //
  2. // AlertManager.swift
  3. // elpha-ios
  4. //
  5. // Created by Dwayne Harris on 10/30/18.
  6. // Copyright © 2018 Elpha. All rights reserved.
  7. //
  8. import UIKit
  9. enum AlertCategory {
  10. case normal, newStatuses, favorited, boosted, toot, noConnection, error
  11. }
  12. class Alert {
  13. let category: AlertCategory
  14. let message: String
  15. init(category: AlertCategory, message: String) {
  16. self.category = category
  17. self.message = message
  18. }
  19. }
  20. class AlertManager {
  21. static let shared = AlertManager()
  22. static let alertStartPosition: CGFloat = 100.0
  23. static let alertEndPosition: CGFloat = -80.0
  24. var done: Bool = true
  25. var alertView: AlertView?
  26. var bottomLayoutConstraint: NSLayoutConstraint?
  27. var alerts: [Alert] = []
  28. func createAlertView() {
  29. guard alertView == nil else {
  30. return
  31. }
  32. alertView = AlertView()
  33. alertView!.translatesAutoresizingMaskIntoConstraints = false
  34. let blurEffectView = UIVisualEffectView(effect: UIBlurEffect(style: UIBlurEffect.Style.prominent))
  35. blurEffectView.translatesAutoresizingMaskIntoConstraints = false
  36. blurEffectView.contentView.addSubview(alertView!)
  37. blurEffectView.layer.cornerRadius = 10
  38. blurEffectView.layer.masksToBounds = true
  39. NSLayoutConstraint.activate([
  40. alertView!.leadingAnchor.constraint(equalTo: blurEffectView.leadingAnchor),
  41. alertView!.trailingAnchor.constraint(equalTo: blurEffectView.trailingAnchor),
  42. alertView!.topAnchor.constraint(equalTo: blurEffectView.topAnchor),
  43. alertView!.bottomAnchor.constraint(equalTo: blurEffectView.bottomAnchor),
  44. ])
  45. let view = UIApplication.shared.keyWindow!
  46. view.addSubview(blurEffectView)
  47. bottomLayoutConstraint = blurEffectView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: AlertManager.alertStartPosition)
  48. NSLayoutConstraint.activate([
  49. blurEffectView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -20),
  50. blurEffectView.widthAnchor.constraint(equalToConstant: 220),
  51. blurEffectView.heightAnchor.constraint(equalToConstant: 50),
  52. bottomLayoutConstraint!,
  53. ])
  54. }
  55. private func start() {
  56. guard done else {
  57. return
  58. }
  59. if alertView == nil {
  60. createAlertView()
  61. }
  62. let view = UIApplication.shared.keyWindow!
  63. done = false
  64. self.alertView?.setAlert(self.alerts.first!)
  65. view.layoutIfNeeded()
  66. UIView.animate(withDuration: 0.2, delay: 0.0, options: .curveEaseInOut, animations: {
  67. self.bottomLayoutConstraint?.constant = AlertManager.alertEndPosition
  68. view.layoutIfNeeded()
  69. }) { _ in
  70. DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
  71. UIView.animate(withDuration: 0.2, delay: 0.0, options: .curveEaseInOut, animations: {
  72. self.bottomLayoutConstraint?.constant = AlertManager.alertStartPosition
  73. view.layoutIfNeeded()
  74. }) { _ in
  75. self.done = true
  76. _ = self.alerts.remove(at: 0)
  77. if self.alerts.count > 0 {
  78. DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
  79. self.start()
  80. }
  81. }
  82. }
  83. }
  84. }
  85. }
  86. func show(message: String, category: AlertCategory = .normal) {
  87. if let alert = alerts.last {
  88. if alert.category == category {
  89. return
  90. }
  91. }
  92. print("Alert: \(message)")
  93. alerts.append(Alert(category: category, message: message))
  94. start()
  95. }
  96. }