NCViewerImagemeter.swift 6.9 KB

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