another.im-ios/ConversationsClassic/AppData/Store/SettingsStore.swift

47 lines
1 KiB
Swift
Raw Normal View History

2024-09-20 15:32:10 +00:00
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
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
}
}
}