NCContentPresenter.swift 9.6 KB

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