Skip to content

Commit 429169c

Browse files
committed
In OpenItemsController download remote or local changed remotely PDFs
1 parent d14d401 commit 429169c

File tree

2 files changed

+36
-11
lines changed

2 files changed

+36
-11
lines changed

Zotero/Controllers/Controllers.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ final class UserControllers {
404404
fullSyncDebugger = FullSyncDebugger(syncScheduler: syncScheduler, debugLogging: controllers.debugLogging, sessionController: controllers.sessionController)
405405
self.idleTimerController = controllers.idleTimerController
406406
self.customUrlController = CustomURLController(dbStorage: dbStorage, fileStorage: controllers.fileStorage)
407-
openItemsController = OpenItemsController(dbStorage: dbStorage, fileStorage: controllers.fileStorage)
407+
openItemsController = OpenItemsController(dbStorage: dbStorage, fileStorage: controllers.fileStorage, attachmentDownloader: fileDownloader)
408408
self.lastBuildNumber = controllers.lastBuildNumber
409409
self.disposeBag = DisposeBag()
410410
}

Zotero/Controllers/OpenItemsController.swift

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -105,17 +105,20 @@ final class OpenItemsController {
105105
// MARK: Properties
106106
private unowned let dbStorage: DbStorage
107107
private unowned let fileStorage: FileStorage
108+
private unowned let attachmentDownloader: AttachmentDownloader
108109
// TODO: Use a better data structure, such as an ordered set
109110
private var itemsBySessionIdentifier: [String: [Item]] = [:]
110111
private var sessionIdentifierByItemKind: [Item.Kind: String] = [:]
111112
private var itemsTokenBySessionIdentifier: [String: NotificationToken] = [:]
112113
private var observableBySessionIdentifier: [String: PublishSubject<[Item]>] = [:]
113114
private let disposeBag: DisposeBag
115+
private var downloadDisposeBag: DisposeBag?
114116

115117
// MARK: Object Lifecycle
116-
init(dbStorage: DbStorage, fileStorage: FileStorage) {
118+
init(dbStorage: DbStorage, fileStorage: FileStorage, attachmentDownloader: AttachmentDownloader) {
117119
self.dbStorage = dbStorage
118120
self.fileStorage = fileStorage
121+
self.attachmentDownloader = attachmentDownloader
119122
disposeBag = DisposeBag()
120123
}
121124

@@ -434,15 +437,31 @@ final class OpenItemsController {
434437
switch attachment.type {
435438
case .file(let filename, let contentType, let location, _, _):
436439
switch location {
437-
case .local, .localAndChangedRemotely:
438-
// TODO: Change .localAndChangedRemotely case to download first
439-
let file = Files.attachmentFile(in: libraryId, key: key, filename: filename, contentType: contentType)
440-
let url = file.createUrl()
441-
completion(.pdf(library: library, key: key, parentKey: parentKey, url: url))
442-
443-
case .remote:
444-
// TODO: Download first
445-
completion(nil)
440+
case .local:
441+
completion(createPDFPresentation(key: key, parentKey: parentKey, library: library, filename: filename, contentType: contentType))
442+
443+
case .localAndChangedRemotely, .remote:
444+
let disposeBag = DisposeBag()
445+
attachmentDownloader.observable
446+
.observe(on: MainScheduler.instance)
447+
.subscribe(onNext: { [weak self] update in
448+
guard let self, update.libraryId == attachment.libraryId, update.key == attachment.key else { return }
449+
switch update.kind {
450+
case .ready:
451+
completion(createPDFPresentation(key: key, parentKey: parentKey, library: library, filename: filename, contentType: contentType))
452+
downloadDisposeBag = nil
453+
454+
case .cancelled, .failed:
455+
completion(nil)
456+
downloadDisposeBag = nil
457+
458+
case .progress:
459+
break
460+
}
461+
})
462+
.disposed(by: disposeBag)
463+
downloadDisposeBag = disposeBag
464+
attachmentDownloader.downloadIfNeeded(attachment: attachment, parentKey: parentKey)
446465

447466
case .remoteMissing:
448467
DDLogError("OpenItemsController: can't load PDF item (key: \(key), library: \(libraryId)) - remote missing")
@@ -458,6 +477,12 @@ final class OpenItemsController {
458477
DDLogError("OpenItemsController: can't load PDF item (key: \(key), library: \(libraryId)) - \(error)")
459478
completion(nil)
460479
}
480+
481+
func createPDFPresentation(key: String, parentKey: String?, library: Library, filename: String, contentType: String) -> Presentation {
482+
let file = Files.attachmentFile(in: library.identifier, key: key, filename: filename, contentType: contentType)
483+
let url = file.createUrl()
484+
return .pdf(library: library, key: key, parentKey: parentKey, url: url)
485+
}
461486
}
462487

463488
func loadNotePresentation(key: String, libraryId: LibraryIdentifier, completion: @escaping (Presentation?) -> Void) {

0 commit comments

Comments
 (0)