NCCreateFormUploadVoiceNote.swift 11 KB

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