@@ -105,17 +105,20 @@ final class OpenItemsController {
105
105
// MARK: Properties
106
106
private unowned let dbStorage : DbStorage
107
107
private unowned let fileStorage : FileStorage
108
+ private unowned let attachmentDownloader : AttachmentDownloader
108
109
// TODO: Use a better data structure, such as an ordered set
109
110
private var itemsBySessionIdentifier : [ String : [ Item ] ] = [ : ]
110
111
private var sessionIdentifierByItemKind : [ Item . Kind : String ] = [ : ]
111
112
private var itemsTokenBySessionIdentifier : [ String : NotificationToken ] = [ : ]
112
113
private var observableBySessionIdentifier : [ String : PublishSubject < [ Item ] > ] = [ : ]
113
114
private let disposeBag : DisposeBag
115
+ private var downloadDisposeBag : DisposeBag ?
114
116
115
117
// MARK: Object Lifecycle
116
- init ( dbStorage: DbStorage , fileStorage: FileStorage ) {
118
+ init ( dbStorage: DbStorage , fileStorage: FileStorage , attachmentDownloader : AttachmentDownloader ) {
117
119
self . dbStorage = dbStorage
118
120
self . fileStorage = fileStorage
121
+ self . attachmentDownloader = attachmentDownloader
119
122
disposeBag = DisposeBag ( )
120
123
}
121
124
@@ -434,15 +437,31 @@ final class OpenItemsController {
434
437
switch attachment. type {
435
438
case . file( let filename, let contentType, let location, _, _) :
436
439
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)
446
465
447
466
case . remoteMissing:
448
467
DDLogError ( " OpenItemsController: can't load PDF item (key: \( key) , library: \( libraryId) ) - remote missing " )
@@ -458,6 +477,12 @@ final class OpenItemsController {
458
477
DDLogError ( " OpenItemsController: can't load PDF item (key: \( key) , library: \( libraryId) ) - \( error) " )
459
478
completion ( nil )
460
479
}
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
+ }
461
486
}
462
487
463
488
func loadNotePresentation( key: String , libraryId: LibraryIdentifier , completion: @escaping ( Presentation ? ) -> Void ) {
0 commit comments