NCCreateFormUploadVoiceNote.swift 9.3 KB

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