NCCreateFormUploadVoiceNote.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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.title = fileName
  102. self.updateFormRow(formRow)
  103. self.form.delegate = self
  104. }
  105. }
  106. override func textFieldDidBeginEditing(_ textField: UITextField) {
  107. let cell = textField.formDescriptorCell()
  108. let tag = cell?.rowDescriptor.tag
  109. if tag == "fileName" {
  110. CCUtility.selectFileName(from: textField)
  111. }
  112. }
  113. //MARK: TableViewDelegate
  114. override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
  115. let header: UITableViewHeaderFooterView = view as! UITableViewHeaderFooterView
  116. header.textLabel?.font = UIFont.systemFont(ofSize: 13.0)
  117. header.textLabel?.textColor = NCBrandColor.sharedInstance.icon //UIColor.lightGray
  118. }
  119. // MARK: - Action
  120. func dismissSelect(serverUrl: String?, metadata: tableMetadata?, type: String) {
  121. if serverUrl != nil {
  122. self.serverUrl = serverUrl!
  123. if serverUrl == CCUtility.getHomeServerUrlActiveUrl(appDelegate.activeUrl) {
  124. self.titleServerUrl = "/"
  125. } else {
  126. self.titleServerUrl = (serverUrl! as NSString).lastPathComponent
  127. }
  128. // Update
  129. let row : XLFormRowDescriptor = self.form.formRow(withTag: "ButtonDestinationFolder")!
  130. row.title = self.titleServerUrl
  131. self.updateFormRow(row)
  132. }
  133. }
  134. @objc func save() {
  135. self.dismiss(animated: true, completion: {
  136. let rowFileName : XLFormRowDescriptor = self.form.formRow(withTag: "fileName")!
  137. guard let name = rowFileName.value else {
  138. return
  139. }
  140. let ext = (name as! NSString).pathExtension.uppercased()
  141. var fileNameSave = ""
  142. if (ext == "") {
  143. fileNameSave = name as! String + ".m4a"
  144. } else if (CCUtility.isDocumentModifiableExtension(ext)) {
  145. fileNameSave = name as! String
  146. } else {
  147. fileNameSave = (name as! NSString).deletingPathExtension + ".m4a"
  148. }
  149. let metadataForUpload = tableMetadata()
  150. metadataForUpload.account = self.appDelegate.activeAccount
  151. metadataForUpload.date = NSDate()
  152. metadataForUpload.fileID = CCUtility.createMetadataID(fromAccount: self.appDelegate.activeAccount, serverUrl: self.serverUrl, fileNameView: fileNameSave, directory: false)
  153. metadataForUpload.fileName = fileNameSave
  154. metadataForUpload.fileNameView = fileNameSave
  155. metadataForUpload.serverUrl = self.serverUrl
  156. metadataForUpload.session = k_upload_session
  157. metadataForUpload.sessionSelector = selectorUploadFile
  158. metadataForUpload.status = Int(k_metadataStatusWaitUpload)
  159. CCUtility.copyFile(atPath: self.fileNamePath, toPath: CCUtility.getDirectoryProviderStorageFileID(metadataForUpload.fileID, fileNameView: fileNameSave))
  160. _ = NCManageDatabase.sharedInstance.addMetadata(metadataForUpload)
  161. NCMainCommon.sharedInstance.reloadDatasource(ServerUrl: self.serverUrl, fileID: nil, action: Int32(k_action_NULL))
  162. self.appDelegate.startLoadAutoDownloadUpload()
  163. })
  164. }
  165. @objc func cancel() {
  166. try? FileManager.default.removeItem(atPath: fileNamePath)
  167. self.dismiss(animated: true, completion: nil)
  168. }
  169. @objc func changeDestinationFolder(_ sender: XLFormRowDescriptor) {
  170. self.deselectFormRow(sender)
  171. let storyboard = UIStoryboard(name: "NCSelect", bundle: nil)
  172. let navigationController = storyboard.instantiateInitialViewController() as! UINavigationController
  173. let viewController = navigationController.topViewController as! NCSelect
  174. viewController.delegate = self
  175. viewController.hideButtonCreateFolder = false
  176. viewController.includeDirectoryE2EEncryption = true
  177. viewController.includeImages = false
  178. viewController.layoutViewSelect = k_layout_view_move
  179. viewController.selectFile = false
  180. viewController.titleButtonDone = NSLocalizedString("_select_", comment: "")
  181. viewController.type = ""
  182. navigationController.modalPresentationStyle = UIModalPresentationStyle.formSheet
  183. self.present(navigationController, animated: true, completion: nil)
  184. }
  185. @IBAction func playStop(_ sender: Any) {
  186. if audioPlayer.isPlaying {
  187. audioPlayer.currentTime = 0.0
  188. audioPlayer.stop()
  189. buttonPlayStop.setImage(UIImage.init(named: "audioPlay"), for: .normal)
  190. } else {
  191. audioPlayer.prepareToPlay()
  192. audioPlayer.play()
  193. buttonPlayStop.setImage(UIImage.init(named: "audioStop"), for: .normal)
  194. }
  195. }
  196. func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) {
  197. buttonPlayStop.setImage(UIImage.init(named: "audioPlay"), for: .normal)
  198. }
  199. }