From ce04baedee0bac9aac2ec3473e0c8e8780c9f86b Mon Sep 17 00:00:00 2001 From: Jackson Harper Date: Tue, 21 Feb 2023 16:33:57 +0800 Subject: [PATCH] When preloading make sure we dont mutate the current document --- .../Services/AudioSession/AudioController.swift | 17 ++++++++++------- .../AudioSession/SpeechSynthesizer.swift | 1 - 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/apple/OmnivoreKit/Sources/Services/AudioSession/AudioController.swift b/apple/OmnivoreKit/Sources/Services/AudioSession/AudioController.swift index b9a5545e7..44403ccdf 100644 --- a/apple/OmnivoreKit/Sources/Services/AudioSession/AudioController.swift +++ b/apple/OmnivoreKit/Sources/Services/AudioSession/AudioController.swift @@ -162,7 +162,7 @@ } public func downloadForOffline(itemID: String) async -> Bool { - if let document = try? await downloadSpeechFile(itemID: itemID, priority: .low) { + if let document = try? await getSpeechFile(itemID: itemID, priority: .low) { let synthesizer = SpeechSynthesizer(appEnvironment: dataService.appEnvironment, networker: dataService.networker, document: document, speechAuthHeader: speechAuthHeader) for item in synthesizer.createPlayerItems(from: 0) { do { @@ -434,7 +434,7 @@ if let itemID = itemAudioProperties?.itemID { Task { - let document = try? await self.downloadSpeechFile(itemID: itemID, priority: .high) + let document = try? await self.getSpeechFile(itemID: itemID, priority: .high) DispatchQueue.main.async { if let document = document { @@ -559,7 +559,7 @@ if let itemID = itemAudioProperties?.itemID { Task { - let document = try? await downloadSpeechFile(itemID: itemID, priority: .high) + let document = try? await getSpeechFile(itemID: itemID, priority: .high) DispatchQueue.main.async { self.setTextItems() @@ -879,18 +879,21 @@ let str = String(decoding: data, as: UTF8.self) print("result speech file: ", str) - document = try? JSONDecoder().decode(SpeechDocument.self, from: data) - - // Cache the file - if it exists - if let document = document { + if let document = try? JSONDecoder().decode(SpeechDocument.self, from: data) { do { try? FileManager.default.createDirectory(at: document.audioDirectory, withIntermediateDirectories: true) try data.write(to: speechFileUrl) + return document } catch { print("error writing file", error) } } + return nil + } + + func getSpeechFile(itemID: String, priority: DownloadPriority) async throws -> SpeechDocument? { + document = try await downloadSpeechFile(itemID: itemID, priority: priority) return document } diff --git a/apple/OmnivoreKit/Sources/Services/AudioSession/SpeechSynthesizer.swift b/apple/OmnivoreKit/Sources/Services/AudioSession/SpeechSynthesizer.swift index c65aa2514..f5f6b2b61 100644 --- a/apple/OmnivoreKit/Sources/Services/AudioSession/SpeechSynthesizer.swift +++ b/apple/OmnivoreKit/Sources/Services/AudioSession/SpeechSynthesizer.swift @@ -129,7 +129,6 @@ struct SpeechSynthesizer { func createPlayerItems(from: Int) -> [SpeechItem] { var result: [SpeechItem] = [] - // TODO: JACKSON for idx in from ..< document.utterances.count { let utterance = document.utterances[idx] let voiceStr = utterance.voice ?? document.defaultVoice