NCContentPresenter.swift 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. //
  2. // NCContentPresenter.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 23/12/2019.
  6. // Copyright © 2019 TWS. All rights reserved.
  7. //
  8. import SwiftEntryKit
  9. import UIKit
  10. import CFNetwork
  11. class NCContentPresenter: NSObject {
  12. @objc static let shared: NCContentPresenter = {
  13. let instance = NCContentPresenter()
  14. return instance
  15. }()
  16. typealias MainFont = Font.HelveticaNeue
  17. enum Font {
  18. enum HelveticaNeue: String {
  19. case ultraLightItalic = "UltraLightItalic"
  20. case medium = "Medium"
  21. case mediumItalic = "MediumItalic"
  22. case ultraLight = "UltraLight"
  23. case italic = "Italic"
  24. case light = "Light"
  25. case thinItalic = "ThinItalic"
  26. case lightItalic = "LightItalic"
  27. case bold = "Bold"
  28. case thin = "Thin"
  29. case condensedBlack = "CondensedBlack"
  30. case condensedBold = "CondensedBold"
  31. case boldItalic = "BoldItalic"
  32. func with(size: CGFloat) -> UIFont {
  33. return UIFont(name: "HelveticaNeue-\(rawValue)", size: size)!
  34. }
  35. }
  36. }
  37. @objc enum messageType: Int {
  38. case error
  39. case success
  40. case info
  41. }
  42. @objc private var lastErrorCode: Int = 0
  43. //MARK: - Message
  44. @objc func messageNotification(_ title: String, description: String?, delay: TimeInterval, type: messageType, errorCode: Int) {
  45. if errorCode == lastErrorCode {
  46. if errorCode == Int(CFNetworkErrors.cfurlErrorNotConnectedToInternet.rawValue) { return }
  47. } else {
  48. lastErrorCode = errorCode
  49. }
  50. DispatchQueue.main.async {
  51. switch errorCode {
  52. case Int(CFNetworkErrors.cfurlErrorNotConnectedToInternet.rawValue):
  53. let image = CCGraphics.changeThemingColorImage(UIImage(named: "networkInProgress")!, width: 40, height: 40, color: .white)
  54. self.noteTop(text: NSLocalizedString(title, comment: ""), image: image, color: .lightGray, delay: delay, name: "\(errorCode)")
  55. //case Int(kOCErrorServerUnauthorized), Int(kOCErrorServerForbidden):
  56. // break
  57. default:
  58. var description = description
  59. if description == nil { description = "" }
  60. if type == messageType.error {
  61. description = NSLocalizedString("_error_", comment: "") + ": \(errorCode), " + NSLocalizedString(description!, comment: "")
  62. } else {
  63. description = NSLocalizedString(description!, comment: "")
  64. }
  65. self.flatTop(title: NSLocalizedString(title, comment: ""), description: description!, delay: delay, imageName: nil, type: type, name: "\(errorCode)")
  66. }
  67. }
  68. }
  69. //MARK: - Flat message
  70. @objc func flatTop(title: String, description: String, delay: TimeInterval, imageName: String?, type: messageType, name: String?) {
  71. if name != nil && SwiftEntryKit.isCurrentlyDisplaying(entryNamed: name) { return }
  72. var attributes = EKAttributes.topFloat
  73. var image: UIImage?
  74. attributes.windowLevel = .normal
  75. attributes.displayDuration = delay
  76. attributes.name = name
  77. attributes.entryBackground = .color(color: EKColor(getBackgroundColorFromType(type)))
  78. attributes.popBehavior = .animated(animation: .init(translate: .init(duration: 0.3), scale: .init(from: 1, to: 0.7, duration: 0.7)))
  79. attributes.shadow = .active(with: .init(color: .black, opacity: 0.5, radius: 10, offset: .zero))
  80. attributes.scroll = .enabled(swipeable: true, pullbackAnimation: .jolt)
  81. let title = EKProperty.LabelContent(text: title, style: .init(font: MainFont.bold.with(size: 16), color: .white))
  82. let description = EKProperty.LabelContent(text: description, style: .init(font: MainFont.medium.with(size: 13), color: .white))
  83. if imageName == nil {
  84. image = getImageFromType(type)
  85. } else {
  86. image = UIImage(named: imageName!)
  87. }
  88. let imageMessage = EKProperty.ImageContent(image: image!, size: CGSize(width: 35, height: 35))
  89. let simpleMessage = EKSimpleMessage(image: imageMessage, title: title, description: description)
  90. let notificationMessage = EKNotificationMessage(simpleMessage: simpleMessage)
  91. let contentView = EKNotificationMessageView(with: notificationMessage)
  92. DispatchQueue.main.async { SwiftEntryKit.display(entry: contentView, using: attributes) }
  93. }
  94. @objc func flatBottom(title: String, description: String, delay: TimeInterval, image: UIImage, type: messageType, name: String?, verticalOffset: CGFloat) {
  95. if name != nil && SwiftEntryKit.isCurrentlyDisplaying(entryNamed: name) { return }
  96. var attributes = EKAttributes.bottomFloat
  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. attributes.positionConstraints.verticalOffset = verticalOffset
  105. let title = EKProperty.LabelContent(text: title, style: .init(font: MainFont.bold.with(size: 16), color: .white))
  106. let description = EKProperty.LabelContent(text: description, style: .init(font: MainFont.medium.with(size: 13), color: .white))
  107. let imageMessage = EKProperty.ImageContent(image: image, size: CGSize(width: 35, height: 35))
  108. let simpleMessage = EKSimpleMessage(image: imageMessage, title: title, description: description)
  109. let notificationMessage = EKNotificationMessage(simpleMessage: simpleMessage)
  110. let contentView = EKNotificationMessageView(with: notificationMessage)
  111. DispatchQueue.main.async {
  112. SwiftEntryKit.dismiss(.displayed)
  113. SwiftEntryKit.display(entry: contentView, using: attributes)
  114. }
  115. }
  116. //MARK: - Note Message
  117. @objc func noteTop(text: String, image: UIImage?, color: UIColor, delay: TimeInterval, name: String?) {
  118. var attributes = EKAttributes.topNote
  119. attributes.windowLevel = .normal
  120. attributes.displayDuration = delay
  121. attributes.name = name
  122. attributes.entryBackground = .color(color: EKColor(color))
  123. let style = EKProperty.LabelStyle(font: MainFont.light.with(size: 14), color: .white, alignment: .center)
  124. let labelContent = EKProperty.LabelContent(text: text, style: style)
  125. if let image = image {
  126. let imageContent = EKProperty.ImageContent(image: image, size: CGSize(width: 17, height: 17))
  127. let contentView = EKImageNoteMessageView(with: labelContent, imageContent: imageContent)
  128. DispatchQueue.main.async { SwiftEntryKit.display(entry: contentView, using: attributes) }
  129. } else {
  130. let contentView = EKNoteMessageView(with: labelContent)
  131. DispatchQueue.main.async { SwiftEntryKit.display(entry: contentView, using: attributes) }
  132. }
  133. }
  134. //MARK: - Dismiss
  135. @objc func dismissAll() {
  136. DispatchQueue.main.async { SwiftEntryKit.dismiss(.all) }
  137. }
  138. @objc func dismissDisplayed() {
  139. DispatchQueue.main.async { SwiftEntryKit.dismiss(.displayed) }
  140. }
  141. //MARK: - Private
  142. private func getBackgroundColorFromType(_ type: messageType) -> UIColor {
  143. switch type {
  144. case .info:
  145. return NCBrandColor.sharedInstance.brand
  146. case .error:
  147. return UIColor(red: 1, green: 0, blue: 0, alpha: 0.9)
  148. case .success:
  149. return UIColor(red: 0.588, green: 0.797, blue: 0, alpha: 0.9)
  150. default:
  151. return .white
  152. }
  153. }
  154. private func getImageFromType(_ type: messageType) -> UIImage? {
  155. switch type {
  156. case .info:
  157. return UIImage(named: "iconInfo")
  158. case .error:
  159. return UIImage(named: "iconError")
  160. case .success:
  161. return UIImage(named: "iconSuccess")
  162. default:
  163. return nil
  164. }
  165. }
  166. }