From 2890a6cd6d3a71525dc952d29f0a8c560dea1c81 Mon Sep 17 00:00:00 2001 From: fmodf Date: Sun, 18 Aug 2024 18:27:18 +0200 Subject: [PATCH] wip --- .../AppData/Model/Roster.swift | 30 +++++++++++++++++-- .../AppData/Store/ClientsStore.swift | 2 +- ...rsationStore.swift => MessagesStore.swift} | 23 ++------------ .../ContactsPicker/ContactsPickerView.swift | 6 ++-- 4 files changed, 33 insertions(+), 28 deletions(-) rename ConversationsClassic/AppData/Store/{ConversationStore.swift => MessagesStore.swift} (75%) diff --git a/ConversationsClassic/AppData/Model/Roster.swift b/ConversationsClassic/AppData/Model/Roster.swift index 11749c0..9909704 100644 --- a/ConversationsClassic/AppData/Model/Roster.swift +++ b/ConversationsClassic/AppData/Model/Roster.swift @@ -72,9 +72,33 @@ extension Roster { } extension Roster { - static func fetchDeletedLocally() async throws -> [Roster] { - try await Database.shared.dbQueue.read { db in - try Roster.filter(Column("locallyDeleted") == true).fetchAll(db) + static var allDeletedLocally: [Roster] { + get async { + do { + let rosters = try await Database.shared.dbQueue.read { db in + try Roster + .filter(Column("locallyDeleted") == true) + .fetchAll(db) + } + return rosters + } catch { + return [] + } + } + } + + static var allActive: [Roster] { + get async { + do { + let rosters = try await Database.shared.dbQueue.read { db in + try Roster + .filter(Column("locallyDeleted") == false) + .fetchAll(db) + } + return rosters + } catch { + return [] + } } } } diff --git a/ConversationsClassic/AppData/Store/ClientsStore.swift b/ConversationsClassic/AppData/Store/ClientsStore.swift index 5dead90..3eaa3d2 100644 --- a/ConversationsClassic/AppData/Store/ClientsStore.swift +++ b/ConversationsClassic/AppData/Store/ClientsStore.swift @@ -100,7 +100,7 @@ extension ClientsStore { func addRoster(_ credentials: Credentials, contactJID: String, name: String?, groups: [String]) async throws { // check that roster exist in db as locally deleted and undelete it - let deletedLocally = try await Roster.fetchDeletedLocally() + let deletedLocally = await Roster.allDeletedLocally if var roster = deletedLocally.first(where: { $0.contactBareJid == contactJID }) { try await roster.setLocallyDeleted(false) return diff --git a/ConversationsClassic/AppData/Store/ConversationStore.swift b/ConversationsClassic/AppData/Store/MessagesStore.swift similarity index 75% rename from ConversationsClassic/AppData/Store/ConversationStore.swift rename to ConversationsClassic/AppData/Store/MessagesStore.swift index d8a4b56..d54576d 100644 --- a/ConversationsClassic/AppData/Store/ConversationStore.swift +++ b/ConversationsClassic/AppData/Store/MessagesStore.swift @@ -5,7 +5,7 @@ import GRDB import Photos @MainActor -final class ConversationStore: ObservableObject { +final class MessagesStore: ObservableObject { @Published private(set) var messages: [Message] = [] @Published var replyText = "" @@ -21,7 +21,7 @@ final class ConversationStore: ObservableObject { } } -extension ConversationStore { +extension MessagesStore { func sendMessage(_ message: String) { Task { var msg = Message.blank @@ -49,24 +49,7 @@ extension ConversationStore { } } -extension ConversationStore { - var contacts: [Roster] { - get async { - do { - let rosters = try await Database.shared.dbQueue.read { db in - try Roster - .filter(Column("locallyDeleted") == false) - .fetchAll(db) - } - return rosters - } catch { - return [] - } - } - } -} - -private extension ConversationStore { +private extension MessagesStore { func subscribe() { messagesCancellable = ValueObservation.tracking(Message .filter( diff --git a/ConversationsClassic/View/Main/Conversation/Attachments/ContactsPicker/ContactsPickerView.swift b/ConversationsClassic/View/Main/Conversation/Attachments/ContactsPicker/ContactsPickerView.swift index c0b4a06..7246a1c 100644 --- a/ConversationsClassic/View/Main/Conversation/Attachments/ContactsPicker/ContactsPickerView.swift +++ b/ConversationsClassic/View/Main/Conversation/Attachments/ContactsPicker/ContactsPickerView.swift @@ -42,15 +42,13 @@ struct ContactsPickerView: View { .clipped() .onTapGesture { if let selectedContact = selectedContact { - Task { - await conversation.sendContact(selectedContact.contactBareJid) - } + conversation.sendContact(selectedContact.contactBareJid) router.dismissEnvironment() } } } .task { - rosters = await conversation.contacts + rosters = await Roster.allActive } } }