From 08f3b548a6ad552178a64e7ce39effb3679cb33c Mon Sep 17 00:00:00 2001 From: fmodf Date: Wed, 10 Jul 2024 13:09:59 +0200 Subject: [PATCH] wip --- .../Database/Database+Migrations.swift | 17 ++++++++ .../AppCore/Models/Attachment.swift | 40 ++++++++++++------- .../AppCore/Models/Message.swift | 2 + .../AttachmentContactsPickerView.swift | 9 ++++- .../AttachmentLocationPickerView.swift | 11 ++++- 5 files changed, 61 insertions(+), 18 deletions(-) diff --git a/ConversationsClassic/AppCore/Database/Database+Migrations.swift b/ConversationsClassic/AppCore/Database/Database+Migrations.swift index 5e0eb2a..7813f15 100644 --- a/ConversationsClassic/AppCore/Database/Database+Migrations.swift +++ b/ConversationsClassic/AppCore/Database/Database+Migrations.swift @@ -58,6 +58,23 @@ extension Database { table.column("date", .datetime).notNull() table.column("pending", .boolean).notNull() table.column("sentError", .boolean).notNull() + table.column("attachment", .text).references("attachments", onDelete: .cascade) + } + + // attachments + try db.create(table: "attachments", options: [.ifNotExists]) { table in + table.column("id", .text).notNull().primaryKey().unique(onConflict: .replace) + } + + // attachment items + try db.create(table: "attachment_items", options: [.ifNotExists]) { table in + table.column("id", .text).notNull().primaryKey().unique(onConflict: .replace) + table.belongsTo("attachments", onDelete: .cascade).notNull() + table.column("type", .integer).notNull() + table.column("localPath", .text) + table.column("remotePath", .text) + table.column("localThumbnailPath", .text) + table.column("string", .text) } } diff --git a/ConversationsClassic/AppCore/Models/Attachment.swift b/ConversationsClassic/AppCore/Models/Attachment.swift index 2c008ed..ddde7b5 100644 --- a/ConversationsClassic/AppCore/Models/Attachment.swift +++ b/ConversationsClassic/AppCore/Models/Attachment.swift @@ -3,22 +3,34 @@ import GRDB import Martin import SwiftUI -enum AttachmentType: Stateable { - case movie - case image - case audio - case file - case location - case contact +enum AttachmentType: Int, Stateable, DatabaseValueConvertible { + case movie = 0 + case image = 1 + case audio = 2 + case file = 3 + case location = 4 + case contact = 5 } -struct AttachmentItem: Stateable { - let type: AttachmentType - let data: Data - let string: String -} +struct AttachmentItem: DBStorable { + static let databaseTableName = "attachment_items" -struct Attachment: Stateable { let id: String - let items: [AttachmentItem] + static let attachment = belongsTo(Attachment.self) + let type: AttachmentType + let localPath: URL? + let remotePath: URL? + let localThumbnailPath: URL? + let string: String? } + +struct Attachment: DBStorable { + static let databaseTableName = "attachments" + + let id: String + static let items = hasMany(AttachmentItem.self) + static let message = hasOne(Message.self) +} + +extension AttachmentItem: Equatable {} +extension Attachment: Equatable {} diff --git a/ConversationsClassic/AppCore/Models/Message.swift b/ConversationsClassic/AppCore/Models/Message.swift index 8ca0992..9c6f0bd 100644 --- a/ConversationsClassic/AppCore/Models/Message.swift +++ b/ConversationsClassic/AppCore/Models/Message.swift @@ -32,6 +32,8 @@ struct Message: DBStorable, Equatable { let date: Date let pending: Bool let sentError: Bool + + static let attachment = hasOne(Attachment.self) } extension Message { diff --git a/ConversationsClassic/View/Screens/Attachments/AttachmentContactsPickerView.swift b/ConversationsClassic/View/Screens/Attachments/AttachmentContactsPickerView.swift index 57c0522..0382312 100644 --- a/ConversationsClassic/View/Screens/Attachments/AttachmentContactsPickerView.swift +++ b/ConversationsClassic/View/Screens/Attachments/AttachmentContactsPickerView.swift @@ -39,8 +39,13 @@ struct AttachmentContactsPickerView: View { } .clipped() .onTapGesture { - // TODO: Send selected media - print("Send location") + if let selectedContact = selectedContact { + let attachment = Attachment(id: UUID().uuidString, items: [ + AttachmentItem(type: .contact, data: Data(), string: selectedContact.contactBareJid) + ]) + store.dispatch(.conversationAction(.sendAttachment(attachment))) + store.dispatch(.conversationAction(.showAttachmentPicker(false))) + } } } } diff --git a/ConversationsClassic/View/Screens/Attachments/AttachmentLocationPickerView.swift b/ConversationsClassic/View/Screens/Attachments/AttachmentLocationPickerView.swift index 59c45ed..dc39f6a 100644 --- a/ConversationsClassic/View/Screens/Attachments/AttachmentLocationPickerView.swift +++ b/ConversationsClassic/View/Screens/Attachments/AttachmentLocationPickerView.swift @@ -62,8 +62,15 @@ struct AttachmentLocationPickerView: View { } .clipped() .onTapGesture { - // TODO: Send selected media - print("Send location") + let attachment = Attachment(id: UUID().uuidString, items: [ + AttachmentItem( + type: .location, + data: Data(), + string: "\(region.center.latitude),\(region.center.longitude)" + ) + ]) + store.dispatch(.conversationAction(.sendAttachment(attachment))) + store.dispatch(.conversationAction(.showAttachmentPicker(false))) } } .onAppear {