Skip to content

Commit

Permalink
Merge pull request #1125 from HedvigInsurance/fix/record-audio-refactor
Browse files Browse the repository at this point in the history
Claim recordings naming and placing
  • Loading branch information
sladan-hedvig authored Dec 1, 2023
2 parents 1c45a36 + 23d9f3a commit 1cd19ab
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 24 deletions.
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

0 comments on commit 1cd19ab

Please sign in to comment.