NCContentPresenter.swift 8.2 KB

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