Implements signals
This commit is contained in:
@@ -10,6 +10,7 @@ import SwiftUI
|
||||
struct ConversationListView: View
|
||||
{
|
||||
@Binding var model: ViewModel
|
||||
@Environment(\.xpcClient) private var xpcClient
|
||||
|
||||
var body: some View {
|
||||
List($model.conversations, selection: $model.selectedConversations) { conv in
|
||||
@@ -19,9 +20,26 @@ struct ConversationListView: View
|
||||
|
||||
Text(conv.wrappedValue.messagePreview)
|
||||
}
|
||||
.id(conv.id)
|
||||
.padding(8.0)
|
||||
}
|
||||
.listStyle(.sidebar)
|
||||
.task { await watchForConversationListChanges() }
|
||||
}
|
||||
|
||||
private func watchForConversationListChanges() async {
|
||||
for await event in xpcClient.eventStream() {
|
||||
switch event {
|
||||
case .conversationsUpdated:
|
||||
model.setNeedsReload()
|
||||
case .messagesUpdated(_):
|
||||
model.setNeedsReload()
|
||||
case .updateStreamReconnected:
|
||||
model.setNeedsReload()
|
||||
default:
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Types
|
||||
@@ -32,9 +50,35 @@ struct ConversationListView: View
|
||||
var conversations: [Display.Conversation]
|
||||
var selectedConversations: Set<Display.Conversation.ID>
|
||||
|
||||
private var needsReload: Bool = true
|
||||
|
||||
public init(conversations: [Display.Conversation] = []) {
|
||||
self.conversations = conversations
|
||||
self.selectedConversations = Set()
|
||||
setNeedsReload()
|
||||
}
|
||||
|
||||
func setNeedsReload() {
|
||||
needsReload = true
|
||||
Task { @MainActor [weak self] in
|
||||
guard let self else { return }
|
||||
await reloadConversations()
|
||||
}
|
||||
}
|
||||
|
||||
func reloadConversations() async {
|
||||
guard needsReload else { return }
|
||||
needsReload = false
|
||||
|
||||
do {
|
||||
let client = XPCClient()
|
||||
let clientConversations = try await client.getConversations()
|
||||
.map { Display.Conversation(from: $0) }
|
||||
|
||||
self.conversations = clientConversations
|
||||
} catch {
|
||||
print("Error reloading conversations: \(error)")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user