Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Claim recordings naming and placing #1125

Merged
merged 1 commit into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 47 additions & 2 deletions Projects/Claims/Sources/SubmitClaimState.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,33 @@
import Foundation
import Presentation
import hCore

public struct SubmitClaimsState: StateProtocol {
@Transient(defaultValue: "") var currentClaimId: String
@Transient(defaultValue: "") var currentClaimId: String {
didSet {
do {
var isDir: ObjCBool = true
if FileManager.default.fileExists(
atPath: claimsAudioRecordingRootPath.relativePath,
isDirectory: &isDir
) {
let content = try FileManager.default
.contentsOfDirectory(atPath: claimsAudioRecordingRootPath.relativePath)
.filter({ URL(string: $0)?.pathExtension == AudioRecorder.audioFileExtension })
try content.forEach({
try FileManager.default.removeItem(
atPath: claimsAudioRecordingRootPath.appendingPathComponent($0).relativePath
)
})
} else {
try FileManager.default.createDirectory(
at: claimsAudioRecordingRootPath,
withIntermediateDirectories: true
)
}
} catch _ {}
}
}
@OptionalTransient var currentClaimContext: String?
@Transient(defaultValue: []) var claimEntrypoints: [ClaimEntryPointResponseModel]
@Transient(defaultValue: []) var claimEntrypointGroups: [ClaimEntryPointGroupResponseModel]
Expand All @@ -23,9 +48,29 @@ public struct SubmitClaimsState: StateProtocol {
@OptionalTransient var glassDamageStep: FlowClaimDeflectStepModel?
@OptionalTransient var progress: Float?
@OptionalTransient var previousProgress: Float?

@Transient(defaultValue: EntrypointState()) var entrypoints: EntrypointState

var claimAudioRecordingPath: URL {
let nameOfFile: String = {
if currentClaimId.isEmpty {
return "audio-file-recoding"
}
return currentClaimId
}()
let audioPath =
claimsAudioRecordingRootPath
.appendingPathComponent(nameOfFile)
.appendingPathExtension(AudioRecorder.audioFileExtension)
return audioPath
}

private var claimsAudioRecordingRootPath: URL {
let tempDirectory = FileManager.default.temporaryDirectory
let claimsAudioRecoringPath =
tempDirectory
.appendingPathComponent("claims")
return claimsAudioRecoringPath
}
public init() {}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import AVFAudio
import Combine
import Presentation
import SwiftUI
import hCoreUI

class AudioRecorder: ObservableObject {
private let filename = "claim-recording.m4a"
static let audioFileExtension = "m4a"
private let filePath: URL
init(filePath: URL) {
self.filePath = filePath
}
private(set) var isRecording = false {
didSet {
objectWillChange.send(self)
Expand Down Expand Up @@ -46,27 +51,22 @@ class AudioRecorder: ObservableObject {
do {
try recordingSession.setCategory(.record)
try recordingSession.setActive(true)
try FileManager.default.removeItem(at: filePath)
} catch {
print("Failed")
}

let settings = [
AVFormatIDKey: Int(kAudioFormatMPEG4AAC),
AVSampleRateKey: 12000,
AVNumberOfChannelsKey: 1,
AVEncoderAudioQualityKey: AVAudioQuality.high.rawValue,
]

let documentPath = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]

let audioFilename = documentPath.appendingPathComponent(filename)

do {
recorder = try AVAudioRecorder(url: audioFilename, settings: settings)
recorder = try AVAudioRecorder(url: filePath, settings: settings)
decibelScale = []
recorder?.record()
recorder?.isMeteringEnabled = true

isRecording = true
} catch {
print("Could not start recording")
Expand All @@ -82,17 +82,9 @@ class AudioRecorder: ObservableObject {
private func stopRecording() {
recorder?.stop()
isRecording = false

let fileManager = FileManager.default
let documentDirectory = fileManager.urls(for: .documentDirectory, in: .userDomainMask)[0]
guard
let directoryContents = try? fileManager.contentsOfDirectory(
at: documentDirectory,
includingPropertiesForKeys: nil
)
else { return }
self.recording = directoryContents.first(where: { $0.absoluteString.contains(filename) })
.map { Recording(url: $0, created: Date(), sample: decibelScale) }
if FileManager.default.fileExists(atPath: filePath.relativePath) {
self.recording = Recording(url: filePath, created: Date(), sample: decibelScale)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ struct RecordButtonStyle: SwiftUI.ButtonStyle {

struct RecordButton_Previews: PreviewProvider {
static var previews: some View {
RecordButton(isRecording: true) {}
.environmentObject(AudioRecorder())
let tempDir = FileManager.default.temporaryDirectory
let path = tempDir.appendingPathComponent("path.m4a")
return RecordButton(isRecording: true) {}
.environmentObject(AudioRecorder(filePath: path))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ public struct SubmitClaimAudioRecordingScreen: View {
url: URL?
) {
audioPlayer = AudioPlayer(url: url)
audioRecorder = AudioRecorder()
let store: SubmitClaimStore = globalPresentableStoreContainer.get()
let path = store.state.claimAudioRecordingPath
audioRecorder = AudioRecorder(filePath: path)

func myFunc(_: URL) {}
self.onSubmit = myFunc
Expand Down
Loading