NCViewerImagemeter.swift 5.7 KB

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