Skip to content

Commit 4fbc13d

Browse files
committed
Simplify speech demo (#1218)
* Simplify speech demo * wip * fix flakey test * increase timeout * wip
1 parent 7fb1261 commit 4fbc13d

File tree

3 files changed

+21
-21
lines changed

3 files changed

+21
-21
lines changed

Examples/SpeechRecognition/SpeechRecognition/SpeechRecognition.swift

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ struct AppState: Equatable {
1818
enum AppAction: Equatable {
1919
case dismissAuthorizationStateAlert
2020
case recordButtonTapped
21-
case speech(Result<SpeechRecognitionResult, SpeechClient.Error>)
21+
case speech(Result<String, SpeechClient.Error>)
2222
case speechRecognizerAuthorizationStatusResponse(SFSpeechRecognizerAuthorizationStatus)
2323
}
2424

@@ -49,14 +49,9 @@ let appReducer = Reducer<AppState, AppAction, AppEnvironment> { state, action, e
4949
.fireAndForget()
5050
}
5151

52-
case let .speech(.success(result)):
53-
state.transcribedText = result.bestTranscription.formattedString
54-
if result.isFinal {
55-
return environment.speechClient.finishTask()
56-
.fireAndForget()
57-
} else {
58-
return .none
59-
}
52+
case let .speech(.success(transcribedText)):
53+
state.transcribedText = transcribedText
54+
return .none
6055

6156
case let .speech(.failure(error)):
6257
state.alert = AlertState(
@@ -92,6 +87,7 @@ let appReducer = Reducer<AppState, AppAction, AppEnvironment> { state, action, e
9287
request.shouldReportPartialResults = true
9388
request.requiresOnDeviceRecognition = false
9489
return environment.speechClient.startTask(request)
90+
.map(\.bestTranscription.formattedString)
9591
.animation()
9692
.catchToEffect(AppAction.speech)
9793

@@ -100,7 +96,7 @@ let appReducer = Reducer<AppState, AppAction, AppEnvironment> { state, action, e
10096
}
10197
}
10298
}
103-
.debug(actionFormat: .labelsOnly)
99+
.debug()
104100

105101
struct AuthorizationStateAlert: Equatable, Identifiable {
106102
var title: String

Examples/SpeechRecognition/SpeechRecognitionTests/SpeechRecognitionTests.swift

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,33 +65,36 @@ class SpeechRecognitionTests: XCTestCase {
6565
store.environment.speechClient.requestAuthorization = { Effect(value: .authorized) }
6666
store.environment.speechClient.startTask = { _ in self.recognitionTaskSubject.output.producer }
6767

68-
let result = SpeechRecognitionResult(
68+
let firstResult = SpeechRecognitionResult(
6969
bestTranscription: Transcription(
7070
formattedString: "Hello",
7171
segments: []
7272
),
7373
isFinal: false,
7474
transcriptions: []
7575
)
76-
var finalResult = result
77-
finalResult.bestTranscription.formattedString = "Hello world"
78-
finalResult.isFinal = true
76+
var secondResult = firstResult
77+
secondResult.bestTranscription.formattedString = "Hello world"
7978

8079
store.send(.recordButtonTapped) {
8180
$0.isRecording = true
8281
}
8382

8483
store.receive(.speechRecognizerAuthorizationStatusResponse(.authorized))
8584

86-
self.recognitionTaskSubject.input.send(value: result)
87-
store.receive(.speech(.success(result))) {
85+
self.recognitionTaskSubject.input.send(value: firstResult)
86+
store.receive(.speech(.success("Hello"))) {
8887
$0.transcribedText = "Hello"
8988
}
9089

91-
self.recognitionTaskSubject.input.send(value: finalResult)
92-
store.receive(.speech(.success(finalResult))) {
90+
self.recognitionTaskSubject.input.send(value: secondResult)
91+
store.receive(.speech(.success("Hello world"))) {
9392
$0.transcribedText = "Hello world"
9493
}
94+
95+
store.send(.recordButtonTapped) {
96+
$0.isRecording = false
97+
}
9598
}
9699

97100
func testAudioSessionFailure() {

Tests/ComposableArchitectureTests/RuntimeWarningTests.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ final class RuntimeWarningTests: XCTestCase {
144144
(including all of its scopes and derived view stores) must be done on the main thread.
145145
"""
146146
]
147-
.contains($0.compactDescription)
147+
.contains($0.compactDescription)
148148
}
149149

150150
enum Action { case tap, response }
@@ -154,7 +154,8 @@ final class RuntimeWarningTests: XCTestCase {
154154
switch action {
155155
case .tap:
156156
return Effect { subscriber, lifetime in
157-
DispatchQueue(label: "background").async {
157+
Thread.detachNewThread {
158+
XCTAssertFalse(Thread.isMainThread, "Effect should send on non-main thread.")
158159
subscriber.send(value: .response)
159160
}
160161
}
@@ -165,7 +166,7 @@ final class RuntimeWarningTests: XCTestCase {
165166
environment: ()
166167
)
167168
ViewStore(store).send(.tap)
168-
_ = XCTWaiter.wait(for: [.init()], timeout: 2)
169+
_ = XCTWaiter.wait(for: [.init()], timeout: 4)
169170
}
170171

171172
func testBindingUnhandledAction() {

0 commit comments

Comments
 (0)