NCViewerImagemeter.swift 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 nameArchiveImagemeter: String = ""
  28. private var pathArchiveImagemeter: String = ""
  29. private var annotation: IMImagemeterCodable.imagemeterAnnotation?
  30. private var audioPlayer = AVAudioPlayer()
  31. private var timer = Timer()
  32. private var durationPlayer: TimeInterval = 0
  33. private var counterSecondPlayer: TimeInterval = 0
  34. private var metadata: tableMetadata!
  35. private var detail: CCDetail!
  36. private var safeAreaBottom: Int = 0
  37. @objc public init(metadata: tableMetadata, detail: CCDetail) {
  38. super.init()
  39. self.metadata = metadata
  40. self.detail = detail
  41. guard let rootView = UIApplication.shared.keyWindow else {
  42. return
  43. }
  44. if #available(iOS 11.0, *) {
  45. safeAreaBottom = Int(rootView.safeAreaInsets.bottom)
  46. }
  47. nameArchiveImagemeter = (metadata.fileNameView as NSString).deletingPathExtension
  48. pathArchiveImagemeter = CCUtility.getDirectoryProviderStorageFileID(metadata.fileID) + "/" + nameArchiveImagemeter
  49. self.imagemeterView = IMImagemeterView.instanceFromNib() as? IMImagemeterView
  50. 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)
  51. detail.view.addSubview(imagemeterView)
  52. do {
  53. let annoPath = (pathArchiveImagemeter + "/anno-" + nameArchiveImagemeter + ".imm").url
  54. let annoData = try Data(contentsOf: annoPath, options: .mappedIfSafe)
  55. if let annotation = IMImagemeterCodable.sharedInstance.decoderAnnotetion(annoData) {
  56. self.annotation = annotation
  57. imageImagemeter()
  58. audioImagemeter()
  59. } else {
  60. appDelegate.messageNotification("_error_", description: "_error_decompressing_", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: Int(k_CCErrorInternalError))
  61. }
  62. } catch {
  63. print("error:\(error)")
  64. }
  65. }
  66. @objc private func updateTimer() {
  67. counterSecondPlayer += 1
  68. imagemeterView.progressView.progress = Float(counterSecondPlayer / durationPlayer)
  69. }
  70. private func imageImagemeter() {
  71. guard let annotation = self.annotation else {
  72. return
  73. }
  74. let imageFilename = annotation.image.filename
  75. if let image = UIImage(contentsOfFile: pathArchiveImagemeter + "/" + imageFilename) {
  76. let factor = image.size.width / image.size.height
  77. imagemeterView.imageHeightConstraint.constant = CGFloat(imagemeterView.bounds.size.width) / factor
  78. imagemeterView.image.image = image
  79. }
  80. }
  81. @objc func audioImagemeter() {
  82. guard let annotation = self.annotation else {
  83. return
  84. }
  85. for view in imagemeterView.image.subviews {
  86. view.removeFromSuperview()
  87. }
  88. for element in annotation.elements {
  89. let coordinateNormalize = IMImagemeterCodable.sharedInstance.convertCoordinate(x: element.center.x, y: element.center.y, width: Double(imagemeterView.bounds.width), height: Double(imagemeterView.imageHeightConstraint.constant), button: 30)
  90. let x = coordinateNormalize.x
  91. let y = coordinateNormalize.y
  92. let button = UIButton()
  93. button.frame = CGRect(x: x, y: y, width: 30, height: 30)
  94. button.setImage(UIImage(named: "audioPlay"), for: .normal)
  95. button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
  96. button.tag = element.id
  97. imagemeterView.image.addSubview(button)
  98. }
  99. }
  100. @objc private func buttonAction(sender: UIButton!) {
  101. guard let annotation = self.annotation else {
  102. return
  103. }
  104. for element in annotation.elements {
  105. if element.id == sender.tag {
  106. do {
  107. let fileNamePath = pathArchiveImagemeter + "/" + element.audio_recording.recording_filename
  108. try audioPlayer = AVAudioPlayer(contentsOf: URL(fileURLWithPath: fileNamePath))
  109. audioPlayer.delegate = self
  110. audioPlayer.prepareToPlay()
  111. audioPlayer.play()
  112. durationPlayer = TimeInterval(audioPlayer.duration)
  113. timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(updateTimer), userInfo: nil, repeats: true)
  114. } catch {
  115. }
  116. }
  117. }
  118. }
  119. }
  120. extension NCViewerImagemeter: AVAudioPlayerDelegate {
  121. func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) {
  122. updateTimer()
  123. timer.invalidate()
  124. counterSecondPlayer = 0
  125. DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
  126. self.imagemeterView.progressView.progress = 0
  127. }
  128. }
  129. }
  130. class IMImagemeterView: UIView {
  131. @IBOutlet weak var image: UIImageView!
  132. @IBOutlet weak var imageHeightConstraint: NSLayoutConstraint!
  133. @IBOutlet weak var progressView: UIProgressView!
  134. class func instanceFromNib() -> UIView {
  135. return UINib(nibName: "IMImagemeterView", bundle: nil).instantiate(withOwner: nil, options: nil)[0] as! UIView
  136. }
  137. override func awakeFromNib() {
  138. super.awakeFromNib()
  139. image.isUserInteractionEnabled = true
  140. progressView.progressTintColor = NCBrandColor.sharedInstance.brandElement
  141. progressView.trackTintColor = UIColor(red: 247.0/255.0, green: 247.0/255.0, blue: 247.0/255.0, alpha: 1.0)
  142. progressView.progress = 0
  143. }
  144. }