NCViewerImagemeter.swift 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. //
  2. // NCViewerImagemeter.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 22/03/2019.
  6. // Copyright © 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 Foundation
  24. class NCViewerImagemeter: NSObject {
  25. private var imagemeterView: IMImagemeterView!
  26. private let appDelegate = UIApplication.shared.delegate as! AppDelegate
  27. private var annotation: IMImagemeterCodable.imagemeterAnnotation?
  28. private var audioPlayer = AVAudioPlayer()
  29. private var timer = Timer()
  30. private var durationPlayer: TimeInterval = 0
  31. private var counterSecondPlayer: TimeInterval = 0
  32. private var metadata: tableMetadata!
  33. private var detail: CCDetail!
  34. private var safeAreaBottom: Int = 0
  35. private let dimButton: CGFloat = 30.0
  36. @objc public init(metadata: tableMetadata, detail: CCDetail) {
  37. super.init()
  38. self.metadata = metadata
  39. self.detail = detail
  40. }
  41. @objc public func viewImagemeter() {
  42. guard let rootView = UIApplication.shared.keyWindow else {
  43. return
  44. }
  45. if #available(iOS 11.0, *) {
  46. safeAreaBottom = Int(rootView.safeAreaInsets.bottom)
  47. }
  48. for view in self.detail.view.subviews {
  49. if view is IMImagemeterView {
  50. view.removeFromSuperview()
  51. }
  52. }
  53. let bundleDirectory = IMImagemeter.sharedInstance.getBundleDirectory(metadata: metadata)
  54. self.imagemeterView = IMImagemeterView.instanceFromNib() as? IMImagemeterView
  55. self.imagemeterView.frame = CGRect(x: 0, y: 0, width: Int(detail.view.frame.width), height: Int(detail.view.frame.height) - Int(k_detail_Toolbar_Height) - safeAreaBottom - 1)
  56. detail.view.addSubview(imagemeterView)
  57. do {
  58. let annoData = try Data(contentsOf: NSURL(fileURLWithPath: bundleDirectory.immPath) as URL, options: .mappedIfSafe)
  59. if let annotation = IMImagemeterCodable.sharedInstance.decoderAnnotetion(annoData) {
  60. self.annotation = annotation
  61. imageImagemeter()
  62. audioImagemeter()
  63. } else {
  64. appDelegate.messageNotification("_error_", description: "_error_json_decoding_", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: Int(k_CCErrorInternalError))
  65. }
  66. } catch {
  67. print("error:\(error)")
  68. }
  69. }
  70. private func imageImagemeter() {
  71. guard let annotation = self.annotation else {
  72. return
  73. }
  74. let imageFilename = annotation.image.filename
  75. let bundleDirectory = IMImagemeter.sharedInstance.getBundleDirectory(metadata: metadata)
  76. if let image = UIImage(contentsOfFile: bundleDirectory.bundleDirectory + "/" + imageFilename) {
  77. let factor = image.size.width / image.size.height
  78. imagemeterView.imageHeightConstraint.constant = imagemeterView.bounds.size.width / factor
  79. imagemeterView.image.image = NCUtility.sharedInstance.resizeImage(image: image, newWidth: imagemeterView.bounds.size.width)
  80. }
  81. }
  82. private func audioImagemeter() {
  83. guard let annotation = self.annotation else {
  84. return
  85. }
  86. if annotation.elements != nil {
  87. for element in annotation.elements! {
  88. if element.audio_recording == nil {
  89. continue
  90. }
  91. let center = IMImagemeterCodable.sharedInstance.convertCoordinate(x: element.center?.x ?? 0, y: element.center?.y ?? 0, width: imagemeterView.bounds.width, height: imagemeterView.imageHeightConstraint.constant)
  92. let button = UIButton()
  93. button.frame = CGRect(x: center.x - dimButton/2, y: center.y - dimButton/2, width: dimButton, height: dimButton)
  94. button.setImage(UIImage(named: "audioPlayFull"), for: .normal)
  95. button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
  96. button.tag = element.id
  97. imagemeterView.image.addSubview(button)
  98. if element.arrows != nil {
  99. for arrow in element.arrows! {
  100. let endPt = IMImagemeterCodable.sharedInstance.convertCoordinate(x: arrow.end_pt.x, y: arrow.end_pt.y, width: imagemeterView.bounds.width, height: imagemeterView.imageHeightConstraint.constant)
  101. imagemeterView.image.image = drawLineOnImage(startingImage: imagemeterView.image.image!, x: center.x, y: center.y, endX: endPt.x, endY: endPt.y, color: .yellow, size: 1.5)
  102. }
  103. }
  104. }
  105. }
  106. }
  107. private func drawLineOnImage(startingImage: UIImage, x: CGFloat, y: CGFloat, endX:CGFloat, endY:CGFloat, color: UIColor, size: CGFloat) -> UIImage {
  108. // Create a context of the starting image size and set it as the current one
  109. UIGraphicsBeginImageContext(startingImage.size)
  110. // Draw the starting image in the current context as background
  111. startingImage.draw(at: CGPoint.zero)
  112. // Get the current context
  113. let context = UIGraphicsGetCurrentContext()!
  114. // Draw a red line
  115. context.setLineWidth(size)
  116. context.setStrokeColor(color.cgColor)
  117. context.move(to: CGPoint(x: x, y: y))
  118. context.addLine(to: CGPoint(x: endX, y: endY))
  119. context.strokePath()
  120. // Save the context as a new UIImage
  121. let image = UIGraphicsGetImageFromCurrentImageContext()
  122. UIGraphicsEndImageContext()
  123. return image!
  124. }
  125. @objc private func buttonAction(sender: UIButton!) {
  126. guard let annotation = self.annotation else {
  127. return
  128. }
  129. for element in annotation.elements! {
  130. if element.id == sender.tag {
  131. do {
  132. let bundleDirectory = IMImagemeter.sharedInstance.getBundleDirectory(metadata: metadata)
  133. let fileNamePath = bundleDirectory.bundleDirectory + "/" + element.audio_recording!.recording_filename
  134. try audioPlayer = AVAudioPlayer(contentsOf: URL(fileURLWithPath: fileNamePath))
  135. audioPlayer.delegate = self
  136. audioPlayer.prepareToPlay()
  137. audioPlayer.play()
  138. durationPlayer = TimeInterval(audioPlayer.duration)
  139. counterSecondPlayer = 0
  140. timer.invalidate()
  141. timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(updateTimer), userInfo: nil, repeats: true)
  142. } catch {
  143. }
  144. }
  145. }
  146. }
  147. @objc private func updateTimer() {
  148. counterSecondPlayer += 1
  149. imagemeterView.progressView.progress = Float(counterSecondPlayer / durationPlayer)
  150. }
  151. }
  152. extension NCViewerImagemeter: AVAudioPlayerDelegate {
  153. func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) {
  154. updateTimer()
  155. timer.invalidate()
  156. counterSecondPlayer = 0
  157. DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
  158. self.imagemeterView.progressView.progress = 0
  159. }
  160. }
  161. }
  162. class IMImagemeterView: UIView {
  163. @IBOutlet weak var image: UIImageView!
  164. @IBOutlet weak var imageHeightConstraint: NSLayoutConstraint!
  165. @IBOutlet weak var progressView: UIProgressView!
  166. class func instanceFromNib() -> UIView {
  167. return UINib(nibName: "IMImagemeterView", bundle: nil).instantiate(withOwner: nil, options: nil)[0] as! UIView
  168. }
  169. override func awakeFromNib() {
  170. super.awakeFromNib()
  171. image.isUserInteractionEnabled = true
  172. progressView.progressTintColor = NCBrandColor.sharedInstance.brandElement
  173. progressView.trackTintColor = UIColor(red: 247.0/255.0, green: 247.0/255.0, blue: 247.0/255.0, alpha: 1.0)
  174. progressView.progress = 0
  175. }
  176. }