This commit is contained in:
fmodf 2024-08-18 18:27:18 +02:00
parent b97c8991bf
commit 2890a6cd6d
4 changed files with 33 additions and 28 deletions

View file

@ -72,9 +72,33 @@ extension Roster {
} }
extension Roster { extension Roster {
static func fetchDeletedLocally() async throws -> [Roster] { static var allDeletedLocally: [Roster] {
try await Database.shared.dbQueue.read { db in get async {
try Roster.filter(Column("locallyDeleted") == true).fetchAll(db) 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 []
}
} }
} }
} }

View file

@ -100,7 +100,7 @@ extension ClientsStore {
func addRoster(_ credentials: Credentials, contactJID: String, name: String?, groups: [String]) async throws { func addRoster(_ credentials: Credentials, contactJID: String, name: String?, groups: [String]) async throws {
// check that roster exist in db as locally deleted and undelete it // 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 }) { if var roster = deletedLocally.first(where: { $0.contactBareJid == contactJID }) {
try await roster.setLocallyDeleted(false) try await roster.setLocallyDeleted(false)
return return

View file

@ -5,7 +5,7 @@ import GRDB
import Photos import Photos
@MainActor @MainActor
final class ConversationStore: ObservableObject { final class MessagesStore: ObservableObject {
@Published private(set) var messages: [Message] = [] @Published private(set) var messages: [Message] = []
@Published var replyText = "" @Published var replyText = ""
@ -21,7 +21,7 @@ final class ConversationStore: ObservableObject {
} }
} }
extension ConversationStore { extension MessagesStore {
func sendMessage(_ message: String) { func sendMessage(_ message: String) {
Task { Task {
var msg = Message.blank var msg = Message.blank
@ -49,24 +49,7 @@ extension ConversationStore {
} }
} }
extension ConversationStore { private extension MessagesStore {
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 {
func subscribe() { func subscribe() {
messagesCancellable = ValueObservation.tracking(Message messagesCancellable = ValueObservation.tracking(Message
.filter( .filter(

View file

@ -42,15 +42,13 @@ struct ContactsPickerView: View {
.clipped() .clipped()
.onTapGesture { .onTapGesture {
if let selectedContact = selectedContact { if let selectedContact = selectedContact {
Task { conversation.sendContact(selectedContact.contactBareJid)
await conversation.sendContact(selectedContact.contactBareJid)
}
router.dismissEnvironment() router.dismissEnvironment()
} }
} }
} }
.task { .task {
rosters = await conversation.contacts rosters = await Roster.allActive
} }
} }
} }