@@ -136,6 +136,10 @@ open class EPUBNavigatorViewController: InputObservableViewController,
136
136
/// Indices of the visible reading order resources.
137
137
public var readingOrderIndices : ClosedRange < [ Link ] . Index>
138
138
139
+ /// Range of visible scroll progressions for each visible reading order
140
+ /// resource.
141
+ public var progressions : [ [ Link ] . Index: ClosedRange < Double > ]
142
+
139
143
/// Range of visible positions.
140
144
public var positions : ClosedRange < Int > ?
141
145
}
@@ -623,44 +627,57 @@ open class EPUBNavigatorViewController: InputObservableViewController,
623
627
return ( nil , nil )
624
628
}
625
629
626
- let index = spreadView. focusedResource ?? spreadView. spread. leading
627
- let link = readingOrder [ index]
628
- let href = link. url ( )
629
- let progressionRange = spreadView. progression ( in: index)
630
- let firstProgression = min ( max ( progressionRange. lowerBound, 0.0 ) , 1.0 )
631
- let lastProgression = min ( max ( progressionRange. upperBound, 0.0 ) , 1.0 )
632
-
633
- let location : Locator ?
634
630
var viewport = Viewport (
635
631
readingOrderIndices: spreadView. spread. readingOrderIndices,
632
+ progressions: spreadView. spread. readingOrderIndices. reduce ( [ : ] ) { progressions, index in
633
+ var progressions = progressions
634
+ progressions [ index] = spreadView. progression ( in: index)
635
+ return progressions
636
+ } ,
636
637
positions: nil
637
638
)
638
639
640
+ let firstIndex = spreadView. spread. readingOrderIndices. lowerBound
641
+ let lastIndex = spreadView. spread. readingOrderIndices. upperBound
642
+ let progressionOfFirstResource = spreadView. progression ( in: firstIndex)
643
+ let progressionOfLastResource = spreadView. progression ( in: lastIndex)
644
+ let firstProgressionInFirstResource = min ( max ( progressionOfFirstResource. lowerBound, 0.0 ) , 1.0 )
645
+ let lastProgressionInLastResource = min ( max ( progressionOfLastResource. upperBound, 0.0 ) , 1.0 )
646
+
647
+ let link = readingOrder [ firstIndex]
648
+ let location : Locator ?
649
+
639
650
if
640
651
// The positions are not always available, for example a Readium
641
652
// WebPub doesn't have any unless a Publication Positions Web
642
653
// Service is provided
643
- let index = readingOrder . firstIndexWithHREF ( href ) ,
644
- let positionList = positionsByReadingOrder. getOrNil ( index ) ,
645
- positionList . count > 0 ,
646
- let positionOffset = positionList [ 0 ] . locations . position
654
+ let positionsOfFirstResource = positionsByReadingOrder . getOrNil ( firstIndex ) ,
655
+ let positionsOfLastResource = positionsByReadingOrder. getOrNil ( lastIndex ) ,
656
+ !positionsOfFirstResource . isEmpty ,
657
+ !positionsOfLastResource . isEmpty
647
658
{
648
- // Gets the current locator from the positionList, and fill its missing data.
649
- let firstPositionIndex = Int ( ceil ( firstProgression * Double( positionList. count - 1 ) ) )
650
- let lastPositionIndex = ( lastProgression == 1.0 )
651
- ? positionList. count - 1
652
- : max ( firstPositionIndex, Int ( ceil ( lastProgression * Double( positionList. count - 1 ) ) ) - 1 )
653
-
654
- location = await positionList [ firstPositionIndex] . copy (
655
- title: tableOfContentsTitleByHref [ equivalent: href] ,
656
- locations: { $0. progression = firstProgression }
659
+ // Gets the current locator from the positions, and fill its missing
660
+ // data.
661
+ let firstPositionIndex = Int ( ceil ( firstProgressionInFirstResource * Double( positionsOfFirstResource. count - 1 ) ) )
662
+ let lastPositionIndex = ( lastProgressionInLastResource == 1.0 )
663
+ ? positionsOfLastResource. count - 1
664
+ : max ( firstPositionIndex, Int ( ceil ( lastProgressionInLastResource * Double( positionsOfLastResource. count - 1 ) ) ) - 1 )
665
+
666
+ location = await positionsOfFirstResource [ firstPositionIndex] . copy (
667
+ title: tableOfContentsTitleByHref [ equivalent: link. url ( ) ] ,
668
+ locations: { $0. progression = firstProgressionInFirstResource }
657
669
)
658
670
659
- viewport. positions = ( positionOffset + firstPositionIndex) ... ( positionOffset + lastPositionIndex)
671
+ if
672
+ let firstPosition = location? . locations. position,
673
+ let lastPosition = positionsOfLastResource [ lastPositionIndex] . locations. position
674
+ {
675
+ viewport. positions = firstPosition ... lastPosition
676
+ }
660
677
661
678
} else {
662
679
location = await publication. locate ( link) ? . copy (
663
- locations: { $0. progression = firstProgression }
680
+ locations: { $0. progression = firstProgressionInFirstResource }
664
681
)
665
682
}
666
683
0 commit comments