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("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)
}
}

View file

@ -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 {}

View file

@ -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 {

View file

@ -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)))
}
}
}
}

View file

@ -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 {