@@ -416,65 +416,65 @@ final class OpenItemsController {
416
416
}
417
417
418
418
private func loadPresentation( for item: Item , completion: @escaping ( Presentation ? ) -> Void ) {
419
- switch item. kind {
420
- case . pdf( let libraryId, let key) :
421
- loadPDFPresentation ( key: key, libraryId: libraryId, completion: completion)
419
+ do {
420
+ try dbStorage. perform ( on: . main) { coordinator in
421
+ switch item. kind {
422
+ case . pdf( let libraryId, let key) :
423
+ try loadPDFPresentation ( key: key, libraryId: libraryId, coordinator: coordinator, completion: completion)
422
424
423
- case . note( let libraryId, let key) :
424
- loadNotePresentation ( key: key, libraryId: libraryId, completion: completion)
425
+ case . note( let libraryId, let key) :
426
+ try loadNotePresentation ( key: key, libraryId: libraryId, coordinator: coordinator, completion: completion)
427
+ }
428
+ }
429
+ } catch let error {
430
+ DDLogError ( " OpenItemsController: can't load item \( item) - \( error) " )
431
+ completion ( nil )
425
432
}
426
433
427
- func loadPDFPresentation( key: String , libraryId: LibraryIdentifier , completion: @escaping ( Presentation ? ) -> Void ) {
428
- do {
429
- try dbStorage. perform ( on: . main) { coordinator in
430
- let library : Library = try coordinator. perform ( request: ReadLibraryDbRequest ( libraryId: libraryId) )
431
- let rItem = try coordinator. perform ( request: ReadItemDbRequest ( libraryId: libraryId, key: key) )
432
- let parentKey = rItem. parent? . key
433
- guard let attachment = AttachmentCreator . attachment ( for: rItem, fileStorage: fileStorage, urlDetector: nil ) else {
434
- completion ( nil )
435
- return
436
- }
437
- switch attachment. type {
438
- case . file( let filename, let contentType, let location, _, _) :
439
- switch location {
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)
465
-
466
- case . remoteMissing:
467
- DDLogError ( " OpenItemsController: can't load PDF item (key: \( key) , library: \( libraryId) ) - remote missing " )
468
- completion ( nil )
469
- }
470
-
471
- case . url:
472
- DDLogError ( " OpenItemsController: can't load PDF item (key: \( key) , library: \( libraryId) ) - not a file attachment " )
473
- completion ( nil )
474
- }
434
+ func loadPDFPresentation( key: String , libraryId: LibraryIdentifier , coordinator: DbCoordinator , completion: @escaping ( Presentation ? ) -> Void ) throws {
435
+ let library : Library = try coordinator. perform ( request: ReadLibraryDbRequest ( libraryId: libraryId) )
436
+ let rItem = try coordinator. perform ( request: ReadItemDbRequest ( libraryId: libraryId, key: key) )
437
+ let parentKey = rItem. parent? . key
438
+ guard let attachment = AttachmentCreator . attachment ( for: rItem, fileStorage: fileStorage, urlDetector: nil ) else {
439
+ completion ( nil )
440
+ return
441
+ }
442
+ switch attachment. type {
443
+ case . file( let filename, let contentType, let location, _, _) :
444
+ switch location {
445
+ case . local:
446
+ completion ( createPDFPresentation ( key: key, parentKey: parentKey, library: library, filename: filename, contentType: contentType) )
447
+
448
+ case . localAndChangedRemotely, . remote:
449
+ let disposeBag = DisposeBag ( )
450
+ attachmentDownloader. observable
451
+ . observe ( on: MainScheduler . instance)
452
+ . subscribe ( onNext: { [ weak self] update in
453
+ guard let self, update. libraryId == attachment. libraryId, update. key == attachment. key else { return }
454
+ switch update. kind {
455
+ case . ready:
456
+ completion ( createPDFPresentation ( key: key, parentKey: parentKey, library: library, filename: filename, contentType: contentType) )
457
+ downloadDisposeBag = nil
458
+
459
+ case . cancelled, . failed:
460
+ completion ( nil )
461
+ downloadDisposeBag = nil
462
+
463
+ case . progress:
464
+ break
465
+ }
466
+ } )
467
+ . disposed ( by: disposeBag)
468
+ downloadDisposeBag = disposeBag
469
+ attachmentDownloader. downloadIfNeeded ( attachment: attachment, parentKey: parentKey)
470
+
471
+ case . remoteMissing:
472
+ DDLogError ( " OpenItemsController: can't load PDF item (key: \( key) , library: \( libraryId) ) - remote missing " )
473
+ completion ( nil )
475
474
}
476
- } catch let error {
477
- DDLogError ( " OpenItemsController: can't load PDF item (key: \( key) , library: \( libraryId) ) - \( error) " )
475
+
476
+ case . url:
477
+ DDLogError ( " OpenItemsController: can't load PDF item (key: \( key) , library: \( libraryId) ) - not a file attachment " )
478
478
completion ( nil )
479
479
}
480
480
@@ -485,22 +485,15 @@ final class OpenItemsController {
485
485
}
486
486
}
487
487
488
- func loadNotePresentation( key: String , libraryId: LibraryIdentifier , completion: @escaping ( Presentation ? ) -> Void ) {
489
- do {
490
- try dbStorage. perform ( on: . main) { coordinator in
491
- let library = try coordinator. perform ( request: ReadLibraryDbRequest ( libraryId: libraryId) )
492
- let rItem = try coordinator. perform ( request: ReadItemDbRequest ( libraryId: libraryId, key: key) )
493
- guard let note = Note ( item: rItem) else {
494
- completion ( nil )
495
- return
496
- }
497
- let parentTitleData : NoteEditorState . TitleData ? = rItem. parent. flatMap { . init( type: $0. rawType, title: $0. displayTitle) }
498
- completion ( . note( library: library, key: note. key, text: note. text, tags: note. tags, parentTitleData: parentTitleData, title: note. title) )
499
- }
500
- } catch let error {
501
- DDLogError ( " OpenItemsController: can't load note item (key: \( key) , library: \( libraryId) ) - \( error) " )
488
+ func loadNotePresentation( key: String , libraryId: LibraryIdentifier , coordinator: DbCoordinator , completion: @escaping ( Presentation ? ) -> Void ) throws {
489
+ let library = try coordinator. perform ( request: ReadLibraryDbRequest ( libraryId: libraryId) )
490
+ let rItem = try coordinator. perform ( request: ReadItemDbRequest ( libraryId: libraryId, key: key) )
491
+ guard let note = Note ( item: rItem) else {
502
492
completion ( nil )
493
+ return
503
494
}
495
+ let parentTitleData : NoteEditorState . TitleData ? = rItem. parent. flatMap { . init( type: $0. rawType, title: $0. displayTitle) }
496
+ completion ( . note( library: library, key: note. key, text: note. text, tags: note. tags, parentTitleData: parentTitleData, title: note. title) )
504
497
}
505
498
}
506
499
}
0 commit comments