NCCreateFormUploadVoiceNote.swift 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. //
  2. // NCCreateFormUploadVoiceNote.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 9/03/2019.
  6. // Copyright © 2018 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 NCCreateFormUploadVoiceNote: XLFormViewController, NCSelectDelegate, AVAudioPlayerDelegate {
  25. @IBOutlet weak var buttonPlayStop: UIButton!
  26. @IBOutlet weak var labelTimer: UILabel!
  27. @IBOutlet weak var labelDuration: UILabel!
  28. @IBOutlet weak var progressView: UIProgressView!
  29. private var serverUrl = ""
  30. private var titleServerUrl = ""
  31. private var fileName = ""
  32. private var fileNamePath = ""
  33. private var durationPlayer: TimeInterval = 0
  34. private var counterSecondPlayer: TimeInterval = 0
  35. private var audioPlayer = AVAudioPlayer()
  36. private var timer = Timer()
  37. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  38. public func setup(serverUrl: String, fileNamePath: String, fileName: String) {
  39. if serverUrl == CCUtility.getHomeServerUrlActiveUrl(appDelegate.activeUrl) {
  40. titleServerUrl = "/"
  41. } else {
  42. titleServerUrl = (serverUrl as NSString).lastPathComponent
  43. }
  44. self.fileName = fileName
  45. self.serverUrl = serverUrl
  46. self.fileNamePath = fileNamePath
  47. // player
  48. do {
  49. try audioPlayer = AVAudioPlayer(contentsOf: URL(fileURLWithPath: fileNamePath))
  50. audioPlayer.prepareToPlay()
  51. audioPlayer.delegate = self
  52. durationPlayer = TimeInterval(audioPlayer.duration)
  53. } catch {
  54. buttonPlayStop.isEnabled = false
  55. }
  56. }
  57. override func viewDidLoad() {
  58. super.viewDidLoad()
  59. let cancelButton : UIBarButtonItem = UIBarButtonItem(title: NSLocalizedString("_cancel_", comment: ""), style: UIBarButtonItem.Style.plain, target: self, action: #selector(cancel))
  60. let saveButton : UIBarButtonItem = UIBarButtonItem(title: NSLocalizedString("_save_", comment: ""), style: UIBarButtonItem.Style.plain, target: self, action: #selector(save))
  61. self.navigationItem.leftBarButtonItem = cancelButton
  62. self.navigationItem.rightBarButtonItem = saveButton
  63. self.navigationController?.navigationBar.isTranslucent = false
  64. self.navigationController?.navigationBar.barTintColor = NCBrandColor.sharedInstance.brand
  65. self.navigationController?.navigationBar.tintColor = NCBrandColor.sharedInstance.brandText
  66. self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: NCBrandColor.sharedInstance.brandText]
  67. self.tableView.separatorStyle = UITableViewCell.SeparatorStyle.none
  68. // title
  69. self.title = NSLocalizedString("_voice_memo_title_", comment: "")
  70. // Button Play Stop
  71. buttonPlayStop.setImage(CCGraphics.changeThemingColorImage(UIImage(named: "audioPlay")!, width: 200, height: 200, color: NCBrandColor.sharedInstance.icon), for: .normal)
  72. // Progress view
  73. progressView.progressTintColor = NCBrandColor.sharedInstance.graySoft
  74. progressView.trackTintColor = UIColor(red: 247.0/255.0, green: 247.0/255.0, blue: 247.0/255.0, alpha: 1.0)
  75. progressView.progress = 0
  76. // form
  77. initializeForm()
  78. }
  79. override func viewWillAppear(_ animated: Bool) {
  80. super.viewWillAppear(animated)
  81. updateTimerUI()
  82. }
  83. func initializeForm() {
  84. let form : XLFormDescriptor = XLFormDescriptor() as XLFormDescriptor
  85. form.rowNavigationOptions = XLFormRowNavigationOptions.stopDisableRow
  86. var section : XLFormSectionDescriptor
  87. var row : XLFormRowDescriptor
  88. // Section: Destination Folder
  89. section = XLFormSectionDescriptor.formSection(withTitle: NSLocalizedString("_save_path_", comment: "").uppercased())
  90. form.addFormSection(section)
  91. row = XLFormRowDescriptor(tag: "ButtonDestinationFolder", rowType: XLFormRowDescriptorTypeButton, title: self.titleServerUrl)
  92. row.action.formSelector = #selector(changeDestinationFolder(_:))
  93. row.cellConfig["imageView.image"] = CCGraphics.changeThemingColorImage(UIImage(named: "folder")!, width: 50, height: 50, color: NCBrandColor.sharedInstance.brandElement) as UIImage
  94. row.cellConfig["textLabel.textAlignment"] = NSTextAlignment.right.rawValue
  95. row.cellConfig["textLabel.font"] = UIFont.systemFont(ofSize: 15.0)
  96. row.cellConfig["textLabel.textColor"] = UIColor.black
  97. section.addFormRow(row)
  98. // Section: File Name
  99. section = XLFormSectionDescriptor.formSection(withTitle: NSLocalizedString("_filename_", comment: "").uppercased())
  100. form.addFormSection(section)
  101. row = XLFormRowDescriptor(tag: "fileName", rowType: XLFormRowDescriptorTypeAccount, title: NSLocalizedString("_filename_", comment: ""))
  102. row.value = self.fileName
  103. row.cellConfig["textLabel.font"] = UIFont.systemFont(ofSize: 15.0)
  104. row.cellConfig["textField.textAlignment"] = NSTextAlignment.right.rawValue
  105. row.cellConfig["textField.font"] = UIFont.systemFont(ofSize: 15.0)
  106. section.addFormRow(row)
  107. self.form = form
  108. }
  109. //MARK: XLFormDescriptorDelegate
  110. override func formRowDescriptorValueHasChanged(_ formRow: XLFormRowDescriptor!, oldValue: Any!, newValue: Any!) {
  111. super.formRowDescriptorValueHasChanged(formRow, oldValue: oldValue, newValue: newValue)
  112. if formRow.tag == "fileName" {
  113. self.form.delegate = nil
  114. if let fileNameNew = formRow.value {
  115. self.fileName = CCUtility.removeForbiddenCharactersServer(fileNameNew as? String)
  116. }
  117. formRow.value = self.fileName
  118. self.updateFormRow(formRow)
  119. self.form.delegate = self
  120. }
  121. }
  122. override func textFieldDidBeginEditing(_ textField: UITextField) {
  123. let cell = textField.formDescriptorCell()
  124. let tag = cell?.rowDescriptor.tag
  125. if tag == "fileName" {
  126. CCUtility.selectFileName(from: textField)
  127. }
  128. }
  129. //MARK: TableViewDelegate
  130. override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
  131. let header: UITableViewHeaderFooterView = view as! UITableViewHeaderFooterView
  132. header.textLabel?.font = UIFont.systemFont(ofSize: 13.0)
  133. header.textLabel?.textColor = NCBrandColor.sharedInstance.icon
  134. }
  135. // MARK: - Action
  136. func dismissSelect(serverUrl: String?, metadata: tableMetadata?, type: String) {
  137. if serverUrl != nil {
  138. self.serverUrl = serverUrl!
  139. if serverUrl == CCUtility.getHomeServerUrlActiveUrl(appDelegate.activeUrl) {
  140. self.titleServerUrl = "/"
  141. } else {
  142. self.titleServerUrl = (serverUrl! as NSString).lastPathComponent
  143. }
  144. // Update
  145. let row : XLFormRowDescriptor = self.form.formRow(withTag: "ButtonDestinationFolder")!
  146. row.title = self.titleServerUrl
  147. self.updateFormRow(row)
  148. }
  149. }
  150. @objc func save() {
  151. self.dismiss(animated: true, completion: {
  152. let rowFileName : XLFormRowDescriptor = self.form.formRow(withTag: "fileName")!
  153. guard let name = rowFileName.value else {
  154. return
  155. }
  156. let ext = (name as! NSString).pathExtension.uppercased()
  157. var fileNameSave = ""
  158. if (ext == "") {
  159. fileNameSave = name as! String + ".m4a"
  160. } else if (CCUtility.isDocumentModifiableExtension(ext)) {
  161. fileNameSave = name as! String
  162. } else {
  163. fileNameSave = (name as! NSString).deletingPathExtension + ".m4a"
  164. }
  165. let metadataForUpload = tableMetadata()
  166. metadataForUpload.account = self.appDelegate.activeAccount
  167. metadataForUpload.date = NSDate()
  168. metadataForUpload.ocId = CCUtility.createMetadataID(fromAccount: self.appDelegate.activeAccount, serverUrl: self.serverUrl, fileNameView: fileNameSave, directory: false)
  169. metadataForUpload.fileName = fileNameSave
  170. metadataForUpload.fileNameView = fileNameSave
  171. metadataForUpload.serverUrl = self.serverUrl
  172. metadataForUpload.session = k_upload_session
  173. metadataForUpload.sessionSelector = selectorUploadFile
  174. metadataForUpload.status = Int(k_metadataStatusWaitUpload)
  175. CCUtility.copyFile(atPath: self.fileNamePath, toPath: CCUtility.getDirectoryProviderStorageOcId(metadataForUpload.ocId, fileNameView: fileNameSave))
  176. _ = NCManageDatabase.sharedInstance.addMetadata(metadataForUpload)
  177. NCMainCommon.sharedInstance.reloadDatasource(ServerUrl: self.serverUrl, ocId: nil, action: Int32(k_action_NULL))
  178. self.appDelegate.startLoadAutoDownloadUpload()
  179. })
  180. }
  181. @objc func cancel() {
  182. try? FileManager.default.removeItem(atPath: fileNamePath)
  183. self.dismiss(animated: true, completion: nil)
  184. }
  185. @objc func changeDestinationFolder(_ sender: XLFormRowDescriptor) {
  186. self.deselectFormRow(sender)
  187. let storyboard = UIStoryboard(name: "NCSelect", bundle: nil)
  188. let navigationController = storyboard.instantiateInitialViewController() as! UINavigationController
  189. let viewController = navigationController.topViewController as! NCSelect
  190. viewController.delegate = self
  191. viewController.hideButtonCreateFolder = false
  192. viewController.includeDirectoryE2EEncryption = true
  193. viewController.includeImages = false
  194. viewController.layoutViewSelect = k_layout_view_move
  195. viewController.selectFile = false
  196. viewController.titleButtonDone = NSLocalizedString("_select_", comment: "")
  197. viewController.type = ""
  198. navigationController.modalPresentationStyle = UIModalPresentationStyle.formSheet
  199. self.present(navigationController, animated: true, completion: nil)
  200. }
  201. //MARK: Player - Timer
  202. func updateTimerUI() {
  203. labelTimer.text = NCUtility.sharedInstance.formatSecondsToString(counterSecondPlayer)
  204. labelDuration.text = NCUtility.sharedInstance.formatSecondsToString(durationPlayer)
  205. progressView.progress = Float(counterSecondPlayer / durationPlayer)
  206. }
  207. @objc func updateTimer() {
  208. counterSecondPlayer += 1
  209. updateTimerUI()
  210. }
  211. @IBAction func playStop(_ sender: Any) {
  212. if audioPlayer.isPlaying {
  213. audioPlayer.currentTime = 0.0
  214. audioPlayer.stop()
  215. timer.invalidate()
  216. counterSecondPlayer = 0
  217. progressView.progress = 0
  218. updateTimerUI()
  219. buttonPlayStop.setImage(CCGraphics.changeThemingColorImage(UIImage(named: "audioPlay")!, width: 200, height: 200, color: NCBrandColor.sharedInstance.icon), for: .normal)
  220. } else {
  221. audioPlayer.prepareToPlay()
  222. audioPlayer.play()
  223. timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(updateTimer), userInfo: nil, repeats: true)
  224. buttonPlayStop.setImage(CCGraphics.changeThemingColorImage(UIImage(named: "audioStop")!, width: 200, height: 200, color: NCBrandColor.sharedInstance.icon), for: .normal)
  225. }
  226. }
  227. func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) {
  228. timer.invalidate()
  229. counterSecondPlayer = 0
  230. progressView.progress = 0
  231. updateTimerUI()
  232. buttonPlayStop.setImage(CCGraphics.changeThemingColorImage(UIImage(named: "audioPlay")!, width: 200, height: 200, color: NCBrandColor.sharedInstance.icon), for: .normal)
  233. }
  234. }