Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chat: fix issue with decode of incoming messages #42

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions Shared/Controllers/RoomContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -170,11 +170,10 @@ final class RoomContext: ObservableObject {
func sendMessage() {
// Make sure the message is not empty
guard !textFieldString.isEmpty else { return }

let roomMessage = ExampleRoomMessage(messageId: UUID().uuidString,
senderSid: room.localParticipant.sid,
senderIdentity: room.localParticipant.identity,
text: textFieldString)
let currentDate = Date()
let roomMessage = ExampleRoomMessage(id: UUID().uuidString,
message: textFieldString,
timestamp: Int(currentDate.timeIntervalSince1970))
textFieldString = ""
messages.append(roomMessage)

Expand Down Expand Up @@ -244,8 +243,12 @@ extension RoomContext: RoomDelegate {
}

func room(_: Room, participant _: RemoteParticipant?, didReceiveData data: Data, forTopic _: String) {
//print("Debug: Received raw data: \(String(data: data, encoding: .utf8) ?? "Invalid data")")

do {
let roomMessage = try jsonDecoder.decode(ExampleRoomMessage.self, from: data)
//print("Debug: Decoded message: \(roomMessage)")

// Update UI from main queue
Task.detached { @MainActor [weak self] in
guard let self else { return }
Expand Down
5 changes: 3 additions & 2 deletions Shared/RoomView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ struct RoomView: View {
@State private var canSwitchCameraPosition = false

func messageView(_ message: ExampleRoomMessage) -> some View {
let isMe = message.senderSid == room.localParticipant.sid
let localSidString = String(describing: room.localParticipant.sid)
let isMe = message.id == localSidString

return HStack {
if isMe {
Expand All @@ -106,7 +107,7 @@ struct RoomView: View {

// VStack(alignment: isMe ? .trailing : .leading) {
// Text(message.identity)
Text(message.text)
Text(message.message)
.padding(8)
.background(isMe ? Color.lkRed : Color.lkGray3)
.foregroundColor(Color.white)
Expand Down
18 changes: 5 additions & 13 deletions Shared/Support/ExampleRoomMessage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,15 @@
import LiveKit

struct ExampleRoomMessage: Identifiable, Equatable, Hashable, Codable {
// Identifiable protocol needs param named id
var id: String {
messageId
}

// message id
let messageId: String

let senderSid: Participant.Sid?
let senderIdentity: Participant.Identity?
let text: String
let id: String
let message: String
let timestamp: Int

static func == (lhs: Self, rhs: Self) -> Bool {
lhs.messageId == rhs.messageId
lhs.id == rhs.id
}

func hash(into hasher: inout Hasher) {
hasher.combine(messageId)
hasher.combine(id)
}
}