NCContentPresenter.swift 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. import NextcloudKit
  27. class NCContentPresenter: NSObject {
  28. @objc static let shared: NCContentPresenter = {
  29. let instance = NCContentPresenter()
  30. return instance
  31. }()
  32. typealias MainFont = Font.HelveticaNeue
  33. enum Font {
  34. enum HelveticaNeue: String {
  35. case ultraLightItalic = "UltraLightItalic"
  36. case medium = "Medium"
  37. case mediumItalic = "MediumItalic"
  38. case ultraLight = "UltraLight"
  39. case italic = "Italic"
  40. case light = "Light"
  41. case thinItalic = "ThinItalic"
  42. case lightItalic = "LightItalic"
  43. case bold = "Bold"
  44. case thin = "Thin"
  45. case condensedBlack = "CondensedBlack"
  46. case condensedBold = "CondensedBold"
  47. case boldItalic = "BoldItalic"
  48. func with(size: CGFloat) -> UIFont {
  49. return UIFont(name: "HelveticaNeue-\(rawValue)", size: size)!
  50. }
  51. }
  52. }
  53. @objc enum messageType: Int {
  54. case error
  55. case success
  56. case info
  57. }
  58. // MARK: - Message
  59. func showError(error: NKError, priority: EKAttributes.Precedence.Priority = .normal) {
  60. messageNotification(
  61. "_error_",
  62. error: error,
  63. delay: NCGlobal.shared.dismissAfterSecond,
  64. type: .error,
  65. priority: priority)
  66. }
  67. @objc func messageNotification(_ title: String, error: NKError, delay: TimeInterval, type: messageType) {
  68. messageNotification(title, error: error, delay: delay, type: type, priority: .normal, dropEnqueuedEntries: false)
  69. }
  70. func messageNotification(_ title: String, error: NKError, delay: TimeInterval, type: messageType, priority: EKAttributes.Precedence.Priority = .normal, dropEnqueuedEntries: Bool = false) {
  71. // No notification message for:
  72. if error.errorCode == -999 { return } // Cancelled transfer
  73. else if error.errorCode == 200 { return } // Transfer stopped
  74. else if error.errorCode == 207 { return } // WebDAV multistatus
  75. else if error.errorCode == NCGlobal.shared.errorNoError && type == messageType.error { return }
  76. DispatchQueue.main.async {
  77. switch error.errorCode {
  78. case Int(CFNetworkErrors.cfurlErrorNotConnectedToInternet.rawValue):
  79. let image = UIImage(named: "networkInProgress")!.image(color: .white, size: 20)
  80. self.noteTop(text: NSLocalizedString(title, comment: ""), image: image, color: .lightGray, delay: delay, priority: .max)
  81. default:
  82. if error.errorDescription.trimmingCharacters(in: .whitespacesAndNewlines) == "" { return }
  83. let description = NSLocalizedString(error.errorDescription, comment: "")
  84. self.flatTop(title: NSLocalizedString(title, comment: ""), description: description, delay: delay, imageName: nil, type: type, priority: priority, dropEnqueuedEntries: dropEnqueuedEntries)
  85. }
  86. }
  87. }
  88. // MARK: - Flat message
  89. private func flatTop(title: String, description: String, delay: TimeInterval, imageName: String?, type: messageType, priority: EKAttributes.Precedence.Priority = .normal, dropEnqueuedEntries: Bool = false) {
  90. if SwiftEntryKit.isCurrentlyDisplaying(entryNamed: title + description) { return }
  91. var attributes = EKAttributes.topFloat
  92. var image: UIImage?
  93. attributes.windowLevel = .normal
  94. attributes.displayDuration = delay
  95. attributes.name = title + description
  96. attributes.entryBackground = .color(color: EKColor(getBackgroundColorFromType(type)))
  97. attributes.popBehavior = .animated(animation: .init(translate: .init(duration: 0.3), scale: .init(from: 1, to: 0.7, duration: 0.7)))
  98. attributes.shadow = .active(with: .init(color: .black, opacity: 0.5, radius: 10, offset: .zero))
  99. attributes.scroll = .enabled(swipeable: true, pullbackAnimation: .jolt)
  100. attributes.precedence = .override(priority: priority, dropEnqueuedEntries: dropEnqueuedEntries)
  101. let title = EKProperty.LabelContent(text: title, style: .init(font: MainFont.bold.with(size: 16), color: .white))
  102. let description = EKProperty.LabelContent(text: description, style: .init(font: MainFont.medium.with(size: 13), color: .white))
  103. if imageName == nil {
  104. image = getImageFromType(type)
  105. } else {
  106. image = UIImage(named: imageName!)
  107. }
  108. let imageMessage = EKProperty.ImageContent(image: image!, size: CGSize(width: 35, height: 35))
  109. let simpleMessage = EKSimpleMessage(image: imageMessage, title: title, description: description)
  110. let notificationMessage = EKNotificationMessage(simpleMessage: simpleMessage)
  111. let contentView = EKNotificationMessageView(with: notificationMessage)
  112. DispatchQueue.main.async { SwiftEntryKit.display(entry: contentView, using: attributes) }
  113. }
  114. // MARK: - Note Message
  115. func noteTop(text: String, image: UIImage?, color: UIColor? = nil, type: messageType? = nil, delay: TimeInterval, priority: EKAttributes.Precedence.Priority = .normal, dropEnqueuedEntries: Bool = false) {
  116. if SwiftEntryKit.isCurrentlyDisplaying(entryNamed: text) { return }
  117. DispatchQueue.main.async {
  118. var attributes = EKAttributes.topNote
  119. attributes.windowLevel = .normal
  120. attributes.displayDuration = delay
  121. attributes.name = text
  122. if let color = color {
  123. attributes.entryBackground = .color(color: EKColor(color))
  124. }
  125. if let type = type {
  126. attributes.entryBackground = .color(color: EKColor(self.getBackgroundColorFromType(type)))
  127. }
  128. attributes.precedence = .override(priority: priority, dropEnqueuedEntries: dropEnqueuedEntries)
  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. SwiftEntryKit.display(entry: contentView, using: attributes)
  135. } else {
  136. let contentView = EKNoteMessageView(with: labelContent)
  137. SwiftEntryKit.display(entry: contentView, using: attributes)
  138. }
  139. }
  140. }
  141. func dismiss(after: TimeInterval = 0) {
  142. DispatchQueue.main.asyncAfter(deadline: .now() + after) {
  143. SwiftEntryKit.dismiss()
  144. }
  145. }
  146. // MARK: - Private
  147. private func getBackgroundColorFromType(_ type: messageType) -> UIColor {
  148. switch type {
  149. case .info:
  150. return NCBrandColor.shared.brandElement
  151. case .error:
  152. return UIColor(red: 1, green: 0, blue: 0, alpha: 0.9)
  153. case .success:
  154. return UIColor(red: 0.588, green: 0.797, blue: 0, alpha: 0.9)
  155. default:
  156. return .white
  157. }
  158. }
  159. private func getImageFromType(_ type: messageType) -> UIImage? {
  160. switch type {
  161. case .info:
  162. return UIImage(named: "iconInfo")
  163. case .error:
  164. return UIImage(named: "iconError")
  165. case .success:
  166. return UIImage(named: "iconSuccess")
  167. default:
  168. return nil
  169. }
  170. }
  171. }