Skip to content

Commit

Permalink
In OpenItemsController download remote or local changed remotely PDFs
Browse files Browse the repository at this point in the history
  • Loading branch information
mvasilak committed Aug 27, 2024
1 parent d14d401 commit 429169c
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Zotero/Controllers/Controllers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ final class UserControllers {
fullSyncDebugger = FullSyncDebugger(syncScheduler: syncScheduler, debugLogging: controllers.debugLogging, sessionController: controllers.sessionController)
self.idleTimerController = controllers.idleTimerController
self.customUrlController = CustomURLController(dbStorage: dbStorage, fileStorage: controllers.fileStorage)
openItemsController = OpenItemsController(dbStorage: dbStorage, fileStorage: controllers.fileStorage)
openItemsController = OpenItemsController(dbStorage: dbStorage, fileStorage: controllers.fileStorage, attachmentDownloader: fileDownloader)
self.lastBuildNumber = controllers.lastBuildNumber
self.disposeBag = DisposeBag()
}
Expand Down
45 changes: 35 additions & 10 deletions Zotero/Controllers/OpenItemsController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -105,17 +105,20 @@ final class OpenItemsController {
// MARK: Properties
private unowned let dbStorage: DbStorage
private unowned let fileStorage: FileStorage
private unowned let attachmentDownloader: AttachmentDownloader
// TODO: Use a better data structure, such as an ordered set
private var itemsBySessionIdentifier: [String: [Item]] = [:]
private var sessionIdentifierByItemKind: [Item.Kind: String] = [:]
private var itemsTokenBySessionIdentifier: [String: NotificationToken] = [:]
private var observableBySessionIdentifier: [String: PublishSubject<[Item]>] = [:]
private let disposeBag: DisposeBag
private var downloadDisposeBag: DisposeBag?

// MARK: Object Lifecycle
init(dbStorage: DbStorage, fileStorage: FileStorage) {
init(dbStorage: DbStorage, fileStorage: FileStorage, attachmentDownloader: AttachmentDownloader) {
self.dbStorage = dbStorage
self.fileStorage = fileStorage
self.attachmentDownloader = attachmentDownloader
disposeBag = DisposeBag()
}

Expand Down Expand Up @@ -434,15 +437,31 @@ final class OpenItemsController {
switch attachment.type {
case .file(let filename, let contentType, let location, _, _):
switch location {
case .local, .localAndChangedRemotely:
// TODO: Change .localAndChangedRemotely case to download first
let file = Files.attachmentFile(in: libraryId, key: key, filename: filename, contentType: contentType)
let url = file.createUrl()
completion(.pdf(library: library, key: key, parentKey: parentKey, url: url))

case .remote:
// TODO: Download first
completion(nil)
case .local:
completion(createPDFPresentation(key: key, parentKey: parentKey, library: library, filename: filename, contentType: contentType))

case .localAndChangedRemotely, .remote:
let disposeBag = DisposeBag()
attachmentDownloader.observable
.observe(on: MainScheduler.instance)
.subscribe(onNext: { [weak self] update in
guard let self, update.libraryId == attachment.libraryId, update.key == attachment.key else { return }
switch update.kind {
case .ready:
completion(createPDFPresentation(key: key, parentKey: parentKey, library: library, filename: filename, contentType: contentType))
downloadDisposeBag = nil

case .cancelled, .failed:
completion(nil)
downloadDisposeBag = nil

case .progress:
break
}
})
.disposed(by: disposeBag)
downloadDisposeBag = disposeBag
attachmentDownloader.downloadIfNeeded(attachment: attachment, parentKey: parentKey)

case .remoteMissing:
DDLogError("OpenItemsController: can't load PDF item (key: \(key), library: \(libraryId)) - remote missing")
Expand All @@ -458,6 +477,12 @@ final class OpenItemsController {
DDLogError("OpenItemsController: can't load PDF item (key: \(key), library: \(libraryId)) - \(error)")
completion(nil)
}

func createPDFPresentation(key: String, parentKey: String?, library: Library, filename: String, contentType: String) -> Presentation {
let file = Files.attachmentFile(in: library.identifier, key: key, filename: filename, contentType: contentType)
let url = file.createUrl()
return .pdf(library: library, key: key, parentKey: parentKey, url: url)
}
}

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

0 comments on commit 429169c

Please sign in to comment.