FloatingNotificationBanner.swift 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. // This file is based on third party code, see below for the original author
  2. // and original license.
  3. // Modifications are (c) by Threema GmbH and licensed under the AGPLv3.
  4. /*
  5. The MIT License (MIT)
  6. Copyright (c) 2018 Denis Kozhukhov
  7. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
  8. to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. 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:
  10. The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  12. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
  13. ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH
  14. THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  15. */
  16. import UIKit
  17. import SnapKit
  18. open class FloatingNotificationBanner: GrowingNotificationBanner {
  19. ///***** BEGIN THREEMA MODIFICATION: add sideViewSize to FloatingNotificationBanner *********/
  20. public init(title: String? = nil,
  21. subtitle: String? = nil,
  22. titleFont: UIFont? = nil,
  23. titleColor: UIColor? = nil,
  24. titleTextAlign: NSTextAlignment? = nil,
  25. subtitleFont: UIFont? = nil,
  26. subtitleColor: UIColor? = nil,
  27. subtitleTextAlign: NSTextAlignment? = nil,
  28. leftView: UIView? = nil,
  29. rightView: UIView? = nil,
  30. style: BannerStyle = .info,
  31. colors: BannerColorsProtocol? = nil,
  32. iconPosition: IconPosition = .center,
  33. sideViewSize: CGFloat = 24.0) {
  34. super.init(title: title, subtitle: subtitle, leftView: leftView, rightView: rightView, style: style, colors: colors, iconPosition: iconPosition, sideViewSize: sideViewSize)
  35. if let titleFont = titleFont {
  36. self.titleFont = titleFont
  37. titleLabel!.font = titleFont
  38. }
  39. if let titleColor = titleColor {
  40. titleLabel!.textColor = titleColor
  41. }
  42. if let titleTextAlign = titleTextAlign {
  43. titleLabel!.textAlignment = titleTextAlign
  44. }
  45. if let subtitleFont = subtitleFont {
  46. self.subtitleFont = subtitleFont
  47. subtitleLabel!.font = subtitleFont
  48. }
  49. if let subtitleColor = subtitleColor {
  50. subtitleLabel!.textColor = subtitleColor
  51. }
  52. if let subtitleTextAlign = subtitleTextAlign {
  53. subtitleLabel!.textAlignment = subtitleTextAlign
  54. }
  55. }
  56. ///***** END THREEMA MODIFICATION: add sideViewSize to FloatingNotificationBanner *********/
  57. public init(customView: UIView) {
  58. super.init(style: .customView)
  59. self.customView = customView
  60. contentView.addSubview(customView)
  61. customView.snp.makeConstraints { (make) in
  62. make.edges.equalTo(contentView)
  63. }
  64. spacerView.backgroundColor = customView.backgroundColor
  65. }
  66. /**
  67. Convenience function to display banner with non .zero default edge insets
  68. */
  69. public func show(queuePosition: QueuePosition = .back,
  70. bannerPosition: BannerPosition = .top,
  71. queue: NotificationBannerQueue = NotificationBannerQueue.default,
  72. on viewController: UIViewController? = nil,
  73. edgeInsets: UIEdgeInsets = UIEdgeInsets(top: 8, left: 8, bottom: 8, right: 8),
  74. cornerRadius: CGFloat? = nil,
  75. shadowColor: UIColor = .black,
  76. shadowOpacity: CGFloat = 1,
  77. shadowBlurRadius: CGFloat = 0,
  78. shadowCornerRadius: CGFloat = 0,
  79. shadowOffset: UIOffset = .zero,
  80. shadowEdgeInsets: UIEdgeInsets? = nil) {
  81. self.bannerEdgeInsets = edgeInsets
  82. if let cornerRadius = cornerRadius {
  83. contentView.layer.cornerRadius = cornerRadius
  84. contentView.subviews.last?.layer.cornerRadius = cornerRadius
  85. }
  86. if style == .customView, let customView = contentView.subviews.last {
  87. customView.backgroundColor = customView.backgroundColor?.withAlphaComponent(transparency)
  88. }
  89. show(queuePosition: queuePosition,
  90. bannerPosition: bannerPosition,
  91. queue: queue,
  92. on: viewController)
  93. applyShadow(withColor: shadowColor,
  94. opacity: shadowOpacity,
  95. blurRadius: shadowBlurRadius,
  96. cornerRadius: shadowCornerRadius,
  97. offset: shadowOffset,
  98. edgeInsets: shadowEdgeInsets)
  99. }
  100. required public init?(coder aDecoder: NSCoder) {
  101. fatalError("init(coder:) has not been implemented")
  102. }
  103. }
  104. private extension FloatingNotificationBanner {
  105. /**
  106. Add shadow for notification with specified parameters.
  107. */
  108. private func applyShadow(withColor color: UIColor = .black,
  109. opacity: CGFloat = 1,
  110. blurRadius: CGFloat = 0,
  111. cornerRadius: CGFloat = 0,
  112. offset: UIOffset = .zero,
  113. edgeInsets: UIEdgeInsets? = nil) {
  114. guard blurRadius >= 0 else { return }
  115. contentView.layer.shadowColor = color.cgColor
  116. contentView.layer.shadowOpacity = Float(opacity)
  117. contentView.layer.shadowRadius = blurRadius
  118. contentView.layer.shadowOffset = CGSize(width: offset.horizontal, height: offset.vertical)
  119. if let edgeInsets = edgeInsets {
  120. var shadowRect = CGRect(origin: .zero, size: bannerPositionFrame.startFrame.size)
  121. shadowRect.size.height -= (spacerViewHeight() - spacerViewDefaultOffset) // to proper handle spacer height affects
  122. shadowRect.origin.x += edgeInsets.left
  123. shadowRect.origin.y += edgeInsets.top
  124. shadowRect.size.width -= (edgeInsets.left + edgeInsets.right)
  125. shadowRect.size.height -= (edgeInsets.top + edgeInsets.bottom)
  126. contentView.layer.shadowPath = UIBezierPath(roundedRect: shadowRect, cornerRadius: cornerRadius).cgPath
  127. }
  128. contentView.layer.rasterizationScale = UIScreen.main.scale
  129. contentView.layer.shouldRasterize = true
  130. }
  131. }