Skip to content
Snippets Groups Projects
Commit dd53d9f1 authored by Dariusz Rybicki's avatar Dariusz Rybicki
Browse files

Handle send errors in ChatFeature

parent 311fbed0
No related branches found
No related tags found
2 merge requests!102Release 1.0.0,!87Messenger example - chat
...@@ -39,12 +39,14 @@ public struct ChatState: Equatable, Identifiable { ...@@ -39,12 +39,14 @@ public struct ChatState: Equatable, Identifiable {
myContactId: Data? = nil, myContactId: Data? = nil,
messages: IdentifiedArrayOf<Message> = [], messages: IdentifiedArrayOf<Message> = [],
failure: String? = nil, failure: String? = nil,
sendFailure: String? = nil,
text: String = "" text: String = ""
) { ) {
self.id = id self.id = id
self.myContactId = myContactId self.myContactId = myContactId
self.messages = messages self.messages = messages
self.failure = failure self.failure = failure
self.sendFailure = sendFailure
self.text = text self.text = text
} }
...@@ -52,6 +54,7 @@ public struct ChatState: Equatable, Identifiable { ...@@ -52,6 +54,7 @@ public struct ChatState: Equatable, Identifiable {
public var myContactId: Data? public var myContactId: Data?
public var messages: IdentifiedArrayOf<Message> public var messages: IdentifiedArrayOf<Message>
public var failure: String? public var failure: String?
public var sendFailure: String?
@BindableState public var text: String @BindableState public var text: String
} }
...@@ -59,6 +62,8 @@ public enum ChatAction: Equatable, BindableAction { ...@@ -59,6 +62,8 @@ public enum ChatAction: Equatable, BindableAction {
case start case start
case didFetchMessages(IdentifiedArrayOf<ChatState.Message>) case didFetchMessages(IdentifiedArrayOf<ChatState.Message>)
case sendTapped case sendTapped
case sendFailed(String)
case dismissSendFailureTapped
case binding(BindingAction<ChatState>) case binding(BindingAction<ChatState>)
} }
...@@ -152,8 +157,7 @@ public let chatReducer = Reducer<ChatState, ChatAction, ChatEnvironment> ...@@ -152,8 +157,7 @@ public let chatReducer = Reducer<ChatState, ChatAction, ChatEnvironment>
text: text, text: text,
to: recipientId, to: recipientId,
onError: { error in onError: { error in
// TODO: handle error subscriber.send(.sendFailed(error.localizedDescription))
print("^^^ ERROR: \(error)")
}, },
completion: { completion: {
subscriber.send(completion: .finished) subscriber.send(completion: .finished)
...@@ -166,6 +170,14 @@ public let chatReducer = Reducer<ChatState, ChatAction, ChatEnvironment> ...@@ -166,6 +170,14 @@ public let chatReducer = Reducer<ChatState, ChatAction, ChatEnvironment>
.receive(on: env.mainQueue) .receive(on: env.mainQueue)
.eraseToEffect() .eraseToEffect()
case .sendFailed(let failure):
state.sendFailure = failure
return .none
case .dismissSendFailureTapped:
state.sendFailure = nil
return .none
case .binding(_): case .binding(_):
return .none return .none
} }
......
...@@ -169,4 +169,42 @@ final class ChatFeatureTests: XCTestCase { ...@@ -169,4 +169,42 @@ final class ChatFeatureTests: XCTestCase {
sendMessageCompletion?() sendMessageCompletion?()
} }
func testSendFailure() {
var sendMessageOnError: SendMessage.OnError?
var sendMessageCompletion: SendMessage.Completion?
let store = TestStore(
initialState: ChatState(
id: .contact("contact-id".data(using: .utf8)!),
text: "Hello"
),
reducer: chatReducer,
environment: .unimplemented
)
store.environment.mainQueue = .immediate
store.environment.bgQueue = .immediate
store.environment.sendMessage.run = { _, _, onError, completion in
sendMessageOnError = onError
sendMessageCompletion = completion
}
store.send(.sendTapped) {
$0.text = ""
}
let error = NSError(domain: "test", code: 123)
sendMessageOnError?(error)
store.receive(.sendFailed(error.localizedDescription)) {
$0.sendFailure = error.localizedDescription
}
sendMessageCompletion?()
store.send(.dismissSendFailureTapped) {
$0.sendFailure = nil
}
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment