ReadKeep/readeck/UI/SpeechPlayer/SpeechPlayerViewModel.swift
Ilyas Hallak 9b89e58115 Refactor TTS system with Combine and VoiceManager
- Replace @MainActor with Combine framework for TTS functionality
- Create VoiceManager class for voice selection and caching
- Add UserDefaults persistence for selected voice
- Optimize performance with voice caching and immediate UI updates
- Remove @MainActor from TTS-related Use Cases
- Add proper pause/resume delegate methods
- Improve reactive UI updates with @StateObject
- Clean up code and remove unnecessary comments
2025-07-09 23:15:23 +02:00

58 lines
1.5 KiB
Swift

import Foundation
import Combine
class SpeechPlayerViewModel: ObservableObject {
private let ttsManager: TTSManager
private let speechQueue: SpeechQueue
private var cancellables = Set<AnyCancellable>()
@Published var isSpeaking: Bool = false
@Published var currentText: String = ""
@Published var queueCount: Int = 0
@Published var queueItems: [String] = []
@Published var hasItems: Bool = false
init(ttsManager: TTSManager = .shared, speechQueue: SpeechQueue = .shared) {
self.ttsManager = ttsManager
self.speechQueue = speechQueue
setupBindings()
}
private func setupBindings() {
// TTSManager bindings
ttsManager.$isSpeaking
.assign(to: \.isSpeaking, on: self)
.store(in: &cancellables)
ttsManager.$currentUtterance
.assign(to: \.currentText, on: self)
.store(in: &cancellables)
// SpeechQueue bindings
speechQueue.$queueItems
.assign(to: \.queueItems, on: self)
.store(in: &cancellables)
speechQueue.$queueItems
.map { $0.count }
.assign(to: \.queueCount, on: self)
.store(in: &cancellables)
speechQueue.$hasItems
.assign(to: \.hasItems, on: self)
.store(in: &cancellables)
}
func pause() {
ttsManager.pause()
}
func resume() {
ttsManager.resume()
}
func stop() {
speechQueue.clear()
}
}