-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Change the voice while changing the settings
- Loading branch information
Showing
4 changed files
with
84 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,47 @@ | ||
import Foundation | ||
import AVFoundation | ||
|
||
extension NSNotification.Name { | ||
static let didChangeRate = Notification.Name("com.onato.typereader.didChangeRate") | ||
static let didChangeVoice = Notification.Name("com.onato.typereader.didChangeVoice") | ||
} | ||
|
||
protocol Notifier { | ||
func post(name: Notification.Name) | ||
} | ||
extension NotificationCenter: Notifier { | ||
func post(name: Notification.Name) { | ||
post(name: name, object: nil) | ||
} | ||
} | ||
|
||
class SpeechSettingsViewModel: ObservableObject { | ||
@Published var selectedVoiceIdentifier: String | ||
|
||
@Published var speechRate: Float | ||
@Published var selectedVoiceIdentifier: String { | ||
didSet { | ||
settings.selectedVoiceIdentifier = selectedVoiceIdentifier | ||
notifier.post(name: .didChangeVoice) | ||
} | ||
} | ||
|
||
@Published var speechRate: Float { | ||
didSet { | ||
settings.speechRate = speechRate | ||
} | ||
} | ||
|
||
var voices: [AVSpeechSynthesisVoice] = [] | ||
let notifier: Notifier | ||
var settings: AppSettings | ||
|
||
init(settings: AppSettings = UserDefaultsSettings()) { | ||
selectedVoiceIdentifier = settings.selectedVoiceIdentifier | ||
init(settings: AppSettings = UserDefaultsSettings(), notifier: Notifier = NotificationCenter.default) { | ||
selectedVoiceIdentifier = settings.selectedVoiceIdentifier ?? "" | ||
speechRate = settings.speechRate | ||
voices = AVSpeechSynthesisVoice.speechVoices() | ||
self.notifier = notifier | ||
self.settings = settings | ||
} | ||
|
||
func releasedSlider() { | ||
notifier.post(name: .didChangeRate) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters