This commit is contained in:
fmodf 2024-07-10 13:09:59 +02:00
parent b910b2ac4b
commit 08f3b548a6
5 changed files with 61 additions and 18 deletions

View file

@ -58,6 +58,23 @@ extension Database {
table.column("date", .datetime).notNull() table.column("date", .datetime).notNull()
table.column("pending", .boolean).notNull() table.column("pending", .boolean).notNull()
table.column("sentError", .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)
} }
} }

View file

@ -3,22 +3,34 @@ import GRDB
import Martin import Martin
import SwiftUI import SwiftUI
enum AttachmentType: Stateable { enum AttachmentType: Int, Stateable, DatabaseValueConvertible {
case movie case movie = 0
case image case image = 1
case audio case audio = 2
case file case file = 3
case location case location = 4
case contact case contact = 5
} }
struct AttachmentItem: Stateable { struct AttachmentItem: DBStorable {
let type: AttachmentType static let databaseTableName = "attachment_items"
let data: Data
let string: String
}
struct Attachment: Stateable {
let id: String 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 {}

View file

@ -32,6 +32,8 @@ struct Message: DBStorable, Equatable {
let date: Date let date: Date
let pending: Bool let pending: Bool
let sentError: Bool let sentError: Bool
static let attachment = hasOne(Attachment.self)
} }
extension Message { extension Message {

View file

@ -39,8 +39,13 @@ struct AttachmentContactsPickerView: View {
} }
.clipped() .clipped()
.onTapGesture { .onTapGesture {
// TODO: Send selected media if let selectedContact = selectedContact {
print("Send location") 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)))
}
} }
} }
} }

View file

@ -62,8 +62,15 @@ struct AttachmentLocationPickerView: View {
} }
.clipped() .clipped()
.onTapGesture { .onTapGesture {
// TODO: Send selected media let attachment = Attachment(id: UUID().uuidString, items: [
print("Send location") AttachmentItem(
type: .location,
data: Data(),
string: "\(region.center.latitude),\(region.center.longitude)"
)
])
store.dispatch(.conversationAction(.sendAttachment(attachment)))
store.dispatch(.conversationAction(.showAttachmentPicker(false)))
} }
} }
.onAppear { .onAppear {