This commit is contained in:
fmodf 2024-08-18 17:29:44 +02:00
parent ac09086eaa
commit 58f5cce5cf
3 changed files with 61 additions and 32 deletions

View file

@ -37,6 +37,13 @@ enum MessageContentType: Codable & Equatable, DatabaseValueConvertible {
case typing case typing
case invite case invite
case attachment(Attachment) case attachment(Attachment)
var isAttachment: Bool {
if case .attachment = self {
return true
}
return false
}
} }
enum MessageStatus: Int, Codable, DatabaseValueConvertible { enum MessageStatus: Int, Codable, DatabaseValueConvertible {

View file

@ -1,5 +1,6 @@
import Combine import Combine
import Foundation import Foundation
import GRDB
import Photos import Photos
import SwiftUI import SwiftUI
@ -12,11 +13,13 @@ final class AttachmentsStore: ObservableObject {
private let client: Client private let client: Client
private let roster: Roster private let roster: Roster
private var messagesCancellable: AnyCancellable?
private var processing: Set<String> = [] private var processing: Set<String> = []
init(roster: Roster, client: Client) { init(roster: Roster, client: Client) {
self.client = client self.client = client
self.roster = roster self.roster = roster
subscribe()
} }
} }
@ -191,38 +194,54 @@ extension AttachmentsStore {
} }
} }
// MARK: - Uploadings/Downloadings // MARK: - Processing attachments
extension AttachmentsStore { private extension AttachmentsStore {
func processAttachment(_ message: Message) { func subscribe() {
// Prevent multiple processing messagesCancellable = ValueObservation.tracking(Message
if processing.contains(message.id) { .filter(
return (Column("to") == roster.bareJid && Column("from") == roster.contactBareJid) ||
} (Column("from") == roster.bareJid && Column("to") == roster.contactBareJid)
)
// Process in background .order(Column("date").desc)
Task(priority: .background) { .fetchAll
// Do needed processing )
if case .attachment(let attachment) = message.contentType { .publisher(in: Database.shared.dbQueue, scheduling: .immediate)
if attachment.localPath != nil, attachment.remotePath == nil { .receive(on: DispatchQueue.main)
// Uploading .sink { _ in
processing.insert(message.id) } receiveValue: { [weak self] messages in
await uploadAttachment(message) let forProcessing = messages
processing.remove(message.id) // .filter { $0.status == .pending }
} else if attachment.localPath == nil, attachment.remotePath != nil { .filter { self?.processing.contains($0.id) == false }
// Downloading .filter { $0.contentType.isAttachment }
processing.insert(message.id) for message in forProcessing {
await downloadAttachment(message) if case .attachment(let attachment) = message.contentType {
processing.remove(message.id) if attachment.localPath != nil, attachment.remotePath == nil {
} else if attachment.localPath != nil, attachment.remotePath != nil, attachment.thumbnailName == nil, attachment.type == .image { // Uploading
// Generate thumbnail self?.processing.insert(message.id)
processing.insert(message.id) Task(priority: .background) {
await generateThumbnail(message) await self?.uploadAttachment(message)
processing.remove(message.id) }
} else if attachment.localPath == nil, attachment.remotePath != nil {
// Downloading
self?.processing.insert(message.id)
Task(priority: .background) {
await self?.downloadAttachment(message)
}
} else if attachment.localPath != nil, attachment.remotePath != nil, attachment.thumbnailName == nil, attachment.type == .image {
// Generate thumbnail
self?.processing.insert(message.id)
Task(priority: .background) {
await self?.generateThumbnail(message)
}
}
} }
} }
} }
} }
}
// MARK: - Uploadings/Downloadings
extension AttachmentsStore {
private func uploadAttachment(_ message: Message) async { private func uploadAttachment(_ message: Message) async {
do { do {
try await message.setStatus(.pending) try await message.setStatus(.pending)
@ -246,8 +265,10 @@ extension AttachmentsStore {
message.oobUrl = remotePath message.oobUrl = remotePath
try await message.save() try await message.save()
try await client.sendMessage(message) try await client.sendMessage(message)
processing.remove(message.id)
try await message.setStatus(.sent) try await message.setStatus(.sent)
} catch { } catch {
processing.remove(message.id)
try? await message.setStatus(.error) try? await message.setStatus(.error)
} }
} }
@ -276,6 +297,7 @@ extension AttachmentsStore {
remotePath: remotePath remotePath: remotePath
) )
) )
processing.remove(message.id)
try await message.save() try await message.save()
} catch { } catch {
logIt(.error, "Can't download attachment: \(error)") logIt(.error, "Can't download attachment: \(error)")
@ -324,6 +346,7 @@ extension AttachmentsStore {
remotePath: attachment.remotePath remotePath: attachment.remotePath
) )
) )
processing.remove(message.id)
try? await message.save() try? await message.save()
} }
} }

View file

@ -150,15 +150,12 @@ private struct AttachmentView: View {
ProgressView() ProgressView()
.scaleEffect(1.5) .scaleEffect(1.5)
.progressViewStyle(CircularProgressViewStyle(tint: .Material.Elements.active)) .progressViewStyle(CircularProgressViewStyle(tint: .Material.Elements.active))
let imageName = progressImageName(attachment.type ?? .file) let imageName = progressImageName(attachment.type)
Image(systemName: imageName) Image(systemName: imageName)
.font(.body1) .font(.body1)
.foregroundColor(.Material.Elements.active) .foregroundColor(.Material.Elements.active)
} }
} }
.onAppear {
attachments.processAttachment(message)
}
} }
@ViewBuilder private var failed: some View { @ViewBuilder private var failed: some View {
@ -178,7 +175,9 @@ private struct AttachmentView: View {
} }
} }
.onTapGesture { .onTapGesture {
attachments.processAttachment(message) Task {
try? await message.setStatus(.pending)
}
} }
} }