wip
This commit is contained in:
parent
c27a935a3f
commit
441409e676
|
@ -32,4 +32,12 @@ extension Chat {
|
||||||
return roster
|
return roster
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func setEncrypted(_ encrypted: Bool) async throws {
|
||||||
|
try await Database.shared.dbQueue.write { db in
|
||||||
|
var chat = self
|
||||||
|
chat.encrypted = encrypted
|
||||||
|
try chat.update(db)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -122,7 +122,8 @@ extension ClientsStore {
|
||||||
|
|
||||||
// MARK: - Produce stores for conversation
|
// MARK: - Produce stores for conversation
|
||||||
extension ClientsStore {
|
extension ClientsStore {
|
||||||
func conversationStores(for roster: Roster) async throws -> (MessagesStore, AttachmentsStore) {
|
// swiftlint:disable:next large_tuple
|
||||||
|
func conversationStores(for roster: Roster) async throws -> (MessagesStore, AttachmentsStore, SettingsStore) {
|
||||||
while !ready {
|
while !ready {
|
||||||
await Task.yield()
|
await Task.yield()
|
||||||
}
|
}
|
||||||
|
@ -133,10 +134,12 @@ extension ClientsStore {
|
||||||
|
|
||||||
let conversationStore = MessagesStore(roster: roster, client: client)
|
let conversationStore = MessagesStore(roster: roster, client: client)
|
||||||
let attachmentsStore = AttachmentsStore(roster: roster, client: client)
|
let attachmentsStore = AttachmentsStore(roster: roster, client: client)
|
||||||
return (conversationStore, attachmentsStore)
|
let settingsStore = SettingsStore(roster: roster, client: client)
|
||||||
|
return (conversationStore, attachmentsStore, settingsStore)
|
||||||
}
|
}
|
||||||
|
|
||||||
func conversationStores(for chat: Chat) async throws -> (MessagesStore, AttachmentsStore) {
|
// swiftlint:disable:next large_tuple
|
||||||
|
func conversationStores(for chat: Chat) async throws -> (MessagesStore, AttachmentsStore, SettingsStore) {
|
||||||
while !ready {
|
while !ready {
|
||||||
await Task.yield()
|
await Task.yield()
|
||||||
}
|
}
|
||||||
|
@ -148,7 +151,8 @@ extension ClientsStore {
|
||||||
let roster = try await chat.fetchRoster()
|
let roster = try await chat.fetchRoster()
|
||||||
let conversationStore = MessagesStore(roster: roster, client: client)
|
let conversationStore = MessagesStore(roster: roster, client: client)
|
||||||
let attachmentsStore = AttachmentsStore(roster: roster, client: client)
|
let attachmentsStore = AttachmentsStore(roster: roster, client: client)
|
||||||
return (conversationStore, attachmentsStore)
|
let settingsStore = SettingsStore(roster: roster, client: client)
|
||||||
|
return (conversationStore, attachmentsStore, settingsStore)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
47
ConversationsClassic/AppData/Store/SettingsStore.swift
Normal file
47
ConversationsClassic/AppData/Store/SettingsStore.swift
Normal file
|
@ -0,0 +1,47 @@
|
||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -61,9 +61,9 @@ private struct ChatsRow: View {
|
||||||
|
|
||||||
do {
|
do {
|
||||||
try? await clientsStore.addRosterForNewChatIfNeeded(chat)
|
try? await clientsStore.addRosterForNewChatIfNeeded(chat)
|
||||||
let (messages, attachments) = try await clientsStore.conversationStores(for: chat)
|
let (messages, attachments, settings) = try await clientsStore.conversationStores(for: chat)
|
||||||
router.showScreen(.push) { _ in
|
router.showScreen(.push) { _ in
|
||||||
ConversationScreen(messagesStore: messages, attachments: attachments)
|
ConversationScreen(messagesStore: messages, attachments: attachments, settings: settings)
|
||||||
.navigationBarHidden(true)
|
.navigationBarHidden(true)
|
||||||
}
|
}
|
||||||
} catch {
|
} catch {
|
||||||
|
|
|
@ -159,9 +159,9 @@ private struct ContactsScreenRow: View {
|
||||||
}
|
}
|
||||||
|
|
||||||
do {
|
do {
|
||||||
let (messages, attachments) = try await clientsStore.conversationStores(for: roster)
|
let (messages, attachments, settings) = try await clientsStore.conversationStores(for: roster)
|
||||||
router.showScreen(.push) { _ in
|
router.showScreen(.push) { _ in
|
||||||
ConversationScreen(messagesStore: messages, attachments: attachments)
|
ConversationScreen(messagesStore: messages, attachments: attachments, settings: settings)
|
||||||
.navigationBarHidden(true)
|
.navigationBarHidden(true)
|
||||||
}
|
}
|
||||||
} catch {
|
} catch {
|
||||||
|
|
|
@ -7,6 +7,7 @@ struct ConversationScreen: View {
|
||||||
@Environment(\.router) var router
|
@Environment(\.router) var router
|
||||||
@StateObject var messagesStore: MessagesStore
|
@StateObject var messagesStore: MessagesStore
|
||||||
@StateObject var attachments: AttachmentsStore
|
@StateObject var attachments: AttachmentsStore
|
||||||
|
@StateObject var settings: SettingsStore
|
||||||
|
|
||||||
@State private var autoScroll = true
|
@State private var autoScroll = true
|
||||||
@State private var firstIsVisible = true
|
@State private var firstIsVisible = true
|
||||||
|
@ -30,6 +31,7 @@ struct ConversationScreen: View {
|
||||||
centerText: .init(text: centerText(), action: {
|
centerText: .init(text: centerText(), action: {
|
||||||
router.showScreen(.fullScreenCover) { _ in
|
router.showScreen(.fullScreenCover) { _ in
|
||||||
ConversationSettingsScreen()
|
ConversationSettingsScreen()
|
||||||
|
.environmentObject(settings)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
|
@ -108,6 +110,7 @@ struct ConversationScreen: View {
|
||||||
ConversationTextInput(autoScroll: $autoScroll)
|
ConversationTextInput(autoScroll: $autoScroll)
|
||||||
.environmentObject(messagesStore)
|
.environmentObject(messagesStore)
|
||||||
.environmentObject(attachments)
|
.environmentObject(attachments)
|
||||||
|
.environmentObject(settings)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -5,9 +5,7 @@ import SwiftUI
|
||||||
|
|
||||||
struct ConversationSettingsScreen: View {
|
struct ConversationSettingsScreen: View {
|
||||||
@Environment(\.router) var router
|
@Environment(\.router) var router
|
||||||
@EnvironmentObject var messagesStore: MessagesStore
|
@EnvironmentObject var settingsStore: SettingsStore
|
||||||
|
|
||||||
@State private var omemoEnabled = true
|
|
||||||
|
|
||||||
var body: some View {
|
var body: some View {
|
||||||
ZStack {
|
ZStack {
|
||||||
|
@ -34,7 +32,14 @@ struct ConversationSettingsScreen: View {
|
||||||
SharedListRow(
|
SharedListRow(
|
||||||
iconType: .none,
|
iconType: .none,
|
||||||
text: L10n.Conversation.Settings.enableOmemo,
|
text: L10n.Conversation.Settings.enableOmemo,
|
||||||
controlType: .switcher(isOn: $omemoEnabled)
|
controlType: .switcher(isOn: Binding(
|
||||||
|
get: { settingsStore.chat?.encrypted ?? false },
|
||||||
|
set: { new in
|
||||||
|
Task {
|
||||||
|
try? await settingsStore.chat?.setEncrypted(new)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
))
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue