NCContentPresenter.swift 7.9 KB

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