mirror of
https://github.com/permissionlesstech/bitchat.git
synced 2026-07-25 09:05:20 +00:00
* Refactor app runtime and view model architecture * Move app ownership into stores and coordinators * Fix smoke test environment injection * Stabilize fragmentation package tests * Fix coordinator build warnings * Clean up chat view model warnings * Fix Nostr relay startup coalescing --------- Co-authored-by: jack <jackjackbits@users.noreply.github.com>
42 lines
1.2 KiB
Swift
42 lines
1.2 KiB
Swift
import BitFoundation
|
|
import Combine
|
|
import SwiftUI
|
|
|
|
@MainActor
|
|
final class PublicChatModel: ObservableObject {
|
|
@Published private(set) var activeChannel: ChannelID
|
|
@Published private(set) var messages: [BitchatMessage] = []
|
|
|
|
private let conversationStore: ConversationStore
|
|
private var cancellables = Set<AnyCancellable>()
|
|
|
|
init(conversationStore: ConversationStore) {
|
|
self.activeChannel = conversationStore.activeChannel
|
|
self.conversationStore = conversationStore
|
|
|
|
bind()
|
|
refreshMessages()
|
|
}
|
|
|
|
private func bind() {
|
|
conversationStore.$activeChannel
|
|
.receive(on: DispatchQueue.main)
|
|
.sink { [weak self] channel in
|
|
self?.activeChannel = channel
|
|
self?.refreshMessages()
|
|
}
|
|
.store(in: &cancellables)
|
|
|
|
conversationStore.$messagesByConversation
|
|
.receive(on: DispatchQueue.main)
|
|
.sink { [weak self] _ in
|
|
self?.refreshMessages()
|
|
}
|
|
.store(in: &cancellables)
|
|
}
|
|
|
|
private func refreshMessages() {
|
|
messages = conversationStore.messages(for: ConversationID(channelID: activeChannel))
|
|
}
|
|
}
|