NCContentPresenter.swift 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. //MARK: - Message
  58. @objc func messageNotification(_ title: String, description: String?, delay: TimeInterval, type: messageType, errorCode: Int) {
  59. messageNotification(title, description: description, delay: delay, type: type, errorCode: errorCode, priority: .normal, dropEnqueuedEntries: false)
  60. }
  61. func messageNotification(_ title: String, description: String?, delay: TimeInterval, type: messageType, errorCode: Int, priority: EKAttributes.Precedence.Priority = .normal, dropEnqueuedEntries: Bool = false) {
  62. // No notification message for:
  63. if errorCode == -999 { return } // Cancelled transfer
  64. else if errorCode == 200 { return } // Transfer stopped
  65. else if errorCode == 207 { return } // WebDAV multistatus
  66. //else if errorCode == 423 { return } // WebDAV locked
  67. //else if errorCode == -1001 { return } // Time out
  68. //else if errorCode == -1005 { return } // Connection lost
  69. else if errorCode == NCGlobal.shared.errorNoError && type == messageType.error { return }
  70. DispatchQueue.main.async {
  71. switch errorCode {
  72. case Int(CFNetworkErrors.cfurlErrorNotConnectedToInternet.rawValue):
  73. let image = UIImage(named: "networkInProgress")!.image(color: .white, size: 20)
  74. self.noteTop(text: NSLocalizedString(title, comment: ""), image: image, color: .lightGray, delay: delay, priority: .max)
  75. //case Int(kOCErrorServerUnauthorized), Int(kOCErrorServerForbidden):
  76. // break
  77. default:
  78. guard var description = description else { return }
  79. if description.trimmingCharacters(in: .whitespacesAndNewlines) == "" { return }
  80. description = NSLocalizedString(description, comment: "")
  81. self.flatTop(title: NSLocalizedString(title, comment: ""), description: description, delay: delay, imageName: nil, type: type)
  82. }
  83. }
  84. }
  85. //MARK: - Flat message
  86. private func flatTop(title: String, description: String, delay: TimeInterval, imageName: String?, type: messageType, priority: EKAttributes.Precedence.Priority = .normal, dropEnqueuedEntries: Bool = false) {
  87. if SwiftEntryKit.isCurrentlyDisplaying(entryNamed: title + description) { return }
  88. var attributes = EKAttributes.topFloat
  89. var image: UIImage?
  90. attributes.windowLevel = .normal
  91. attributes.displayDuration = delay
  92. attributes.name = title+description
  93. attributes.entryBackground = .color(color: EKColor(getBackgroundColorFromType(type)))
  94. attributes.popBehavior = .animated(animation: .init(translate: .init(duration: 0.3), scale: .init(from: 1, to: 0.7, duration: 0.7)))
  95. attributes.shadow = .active(with: .init(color: .black, opacity: 0.5, radius: 10, offset: .zero))
  96. attributes.scroll = .enabled(swipeable: true, pullbackAnimation: .jolt)
  97. attributes.precedence = .override(priority: priority, dropEnqueuedEntries: dropEnqueuedEntries)
  98. let title = EKProperty.LabelContent(text: title, style: .init(font: MainFont.bold.with(size: 16), color: .white))
  99. let description = EKProperty.LabelContent(text: description, style: .init(font: MainFont.medium.with(size: 13), color: .white))
  100. if imageName == nil {
  101. image = getImageFromType(type)
  102. } else {
  103. image = UIImage(named: imageName!)
  104. }
  105. let imageMessage = EKProperty.ImageContent(image: image!, size: CGSize(width: 35, height: 35))
  106. let simpleMessage = EKSimpleMessage(image: imageMessage, title: title, description: description)
  107. let notificationMessage = EKNotificationMessage(simpleMessage: simpleMessage)
  108. let contentView = EKNotificationMessageView(with: notificationMessage)
  109. DispatchQueue.main.async { SwiftEntryKit.display(entry: contentView, using: attributes) }
  110. }
  111. //MARK: - Note Message
  112. func noteTop(text: String, image: UIImage?, color: UIColor? = nil, type: messageType? = nil, delay: TimeInterval, priority: EKAttributes.Precedence.Priority = .normal, dropEnqueuedEntries: Bool = false) {
  113. if SwiftEntryKit.isCurrentlyDisplaying(entryNamed: text) { return }
  114. var attributes = EKAttributes.topNote
  115. attributes.windowLevel = .normal
  116. attributes.displayDuration = delay
  117. attributes.name = text
  118. if let color = color {
  119. attributes.entryBackground = .color(color: EKColor(color))
  120. }
  121. if let type = type {
  122. attributes.entryBackground = .color(color: EKColor(getBackgroundColorFromType(type)))
  123. }
  124. attributes.precedence = .override(priority: priority, dropEnqueuedEntries: dropEnqueuedEntries)
  125. let style = EKProperty.LabelStyle(font: MainFont.light.with(size: 14), color: .white, alignment: .center)
  126. let labelContent = EKProperty.LabelContent(text: text, style: style)
  127. if let image = image {
  128. let imageContent = EKProperty.ImageContent(image: image, size: CGSize(width: 17, height: 17))
  129. let contentView = EKImageNoteMessageView(with: labelContent, imageContent: imageContent)
  130. DispatchQueue.main.async { SwiftEntryKit.display(entry: contentView, using: attributes) }
  131. } else {
  132. let contentView = EKNoteMessageView(with: labelContent)
  133. DispatchQueue.main.async { SwiftEntryKit.display(entry: contentView, using: attributes) }
  134. }
  135. }
  136. //MARK: - Private
  137. private func getBackgroundColorFromType(_ type: messageType) -> UIColor {
  138. switch type {
  139. case .info:
  140. return NCBrandColor.shared.brandElement
  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. }