NCCreateFormUploadVoiceNote.swift 12 KB

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