NCViewerImagemeter.swift 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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: UIViewController {
  25. @IBOutlet weak var img: UIImageView!
  26. @IBOutlet weak var imgHeightConstraint: NSLayoutConstraint!
  27. @IBOutlet weak var progressView: UIProgressView!
  28. private let appDelegate = UIApplication.shared.delegate as! AppDelegate
  29. private var nameArchiveImagemeter: String = ""
  30. private var pathArchiveImagemeter: String = ""
  31. private var annotation: IMImagemeterCodable.imagemeterAnnotation?
  32. private var audioPlayer = AVAudioPlayer()
  33. private var timer = Timer()
  34. private var durationPlayer: TimeInterval = 0
  35. private var counterSecondPlayer: TimeInterval = 0
  36. var metadata: tableMetadata?
  37. override func viewDidLoad() {
  38. super.viewDidLoad()
  39. self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: NSLocalizedString("_done_", comment: ""), style: UIBarButtonItem.Style.plain, target: self, action: #selector(close))
  40. self.navigationController?.navigationBar.isTranslucent = false
  41. self.navigationController?.navigationBar.barTintColor = NCBrandColor.sharedInstance.brand
  42. self.navigationController?.navigationBar.tintColor = NCBrandColor.sharedInstance.brandText
  43. self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: NCBrandColor.sharedInstance.brandText]
  44. nameArchiveImagemeter = (metadata!.fileNameView as NSString).deletingPathExtension
  45. pathArchiveImagemeter = CCUtility.getDirectoryProviderStorageFileID(metadata?.fileID) + "/" + nameArchiveImagemeter
  46. self.navigationItem.title = nameArchiveImagemeter
  47. // Progress view
  48. progressView.progressTintColor = NCBrandColor.sharedInstance.brandElement
  49. progressView.trackTintColor = UIColor(red: 247.0/255.0, green: 247.0/255.0, blue: 247.0/255.0, alpha: 1.0)
  50. progressView.progress = 0
  51. }
  52. override func viewWillAppear(_ animated: Bool) {
  53. super.viewWillAppear(animated)
  54. do {
  55. let annoPath = (pathArchiveImagemeter + "/anno-" + nameArchiveImagemeter + ".imm").url
  56. let annoData = try Data(contentsOf: annoPath, options: .mappedIfSafe)
  57. if let annotation = IMImagemeterCodable.sharedInstance.decoderAnnotetion(annoData) {
  58. self.annotation = annotation
  59. imgThumbnails()
  60. imgAudio()
  61. } else {
  62. appDelegate.messageNotification("_error_", description: "_error_decompressing_", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: Int(k_CCErrorInternalError))
  63. }
  64. } catch {
  65. print("error:\(error)")
  66. }
  67. }
  68. @objc func updateTimer() {
  69. counterSecondPlayer += 1
  70. progressView.progress = Float(counterSecondPlayer / durationPlayer)
  71. }
  72. func imgThumbnails() {
  73. guard let annotation = self.annotation else {
  74. return
  75. }
  76. if let thumbnailsFilename = annotation.thumbnails.first?.filename {
  77. if let thumbnailsWidth = annotation.thumbnails.first?.width {
  78. if let thumbnailsHeight = annotation.thumbnails.first?.height {
  79. let factor = Float(thumbnailsWidth) / Float(thumbnailsHeight)
  80. let imageWidth = self.view.bounds.size.width
  81. imgHeightConstraint.constant = CGFloat((Float(imageWidth) / factor))
  82. img.image = UIImage(contentsOfFile: pathArchiveImagemeter + "/" + thumbnailsFilename)
  83. }
  84. }
  85. }
  86. }
  87. func imgAudio() {
  88. guard let annotation = self.annotation else {
  89. return
  90. }
  91. for element in annotation.elements {
  92. let coordinateNormalize = IMImagemeterCodable.sharedInstance.convertCoordinate(x: element.center.x, y: element.center.y, width: Double(self.view.bounds.width), height: Double(imgHeightConstraint.constant), button: 30)
  93. let x = coordinateNormalize.x
  94. let y = coordinateNormalize.y + Double(img.frame.origin.y)
  95. let button = UIButton()
  96. button.frame = CGRect(x: x, y: y, width: 30, height: 30)
  97. button.setImage(UIImage(named: "audioPlay"), for: .normal)
  98. button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
  99. button.tag = element.id
  100. self.view.addSubview(button)
  101. }
  102. }
  103. @objc func buttonAction(sender: UIButton!) {
  104. guard let annotation = self.annotation else {
  105. return
  106. }
  107. for element in annotation.elements {
  108. if element.id == sender.tag {
  109. do {
  110. let fileNamePath = pathArchiveImagemeter + "/" + element.audio_recording.recording_filename
  111. try audioPlayer = AVAudioPlayer(contentsOf: URL(fileURLWithPath: fileNamePath))
  112. audioPlayer.delegate = self
  113. audioPlayer.prepareToPlay()
  114. audioPlayer.play()
  115. durationPlayer = TimeInterval(audioPlayer.duration)
  116. timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(updateTimer), userInfo: nil, repeats: true)
  117. } catch {
  118. }
  119. }
  120. }
  121. }
  122. @objc func close() {
  123. self.dismiss(animated: true, completion: nil)
  124. }
  125. }
  126. extension NCViewerImagemeter: AVAudioPlayerDelegate {
  127. func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) {
  128. updateTimer()
  129. timer.invalidate()
  130. counterSecondPlayer = 0
  131. DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
  132. self.progressView.progress = 0
  133. }
  134. }
  135. }