@@ -7,7 +7,7 @@ private let readMe = """
77
88 A lightweight wrapper is made for `URLSession`'s API for web sockets so that we can send, \
99 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 .
1111 """
1212
1313// MARK: - Feature domain
@@ -111,7 +111,7 @@ struct WebSocket: ReducerProtocol {
111111 . cancellable ( id: WebSocketID . self)
112112
113113 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. " ) )
115115 return . none
116116
117117 case . sendResponse( didSucceed: true ) :
@@ -123,6 +123,7 @@ struct WebSocket: ReducerProtocol {
123123
124124 case . webSocket( . didOpen) :
125125 state. connectivityState = . connected
126+ state. receivedMessages. removeAll ( )
126127 return . none
127128 }
128129 }
@@ -135,41 +136,49 @@ struct WebSocketView: View {
135136
136137 var body : some View {
137138 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
151148 ? " Disconnect "
152149 : viewStore. connectivityState == . disconnected
153150 ? " Connect "
154151 : " 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+ }
157171 }
158172 }
159173
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 " )
162180 }
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 " ) )
171181 }
172- . padding ( )
173182 . alert ( self . store. scope ( state: \. alert) , dismiss: . alertDismissed)
174183 . navigationTitle ( " Web Socket " )
175184 }
@@ -336,7 +345,7 @@ struct WebSocketView_Previews: PreviewProvider {
336345 NavigationView {
337346 WebSocketView (
338347 store: Store (
339- initialState: WebSocket . State ( receivedMessages: [ " Echo " ] ) ,
348+ initialState: WebSocket . State ( receivedMessages: [ " Hi " ] ) ,
340349 reducer: WebSocket ( )
341350 )
342351 )
0 commit comments