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