captains-log

[TO-FIX] audiojournaling app
git clone git://git.figbert.com/captains-log.git
Log | Files | Refs

commit ce661d255692b70c3e28e8506dbb920786e8ef4b
parent dda4c9ad3227c7cbd5f1ae96b21a41abc7934b0b
Author: therealFIGBERT <figbertwelner@gmail.com>
Date:   Thu,  7 Nov 2019 07:45:59 -0800

Updating recording to create json details file for display with previous recordings page

Diffstat:
MAudioRecorder.swift | 73++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------
McaptainsLog/AudioPlayer.swift | 2+-
McaptainsLog/PreviousRecordings.swift | 3++-
3 files changed, 69 insertions(+), 9 deletions(-)

diff --git a/AudioRecorder.swift b/AudioRecorder.swift @@ -33,7 +33,14 @@ class AudioRecorder: NSObject, ObservableObject { print("Failed to set up audio session") } let documentPath = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0] - let audioFilename = documentPath.appendingPathComponent("\(Date().toString(dateFormat: "dd-MM-YY_'at'_HH:mm:ss")).m4a") + let rawDate = Date() + let dateMatch = rawDate.toString(dateFormat: "dd-MM-YY_'at'_HH:mm:ss") + let audioFilename = documentPath.appendingPathComponent("\(dateMatch).m4a") + let jsonFilename = documentPath.appendingPathComponent("\(dateMatch).json") + let title = "alertHere" + let number = recordings.count+1 + let detailObject = Recording(audioURL: audioFilename, fileURL: jsonFilename, createdAt: rawDate, title: title, number: number) + let json = try! JSONEncoder().encode(detailObject) let settings = [ AVFormatIDKey: Int(kAudioFormatMPEG4AAC), AVSampleRateKey: 12000, @@ -48,6 +55,11 @@ class AudioRecorder: NSObject, ObservableObject { } catch { print("Could not start recording") } + do { + try json.write(to: jsonFilename) + } catch { + print("Could not write files") + } } else { audioRecorder.record() recording = true @@ -79,9 +91,13 @@ class AudioRecorder: NSObject, ObservableObject { let fileManager = FileManager.default let documentDirectory = fileManager.urls(for: .documentDirectory, in: .userDomainMask)[0] let directoryContents = try! fileManager.contentsOfDirectory(at: documentDirectory, includingPropertiesForKeys: nil) - for audio in directoryContents { - let recording = Recording(fileURL: audio, createdAt: getCreationDate(for: audio)) - recordings.append(recording) + for file in directoryContents { + if (file.pathExtension == "json") { + let contents = try! String(contentsOf: file, encoding: .utf8) + let serialized = contents.data(using: .utf8)! + let recording = try! JSONDecoder().decode(Recording.self, from: serialized) + recordings.append(recording) + } } recordings.sort(by: { $0.createdAt.compare($1.createdAt) == .orderedAscending}) } @@ -100,10 +116,51 @@ class AudioRecorder: NSObject, ObservableObject { struct RecordingRow: View { var audioURL: URL + let createdAt: Date + let title: String + let number: Int + @State private var strDate: String = "" @ObservedObject var audioPlayer = AudioPlayer() + @EnvironmentObject var globalVars: GlobalVars var body: some View { HStack { - Text("\(audioURL.lastPathComponent)") + VStack(alignment: .leading) { + Text("\(title)") + .font(.headline) + Group { + if (globalVars.currentDateFormat == 1) { + Text("#\(number) – \(createdAt.multi(type: "Jewish").joined(separator: ""))") + .font(.caption) + } else if (globalVars.currentDateFormat == 2) { + Text("#\(number) – \(createdAt.multi(type: "Islamic").joined(separator: ""))") + .font(.caption) + } else if (globalVars.currentDateFormat == 3) { + Text("#\(number) – \(createdAt.multi(type: "Persian").joined(separator: ""))") + .font(.caption) + } else if (globalVars.currentDateFormat == 4) { + Text("#\(number) – \(createdAt.multi(type: "Indian").joined(separator: ""))") + .font(.caption) + } else if (globalVars.currentDateFormat == 5) { + Text("#\(number) – \(createdAt.multi(type: "Coptic").joined(separator: ""))") + .font(.caption) + } else if (globalVars.currentDateFormat == 6) { + Text("#\(number) – \(createdAt.multi(type: "Chinese").joined(separator: ""))") + .font(.caption) + } else if (globalVars.currentDateFormat == 7) { + Text("#\(number) – \(createdAt.multi(type: "Japanese").joined(separator: ""))") + .font(.caption) + } else if (globalVars.currentDateFormat == 8) { + Text("#\(number) – \(createdAt.multi(type: "Ethiopian").joined(separator: ""))") + .font(.caption) + } else if (globalVars.currentDateFormat == 9) { + Text("#\(number) – \(createdAt.multi(type: "Gregorian").joined(separator: ""))") + .font(.caption) + } else { + Text("#\(number) – \(createdAt.stardate().joined(separator: " "))") + .font(.caption) + } + } + } Spacer() if (!audioPlayer.isPlaying) { Button(action: { @@ -140,8 +197,10 @@ struct RecordingRow: View { } } - -struct Recording { +struct Recording: Codable { + let audioURL: URL let fileURL: URL let createdAt: Date + let title: String + let number: Int } diff --git a/captainsLog/AudioPlayer.swift b/captainsLog/AudioPlayer.swift @@ -28,7 +28,7 @@ class AudioPlayer: NSObject, ObservableObject, AVAudioPlayerDelegate { do { audioPlayer = try AVAudioPlayer(contentsOf: audio) audioPlayer.delegate = self - audioPlayer.play(atTime: TimeInterval(0)) + audioPlayer.play() isPlaying = true hasPlayed = true } catch { diff --git a/captainsLog/PreviousRecordings.swift b/captainsLog/PreviousRecordings.swift @@ -15,7 +15,7 @@ struct PreviousRecordings: View { var body: some View { Form { ForEach(audioRecorder.recordings, id: \.createdAt) { recording in - RecordingRow(audioURL: recording.fileURL) + RecordingRow(audioURL: recording.audioURL, createdAt: recording.createdAt, title: recording.title, number: recording.number) } .onDelete(perform: delete) } @@ -29,6 +29,7 @@ struct PreviousRecordings: View { var urlsToDelete = [URL]() for index in offsets { urlsToDelete.append(audioRecorder.recordings[index].fileURL) + urlsToDelete.append(audioRecorder.recordings[index].audioURL) } audioRecorder.deleteRecording(urlsToDelete: urlsToDelete) }