From 95b6d33c27326aab94d126bd51a17470ac9601ec Mon Sep 17 00:00:00 2001 From: Nicolas Mauri Date: Tue, 24 Oct 2023 16:32:34 +0200 Subject: [PATCH] Code cleanup --- .../Audio/Recorder/AudioRecorder.swift | 29 ++++++++++--------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/ElementX/Sources/Services/Audio/Recorder/AudioRecorder.swift b/ElementX/Sources/Services/Audio/Recorder/AudioRecorder.swift index 4a27677595..90a851e679 100644 --- a/ElementX/Sources/Services/Audio/Recorder/AudioRecorder.swift +++ b/ElementX/Sources/Services/Audio/Recorder/AudioRecorder.swift @@ -50,21 +50,14 @@ class AudioRecorder: NSObject, AudioRecorderProtocol, AVAudioRecorderDelegate { guard await requestRecordPermission() else { return .failure(.recordPermissionNotGranted) } - return await withCheckedContinuation { continuation in - startRecording(with: recordID) { [weak self] result in - guard let self else { - continuation.resume(returning: .failure(.recordingCancelled)) - return - } - switch result { - case .success: - actionsSubject.send(.didStartRecording) - case .failure(let error): - actionsSubject.send(.didFailWithError(error: error)) - } - continuation.resume(returning: result) - } + let result = await startRecording(with: recordID) + switch result { + case .success: + actionsSubject.send(.didStartRecording) + case .failure(let error): + actionsSubject.send(.didFailWithError(error: error)) } + return result } func stopRecording() async { @@ -127,6 +120,14 @@ class AudioRecorder: NSObject, AudioRecorderProtocol, AVAudioRecorderDelegate { // MARK: - Private + private func startRecording(with recordID: AudioRecordingIdentifier) async -> Result { + await withCheckedContinuation { continuation in + startRecording(with: recordID) { result in + continuation.resume(returning: result) + } + } + } + private func startRecording(with recordID: AudioRecordingIdentifier, completion: @escaping (Result) -> Void) { dispatchQueue.async { [weak self] in guard let self, !self.stopped else {