47 lines
1.1 KiB
Swift
47 lines
1.1 KiB
Swift
import Combine
|
|
import Foundation
|
|
import GRDB
|
|
import Photos
|
|
import SwiftUI
|
|
|
|
@MainActor
|
|
final class ChatSettingsStore: ObservableObject {
|
|
@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()
|
|
}
|
|
}
|
|
|
|
extension ChatSettingsStore {
|
|
func setSecured(_ secured: Bool) {
|
|
Task {
|
|
try? await chat?.setEncrypted(secured)
|
|
}
|
|
}
|
|
}
|
|
|
|
// MARK: - Processing attachments
|
|
private extension ChatSettingsStore {
|
|
func subscribe() {
|
|
chatCancellable = ValueObservation.tracking(Chat
|
|
.filter(Column("account") == roster.bareJid && Column("participant") == roster.contactBareJid)
|
|
.fetchOne
|
|
)
|
|
.publisher(in: Database.shared.dbQueue, scheduling: .immediate)
|
|
.receive(on: DispatchQueue.main)
|
|
.sink { _ in
|
|
} receiveValue: { [weak self] chat in
|
|
self?.chat = chat
|
|
}
|
|
}
|
|
}
|