NCContentPresenter.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. @objc private var lastErrorCode: Int = 0
  58. //MARK: - Message
  59. @objc func messageNotification(_ title: String, description: String?, delay: TimeInterval, type: messageType, errorCode: Int, forced: Bool = false) {
  60. // No notification message
  61. if forced == false {
  62. if errorCode == -999 { return } // Cancelled transfer
  63. else if errorCode == 200 { return } // Transfer stopped
  64. else if errorCode == 207 { return } // WebDAV multistatus
  65. else if errorCode == 423 { return } // WebDAV locked
  66. else if errorCode == -1001 { return } // Time out
  67. else if errorCode == -1005 { return } // Connection lost
  68. else if errorCode == 0 && type == messageType.error { return }
  69. // No repeat message for:
  70. if errorCode == lastErrorCode {
  71. if errorCode == Int(CFNetworkErrors.cfurlErrorNotConnectedToInternet.rawValue) { return }
  72. } else {
  73. lastErrorCode = errorCode
  74. }
  75. }
  76. DispatchQueue.main.async {
  77. switch errorCode {
  78. case Int(CFNetworkErrors.cfurlErrorNotConnectedToInternet.rawValue):
  79. let image = UIImage(named: "networkInProgress")!.image(color: .white, size: 20)
  80. self.noteTop(text: NSLocalizedString(title, comment: ""), image: image, color: .lightGray, delay: delay, name: "\(errorCode)")
  81. //case Int(kOCErrorServerUnauthorized), Int(kOCErrorServerForbidden):
  82. // break
  83. default:
  84. guard var description = description else { return }
  85. if description.trimmingCharacters(in: .whitespacesAndNewlines) == "" { return }
  86. description = NSLocalizedString(description, comment: "")
  87. self.flatTop(title: NSLocalizedString(title, comment: ""), description: description, delay: delay, imageName: nil, type: type, name: "\(errorCode)")
  88. }
  89. }
  90. }
  91. //MARK: - Flat message
  92. @objc func flatTop(title: String, description: String, delay: TimeInterval, imageName: String?, type: messageType, name: String?) {
  93. if name != nil && SwiftEntryKit.isCurrentlyDisplaying(entryNamed: name) { return }
  94. var attributes = EKAttributes.topFloat
  95. var image: UIImage?
  96. attributes.windowLevel = .normal
  97. attributes.displayDuration = delay
  98. attributes.name = name
  99. attributes.entryBackground = .color(color: EKColor(getBackgroundColorFromType(type)))
  100. attributes.popBehavior = .animated(animation: .init(translate: .init(duration: 0.3), scale: .init(from: 1, to: 0.7, duration: 0.7)))
  101. attributes.shadow = .active(with: .init(color: .black, opacity: 0.5, radius: 10, offset: .zero))
  102. attributes.scroll = .enabled(swipeable: true, pullbackAnimation: .jolt)
  103. let title = EKProperty.LabelContent(text: title, style: .init(font: MainFont.bold.with(size: 16), color: .white))
  104. let description = EKProperty.LabelContent(text: description, style: .init(font: MainFont.medium.with(size: 13), color: .white))
  105. if imageName == nil {
  106. image = getImageFromType(type)
  107. } else {
  108. image = UIImage(named: imageName!)
  109. }
  110. let imageMessage = EKProperty.ImageContent(image: image!, size: CGSize(width: 35, height: 35))
  111. let simpleMessage = EKSimpleMessage(image: imageMessage, title: title, description: description)
  112. let notificationMessage = EKNotificationMessage(simpleMessage: simpleMessage)
  113. let contentView = EKNotificationMessageView(with: notificationMessage)
  114. DispatchQueue.main.async { SwiftEntryKit.display(entry: contentView, using: attributes) }
  115. }
  116. @objc func flatBottom(title: String, description: String, delay: TimeInterval, image: UIImage, type: messageType, name: String?, verticalOffset: CGFloat) {
  117. if name != nil && SwiftEntryKit.isCurrentlyDisplaying(entryNamed: name) { return }
  118. var attributes = EKAttributes.bottomFloat
  119. attributes.windowLevel = .normal
  120. attributes.displayDuration = delay
  121. attributes.name = name
  122. attributes.entryBackground = .color(color: EKColor(getBackgroundColorFromType(type)))
  123. attributes.popBehavior = .animated(animation: .init(translate: .init(duration: 0.3), scale: .init(from: 1, to: 0.7, duration: 0.7)))
  124. attributes.shadow = .active(with: .init(color: .black, opacity: 0.5, radius: 10, offset: .zero))
  125. attributes.scroll = .enabled(swipeable: true, pullbackAnimation: .jolt)
  126. attributes.positionConstraints.verticalOffset = verticalOffset
  127. let title = EKProperty.LabelContent(text: title, style: .init(font: MainFont.bold.with(size: 16), color: .white))
  128. let description = EKProperty.LabelContent(text: description, style: .init(font: MainFont.medium.with(size: 13), color: .white))
  129. let imageMessage = EKProperty.ImageContent(image: image, size: CGSize(width: 35, height: 35))
  130. let simpleMessage = EKSimpleMessage(image: imageMessage, title: title, description: description)
  131. let notificationMessage = EKNotificationMessage(simpleMessage: simpleMessage)
  132. let contentView = EKNotificationMessageView(with: notificationMessage)
  133. DispatchQueue.main.async {
  134. SwiftEntryKit.dismiss(.displayed)
  135. SwiftEntryKit.display(entry: contentView, using: attributes)
  136. }
  137. }
  138. //MARK: - Note Message
  139. func noteTop(text: String, image: UIImage?, color: UIColor? = nil, type: messageType? = nil, delay: TimeInterval, name: String?) {
  140. var attributes = EKAttributes.topNote
  141. attributes.windowLevel = .normal
  142. attributes.displayDuration = delay
  143. attributes.name = name
  144. if let color = color {
  145. attributes.entryBackground = .color(color: EKColor(color))
  146. }
  147. if let type = type {
  148. attributes.entryBackground = .color(color: EKColor(getBackgroundColorFromType(type)))
  149. }
  150. let style = EKProperty.LabelStyle(font: MainFont.light.with(size: 14), color: .white, alignment: .center)
  151. let labelContent = EKProperty.LabelContent(text: text, style: style)
  152. if let image = image {
  153. let imageContent = EKProperty.ImageContent(image: image, size: CGSize(width: 17, height: 17))
  154. let contentView = EKImageNoteMessageView(with: labelContent, imageContent: imageContent)
  155. DispatchQueue.main.async { SwiftEntryKit.display(entry: contentView, using: attributes) }
  156. } else {
  157. let contentView = EKNoteMessageView(with: labelContent)
  158. DispatchQueue.main.async { SwiftEntryKit.display(entry: contentView, using: attributes) }
  159. }
  160. }
  161. func noteBottom(text: String, image: UIImage?, color: UIColor? = nil, type: messageType? = nil, delay: TimeInterval, name: String?) {
  162. var attributes = EKAttributes.bottomNote
  163. attributes.windowLevel = .normal
  164. attributes.displayDuration = delay
  165. attributes.name = name
  166. if let color = color {
  167. attributes.entryBackground = .color(color: EKColor(color))
  168. }
  169. if let type = type {
  170. attributes.entryBackground = .color(color: EKColor(getBackgroundColorFromType(type)))
  171. }
  172. let style = EKProperty.LabelStyle(font: MainFont.light.with(size: 14), color: .white, alignment: .center)
  173. let labelContent = EKProperty.LabelContent(text: text, style: style)
  174. if let image = image {
  175. let imageContent = EKProperty.ImageContent(image: image, size: CGSize(width: 17, height: 17))
  176. let contentView = EKImageNoteMessageView(with: labelContent, imageContent: imageContent)
  177. DispatchQueue.main.async { SwiftEntryKit.display(entry: contentView, using: attributes) }
  178. } else {
  179. let contentView = EKNoteMessageView(with: labelContent)
  180. DispatchQueue.main.async { SwiftEntryKit.display(entry: contentView, using: attributes) }
  181. }
  182. }
  183. //MARK: - Dismiss
  184. @objc func dismissAll() {
  185. DispatchQueue.main.async { SwiftEntryKit.dismiss(.all) }
  186. }
  187. @objc func dismissDisplayed() {
  188. DispatchQueue.main.async { SwiftEntryKit.dismiss(.displayed) }
  189. }
  190. //MARK: - Private
  191. private func getBackgroundColorFromType(_ type: messageType) -> UIColor {
  192. switch type {
  193. case .info:
  194. return NCBrandColor.shared.brandElement
  195. case .error:
  196. return UIColor(red: 1, green: 0, blue: 0, alpha: 0.9)
  197. case .success:
  198. return UIColor(red: 0.588, green: 0.797, blue: 0, alpha: 0.9)
  199. default:
  200. return .white
  201. }
  202. }
  203. private func getImageFromType(_ type: messageType) -> UIImage? {
  204. switch type {
  205. case .info:
  206. return UIImage(named: "iconInfo")
  207. case .error:
  208. return UIImage(named: "iconError")
  209. case .success:
  210. return UIImage(named: "iconSuccess")
  211. default:
  212. return nil
  213. }
  214. }
  215. }