@@ -346,9 +346,9 @@ open class EPUBNavigatorViewController: UIViewController,
346
346
hasPositions: !positionsByReadingOrder. isEmpty
347
347
)
348
348
349
- paginationView. frame = view. bounds
350
- paginationView. autoresizingMask = [ . flexibleHeight, . flexibleWidth]
351
- view. addSubview ( paginationView)
349
+ paginationView! . frame = view. bounds
350
+ paginationView! . autoresizingMask = [ . flexibleHeight, . flexibleWidth]
351
+ view. addSubview ( paginationView! )
352
352
353
353
applySettings ( )
354
354
@@ -481,7 +481,10 @@ open class EPUBNavigatorViewController: UIViewController,
481
481
482
482
/// Goes to the next or previous page in the given scroll direction.
483
483
private func go( to direction: EPUBSpreadView . Direction , options: NavigatorGoOptions ) async -> Bool {
484
- guard on ( . move( direction) ) else {
484
+ guard
485
+ let paginationView = paginationView,
486
+ on ( . move( direction) )
487
+ else {
485
488
return false
486
489
}
487
490
@@ -515,7 +518,7 @@ open class EPUBNavigatorViewController: UIViewController,
515
518
516
519
// MARK: - Pagination and spreads
517
520
518
- private var paginationView : PaginationView !
521
+ private var paginationView : PaginationView ?
519
522
520
523
private func makePaginationView( hasPositions: Bool ) -> PaginationView {
521
524
let view = PaginationView (
@@ -529,11 +532,20 @@ open class EPUBNavigatorViewController: UIViewController,
529
532
return view
530
533
}
531
534
535
+ private func invalidatePaginationView( ) async {
536
+ guard let paginationView = paginationView else {
537
+ return
538
+ }
539
+
540
+ paginationView. isScrollEnabled = isPaginationViewScrollingEnabled
541
+ await reloadSpreads ( force: true )
542
+ }
543
+
532
544
private var spreads : [ EPUBSpread ] = [ ]
533
545
534
546
/// Index of the currently visible spread.
535
547
private var currentSpreadIndex : Int {
536
- paginationView. currentIndex
548
+ paginationView? . currentIndex ?? 0
537
549
}
538
550
539
551
// Reading order index of the left-most resource in the visible spread.
@@ -575,6 +587,7 @@ open class EPUBNavigatorViewController: UIViewController,
575
587
let locator = locator ?? currentLocation
576
588
577
589
guard
590
+ let paginationView = paginationView,
578
591
// Already loaded with the expected amount of spreads?
579
592
force || spreads. first? . spread != viewModel. spreadEnabled,
580
593
on ( . load( locator) )
@@ -607,7 +620,7 @@ open class EPUBNavigatorViewController: UIViewController,
607
620
}
608
621
609
622
private func loadedSpreadViewForHREF< T: URLConvertible > ( _ href: T ) -> EPUBSpreadView ? {
610
- paginationView. loadedViews
623
+ paginationView? . loadedViews
611
624
. compactMap { _, view in view as? EPUBSpreadView }
612
625
. first { $0. spread. links. firstWithHREF ( href) != nil }
613
626
}
@@ -640,7 +653,7 @@ open class EPUBNavigatorViewController: UIViewController,
640
653
return pendingLocator
641
654
}
642
655
643
- guard let spreadView = paginationView. currentView as? EPUBSpreadView else {
656
+ guard let spreadView = paginationView? . currentView as? EPUBSpreadView else {
644
657
return nil
645
658
}
646
659
@@ -670,7 +683,7 @@ open class EPUBNavigatorViewController: UIViewController,
670
683
}
671
684
672
685
public func firstVisibleElementLocator( ) async -> Locator ? {
673
- guard let spreadView = paginationView. currentView as? EPUBSpreadView else {
686
+ guard let spreadView = paginationView? . currentView as? EPUBSpreadView else {
674
687
return nil
675
688
}
676
689
return await spreadView. findFirstVisibleElementLocator ( )
@@ -705,6 +718,7 @@ open class EPUBNavigatorViewController: UIViewController,
705
718
let locator = publication. normalizeLocator ( locator)
706
719
707
720
guard
721
+ let paginationView = paginationView,
708
722
let spreadIndex = spreads. firstIndexWithHREF ( locator. href) ,
709
723
on ( . jump( locator) )
710
724
else {
@@ -759,6 +773,10 @@ open class EPUBNavigatorViewController: UIViewController,
759
773
}
760
774
761
775
public func clearSelection( ) {
776
+ guard let paginationView = paginationView else {
777
+ return
778
+ }
779
+
762
780
for (_, pageView) in paginationView. loadedViews {
763
781
( pageView as? EPUBSpreadView ) ? . webView. clearSelection ( )
764
782
}
@@ -779,6 +797,10 @@ open class EPUBNavigatorViewController: UIViewController,
779
797
Task {
780
798
await initialized ( )
781
799
800
+ guard let paginationView = paginationView else {
801
+ return
802
+ }
803
+
782
804
await withTaskGroup ( of: Void . self) { tasks in
783
805
let source = self . decorations [ group] ?? [ ]
784
806
let target = decorations. map {
@@ -821,6 +843,10 @@ open class EPUBNavigatorViewController: UIViewController,
821
843
Task {
822
844
await initialized ( )
823
845
846
+ guard let paginationView = paginationView else {
847
+ return
848
+ }
849
+
824
850
await withTaskGroup ( of: Void . self) { tasks in
825
851
for (_, view) in paginationView. loadedViews {
826
852
tasks. addTask {
@@ -854,7 +880,7 @@ open class EPUBNavigatorViewController: UIViewController,
854
880
}
855
881
856
882
view. backgroundColor = settings. effectiveBackgroundColor. uiColor
857
- paginationView. isScrollEnabled = isPaginationViewScrollingEnabled
883
+ paginationView? . isScrollEnabled = isPaginationViewScrollingEnabled
858
884
}
859
885
860
886
// MARK: - User interactions
@@ -872,7 +898,7 @@ open class EPUBNavigatorViewController: UIViewController,
872
898
/// Evaluates the given JavaScript on the currently visible HTML resource.
873
899
@discardableResult
874
900
public func evaluateJavaScript( _ script: String ) async -> Result < Any , Error > {
875
- guard let spreadView = paginationView. currentView as? EPUBSpreadView else {
901
+ guard let spreadView = paginationView? . currentView as? EPUBSpreadView else {
876
902
return . failure( EPUBError . spreadNotLoaded)
877
903
}
878
904
return await spreadView. evaluateScript ( script)
@@ -913,15 +939,18 @@ open class EPUBNavigatorViewController: UIViewController,
913
939
extension EPUBNavigatorViewController : EPUBNavigatorViewModelDelegate {
914
940
func epubNavigatorViewModelInvalidatePaginationView( _ viewModel: EPUBNavigatorViewModel ) {
915
941
Task {
916
- paginationView. isScrollEnabled = isPaginationViewScrollingEnabled
917
- await reloadSpreads ( force: true )
942
+ await invalidatePaginationView ( )
918
943
}
919
944
}
920
945
921
946
func epubNavigatorViewModel( _ viewModel: EPUBNavigatorViewModel , runScript script: String , in scope: EPUBScriptScope ) {
922
947
Task {
923
948
await initialized ( )
924
949
950
+ guard let paginationView = paginationView else {
951
+ return
952
+ }
953
+
925
954
switch scope {
926
955
case . currentResource:
927
956
await ( paginationView. currentView as? EPUBSpreadView ) ? . evaluateScript ( script)
@@ -1159,7 +1188,7 @@ extension EPUBNavigatorViewController: EPUBSpreadViewDelegate {
1159
1188
}
1160
1189
1161
1190
func spreadViewPagesDidChange( _ spreadView: EPUBSpreadView ) {
1162
- if paginationView. currentView == spreadView {
1191
+ if paginationView? . currentView == spreadView {
1163
1192
updateCurrentLocation ( )
1164
1193
}
1165
1194
}
0 commit comments