Skip to content

Commit 5060914

Browse files
Jager-yootgrapperon
authored andcommitted
Improve the layout of Web Socket case study (#1529)
* Improve layout of Web Socket case study * Update Examples/CaseStudies/SwiftUICaseStudies/02-Effects-WebSocket.swift Co-authored-by: Thomas Grapperon <[email protected]> Co-authored-by: Thomas Grapperon <[email protected]> (cherry picked from commit c1302e5e207052a44d3882aaf8a3c0a9ac6cece6)
1 parent 8701a55 commit 5060914

File tree

2 files changed

+39
-30
lines changed

2 files changed

+39
-30
lines changed

Examples/CaseStudies/SwiftUICaseStudies/02-Effects-WebSocket.swift

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ private let readMe = """
88
99
A lightweight wrapper is made for `URLSession`'s API for web sockets so that we can send, \
1010
receive and ping a socket endpoint. To test, connect to the socket server, and then send a \
11-
message. The socket server should immediately reply with the exact message you send it.
11+
message. The socket server should immediately reply with the exact message you sent in.
1212
"""
1313

1414
// MARK: - Feature domain
@@ -112,7 +112,7 @@ struct WebSocket: ReducerProtocol {
112112
.cancellable(id: WebSocketID.self)
113113

114114
case .sendResponse(didSucceed: false):
115-
state.alert = AlertState(title: TextState("Could not send socket message. Try again."))
115+
state.alert = AlertState(title: TextState("Could not send socket message. Connect to the server first, and try again."))
116116
return .none
117117

118118
case .sendResponse(didSucceed: true):
@@ -124,6 +124,7 @@ struct WebSocket: ReducerProtocol {
124124

125125
case .webSocket(.didOpen):
126126
state.connectivityState = .connected
127+
state.receivedMessages.removeAll()
127128
return .none
128129
}
129130
}
@@ -136,41 +137,49 @@ struct WebSocketView: View {
136137

137138
var body: some View {
138139
WithViewStore(self.store, observe: { $0 }) { viewStore in
139-
VStack(alignment: .leading) {
140-
AboutView(readMe: readMe)
141-
.padding(.bottom)
142-
143-
HStack {
144-
TextField(
145-
"Message to send",
146-
text: viewStore.binding(
147-
get: \.messageToSend, send: WebSocket.Action.messageToSendChanged)
148-
)
149-
150-
Button(
151-
viewStore.connectivityState == .connected
140+
Form {
141+
Section {
142+
AboutView(readMe: readMe)
143+
}
144+
145+
Section {
146+
VStack(alignment: .leading) {
147+
Button(
148+
viewStore.connectivityState == .connected
152149
? "Disconnect"
153150
: viewStore.connectivityState == .disconnected
154151
? "Connect"
155152
: "Connecting..."
156-
) {
157-
viewStore.send(.connectButtonTapped)
153+
) {
154+
viewStore.send(.connectButtonTapped)
155+
}
156+
.buttonStyle(.bordered)
157+
.tint(viewStore.connectivityState == .connected ? .red : .green)
158+
159+
HStack {
160+
TextField(
161+
"Type message here",
162+
text: viewStore.binding(
163+
get: \.messageToSend, send: WebSocket.Action.messageToSendChanged)
164+
)
165+
.textFieldStyle(.roundedBorder)
166+
167+
Button("Send") {
168+
viewStore.send(.sendButtonTapped)
169+
}
170+
.buttonStyle(.borderless)
171+
}
158172
}
159173
}
160174

161-
Button("Send message") {
162-
viewStore.send(.sendButtonTapped)
175+
Section {
176+
Text("Status: \(viewStore.connectivityState.rawValue)")
177+
.foregroundStyle(.secondary)
178+
Text(viewStore.receivedMessages.reversed().joined(separator: "\n"))
179+
} header: {
180+
Text("Received messages")
163181
}
164-
165-
Spacer()
166-
167-
Text("Status: \(viewStore.connectivityState.rawValue)")
168-
.foregroundStyle(.secondary)
169-
Text("Received messages:")
170-
.foregroundStyle(.secondary)
171-
Text(viewStore.receivedMessages.joined(separator: "\n"))
172182
}
173-
.padding()
174183
.alert(self.store.scope(state: \.alert), dismiss: .alertDismissed)
175184
.navigationTitle("Web Socket")
176185
}
@@ -337,7 +346,7 @@ struct WebSocketView_Previews: PreviewProvider {
337346
NavigationView {
338347
WebSocketView(
339348
store: Store(
340-
initialState: WebSocket.State(receivedMessages: ["Echo"]),
349+
initialState: WebSocket.State(receivedMessages: ["Hi"]),
341350
reducer: WebSocket()
342351
)
343352
)

Examples/CaseStudies/SwiftUICaseStudiesTests/02-Effects-WebSocketTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ final class WebSocketTests: XCTestCase {
9292
$0.messageToSend = ""
9393
}
9494
await store.receive(.sendResponse(didSucceed: false)) {
95-
$0.alert = AlertState(title: TextState("Could not send socket message. Try again."))
95+
$0.alert = AlertState(title: TextState("Could not send socket message. Connect to the server first, and try again."))
9696
}
9797

9898
// Disconnect from the socket

0 commit comments

Comments
 (0)