NCContentPresenter.swift 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 showGenericError(description: String) {
  59. messageNotification(
  60. "_error_", description: description,
  61. delay: NCGlobal.shared.dismissAfterSecond,
  62. type: .error,
  63. errorCode: NCGlobal.shared.errorGeneric)
  64. }
  65. @objc func messageNotification(_ title: String, description: String?, delay: TimeInterval, type: messageType, errorCode: Int) {
  66. messageNotification(title, description: description, delay: delay, type: type, errorCode: errorCode, priority: .normal, dropEnqueuedEntries: false)
  67. }
  68. func messageNotification(_ title: String, description: String?, delay: TimeInterval, type: messageType, errorCode: Int, priority: EKAttributes.Precedence.Priority = .normal, dropEnqueuedEntries: Bool = false) {
  69. // No notification message for:
  70. if errorCode == -999 { return } // Cancelled transfer
  71. else if errorCode == 200 { return } // Transfer stopped
  72. else if errorCode == 207 { return } // WebDAV multistatus
  73. else if errorCode == NCGlobal.shared.errorNoError && type == messageType.error { return }
  74. DispatchQueue.main.async {
  75. switch errorCode {
  76. case Int(CFNetworkErrors.cfurlErrorNotConnectedToInternet.rawValue):
  77. let image = UIImage(named: "networkInProgress")!.image(color: .white, size: 20)
  78. self.noteTop(text: NSLocalizedString(title, comment: ""), image: image, color: .lightGray, delay: delay, priority: .max)
  79. default:
  80. guard var description = description else { return }
  81. if description.trimmingCharacters(in: .whitespacesAndNewlines) == "" { return }
  82. description = NSLocalizedString(description, comment: "")
  83. self.flatTop(title: NSLocalizedString(title, comment: ""), description: description, delay: delay, imageName: nil, type: type, priority: priority, dropEnqueuedEntries: dropEnqueuedEntries)
  84. }
  85. }
  86. }
  87. // MARK: - Flat message
  88. private func flatTop(title: String, description: String, delay: TimeInterval, imageName: String?, type: messageType, priority: EKAttributes.Precedence.Priority = .normal, dropEnqueuedEntries: Bool = false) {
  89. if SwiftEntryKit.isCurrentlyDisplaying(entryNamed: title + description) { return }
  90. var attributes = EKAttributes.topFloat
  91. var image: UIImage?
  92. attributes.windowLevel = .normal
  93. attributes.displayDuration = delay
  94. attributes.name = title + description
  95. attributes.entryBackground = .color(color: EKColor(getBackgroundColorFromType(type)))
  96. attributes.popBehavior = .animated(animation: .init(translate: .init(duration: 0.3), scale: .init(from: 1, to: 0.7, duration: 0.7)))
  97. attributes.shadow = .active(with: .init(color: .black, opacity: 0.5, radius: 10, offset: .zero))
  98. attributes.scroll = .enabled(swipeable: true, pullbackAnimation: .jolt)
  99. attributes.precedence = .override(priority: priority, dropEnqueuedEntries: dropEnqueuedEntries)
  100. let title = EKProperty.LabelContent(text: title, style: .init(font: MainFont.bold.with(size: 16), color: .white))
  101. let description = EKProperty.LabelContent(text: description, style: .init(font: MainFont.medium.with(size: 13), color: .white))
  102. if imageName == nil {
  103. image = getImageFromType(type)
  104. } else {
  105. image = UIImage(named: imageName!)
  106. }
  107. let imageMessage = EKProperty.ImageContent(image: image!, size: CGSize(width: 35, height: 35))
  108. let simpleMessage = EKSimpleMessage(image: imageMessage, title: title, description: description)
  109. let notificationMessage = EKNotificationMessage(simpleMessage: simpleMessage)
  110. let contentView = EKNotificationMessageView(with: notificationMessage)
  111. DispatchQueue.main.async { SwiftEntryKit.display(entry: contentView, using: attributes) }
  112. }
  113. // MARK: - Note Message
  114. func noteTop(text: String, image: UIImage?, color: UIColor? = nil, type: messageType? = nil, delay: TimeInterval, priority: EKAttributes.Precedence.Priority = .normal, dropEnqueuedEntries: Bool = false) {
  115. if SwiftEntryKit.isCurrentlyDisplaying(entryNamed: text) { return }
  116. var attributes = EKAttributes.topNote
  117. attributes.windowLevel = .normal
  118. attributes.displayDuration = delay
  119. attributes.name = text
  120. if let color = color {
  121. attributes.entryBackground = .color(color: EKColor(color))
  122. }
  123. if let type = type {
  124. attributes.entryBackground = .color(color: EKColor(getBackgroundColorFromType(type)))
  125. }
  126. attributes.precedence = .override(priority: priority, dropEnqueuedEntries: dropEnqueuedEntries)
  127. let style = EKProperty.LabelStyle(font: MainFont.light.with(size: 14), color: .white, alignment: .center)
  128. let labelContent = EKProperty.LabelContent(text: text, style: style)
  129. if let image = image {
  130. let imageContent = EKProperty.ImageContent(image: image, size: CGSize(width: 17, height: 17))
  131. let contentView = EKImageNoteMessageView(with: labelContent, imageContent: imageContent)
  132. DispatchQueue.main.async { SwiftEntryKit.display(entry: contentView, using: attributes) }
  133. } else {
  134. let contentView = EKNoteMessageView(with: labelContent)
  135. DispatchQueue.main.async { SwiftEntryKit.display(entry: contentView, using: attributes) }
  136. }
  137. }
  138. // MARK: - Private
  139. private func getBackgroundColorFromType(_ type: messageType) -> UIColor {
  140. switch type {
  141. case .info:
  142. return NCBrandColor.shared.brandElement
  143. case .error:
  144. return UIColor(red: 1, green: 0, blue: 0, alpha: 0.9)
  145. case .success:
  146. return UIColor(red: 0.588, green: 0.797, blue: 0, alpha: 0.9)
  147. default:
  148. return .white
  149. }
  150. }
  151. private func getImageFromType(_ type: messageType) -> UIImage? {
  152. switch type {
  153. case .info:
  154. return UIImage(named: "iconInfo")
  155. case .error:
  156. return UIImage(named: "iconError")
  157. case .success:
  158. return UIImage(named: "iconSuccess")
  159. default:
  160. return nil
  161. }
  162. }
  163. }