NCContentPresenter.swift 9.5 KB

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