// // NCCreateFormUploadVoiceNote.swift // Nextcloud // // Created by Marino Faggiana on 9/03/2019. // Copyright © 2018 Marino Faggiana. All rights reserved. // // Author Marino Faggiana // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program. If not, see . // import Foundation class NCCreateFormUploadVoiceNote: XLFormViewController, NCSelectDelegate, AVAudioPlayerDelegate { @IBOutlet weak var buttonPlayStop: UIButton! private var serverUrl = "" private var titleServerUrl = "" private var fileName = "" private var fileNamePath = "" private var audioPlayer = AVAudioPlayer() let appDelegate = UIApplication.shared.delegate as! AppDelegate public func setup(serverUrl: String, fileNamePath: String, fileName: String) { if serverUrl == CCUtility.getHomeServerUrlActiveUrl(appDelegate.activeUrl) { titleServerUrl = "/" } else { titleServerUrl = (serverUrl as NSString).lastPathComponent } self.fileName = fileName self.serverUrl = serverUrl self.fileNamePath = fileNamePath // player do { try audioPlayer = AVAudioPlayer(contentsOf: URL(fileURLWithPath: fileNamePath)) audioPlayer.prepareToPlay() audioPlayer.delegate = self } catch { buttonPlayStop.isEnabled = false } } override func viewDidLoad() { super.viewDidLoad() let cancelButton : UIBarButtonItem = UIBarButtonItem(title: NSLocalizedString("_cancel_", comment: ""), style: UIBarButtonItem.Style.plain, target: self, action: #selector(cancel)) let saveButton : UIBarButtonItem = UIBarButtonItem(title: NSLocalizedString("_save_", comment: ""), style: UIBarButtonItem.Style.plain, target: self, action: #selector(save)) self.navigationItem.leftBarButtonItem = cancelButton self.navigationItem.rightBarButtonItem = saveButton self.navigationController?.navigationBar.isTranslucent = false self.navigationController?.navigationBar.barTintColor = NCBrandColor.sharedInstance.brand self.navigationController?.navigationBar.tintColor = NCBrandColor.sharedInstance.brandText self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: NCBrandColor.sharedInstance.brandText] self.tableView.separatorStyle = UITableViewCell.SeparatorStyle.none // title self.title = NSLocalizedString("_voice_memo_title_", comment: "") // form initializeForm() } func initializeForm() { let form : XLFormDescriptor = XLFormDescriptor() as XLFormDescriptor form.rowNavigationOptions = XLFormRowNavigationOptions.stopDisableRow var section : XLFormSectionDescriptor var row : XLFormRowDescriptor // Section: Destination Folder section = XLFormSectionDescriptor.formSection(withTitle: NSLocalizedString("_save_path_", comment: "").uppercased()) form.addFormSection(section) row = XLFormRowDescriptor(tag: "ButtonDestinationFolder", rowType: XLFormRowDescriptorTypeButton, title: self.titleServerUrl) row.action.formSelector = #selector(changeDestinationFolder(_:)) let imageFolder = CCGraphics.changeThemingColorImage(UIImage(named: "folder")!, multiplier:1, color: NCBrandColor.sharedInstance.brandElement) as UIImage row.cellConfig["imageView.image"] = imageFolder row.cellConfig["textLabel.textAlignment"] = NSTextAlignment.right.rawValue row.cellConfig["textLabel.font"] = UIFont.systemFont(ofSize: 15.0) section.addFormRow(row) // Section: File Name section = XLFormSectionDescriptor.formSection(withTitle: NSLocalizedString("_filename_", comment: "")) form.addFormSection(section) row = XLFormRowDescriptor(tag: "fileName", rowType: XLFormRowDescriptorTypeAccount, title: NSLocalizedString("_filename_", comment: "").uppercased()) row.value = self.fileName row.cellConfig["textLabel.font"] = UIFont.systemFont(ofSize: 15.0) row.cellConfig["textField.textAlignment"] = NSTextAlignment.right.rawValue row.cellConfig["textField.font"] = UIFont.systemFont(ofSize: 15.0) section.addFormRow(row) self.form = form } //MARK: XLFormDescriptorDelegate override func formRowDescriptorValueHasChanged(_ formRow: XLFormRowDescriptor!, oldValue: Any!, newValue: Any!) { super.formRowDescriptorValueHasChanged(formRow, oldValue: oldValue, newValue: newValue) if formRow.tag == "fileName" { self.form.delegate = nil if let fileNameNew = formRow.value { self.fileName = CCUtility.removeForbiddenCharactersServer(fileNameNew as? String) } formRow.value = self.fileName self.title = fileName self.updateFormRow(formRow) self.form.delegate = self } } override func textFieldDidBeginEditing(_ textField: UITextField) { let cell = textField.formDescriptorCell() let tag = cell?.rowDescriptor.tag if tag == "fileName" { CCUtility.selectFileName(from: textField) } } //MARK: TableViewDelegate override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) { let header: UITableViewHeaderFooterView = view as! UITableViewHeaderFooterView header.textLabel?.font = UIFont.systemFont(ofSize: 13.0) header.textLabel?.textColor = NCBrandColor.sharedInstance.icon //UIColor.lightGray } // MARK: - Action func dismissSelect(serverUrl: String?, metadata: tableMetadata?, type: String) { if serverUrl != nil { self.serverUrl = serverUrl! if serverUrl == CCUtility.getHomeServerUrlActiveUrl(appDelegate.activeUrl) { self.titleServerUrl = "/" } else { self.titleServerUrl = (serverUrl! as NSString).lastPathComponent } // Update let row : XLFormRowDescriptor = self.form.formRow(withTag: "ButtonDestinationFolder")! row.title = self.titleServerUrl self.updateFormRow(row) } } @objc func save() { self.dismiss(animated: true, completion: { let rowFileName : XLFormRowDescriptor = self.form.formRow(withTag: "fileName")! guard let name = rowFileName.value else { return } let ext = (name as! NSString).pathExtension.uppercased() var fileNameSave = "" if (ext == "") { fileNameSave = name as! String + ".m4a" } else if (CCUtility.isDocumentModifiableExtension(ext)) { fileNameSave = name as! String } else { fileNameSave = (name as! NSString).deletingPathExtension + ".m4a" } let metadataForUpload = tableMetadata() metadataForUpload.account = self.appDelegate.activeAccount metadataForUpload.date = NSDate() metadataForUpload.fileID = CCUtility.createMetadataID(fromAccount: self.appDelegate.activeAccount, serverUrl: self.serverUrl, fileNameView: fileNameSave, directory: false) metadataForUpload.fileName = fileNameSave metadataForUpload.fileNameView = fileNameSave metadataForUpload.serverUrl = self.serverUrl metadataForUpload.session = k_upload_session metadataForUpload.sessionSelector = selectorUploadFile metadataForUpload.status = Int(k_metadataStatusWaitUpload) CCUtility.copyFile(atPath: self.fileNamePath, toPath: CCUtility.getDirectoryProviderStorageFileID(metadataForUpload.fileID, fileNameView: fileNameSave)) _ = NCManageDatabase.sharedInstance.addMetadata(metadataForUpload) NCMainCommon.sharedInstance.reloadDatasource(ServerUrl: self.serverUrl, fileID: nil, action: Int32(k_action_NULL)) self.appDelegate.startLoadAutoDownloadUpload() }) } @objc func cancel() { try? FileManager.default.removeItem(atPath: fileNamePath) self.dismiss(animated: true, completion: nil) } @objc func changeDestinationFolder(_ sender: XLFormRowDescriptor) { self.deselectFormRow(sender) let storyboard = UIStoryboard(name: "NCSelect", bundle: nil) let navigationController = storyboard.instantiateInitialViewController() as! UINavigationController let viewController = navigationController.topViewController as! NCSelect viewController.delegate = self viewController.hideButtonCreateFolder = false viewController.includeDirectoryE2EEncryption = true viewController.includeImages = false viewController.layoutViewSelect = k_layout_view_move viewController.selectFile = false viewController.titleButtonDone = NSLocalizedString("_select_", comment: "") viewController.type = "" navigationController.modalPresentationStyle = UIModalPresentationStyle.formSheet self.present(navigationController, animated: true, completion: nil) } @IBAction func playStop(_ sender: Any) { if audioPlayer.isPlaying { audioPlayer.currentTime = 0.0 audioPlayer.stop() buttonPlayStop.setImage(UIImage.init(named: "audioPlay"), for: .normal) } else { audioPlayer.prepareToPlay() audioPlayer.play() buttonPlayStop.setImage(UIImage.init(named: "audioStop"), for: .normal) } } func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) { buttonPlayStop.setImage(UIImage.init(named: "audioPlay"), for: .normal) } }