NCContentPresenter.swift 8.8 KB

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