@@ -8,7 +8,7 @@ private let readMe = """
8
8
9
9
A lightweight wrapper is made for `URLSession`'s API for web sockets so that we can send, \
10
10
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 .
12
12
"""
13
13
14
14
// MARK: - Feature domain
@@ -112,7 +112,7 @@ struct WebSocket: ReducerProtocol {
112
112
. cancellable ( id: WebSocketID . self)
113
113
114
114
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. " ) )
116
116
return . none
117
117
118
118
case . sendResponse( didSucceed: true ) :
@@ -124,6 +124,7 @@ struct WebSocket: ReducerProtocol {
124
124
125
125
case . webSocket( . didOpen) :
126
126
state. connectivityState = . connected
127
+ state. receivedMessages. removeAll ( )
127
128
return . none
128
129
}
129
130
}
@@ -136,41 +137,49 @@ struct WebSocketView: View {
136
137
137
138
var body : some View {
138
139
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
152
149
? " Disconnect "
153
150
: viewStore. connectivityState == . disconnected
154
151
? " Connect "
155
152
: " 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
+ }
158
172
}
159
173
}
160
174
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 " )
163
181
}
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 " ) )
172
182
}
173
- . padding ( )
174
183
. alert ( self . store. scope ( state: \. alert) , dismiss: . alertDismissed)
175
184
. navigationTitle ( " Web Socket " )
176
185
}
@@ -337,7 +346,7 @@ struct WebSocketView_Previews: PreviewProvider {
337
346
NavigationView {
338
347
WebSocketView (
339
348
store: Store (
340
- initialState: WebSocket . State ( receivedMessages: [ " Echo " ] ) ,
349
+ initialState: WebSocket . State ( receivedMessages: [ " Hi " ] ) ,
341
350
reducer: WebSocket ( )
342
351
)
343
352
)
0 commit comments