Skip to content

Commit

Permalink
Retrieve metadata and create parent if needed for added files
Browse files Browse the repository at this point in the history
  • Loading branch information
mvasilak committed Feb 17, 2025
1 parent d59b444 commit 0a6f159
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import CocoaLumberjackSwift
import RealmSwift

struct CreateAttachmentsDbRequest: DbResponseRequest {
typealias Response = [(String, String)]
typealias Response = (succeeded: [Attachment], failed: [(String, String)])

let attachments: [Attachment]
let parentKey: String?
Expand All @@ -21,20 +21,21 @@ struct CreateAttachmentsDbRequest: DbResponseRequest {

var needsWrite: Bool { return true }

func process(in database: Realm) throws -> [(String, String)] {
guard let libraryId = attachments.first?.libraryId else { return [] }
func process(in database: Realm) throws -> (succeeded: [Attachment], failed: [(String, String)]) {
guard let libraryId = attachments.first?.libraryId else { return ([], []) }

let parent = parentKey.flatMap({ database.objects(RItem.self).uniqueObject(key: $0, libraryId: libraryId) })
if let parent = parent {
// This is to mitigate the issue in item detail screen (ItemDetailActionHandler.shouldReloadData) where observing of `children` doesn't report changes between `oldValue` and `newValue`.
parent.version = parent.version
}

var succeeded: [Attachment] = []
var failed: [(String, String)] = []

for attachment in attachments {
do {
let attachment = try CreateAttachmentDbRequest(
let item = try CreateAttachmentDbRequest(
attachment: attachment,
parentKey: nil,
localizedType: localizedType,
Expand All @@ -43,15 +44,16 @@ struct CreateAttachmentsDbRequest: DbResponseRequest {
tags: []
).process(in: database)
if let parent = parent {
attachment.parent = parent
attachment.changes.append(RObjectChange.create(changes: RItemChanges.parent))
item.parent = parent
item.changes.append(RObjectChange.create(changes: RItemChanges.parent))
}
succeeded.append(attachment)
} catch let error {
DDLogError("CreateAttachmentsDbRequest: could not create attachment - \(error)")
failed.append((attachment.key, attachment.title))
}
}

return failed
return (succeeded, failed)
}
}
3 changes: 2 additions & 1 deletion Zotero/Scenes/Detail/DetailCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,8 @@ final class DetailCoordinator: Coordinator {
citationController: userControllers.citationController,
fileCleanupController: userControllers.fileCleanupController,
syncScheduler: userControllers.syncScheduler,
htmlAttributedStringConverter: controllers.htmlAttributedStringConverter
htmlAttributedStringConverter: controllers.htmlAttributedStringConverter,
recognizerController: userControllers.recognizerController
)
let controller = ItemsViewController(viewModel: ViewModel(initialState: state, handler: handler), controllers: controllers, coordinatorDelegate: self)
controller.tagFilterDelegate = itemsTagFilterDelegate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ final class ItemDetailActionHandler: ViewModelActionHandler, BackgroundDbProcess
state.error = .cantAddAttachments(.allFailedCreation)
state.attachments.removeAll(where: { attachment in return attachments.contains(where: { $0.key == attachment.key }) })

case .success(let failed):
case .success(let (_, failed)):
guard !failed.isEmpty else { return }
state.error = .cantAddAttachments(.someFailedCreation(failed.map({ $0.1 })))
state.attachments.removeAll(where: { attachment in return failed.contains(where: { $0.0 == attachment.key }) })
Expand Down
19 changes: 17 additions & 2 deletions Zotero/Scenes/Detail/Items/ViewModels/ItemsActionHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ final class ItemsActionHandler: BaseItemsActionHandler, ViewModelActionHandler {
private unowned let fileCleanupController: AttachmentFileCleanupController
private unowned let syncScheduler: SynchronizationScheduler
private unowned let htmlAttributedStringConverter: HtmlAttributedStringConverter
private unowned let recognizerController: RecognizerController
private let disposeBag: DisposeBag

init(
Expand All @@ -37,7 +38,8 @@ final class ItemsActionHandler: BaseItemsActionHandler, ViewModelActionHandler {
citationController: CitationController,
fileCleanupController: AttachmentFileCleanupController,
syncScheduler: SynchronizationScheduler,
htmlAttributedStringConverter: HtmlAttributedStringConverter
htmlAttributedStringConverter: HtmlAttributedStringConverter,
recognizerController: RecognizerController
) {
self.fileStorage = fileStorage
self.schemaController = schemaController
Expand All @@ -47,6 +49,7 @@ final class ItemsActionHandler: BaseItemsActionHandler, ViewModelActionHandler {
self.fileCleanupController = fileCleanupController
self.syncScheduler = syncScheduler
self.htmlAttributedStringConverter = htmlAttributedStringConverter
self.recognizerController = recognizerController
self.disposeBag = DisposeBag()
super.init(dbStorage: dbStorage)
}
Expand Down Expand Up @@ -496,7 +499,19 @@ final class ItemsActionHandler: BaseItemsActionHandler, ViewModelActionHandler {
guard let self, let viewModel else { return }

switch result {
case .success(let failed):
case .success(let (succeeded, failed)):
for attachment in succeeded {
if let file = attachment.file as? FileData, file.mimeType == "application/pdf" {
let task = RecognizerController.RecognizerTask(file: file, kind: .createParentForItem(libraryId: libraryId, key: attachment.key))
recognizerController.queue(task: task) { [weak self] observable in
guard let self else { return }
observable?.subscribe { _ in
// TODO: Implement update of UI according to updates
}
.disposed(by: disposeBag)
}
}
}
guard !failed.isEmpty else { return }
update(viewModel: viewModel) { state in
state.error = .attachmentAdding(.someFailed(failed.map({ $0.1 })))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ struct NoteEditorActionHandler: ViewModelActionHandler, BackgroundDbProcessingAc
DDLogInfo("NoteEditorActionHandler: submit \(images.count) images")

do {
let failedKeys = try dbStorage.perform(request: request, on: backgroundQueue).map({ $0.0 })
let failedKeys = try dbStorage.perform(request: request, on: backgroundQueue).1.map({ $0.0 })
let successfulImages = images.filter({ !failedKeys.contains($0.1.key) }).map({ NoteEditorState.CreatedImage(nodeId: $0.0, key: $0.1.key) })
DDLogInfo("NoteEditorActionHandler: successfully created \(successfulImages)")
update(viewModel: viewModel) { state in
Expand Down

0 comments on commit 0a6f159

Please sign in to comment.