NCContentPresenter.swift 9.7 KB

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