@@ -7,7 +7,7 @@ private let readMe = """
7
7
8
8
A lightweight wrapper is made for `URLSession`'s API for web sockets so that we can send, \
9
9
receive and ping a socket endpoint. To test, connect to the socket server, and then send a \
10
- message. The socket server should immediately reply with the exact message you send it .
10
+ message. The socket server should immediately reply with the exact message you sent in .
11
11
"""
12
12
13
13
// MARK: - Feature domain
@@ -111,7 +111,7 @@ struct WebSocket: ReducerProtocol {
111
111
. cancellable ( id: WebSocketID . self)
112
112
113
113
case . sendResponse( didSucceed: false ) :
114
- state. alert = AlertState ( title: TextState ( " Could not send socket message. Try again. " ) )
114
+ state. alert = AlertState ( title: TextState ( " Could not send socket message. Connect to the server first, and try again. " ) )
115
115
return . none
116
116
117
117
case . sendResponse( didSucceed: true ) :
@@ -123,6 +123,7 @@ struct WebSocket: ReducerProtocol {
123
123
124
124
case . webSocket( . didOpen) :
125
125
state. connectivityState = . connected
126
+ state. receivedMessages. removeAll ( )
126
127
return . none
127
128
}
128
129
}
@@ -135,41 +136,49 @@ struct WebSocketView: View {
135
136
136
137
var body : some View {
137
138
WithViewStore ( self . store, observe: { $0 } ) { viewStore in
138
- VStack ( alignment: . leading) {
139
- AboutView ( readMe: readMe)
140
- . padding ( . bottom)
141
-
142
- HStack {
143
- TextField (
144
- " Message to send " ,
145
- text: viewStore. binding (
146
- get: \. messageToSend, send: WebSocket . Action. messageToSendChanged)
147
- )
148
-
149
- Button (
150
- viewStore. connectivityState == . connected
139
+ Form {
140
+ Section {
141
+ AboutView ( readMe: readMe)
142
+ }
143
+
144
+ Section {
145
+ VStack ( alignment: . leading) {
146
+ Button (
147
+ viewStore. connectivityState == . connected
151
148
? " Disconnect "
152
149
: viewStore. connectivityState == . disconnected
153
150
? " Connect "
154
151
: " Connecting... "
155
- ) {
156
- viewStore. send ( . connectButtonTapped)
152
+ ) {
153
+ viewStore. send ( . connectButtonTapped)
154
+ }
155
+ . buttonStyle ( . bordered)
156
+ . tint ( viewStore. connectivityState == . connected ? . red : . green)
157
+
158
+ HStack {
159
+ TextField (
160
+ " Type message here " ,
161
+ text: viewStore. binding (
162
+ get: \. messageToSend, send: WebSocket . Action. messageToSendChanged)
163
+ )
164
+ . textFieldStyle ( . roundedBorder)
165
+
166
+ Button ( " Send " ) {
167
+ viewStore. send ( . sendButtonTapped)
168
+ }
169
+ . buttonStyle ( . borderless)
170
+ }
157
171
}
158
172
}
159
173
160
- Button ( " Send message " ) {
161
- viewStore. send ( . sendButtonTapped)
174
+ Section {
175
+ Text ( " Status: \( viewStore. connectivityState. rawValue) " )
176
+ . foregroundStyle ( . secondary)
177
+ Text ( viewStore. receivedMessages. reversed ( ) . joined ( separator: " \n " ) )
178
+ } header: {
179
+ Text ( " Received messages " )
162
180
}
163
-
164
- Spacer ( )
165
-
166
- Text ( " Status: \( viewStore. connectivityState. rawValue) " )
167
- . foregroundStyle ( . secondary)
168
- Text ( " Received messages: " )
169
- . foregroundStyle ( . secondary)
170
- Text ( viewStore. receivedMessages. joined ( separator: " \n " ) )
171
181
}
172
- . padding ( )
173
182
. alert ( self . store. scope ( state: \. alert) , dismiss: . alertDismissed)
174
183
. navigationTitle ( " Web Socket " )
175
184
}
@@ -336,7 +345,7 @@ struct WebSocketView_Previews: PreviewProvider {
336
345
NavigationView {
337
346
WebSocketView (
338
347
store: Store (
339
- initialState: WebSocket . State ( receivedMessages: [ " Echo " ] ) ,
348
+ initialState: WebSocket . State ( receivedMessages: [ " Hi " ] ) ,
340
349
reducer: WebSocket ( )
341
350
)
342
351
)
0 commit comments