Skip to content

Commit 6bab4bf

Browse files
committed
More clean up in speech demo (#1217)
* Status is unusued * clean up * wip
1 parent c4ea505 commit 6bab4bf

File tree

3 files changed

+44
-64
lines changed

3 files changed

+44
-64
lines changed

Examples/SpeechRecognition/SpeechRecognition/SpeechClient/Live.swift

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,6 @@ extension SpeechClient {
2424
},
2525
startTask: { request in
2626
Effect { subscriber, lifetime in
27-
let speechRecognizer = SFSpeechRecognizer(locale: Locale(identifier: "en-US"))!
28-
let cancellable = AnyDisposable {
29-
audioEngine?.stop()
30-
audioEngine?.inputNode.removeTap(onBus: 0)
31-
recognitionTask?.cancel()
32-
_ = speechRecognizer
33-
}
34-
35-
lifetime += cancellable
36-
3727
audioEngine = AVAudioEngine()
3828
let audioSession = AVAudioSession.sharedInstance()
3929
do {
@@ -44,6 +34,7 @@ extension SpeechClient {
4434
return
4535
}
4636

37+
let speechRecognizer = SFSpeechRecognizer(locale: Locale(identifier: "en-US"))!
4738
recognitionTask = speechRecognizer.recognitionTask(with: request) { result, error in
4839
switch (result, error) {
4940
case let (.some(result), _):
@@ -55,6 +46,13 @@ extension SpeechClient {
5546
}
5647
}
5748

49+
let cancellable = AnyDisposable {
50+
audioEngine?.stop()
51+
audioEngine?.inputNode.removeTap(onBus: 0)
52+
recognitionTask?.cancel()
53+
}
54+
lifetime += cancellable
55+
5856
audioEngine?.inputNode.installTap(
5957
onBus: 0,
6058
bufferSize: 1024,

Examples/SpeechRecognition/SpeechRecognition/SpeechRecognition.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ private let readMe = """
1212
struct AppState: Equatable {
1313
var alert: AlertState<AppAction>?
1414
var isRecording = false
15-
var speechRecognizerAuthorizationStatus = SFSpeechRecognizerAuthorizationStatus.notDetermined
1615
var transcribedText = ""
1716
}
1817

@@ -68,7 +67,6 @@ let appReducer = Reducer<AppState, AppAction, AppEnvironment> { state, action, e
6867

6968
case let .speechRecognizerAuthorizationStatusResponse(status):
7069
state.isRecording = status == .authorized
71-
state.speechRecognizerAuthorizationStatus = status
7270

7371
switch status {
7472
case .notDetermined:

Examples/SpeechRecognition/SpeechRecognitionTests/SpeechRecognitionTests.swift

Lines changed: 36 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,15 @@ class SpeechRecognitionTests: XCTestCase {
88
let recognitionTaskSubject = Signal<SpeechRecognitionResult, SpeechClient.Error>.pipe()
99

1010
func testDenyAuthorization() {
11-
var speechClient = SpeechClient.unimplemented
12-
speechClient.requestAuthorization = { Effect(value: .denied) }
13-
1411
let store = TestStore(
1512
initialState: AppState(),
1613
reducer: appReducer,
17-
environment: AppEnvironment(
18-
mainQueue: ImmediateScheduler(),
19-
speechClient: speechClient
20-
)
14+
environment: .unimplemented
2115
)
2216

17+
store.environment.mainQueue = ImmediateScheduler()
18+
store.environment.speechClient.requestAuthorization = { Effect(value: .denied) }
19+
2320
store.send(.recordButtonTapped) {
2421
$0.isRecording = true
2522
}
@@ -32,50 +29,42 @@ class SpeechRecognitionTests: XCTestCase {
3229
)
3330
)
3431
$0.isRecording = false
35-
$0.speechRecognizerAuthorizationStatus = .denied
3632
}
3733
}
3834

3935
func testRestrictedAuthorization() {
40-
var speechClient = SpeechClient.unimplemented
41-
speechClient.requestAuthorization = { Effect(value: .restricted) }
42-
4336
let store = TestStore(
4437
initialState: AppState(),
4538
reducer: appReducer,
46-
environment: AppEnvironment(
47-
mainQueue: ImmediateScheduler(),
48-
speechClient: speechClient
49-
)
39+
environment: .unimplemented
5040
)
5141

42+
store.environment.mainQueue = ImmediateScheduler()
43+
store.environment.speechClient.requestAuthorization = { Effect(value: .restricted) }
44+
5245
store.send(.recordButtonTapped) {
5346
$0.isRecording = true
5447
}
5548
store.receive(.speechRecognizerAuthorizationStatusResponse(.restricted)) {
5649
$0.alert = AlertState(title: TextState("Your device does not allow speech recognition."))
5750
$0.isRecording = false
58-
$0.speechRecognizerAuthorizationStatus = .restricted
5951
}
6052
}
6153

6254
func testAllowAndRecord() {
63-
var speechClient = SpeechClient.unimplemented
64-
speechClient.finishTask = {
65-
.fireAndForget { self.recognitionTaskSubject.input.sendCompleted() }
66-
}
67-
speechClient.startTask = { _ in self.recognitionTaskSubject.output.producer }
68-
speechClient.requestAuthorization = { Effect(value: .authorized) }
69-
7055
let store = TestStore(
7156
initialState: AppState(),
7257
reducer: appReducer,
73-
environment: AppEnvironment(
74-
mainQueue: ImmediateScheduler(),
75-
speechClient: speechClient
76-
)
58+
environment: .unimplemented
7759
)
7860

61+
store.environment.mainQueue = ImmediateScheduler()
62+
store.environment.speechClient.finishTask = {
63+
.fireAndForget { self.recognitionTaskSubject.input.sendCompleted() }
64+
}
65+
store.environment.speechClient.requestAuthorization = { Effect(value: .authorized) }
66+
store.environment.speechClient.startTask = { _ in self.recognitionTaskSubject.output.producer }
67+
7968
let result = SpeechRecognitionResult(
8069
bestTranscription: Transcription(
8170
formattedString: "Hello",
@@ -92,9 +81,7 @@ class SpeechRecognitionTests: XCTestCase {
9281
$0.isRecording = true
9382
}
9483

95-
store.receive(.speechRecognizerAuthorizationStatusResponse(.authorized)) {
96-
$0.speechRecognizerAuthorizationStatus = .authorized
97-
}
84+
store.receive(.speechRecognizerAuthorizationStatusResponse(.authorized))
9885

9986
self.recognitionTaskSubject.input.send(value: result)
10087
store.receive(.speech(.success(result))) {
@@ -108,26 +95,21 @@ class SpeechRecognitionTests: XCTestCase {
10895
}
10996

11097
func testAudioSessionFailure() {
111-
var speechClient = SpeechClient.unimplemented
112-
speechClient.startTask = { _ in self.recognitionTaskSubject.output.producer }
113-
speechClient.requestAuthorization = { Effect(value: .authorized) }
114-
11598
let store = TestStore(
11699
initialState: AppState(),
117100
reducer: appReducer,
118-
environment: AppEnvironment(
119-
mainQueue: ImmediateScheduler(),
120-
speechClient: speechClient
121-
)
101+
environment: .unimplemented
122102
)
123103

104+
store.environment.mainQueue = ImmediateScheduler()
105+
store.environment.speechClient.startTask = { _ in self.recognitionTaskSubject.output.producer }
106+
store.environment.speechClient.requestAuthorization = { Effect(value: .authorized) }
107+
124108
store.send(.recordButtonTapped) {
125109
$0.isRecording = true
126110
}
127111

128-
store.receive(.speechRecognizerAuthorizationStatusResponse(.authorized)) {
129-
$0.speechRecognizerAuthorizationStatus = .authorized
130-
}
112+
store.receive(.speechRecognizerAuthorizationStatusResponse(.authorized))
131113

132114
self.recognitionTaskSubject.input.send(error: .couldntConfigureAudioSession)
133115
store.receive(.speech(.failure(.couldntConfigureAudioSession))) {
@@ -138,26 +120,21 @@ class SpeechRecognitionTests: XCTestCase {
138120
}
139121

140122
func testAudioEngineFailure() {
141-
var speechClient = SpeechClient.unimplemented
142-
speechClient.startTask = { _ in self.recognitionTaskSubject.output.producer }
143-
speechClient.requestAuthorization = { Effect(value: .authorized) }
144-
145123
let store = TestStore(
146124
initialState: AppState(),
147125
reducer: appReducer,
148-
environment: AppEnvironment(
149-
mainQueue: ImmediateScheduler(),
150-
speechClient: speechClient
151-
)
126+
environment: .unimplemented
152127
)
153128

129+
store.environment.mainQueue = ImmediateScheduler()
130+
store.environment.speechClient.startTask = { _ in self.recognitionTaskSubject.output.producer }
131+
store.environment.speechClient.requestAuthorization = { Effect(value: .authorized) }
132+
154133
store.send(.recordButtonTapped) {
155134
$0.isRecording = true
156135
}
157136

158-
store.receive(.speechRecognizerAuthorizationStatusResponse(.authorized)) {
159-
$0.speechRecognizerAuthorizationStatus = .authorized
160-
}
137+
store.receive(.speechRecognizerAuthorizationStatusResponse(.authorized))
161138

162139
self.recognitionTaskSubject.input.send(error: .couldntStartAudioEngine)
163140
store.receive(.speech(.failure(.couldntStartAudioEngine))) {
@@ -167,3 +144,10 @@ class SpeechRecognitionTests: XCTestCase {
167144
self.recognitionTaskSubject.input.sendCompleted()
168145
}
169146
}
147+
148+
extension AppEnvironment {
149+
static let unimplemented = Self(
150+
mainQueue: UnimplementedScheduler(),
151+
speechClient: .unimplemented
152+
)
153+
}

0 commit comments

Comments
 (0)