Skip to content

Commit f1e6872

Browse files
authored
Simplify speech demo (#1218)
* Simplify speech demo * wip * fix flakey test * increase timeout * wip
1 parent b6a9809 commit f1e6872

File tree

3 files changed

+21
-22
lines changed

3 files changed

+21
-22
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.eraseToEffect() }
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.send(result)
87-
store.receive(.speech(.success(result))) {
85+
self.recognitionTaskSubject.send(firstResult)
86+
store.receive(.speech(.success("Hello"))) {
8887
$0.transcribedText = "Hello"
8988
}
9089

91-
self.recognitionTaskSubject.send(finalResult)
92-
store.receive(.speech(.success(finalResult))) {
90+
self.recognitionTaskSubject.send(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 & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,11 +153,11 @@ final class RuntimeWarningTests: XCTestCase {
153153
switch action {
154154
case .tap:
155155
return .run { subscriber in
156-
DispatchQueue(label: "background").async {
156+
Thread.detachNewThread {
157+
XCTAssertFalse(Thread.isMainThread, "Effect should send on non-main thread.")
157158
subscriber.send(.response)
158159
}
159-
return AnyCancellable {
160-
}
160+
return AnyCancellable {}
161161
}
162162
case .response:
163163
return .none
@@ -166,7 +166,7 @@ final class RuntimeWarningTests: XCTestCase {
166166
environment: ()
167167
)
168168
ViewStore(store).send(.tap)
169-
_ = XCTWaiter.wait(for: [.init()], timeout: 2)
169+
_ = XCTWaiter.wait(for: [.init()], timeout: 4)
170170
}
171171

172172
func testBindingUnhandledAction() {

0 commit comments

Comments
 (0)