@@ -286,9 +286,9 @@ open class EPUBNavigatorViewController: UIViewController, VisualNavigator, Selec
286286 open override func viewWillTransition( to size: CGSize , with coordinator: UIViewControllerTransitionCoordinator ) {
287287 super. viewWillTransition ( to: size, with: coordinator)
288288
289- coordinator. animate ( alongsideTransition: { [ weak self] context in
289+ coordinator. animate ( alongsideTransition: nil ) { [ weak self] context in
290290 self ? . reloadSpreads ( )
291- } )
291+ }
292292 }
293293
294294 @discardableResult
@@ -429,31 +429,57 @@ open class EPUBNavigatorViewController: UIViewController, VisualNavigator, Selec
429429 return publication. readingOrder. firstIndex ( withHREF: spreads [ currentSpreadIndex] . left. href)
430430 }
431431
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)
435459
436460 guard
437461 // 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)
440464 else {
465+ completion ( )
441466 return
442467 }
443468
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)
446471
447472 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) {
449474 return foundIndex
450475 } else {
451476 return 0
452477 }
453478 } ( )
454479
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) {
456481 self . on ( . loaded)
482+ completion ( )
457483 }
458484 }
459485
0 commit comments