GetStream / stream-chat-swiftui

SwiftUI Chat SDK ➜ Stream Chat 💬
https://getstream.io/chat/sdk/swiftui/
Other
351 stars 88 forks source link

Add ability to handle errors #637

Closed f3dm76 closed 1 day ago

f3dm76 commented 3 weeks ago

What are you trying to achieve?

I'm displaying the chat like this: ChatChannelView(viewFactory: viewFactory, channelController: controller) Sometimes it just shows a loading indicator indefinitely, I guess there was some error inside ChatChannelController, I'd like to receive that error, display it for the user, and handle it somehow

If possible, how can you achieve this currently?

It's doable through ChatChannelVC.didFinishSynchronizing(error:), but I didn't find a way in SwiftUI

What would be the better way?

For example ChatChannelController can have an error publisher. Or ChatChannelView can have a closure parameter to handle errors.

GetStream Chat version: 4.62.0 GetStream Chat frameworks: StreamChat, StreamChatUI iOS version: 18.0 Swift version: 6.0 Xcode version: 16.0 Device: sim iPhone 16 pro max

nuno-vieira commented 2 weeks ago

Hi @f3dm76,

We do show an error if synchronize fails, like in the picture below. If there is an error it will be available in the ChatChannelListViewModel.channelAlertType == .error

Also, make sure you update to the last version.

f3dm76 commented 2 weeks ago

Hey @nuno-vieira, thank you for your answer. Sorry, but what is a ChatChannelListViewModel? I mean how do I get it from @Injected(\.chatClient)? I only have ChatChannelController. Your screenshot shows chats list, how did you get the error while still being there?

My situation is this - I navigate to chat details screen:

struct MessagesChatView: View {
    var controller: ChatChannelController
    var body: some View {
        // SC chat view which sometimes shows an endless loading spinner and nothing else
         ChatChannelView(viewFactory: viewFactory, channelController: controller) 
    }
}

asking for chat controller like this chatClient.channelController(for: ...). Then sometimes (very rarely) I see an endless loading spinner in chat details, and I need to be able to get the error (or I don't know what - I just see the spinner from inside the SC library) and show the error while inside the chat details.

I'd expect smth like:

var controller: ChatChannelController
var body: some View {
     ChatChannelView(viewFactory: viewFactory, channelController: controller)
          .onReceive(controller.errorPublisher) {
                   // show the error, suggest to navigate back or ideally retry
          }
}
nuno-vieira commented 1 week ago

Hi @f3dm76,

My bad I got confused with the Channel List.

Indeed, it seems on SwiftUI this is missing in the channel view.

We will add this to our backlog and we will let you know once we pick this one up 👍

Best, Nuno

nuno-vieira commented 2 days ago

Hi @f3dm76,

While analysing this, I think you can already do it. Would something like this work for you:

ChatChannelView(viewFactory: viewFactory, channelController: controller)
         .onReceive(controller.statePublisher.$state) { state in
            switch state {
                case .remoteDataFetchFailed(let error):
                    // handle error
                default:
                    break
            }
        }
nuno-vieira commented 1 day ago

@f3dm76 I'm closing this one for now. Please let us know if you have any troubles.

Best, Nuno