@@ -286,9 +286,9 @@ open class EPUBNavigatorViewController: UIViewController, VisualNavigator, Selec
286
286
open override func viewWillTransition( to size: CGSize , with coordinator: UIViewControllerTransitionCoordinator ) {
287
287
super. viewWillTransition ( to: size, with: coordinator)
288
288
289
- coordinator. animate ( alongsideTransition: { [ weak self] context in
289
+ coordinator. animate ( alongsideTransition: nil ) { [ weak self] context in
290
290
self ? . reloadSpreads ( )
291
- } )
291
+ }
292
292
}
293
293
294
294
@discardableResult
@@ -429,31 +429,57 @@ open class EPUBNavigatorViewController: UIViewController, VisualNavigator, Selec
429
429
return publication. readingOrder. firstIndex ( withHREF: spreads [ currentSpreadIndex] . left. href)
430
430
}
431
431
432
- private func reloadSpreads( at locator: Locator ? = nil ) {
433
- let isLandscape = ( view. bounds. width > view. bounds. height)
434
- let pageCountPerSpread = EPUBSpread . pageCountPerSpread ( for: publication, userSettings: userSettings, isLandscape: isLandscape)
432
+ private let reloadSpreadsCompletions = CompletionList ( )
433
+ private var needsReloadSpreads = false
434
+
435
+ private func reloadSpreads( at locator: Locator ? = nil , completion: ( ( ) -> Void ) ? = nil ) {
436
+ assert ( Thread . isMainThread, " reloadSpreads() must be called from the main thread " )
437
+
438
+ guard !needsReloadSpreads else {
439
+ if let completion = completion {
440
+ reloadSpreadsCompletions. add ( completion)
441
+ }
442
+ return
443
+ }
444
+
445
+ needsReloadSpreads = true
446
+
447
+ DispatchQueue . main. async {
448
+ self . needsReloadSpreads = false
449
+
450
+ self . _reloadSpreads ( at: locator) {
451
+ self . reloadSpreadsCompletions. complete ( )
452
+ }
453
+ }
454
+ }
455
+
456
+ private func _reloadSpreads( at locator: Locator ? = nil , completion: @escaping ( ) -> Void ) {
457
+ let isLandscape = ( self . view. bounds. width > self . view. bounds. height)
458
+ let pageCountPerSpread = EPUBSpread . pageCountPerSpread ( for: self . publication, userSettings: self . userSettings, isLandscape: isLandscape)
435
459
436
460
guard
437
461
// Already loaded with the expected amount of spreads.
438
- spreads. first? . pageCount != pageCountPerSpread,
439
- on ( . load)
462
+ self . spreads. first? . pageCount != pageCountPerSpread,
463
+ self . on ( . load)
440
464
else {
465
+ completion ( )
441
466
return
442
467
}
443
468
444
- let locator = locator ?? currentLocation
445
- spreads = EPUBSpread . makeSpreads ( for: publication, readingProgression: readingProgression, pageCountPerSpread: pageCountPerSpread)
469
+ let locator = locator ?? self . currentLocation
470
+ self . spreads = EPUBSpread . makeSpreads ( for: self . publication, readingProgression: self . readingProgression, pageCountPerSpread: pageCountPerSpread)
446
471
447
472
let initialIndex : Int = {
448
- if let href = locator? . href, let foundIndex = spreads. firstIndex ( withHref: href) {
473
+ if let href = locator? . href, let foundIndex = self . spreads. firstIndex ( withHref: href) {
449
474
return foundIndex
450
475
} else {
451
476
return 0
452
477
}
453
478
} ( )
454
479
455
- paginationView. reloadAtIndex ( initialIndex, location: PageLocation ( locator) , pageCount: spreads. count, readingProgression: readingProgression) {
480
+ self . paginationView. reloadAtIndex ( initialIndex, location: PageLocation ( locator) , pageCount: self . spreads. count, readingProgression: self . readingProgression) {
456
481
self . on ( . loaded)
482
+ completion ( )
457
483
}
458
484
}
459
485
0 commit comments