Chat icon indicating copy to clipboard operation
Chat copied to clipboard

ChatView not initializing

Open simongiesen opened this issue 10 months ago • 4 comments

Hi,

I am trying to get the Example Project as well as my own project working with Chat.

The code is not compiling. Do you experience same issues? What is the correct syntax to embedd a basic ChatView?

This is my own approach, which point am I missing?

`

import SwiftUI
import ExyteChat

struct ContentView: View {
    @State var messages: [Message] = [
        Message(id: UUID().uuidString, text: "Hallo!", user: User(id: "user1", isCurrentUser: false), sentDate: Date().addingTimeInterval(-3600)),
        Message(id: UUID().uuidString, text: "Wie geht's?", user: User(id: "user2", isCurrentUser: true), sentDate: Date().addingTimeInterval(-3540)),
        Message(id: UUID().uuidString, text: "Mir geht's gut, danke!", user: User(id: "user1", isCurrentUser: false), sentDate: Date().addingTimeInterval(-3480)),
    ]

    func handleSendMessage(draftMessage: DraftMessage) {
        print("Nachricht gesendet: \(draftMessage.text)")
        let newMessage = Message(id: UUID().uuidString, text: draftMessage.text, user: User(id: "user2", isCurrentUser: true), sentDate: Date())
        messages.append(newMessage)
    }

    var body: some View {
        NavigationView {
            ChatView(
                messages: messages,
                didSendMessage: handleSendMessage,
                messageMenuAction: nil,
                localization: .defaultLocalization
            )
            .navigationTitle("Chat")
        }
    }
}

// Dummy-Definitionen für die benötigten Typen
public struct Message: Identifiable {
    public let id: String
    public var text: String?
    public var user: User
    public var attachments: [Attachment] = []
    public var sentDate: Date
}

public struct User: Identifiable {
    public let id: String
    public var isCurrentUser: Bool = false // Standardmäßig nicht der aktuelle Benutzer
}

public struct Attachment: Identifiable {
    public let id = UUID().uuidString
    // Füge hier weitere Eigenschaften für Anhänge hinzu, falls benötigt
}

public struct DraftMessage {
    public let text: String
    public let attachments: [ExyteMediaPicker.MediaItem] // Auch wenn wir sie hier nicht direkt nutzen, wird sie im Closure erwartet
}

// Dummy für MessageMenuAction, da es im ChatView generisch ist
public protocol MessageMenuAction {}
public enum DefaultMessageMenuAction: String, CaseIterable, MessageMenuAction {
    case reply
    case copy
    case edit
    case delete
}

// Dummy für ChatLocalization, falls nicht bereits im Projekt definiert
public struct ChatLocalization {
    public static let defaultLocalization = ChatLocalization()
    public let waitingForNetwork = "Warten auf Netzwerk..."
}
`

simongiesen avatar Mar 27 '25 07:03 simongiesen

Hey @simongiesen,

You're defining your own Message, DraftMessage, Attachment and User structs which is causing the issues you're seeing. You should be using ExyteChats version of these items instead.

If you need to augment them for data storage or network calls you can use a bridging struct but you'll need to write functions to convert them from one to the other. The ChatExampleApp in this repo does this with the use of MockMessage, MockUser and MockAttachment.

A good way to get started is to clone the example app and then start altering / changing the code from there.

I made the changes necessary in order to get your code to compile...

import SwiftUI
import ExyteChat

struct ContentView: View {
    static let User1 = User(id: "user1", name: "Alice", avatarURL: nil, isCurrentUser: false)
    static let User2 = User(id: "user2", name: "Bob", avatarURL: nil, isCurrentUser: true)
    
    @State var messages: [Message] = [
        Message(id: UUID().uuidString, user: User1, status: .sent, createdAt: Date().addingTimeInterval(-3600), text: "Hallo!"),
        Message(id: UUID().uuidString, user: User2, status: .sent, createdAt: Date().addingTimeInterval(-3540), text: "Wie geht's?"),
        Message(id: UUID().uuidString, user: User1, status: .sent, createdAt: Date().addingTimeInterval(-3480), text: "Mir geht's gut, danke!"),
    ]

    func handleSendMessage(draftMessage: DraftMessage) -> Void {
        print("Nachricht gesendet: \(draftMessage.text)")
        let newMessage = Message(id: UUID().uuidString, user: ContentView.User2, status: .sent, createdAt: Date.now, text: draftMessage.text)
        messages.append(newMessage)
    }

    var body: some View {
        NavigationView {
            ChatView(messages: messages, didSendMessage: handleSendMessage)
                .navigationTitle("Chat")
        }
    }
}

#Preview {
    ContentView()
}

btoms20 avatar Mar 29 '25 23:03 btoms20

Hi!

Thanks a lot for your reply! The Chat View is starting now but it crashes every time I try to import media or to start the camera ...

simongiesen avatar Mar 30 '25 20:03 simongiesen

Hey @simongiesen, please attach a sample project demonstrating the issue, otherwise it is impossible to help you

f3dm76 avatar Apr 03 '25 08:04 f3dm76

Hi, it was my fault. I did not provide Privacy Keys in my info.plist. Now it is working. Where and how can I do proper localization (German) ? Kind regards!

simongiesen avatar Apr 22 '25 15:04 simongiesen