Commit 058782e5 authored by Evan W. Patton's avatar Evan W. Patton Committed by Evan W. Patton

Fix lack of lifecycle management in TextToSpeech on iOS

Change-Id: Ib8732739e437f3aa32f6203589cc9e6e9fb30228
parent 96ff61f0
......@@ -72,6 +72,7 @@ public class ViewController: UINavigationController, UITextFieldDelegate {
SCMInterpreter.shared.protect(self)
ViewController.controller = self
NotificationCenter.default.addObserver(self, selector: #selector(settingsChanged(_:)), name: UserDefaults.didChangeNotification, object: nil)
self.delegate = self
}
@objc func settingsChanged(_ sender: AnyObject?) {
......@@ -386,3 +387,20 @@ public class ViewController: UINavigationController, UITextFieldDelegate {
}
}
}
extension ViewController: UINavigationControllerDelegate {
public func navigationController(_ navigationController: UINavigationController,
animationControllerFor operation: UINavigationController.Operation,
from fromVC: UIViewController,
to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {
guard let oldForm = fromVC as? Form, let newForm = toVC as? Form else {
return nil
}
oldForm.onPause()
if operation == .pop {
oldForm.onDestroy()
}
newForm.onResume()
return nil
}
}
......@@ -167,6 +167,10 @@ let kMinimumToastWait = 10.0
view.accessibilityIdentifier = String(describing: type(of: self))
}
open func add(_ component: NonvisibleComponent) {
_components.append(component)
}
open func add(_ component: ViewComponent) {
_components.append(component)
_linearView.addItem(LinearViewItem(component.view))
......
......@@ -10,6 +10,8 @@ open class NonvisibleComponent: NSObject, Component {
@objc public init(_ container: ComponentContainer) {
self._form = container.form
super.init()
_form?.add(self)
}
open func copy(with zone: NSZone? = nil) -> Any {
......
......@@ -163,15 +163,17 @@ open class TextToSpeech: NonvisibleComponent, AVSpeechSynthesizerDelegate {
// MARK: Methods
@objc open func Speak(_ message: String) throws {
BeforeSpeaking()
try AVAudioSession.sharedInstance().setCategory(AVAudioSession.Category.playback)
try AVAudioSession.sharedInstance().setActive(true)
DispatchQueue.main.async {
self.BeforeSpeaking()
let utterance = AVSpeechUtterance(string: message)
utterance.pitchMultiplier = _pitch
utterance.rate = _speechRate
utterance.voice = _voice
_tts.stopSpeaking(at: AVSpeechBoundary.immediate)
_tts.speak(utterance)
utterance.pitchMultiplier = self._pitch
utterance.rate = self._speechRate
utterance.voice = self._voice
self._tts.stopSpeaking(at: AVSpeechBoundary.immediate)
self._tts.speak(utterance)
}
}
@objc open func Stop() {
......@@ -214,3 +216,23 @@ open class TextToSpeech: NonvisibleComponent, AVSpeechSynthesizerDelegate {
}
}
}
extension TextToSpeech: LifecycleDelegate {
public func onResume() {
if _tts.isPaused {
_tts.continueSpeaking()
}
}
public func onPause() {
_tts.pauseSpeaking(at: .immediate)
}
public func onDestroy() {
Stop()
}
public func onDelete() {
Stop()
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment