33 lines
888 B
Swift
33 lines
888 B
Swift
import Foundation
|
|
import HelderCore
|
|
import HelderServices
|
|
import Observation
|
|
|
|
/// Read aloud: whether Helder is reading, for the title bar's Stop button.
|
|
@Observable
|
|
@MainActor
|
|
public final class ReadAloudStore {
|
|
public private(set) var isReading = false
|
|
|
|
@ObservationIgnored private let speech: SpeechServicing
|
|
|
|
public init(speech: SpeechServicing) {
|
|
self.speech = speech
|
|
speech.onFinish = { [weak self] in self?.isReading = false }
|
|
}
|
|
|
|
/// Starts reading, replacing what is being read; false for blank text, which reads nothing.
|
|
@discardableResult
|
|
public func read(_ text: String) -> Bool {
|
|
guard !text.allSatisfy(\.isWhitespace) else { return false }
|
|
speech.speak(MarkdownSpeech.spoken(text))
|
|
isReading = true
|
|
return true
|
|
}
|
|
|
|
public func stop() {
|
|
speech.stop()
|
|
isReading = false
|
|
}
|
|
}
|