NCContentPresenter.swift 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. //
  2. // NCContentPresenter.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 23/12/2019.
  6. // Copyright (c) 2019 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. import SwiftEntryKit
  24. import UIKit
  25. import CFNetwork
  26. class NCContentPresenter: NSObject {
  27. @objc static let shared: NCContentPresenter = {
  28. let instance = NCContentPresenter()
  29. return instance
  30. }()
  31. typealias MainFont = Font.HelveticaNeue
  32. enum Font {
  33. enum HelveticaNeue: String {
  34. case ultraLightItalic = "UltraLightItalic"
  35. case medium = "Medium"
  36. case mediumItalic = "MediumItalic"
  37. case ultraLight = "UltraLight"
  38. case italic = "Italic"
  39. case light = "Light"
  40. case thinItalic = "ThinItalic"
  41. case lightItalic = "LightItalic"
  42. case bold = "Bold"
  43. case thin = "Thin"
  44. case condensedBlack = "CondensedBlack"
  45. case condensedBold = "CondensedBold"
  46. case boldItalic = "BoldItalic"
  47. func with(size: CGFloat) -> UIFont {
  48. return UIFont(name: "HelveticaNeue-\(rawValue)", size: size)!
  49. }
  50. }
  51. }
  52. @objc enum messageType: Int {
  53. case error
  54. case success
  55. case info
  56. }
  57. //MARK: - Message
  58. @objc func messageNotification(_ title: String, description: String?, delay: TimeInterval, type: messageType, errorCode: Int) {
  59. messageNotification(title, description: description, delay: delay, type: type, errorCode: errorCode, priority: .normal, dropEnqueuedEntries: false)
  60. }
  61. func messageNotification(_ title: String, description: String?, delay: TimeInterval, type: messageType, errorCode: Int, priority: EKAttributes.Precedence.Priority = .normal, dropEnqueuedEntries: Bool = false) {
  62. // No notification message for:
  63. if errorCode == -999 { return } // Cancelled transfer
  64. else if errorCode == 200 { return } // Transfer stopped
  65. else if errorCode == 207 { return } // WebDAV multistatus
  66. else if errorCode == NCGlobal.shared.errorNoError && type == messageType.error { return }
  67. DispatchQueue.main.async {
  68. switch errorCode {
  69. case Int(CFNetworkErrors.cfurlErrorNotConnectedToInternet.rawValue):
  70. let image = UIImage(named: "networkInProgress")!.image(color: .white, size: 20)
  71. self.noteTop(text: NSLocalizedString(title, comment: ""), image: image, color: .lightGray, delay: delay, priority: .max)
  72. default:
  73. guard var description = description else { return }
  74. if description.trimmingCharacters(in: .whitespacesAndNewlines) == "" { return }
  75. description = NSLocalizedString(description, comment: "")
  76. self.flatTop(title: NSLocalizedString(title, comment: ""), description: description, delay: delay, imageName: nil, type: type, priority: priority, dropEnqueuedEntries: dropEnqueuedEntries)
  77. }
  78. }
  79. }
  80. //MARK: - Flat message
  81. private func flatTop(title: String, description: String, delay: TimeInterval, imageName: String?, type: messageType, priority: EKAttributes.Precedence.Priority = .normal, dropEnqueuedEntries: Bool = false) {
  82. if SwiftEntryKit.isCurrentlyDisplaying(entryNamed: title + description) { return }
  83. var attributes = EKAttributes.topFloat
  84. var image: UIImage?
  85. attributes.windowLevel = .normal
  86. attributes.displayDuration = delay
  87. attributes.name = title + description
  88. attributes.entryBackground = .color(color: EKColor(getBackgroundColorFromType(type)))
  89. attributes.popBehavior = .animated(animation: .init(translate: .init(duration: 0.3), scale: .init(from: 1, to: 0.7, duration: 0.7)))
  90. attributes.shadow = .active(with: .init(color: .black, opacity: 0.5, radius: 10, offset: .zero))
  91. attributes.scroll = .enabled(swipeable: true, pullbackAnimation: .jolt)
  92. attributes.precedence = .override(priority: priority, dropEnqueuedEntries: dropEnqueuedEntries)
  93. let title = EKProperty.LabelContent(text: title, style: .init(font: MainFont.bold.with(size: 16), color: .white))
  94. let description = EKProperty.LabelContent(text: description, style: .init(font: MainFont.medium.with(size: 13), color: .white))
  95. if imageName == nil {
  96. image = getImageFromType(type)
  97. } else {
  98. image = UIImage(named: imageName!)
  99. }
  100. let imageMessage = EKProperty.ImageContent(image: image!, size: CGSize(width: 35, height: 35))
  101. let simpleMessage = EKSimpleMessage(image: imageMessage, title: title, description: description)
  102. let notificationMessage = EKNotificationMessage(simpleMessage: simpleMessage)
  103. let contentView = EKNotificationMessageView(with: notificationMessage)
  104. DispatchQueue.main.async { SwiftEntryKit.display(entry: contentView, using: attributes) }
  105. }
  106. //MARK: - Note Message
  107. func noteTop(text: String, image: UIImage?, color: UIColor? = nil, type: messageType? = nil, delay: TimeInterval, priority: EKAttributes.Precedence.Priority = .normal, dropEnqueuedEntries: Bool = false) {
  108. if SwiftEntryKit.isCurrentlyDisplaying(entryNamed: text) { return }
  109. var attributes = EKAttributes.topNote
  110. attributes.windowLevel = .normal
  111. attributes.displayDuration = delay
  112. attributes.name = text
  113. if let color = color {
  114. attributes.entryBackground = .color(color: EKColor(color))
  115. }
  116. if let type = type {
  117. attributes.entryBackground = .color(color: EKColor(getBackgroundColorFromType(type)))
  118. }
  119. attributes.precedence = .override(priority: priority, dropEnqueuedEntries: dropEnqueuedEntries)
  120. let style = EKProperty.LabelStyle(font: MainFont.light.with(size: 14), color: .white, alignment: .center)
  121. let labelContent = EKProperty.LabelContent(text: text, style: style)
  122. if let image = image {
  123. let imageContent = EKProperty.ImageContent(image: image, size: CGSize(width: 17, height: 17))
  124. let contentView = EKImageNoteMessageView(with: labelContent, imageContent: imageContent)
  125. DispatchQueue.main.async { SwiftEntryKit.display(entry: contentView, using: attributes) }
  126. } else {
  127. let contentView = EKNoteMessageView(with: labelContent)
  128. DispatchQueue.main.async { SwiftEntryKit.display(entry: contentView, using: attributes) }
  129. }
  130. }
  131. //MARK: - Private
  132. private func getBackgroundColorFromType(_ type: messageType) -> UIColor {
  133. switch type {
  134. case .info:
  135. return NCBrandColor.shared.brandElement
  136. case .error:
  137. return UIColor(red: 1, green: 0, blue: 0, alpha: 0.9)
  138. case .success:
  139. return UIColor(red: 0.588, green: 0.797, blue: 0, alpha: 0.9)
  140. default:
  141. return .white
  142. }
  143. }
  144. private func getImageFromType(_ type: messageType) -> UIImage? {
  145. switch type {
  146. case .info:
  147. return UIImage(named: "iconInfo")
  148. case .error:
  149. return UIImage(named: "iconError")
  150. case .success:
  151. return UIImage(named: "iconSuccess")
  152. default:
  153. return nil
  154. }
  155. }
  156. }