2024-09-20 15:32:10 +00:00
|
|
|
import Combine
|
|
|
|
import Foundation
|
|
|
|
import GRDB
|
|
|
|
import Photos
|
|
|
|
import SwiftUI
|
|
|
|
|
|
|
|
@MainActor
|
2024-10-15 11:39:23 +00:00
|
|
|
final class ChatSettingsStore: ObservableObject {
|
2024-09-20 15:32:10 +00:00
|
|
|
@Published var chat: Chat?
|
|
|
|
|
|
|
|
private let client: Client
|
|
|
|
private let roster: Roster
|
|
|
|
|
|
|
|
private var chatCancellable: AnyCancellable?
|
|
|
|
|
|
|
|
init(roster: Roster, client: Client) {
|
|
|
|
self.client = client
|
|
|
|
self.roster = roster
|
|
|
|
|
|
|
|
subscribe()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-15 11:39:23 +00:00
|
|
|
extension ChatSettingsStore {
|
2024-09-20 15:32:10 +00:00
|
|
|
func setSecured(_ secured: Bool) {
|
|
|
|
Task {
|
|
|
|
try? await chat?.setEncrypted(secured)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - Processing attachments
|
2024-10-15 11:39:23 +00:00
|
|
|
private extension ChatSettingsStore {
|
2024-09-20 15:32:10 +00:00
|
|
|
func subscribe() {
|
|
|
|
chatCancellable = ValueObservation.tracking(Chat
|
2024-09-30 13:24:38 +00:00
|
|
|
.filter(Column("account") == roster.bareJid && Column("participant") == roster.contactBareJid)
|
2024-09-20 15:32:10 +00:00
|
|
|
.fetchOne
|
|
|
|
)
|
|
|
|
.publisher(in: Database.shared.dbQueue, scheduling: .immediate)
|
|
|
|
.receive(on: DispatchQueue.main)
|
|
|
|
.sink { _ in
|
|
|
|
} receiveValue: { [weak self] chat in
|
2024-09-30 13:24:38 +00:00
|
|
|
self?.chat = chat
|
2024-09-20 15:32:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|