NCContentPresenter.swift 11 KB

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