NCViewerImagemeter.swift 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. var metadata: tableMetadata?
  34. override func viewDidLoad() {
  35. super.viewDidLoad()
  36. self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: NSLocalizedString("_done_", comment: ""), style: UIBarButtonItem.Style.plain, target: self, action: #selector(close))
  37. self.navigationController?.navigationBar.isTranslucent = false
  38. self.navigationController?.navigationBar.barTintColor = NCBrandColor.sharedInstance.brand
  39. self.navigationController?.navigationBar.tintColor = NCBrandColor.sharedInstance.brandText
  40. self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: NCBrandColor.sharedInstance.brandText]
  41. nameArchiveImagemeter = (metadata!.fileNameView as NSString).deletingPathExtension
  42. pathArchiveImagemeter = CCUtility.getDirectoryProviderStorageFileID(metadata?.fileID) + "/" + nameArchiveImagemeter
  43. self.navigationItem.title = nameArchiveImagemeter
  44. }
  45. override func viewWillAppear(_ animated: Bool) {
  46. super.viewWillAppear(animated)
  47. do {
  48. let annoPath = (pathArchiveImagemeter + "/anno-" + nameArchiveImagemeter + ".imm").url
  49. let annoData = try Data(contentsOf: annoPath, options: .mappedIfSafe)
  50. if let annotation = IMImagemeterCodable.sharedInstance.decoderAnnotetion(annoData) {
  51. self.annotation = annotation
  52. imgThumbnails()
  53. imgAudio()
  54. } else {
  55. appDelegate.messageNotification("_error_", description: "_error_decompressing_", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: Int(k_CCErrorInternalError))
  56. }
  57. } catch {
  58. print("error:\(error)")
  59. }
  60. }
  61. func imgThumbnails() {
  62. guard let annotation = self.annotation else {
  63. return
  64. }
  65. if let thumbnailsFilename = annotation.thumbnails.first?.filename {
  66. if let thumbnailsWidth = annotation.thumbnails.first?.width {
  67. if let thumbnailsHeight = annotation.thumbnails.first?.height {
  68. let factor = Float(thumbnailsWidth) / Float(thumbnailsHeight)
  69. let imageWidth = self.view.bounds.size.width
  70. imgHeightConstraint.constant = CGFloat((Float(imageWidth) / factor))
  71. img.image = UIImage(contentsOfFile: pathArchiveImagemeter + "/" + thumbnailsFilename)
  72. }
  73. }
  74. }
  75. }
  76. func imgAudio() {
  77. guard let annotation = self.annotation else {
  78. return
  79. }
  80. for element in annotation.elements {
  81. 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)
  82. let x = coordinateNormalize.x
  83. let y = coordinateNormalize.y + Double(img.frame.origin.y)
  84. let button = UIButton()
  85. button.frame = CGRect(x: x, y: y, width: 30, height: 30)
  86. button.setImage(UIImage(named: "audioPlay"), for: .normal)
  87. button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
  88. button.tag = element.id
  89. self.view.addSubview(button)
  90. }
  91. }
  92. @objc func buttonAction(sender: UIButton!) {
  93. guard let annotation = self.annotation else {
  94. return
  95. }
  96. for element in annotation.elements {
  97. if element.id == sender.tag {
  98. let fileNamePath = pathArchiveImagemeter + "/" + element.audio_recording.recording_filename
  99. // player
  100. do {
  101. try audioPlayer = AVAudioPlayer(contentsOf: URL(fileURLWithPath: fileNamePath))
  102. audioPlayer.delegate = self
  103. audioPlayer.prepareToPlay()
  104. audioPlayer.play()
  105. } catch {
  106. }
  107. }
  108. }
  109. }
  110. @objc func close() {
  111. self.dismiss(animated: true, completion: nil)
  112. }
  113. }
  114. extension NCViewerImagemeter: AVAudioPlayerDelegate {
  115. func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) {
  116. progressView.progress = 0
  117. }
  118. }