1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
|
//
// NCCreateFormUploadVoiceNote.swift
// Nextcloud
//
// Created by Marino Faggiana on 9/03/2019.
// Copyright © 2019 Marino Faggiana. All rights reserved.
//
// Author Marino Faggiana <marino.faggiana@nextcloud.com>
//
// 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 <http://www.gnu.org/licenses/>.
//
import UIKit
import NCCommunication
class NCCreateFormUploadVoiceNote: XLFormViewController, NCSelectDelegate, AVAudioPlayerDelegate, NCCreateFormUploadConflictDelegate {
@IBOutlet weak var buttonPlayStop: UIButton!
@IBOutlet weak var labelTimer: UILabel!
@IBOutlet weak var labelDuration: UILabel!
@IBOutlet weak var progressView: UIProgressView!
let appDelegate = UIApplication.shared.delegate as! AppDelegate
private var serverUrl = ""
private var titleServerUrl = ""
private var fileName = ""
private var fileNamePath = ""
private var durationPlayer: TimeInterval = 0
private var counterSecondPlayer: TimeInterval = 0
private var audioPlayer: AVAudioPlayer!
private var timer = Timer()
var cellBackgoundColor = NCBrandColor.shared.secondarySystemGroupedBackground
// MARK: - View Life Cycle
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: NSLocalizedString("_cancel_", comment: ""), style: UIBarButtonItem.Style.plain, target: self, action: #selector(cancel))
self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: NSLocalizedString("_save_", comment: ""), style: UIBarButtonItem.Style.plain, target: self, action: #selector(save))
self.tableView.separatorStyle = UITableViewCell.SeparatorStyle.none
view.backgroundColor = NCBrandColor.shared.systemGroupedBackground
tableView.backgroundColor = NCBrandColor.shared.systemGroupedBackground
cellBackgoundColor = NCBrandColor.shared.secondarySystemGroupedBackground
self.title = NSLocalizedString("_voice_memo_title_", comment: "")
// Button Play Stop
buttonPlayStop.setImage(UIImage(named: "audioPlay")!.image(color: NCBrandColor.shared.gray, size: 100), for: .normal)
// Progress view
progressView.progress = 0
progressView.progressTintColor = .green
progressView.trackTintColor = UIColor(red: 247.0/255.0, green: 247.0/255.0, blue: 247.0/255.0, alpha: 1.0)
labelTimer.textColor = NCBrandColor.shared.label
labelDuration.textColor = NCBrandColor.shared.label
initializeForm()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
updateTimerUI()
}
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
if audioPlayer.isPlaying {
stop()
}
}
public func setup(serverUrl: String, fileNamePath: String, fileName: String) {
if serverUrl == NCUtilityFileSystem.shared.getHomeServer(account: appDelegate.account) {
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
durationPlayer = TimeInterval(audioPlayer.duration)
} catch {
buttonPlayStop.isEnabled = false
}
}
// MARK: XLForm
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(_:))
row.cellConfig["backgroundColor"] = cellBackgoundColor
row.cellConfig["imageView.image"] = UIImage(named: "folder")!.image(color: NCBrandColor.shared.brandElement, size: 25)
row.cellConfig["textLabel.textAlignment"] = NSTextAlignment.right.rawValue
row.cellConfig["textLabel.font"] = UIFont.systemFont(ofSize: 15.0)
row.cellConfig["textLabel.textColor"] = NCBrandColor.shared.label
section.addFormRow(row)
// Section: File Name
section = XLFormSectionDescriptor.formSection(withTitle: NSLocalizedString("_filename_", comment: "").uppercased())
form.addFormSection(section)
row = XLFormRowDescriptor(tag: "fileName", rowType: XLFormRowDescriptorTypeText, title: NSLocalizedString("_filename_", comment: ""))
row.value = self.fileName
row.cellConfig["backgroundColor"] = cellBackgoundColor
row.cellConfig["textLabel.font"] = UIFont.systemFont(ofSize: 15.0)
row.cellConfig["textLabel.textColor"] = NCBrandColor.shared.label
row.cellConfig["textField.textAlignment"] = NSTextAlignment.right.rawValue
row.cellConfig["textField.font"] = UIFont.systemFont(ofSize: 15.0)
row.cellConfig["textField.textColor"] = NCBrandColor.shared.label
section.addFormRow(row)
self.form = form
}
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.updateFormRow(formRow)
self.form.delegate = self
}
}
// 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 = .gray
header.tintColor = cellBackgoundColor
}
// MARK: - Action
func dismissSelect(serverUrl: String?, metadata: tableMetadata?, type: String, items: [Any], overwrite: Bool, copy: Bool, move: Bool) {
if serverUrl != nil {
self.serverUrl = serverUrl!
if serverUrl == NCUtilityFileSystem.shared.getHomeServer(account: appDelegate.account) {
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() {
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 {
fileNameSave = (name as! NSString).deletingPathExtension + ".m4a"
}
let metadataForUpload = NCManageDatabase.shared.createMetadata(account: self.appDelegate.account, user: self.appDelegate.user, userId: self.appDelegate.userId, fileName: fileNameSave, fileNameView: fileNameSave, ocId: UUID().uuidString, serverUrl: self.serverUrl, urlBase: self.appDelegate.urlBase, url: "", contentType: "")
metadataForUpload.session = NCNetworking.shared.sessionIdentifierBackground
metadataForUpload.sessionSelector = NCGlobal.shared.selectorUploadFile
metadataForUpload.status = NCGlobal.shared.metadataStatusWaitUpload
metadataForUpload.size = NCUtilityFileSystem.shared.getFileSize(filePath: fileNamePath)
if NCManageDatabase.shared.getMetadataConflict(account: appDelegate.account, serverUrl: serverUrl, fileName: fileNameSave) != nil {
guard let conflictViewController = UIStoryboard(name: "NCCreateFormUploadConflict", bundle: nil).instantiateInitialViewController() as? NCCreateFormUploadConflict else { return }
conflictViewController.textLabelDetailNewFile = NSLocalizedString("_now_", comment: "")
conflictViewController.serverUrl = serverUrl
conflictViewController.metadatasUploadInConflict = [metadataForUpload]
conflictViewController.delegate = self
self.present(conflictViewController, animated: true, completion: nil)
} else {
dismissAndUpload(metadataForUpload)
}
}
func dismissCreateFormUploadConflict(metadatas: [tableMetadata]?) {
if metadatas != nil && metadatas!.count > 0 {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
self.dismissAndUpload(metadatas![0])
}
}
}
func dismissAndUpload(_ metadata: tableMetadata) {
CCUtility.copyFile(atPath: self.fileNamePath, toPath: CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView))
appDelegate.networkingProcessUpload?.createProcessUploads(metadatas: [metadata])
self.dismiss(animated: true, completion: nil)
}
@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.typeOfCommandView = .selectCreateFolder
viewController.includeDirectoryE2EEncryption = true
self.present(navigationController, animated: true, completion: nil)
}
// MARK: Player - Timer
func updateTimerUI() {
labelTimer.text = String().formatSecondsToString(counterSecondPlayer)
labelDuration.text = String().formatSecondsToString(durationPlayer)
progressView.progress = Float(counterSecondPlayer / durationPlayer)
}
@objc func updateTimer() {
counterSecondPlayer += 1
updateTimerUI()
}
@IBAction func playStop(_ sender: Any) {
if audioPlayer.isPlaying {
stop()
} else {
start()
}
}
func start() {
audioPlayer.prepareToPlay()
audioPlayer.play()
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(updateTimer), userInfo: nil, repeats: true)
buttonPlayStop.setImage(UIImage(named: "stop")!.image(color: NCBrandColor.shared.gray, size: 100), for: .normal)
}
func stop() {
audioPlayer.currentTime = 0.0
audioPlayer.stop()
timer.invalidate()
counterSecondPlayer = 0
progressView.progress = 0
updateTimerUI()
buttonPlayStop.setImage(UIImage(named: "audioPlay")!.image(color: NCBrandColor.shared.gray, size: 100), for: .normal)
}
func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) {
timer.invalidate()
counterSecondPlayer = 0
progressView.progress = 0
updateTimerUI()
buttonPlayStop.setImage(UIImage(named: "audioPlay")!.image(color: NCBrandColor.shared.gray, size: 100), for: .normal)
}
}
|