NotificationBannerHelper.swift 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2020 Threema GmbH
  8. //
  9. // This program is free software: you can redistribute it and/or modify
  10. // it under the terms of the GNU Affero General Public License, version 3,
  11. // as published by the Free Software Foundation.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU Affero General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Affero General Public License
  19. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. import Foundation
  21. import SnapKit
  22. import MarqueeLabel
  23. @objc class NotificationBannerHelper: NSObject {
  24. @objc class func newBanner(message: BaseMessage) {
  25. DispatchQueue.main.async {
  26. var contactImageView: UIImageView?
  27. var thumbnailImageView: UIImageView?
  28. if let contactImage = AvatarMaker.shared()?.avatar(for: message.conversation, size: 56.0, masked: true) {
  29. contactImageView = UIImageView.init(image: contactImage)
  30. }
  31. if message.isKind(of: ImageMessage.self) {
  32. let imageMessage = message as! ImageMessage
  33. if let thumbnail = imageMessage.thumbnail {
  34. thumbnailImageView = UIImageView.init(image: thumbnail.uiImage)
  35. thumbnailImageView?.contentMode = .scaleAspectFit
  36. }
  37. }
  38. else if message.isKind(of: FileMessage.self) {
  39. let fileMessage = message as! FileMessage
  40. if let thumbnail = fileMessage.thumbnail {
  41. thumbnailImageView = UIImageView.init(image: thumbnail.uiImage)
  42. thumbnailImageView?.contentMode = .scaleAspectFit
  43. }
  44. }
  45. else if message.isKind(of: AudioMessage.self) {
  46. let thumbnail = BundleUtil.imageNamed("ActionMicrophone")?.withTint(Colors.fontNormal())
  47. thumbnailImageView = UIImageView.init(image: thumbnail)
  48. thumbnailImageView?.contentMode = .center
  49. }
  50. let titleFontDescriptor = UIFontDescriptor.preferredFontDescriptor(withTextStyle: .headline)
  51. let bodyFontDescriptor = UIFontDescriptor.preferredFontDescriptor(withTextStyle: .subheadline)
  52. var body = message.previewText()
  53. body = TextStyleUtils.makeMentionsString(forText: body)
  54. if message.conversation.groupId != nil {
  55. body = "\(message.sender.displayName ?? ""): \(body ?? "")"
  56. }
  57. let banner = FloatingNotificationBanner.init(title: message.conversation.displayName, subtitle: "", titleFont: UIFont.boldSystemFont(ofSize: titleFontDescriptor.pointSize), titleColor: Colors.fontNormal(), subtitleFont: UIFont.systemFont(ofSize: bodyFontDescriptor.pointSize), subtitleColor: Colors.fontNormal(), leftView: contactImageView, rightView: thumbnailImageView, style: .info, colors: CustomBannerColors(), sideViewSize: 50.0)
  58. if message.conversation.groupId != nil {
  59. banner.identifier = message.conversation.groupId.hexEncodedString()
  60. } else {
  61. banner.identifier = message.conversation.contact.identity
  62. }
  63. banner.transparency = 0.9
  64. banner.duration = 3.0
  65. banner.applyStyling(cornerRadius: 8)
  66. banner.subtitleLabel?.numberOfLines = 2
  67. var formattedAttributeString: NSMutableAttributedString?
  68. if message.conversation.groupId != nil {
  69. var contactString = ""
  70. if !message.isKind(of: SystemMessage.self) {
  71. if message.sender == nil {
  72. contactString = String.init(format: "%@: ", BundleUtil.localizedString(forKey: "me"))
  73. } else {
  74. contactString = String.init(format: "%@: ", message.sender.displayName)
  75. }
  76. }
  77. let attributed = TextStyleUtils.makeAttributedString(from: message.previewText(), with: UIFont.systemFont(ofSize: bodyFontDescriptor.pointSize), textColor: Colors.fontNormal(), isOwn: true, application: UIApplication.shared)
  78. let messageAttributedString = NSMutableAttributedString.init(attributedString: banner.subtitleLabel!.applyMarkup(for: attributed))
  79. let attributedContact = TextStyleUtils.makeAttributedString(from: contactString, with: UIFont.systemFont(ofSize: bodyFontDescriptor.pointSize), textColor: Colors.fontNormal(), isOwn: true, application: UIApplication.shared)
  80. formattedAttributeString = NSMutableAttributedString.init(attributedString: attributedContact!)
  81. formattedAttributeString?.append(messageAttributedString)
  82. } else {
  83. let attributed = TextStyleUtils.makeAttributedString(from:message.previewText(), with: UIFont.systemFont(ofSize: bodyFontDescriptor.pointSize), textColor: Colors.fontNormal(), isOwn: true, application: UIApplication.shared)
  84. formattedAttributeString = NSMutableAttributedString.init(attributedString: banner.subtitleLabel!.applyMarkup(for: attributed))
  85. }
  86. banner.subtitleLabel!.attributedText = TextStyleUtils.makeMentionsAttributedString(for: formattedAttributeString, textFont: banner.subtitleLabel!.font, at: Colors.fontLight()?.withAlphaComponent(0.6), messageInfo: 2, application: UIApplication.shared)
  87. banner.onTap = {
  88. banner.bannerQueue.dismissAllForced()
  89. /* switch to selected conversation */
  90. if let conversation = message.conversation {
  91. NotificationCenter.default.post(name: NSNotification.Name(rawValue: kNotificationShowConversation), object: nil, userInfo: [kKeyConversation: conversation])
  92. }
  93. }
  94. banner.onSwipeUp = {
  95. banner.bannerQueue.removeAll()
  96. }
  97. let shadowEdgeInsets = UIEdgeInsets(top: 8, left: 2, bottom: 0, right: 2)
  98. if Colors.getTheme() == ColorThemeDark || Colors.getTheme() == ColorThemeDarkWork {
  99. banner.show( shadowColor: Colors.notificationShadow(), shadowOpacity: 0.5, shadowBlurRadius: 7, shadowEdgeInsets: shadowEdgeInsets)
  100. } else {
  101. banner.show(shadowColor: Colors.notificationShadow(), shadowOpacity: 1.0, shadowBlurRadius: 10, shadowEdgeInsets: shadowEdgeInsets)
  102. }
  103. }
  104. }
  105. @objc class func dismissAllNotifications() {
  106. DispatchQueue.main.async {
  107. NotificationBannerQueue.default.removeAll()
  108. }
  109. }
  110. @objc class func dismissAllNotifications(for conversation:Conversation) {
  111. DispatchQueue.main.async {
  112. var identifier: String?
  113. if let groupId = conversation.groupId {
  114. identifier = groupId.hexEncodedString()
  115. }
  116. else if let contact = conversation.contact {
  117. identifier = contact.identity
  118. } else {
  119. return
  120. }
  121. let banners = NotificationBannerQueue.default.banners
  122. for banner in banners {
  123. if banner.identifier == identifier {
  124. if banner.isDisplaying == true {
  125. banner.dismiss()
  126. }
  127. NotificationBannerQueue.default.removeBanner(banner)
  128. }
  129. }
  130. }
  131. }
  132. @objc class func newInfoToast(title: String, body: String) {
  133. newToast(title: title, body: body, bannerStyle: .warning)
  134. }
  135. @objc class func newErrorToast(title: String, body: String) {
  136. newToast(title: title, body: body, bannerStyle: .danger)
  137. }
  138. private class func newToast(title: String, body: String, bannerStyle: BannerStyle) {
  139. DispatchQueue.main.async {
  140. let titleFontDescriptor = UIFontDescriptor.preferredFontDescriptor(withTextStyle: .headline)
  141. let bodyFontDescriptor = UIFontDescriptor.preferredFontDescriptor(withTextStyle: .subheadline)
  142. let banner = FloatingNotificationBanner.init(title: title, subtitle: body, titleFont: UIFont.boldSystemFont(ofSize: titleFontDescriptor.pointSize), titleColor: Colors.fontNormal(), subtitleFont: UIFont.systemFont(ofSize: bodyFontDescriptor.pointSize), subtitleColor: Colors.white(), leftView: UIImageView.init(image: BundleUtil.imageNamed("InfoFilled")), rightView: nil, style: bannerStyle, colors: CustomBannerColors())
  143. banner.titleLabel!.attributedText = NSAttributedString.init(string: title, attributes: [NSAttributedString.Key.foregroundColor: Colors.white() as Any, NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: titleFontDescriptor.pointSize) as Any])
  144. banner.subtitleLabel!.attributedText = NSAttributedString.init(string: body, attributes: [NSAttributedString.Key.foregroundColor: Colors.white() as Any, NSAttributedString.Key.font: UIFont.systemFont(ofSize: bodyFontDescriptor.pointSize) as Any])
  145. banner.transparency = 1.0
  146. banner.duration = 5.0
  147. banner.subtitleLabel?.numberOfLines = 0
  148. banner.show()
  149. }
  150. }
  151. }
  152. class CustomBannerColors: BannerColorsProtocol {
  153. internal func color(for style: BannerStyle) -> UIColor {
  154. switch style {
  155. case .danger: return Colors.red()
  156. case .info: return Colors.notificationBackground()
  157. case .customView: return Colors.notificationBackground()
  158. case .success: return Colors.green()
  159. case .warning: return Colors.orange()
  160. }
  161. }
  162. }