NCCreateFormUploadVoiceNote.swift 10 KB

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