NotificationBannerQueue.swift 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. The MIT License (MIT)
  3. Copyright (c) 2017-2018 Dalton Hinterscher
  4. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
  5. to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
  6. and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
  7. The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
  8. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  9. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
  10. ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH
  11. THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  12. */
  13. import UIKit
  14. @objc
  15. public enum QueuePosition: Int {
  16. case back
  17. case front
  18. }
  19. @objcMembers
  20. open class NotificationBannerQueue: NSObject {
  21. /// The default instance of the NotificationBannerQueue
  22. public static let `default` = NotificationBannerQueue()
  23. /// The notification banners currently placed on the queue
  24. private(set) var banners: [BaseNotificationBanner] = []
  25. /// The notification banners currently placed on the queue
  26. private(set) var maxBannersOnScreenSimultaneously: Int = 1
  27. /// The current number of notification banners on the queue
  28. public var numberOfBanners: Int {
  29. return banners.count
  30. }
  31. public init(maxBannersOnScreenSimultaneously: Int = 1) {
  32. self.maxBannersOnScreenSimultaneously = maxBannersOnScreenSimultaneously
  33. }
  34. /**
  35. Adds a banner to the queue
  36. -parameter banner: The notification banner to add to the queue
  37. -parameter queuePosition: The position to show the notification banner. If the position is .front, the
  38. banner will be displayed immediately
  39. */
  40. func addBanner(_ banner: BaseNotificationBanner,
  41. bannerPosition: BannerPosition,
  42. queuePosition: QueuePosition) {
  43. if queuePosition == .back {
  44. banners.append(banner)
  45. let bannersCount = banners.filter { $0.isDisplaying }.count
  46. if bannersCount < maxBannersOnScreenSimultaneously && banners.count == 1 {
  47. banner.show(placeOnQueue: false, bannerPosition: banner.bannerPosition)
  48. }
  49. } else {
  50. banner.show(placeOnQueue: false, bannerPosition: bannerPosition)
  51. if let firstBanner = firstNotDisplayedBanner() {
  52. firstBanner.suspend()
  53. }
  54. banners.insert(banner, at: 0)
  55. }
  56. }
  57. /**
  58. Removes a banner from the queue
  59. -parameter banner: A notification banner to remove from the queue.
  60. */
  61. func removeBanner(_ banner: BaseNotificationBanner) {
  62. if let index = banners.firstIndex(of: banner) {
  63. banners.remove(at: index)
  64. }
  65. banners.forEach {
  66. $0.updateBannerPositionFrames()
  67. if $0.isDisplaying {
  68. $0.animateUpdatedBannerPositionFrames()
  69. }
  70. }
  71. }
  72. /**
  73. Shows the next notificaiton banner on the queue if one exists
  74. -parameter callback: The closure to execute after a banner is shown or when the queue is empty
  75. */
  76. func showNext(callback: ((_ isEmpty: Bool) -> Void)) {
  77. if let banner = firstNotDisplayedBanner() {
  78. if banner.isSuspended {
  79. banner.resume()
  80. } else {
  81. banner.show(placeOnQueue: false, bannerPosition: banner.bannerPosition)
  82. }
  83. callback(false)
  84. }
  85. else {
  86. callback(true)
  87. return
  88. }
  89. }
  90. func firstNotDisplayedBanner() -> BaseNotificationBanner? {
  91. return banners.filter { !$0.isDisplaying }.first
  92. }
  93. /**
  94. Removes all notification banners from the queue
  95. */
  96. public func removeAll() {
  97. banners.removeAll()
  98. }
  99. /**
  100. Forced dissmiss all notification banners from the queue
  101. */
  102. public func dismissAllForced() {
  103. banners.forEach { $0.dismiss(forced: true) }
  104. banners.removeAll()
  105. }
  106. }