Skip to content

Commit dbc07cc

Browse files
authored
Add option to disable horizontal swiping when EPUB scroll is true (#531)
1 parent ebcfcf3 commit dbc07cc

File tree

5 files changed

+38
-8
lines changed

5 files changed

+38
-8
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ All notable changes to this project will be documented in this file. Take a look
88

99
* The Readium Swift toolkit now requires a minimum of iOS 13.4.
1010

11+
### Added
12+
13+
#### Navigator
14+
15+
* The `EPUBNavigatorViewController.Configuration.disablePageTurnsWhileScrolling` property disables horizontal swipes for navigating to previous or next resources when scroll mode is enabled. When set to `true`, you must implement your own mechanism to move to the next resource (contributed by [@alecdhansen](https://github.com/readium/swift-toolkit/pull/531)).
16+
1117
### Changed
1218

1319
#### Shared

Sources/Navigator/EPUB/EPUBNavigatorViewController.swift

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ open class EPUBNavigatorViewController: UIViewController,
6868
/// `UIViewController` wrapping the `EPUBNavigatorViewController`.
6969
public var editingActions: [EditingAction]
7070

71+
/// Disables horizontal page turning when scroll is enabled.
72+
public var disablePageTurnsWhileScrolling: Bool
73+
7174
/// Content insets used to add some vertical margins around reflowable EPUB publications.
7275
/// The insets can be configured for each size class to allow smaller margins on compact
7376
/// screens.
@@ -97,6 +100,7 @@ open class EPUBNavigatorViewController: UIViewController,
97100
preferences: EPUBPreferences = .empty,
98101
defaults: EPUBDefaults = EPUBDefaults(),
99102
editingActions: [EditingAction] = EditingAction.defaultActions,
103+
disablePageTurnsWhileScrolling: Bool = false,
100104
contentInset: [UIUserInterfaceSizeClass: EPUBContentInsets] = [
101105
.compact: (top: 20, bottom: 20),
102106
.regular: (top: 44, bottom: 44),
@@ -111,6 +115,7 @@ open class EPUBNavigatorViewController: UIViewController,
111115
self.preferences = preferences
112116
self.defaults = defaults
113117
self.editingActions = editingActions
118+
self.disablePageTurnsWhileScrolling = disablePageTurnsWhileScrolling
114119
self.contentInset = contentInset
115120
self.preloadPreviousPositionCount = preloadPreviousPositionCount
116121
self.preloadNextPositionCount = preloadNextPositionCount
@@ -516,7 +521,8 @@ open class EPUBNavigatorViewController: UIViewController,
516521
let view = PaginationView(
517522
frame: .zero,
518523
preloadPreviousPositionCount: hasPositions ? config.preloadPreviousPositionCount : 0,
519-
preloadNextPositionCount: hasPositions ? config.preloadNextPositionCount : 0
524+
preloadNextPositionCount: hasPositions ? config.preloadNextPositionCount : 0,
525+
isScrollEnabled: isPaginationViewScrollingEnabled
520526
)
521527
view.delegate = self
522528
view.backgroundColor = .clear
@@ -608,6 +614,10 @@ open class EPUBNavigatorViewController: UIViewController,
608614

609615
// MARK: - Navigator
610616

617+
private var isPaginationViewScrollingEnabled: Bool {
618+
!(config.disablePageTurnsWhileScrolling && settings.scroll)
619+
}
620+
611621
public var presentation: VisualNavigatorPresentation {
612622
VisualNavigatorPresentation(
613623
readingProgression: settings.readingProgression,
@@ -844,6 +854,7 @@ open class EPUBNavigatorViewController: UIViewController,
844854
}
845855

846856
view.backgroundColor = settings.effectiveBackgroundColor.uiColor
857+
paginationView.isScrollEnabled = isPaginationViewScrollingEnabled
847858
}
848859

849860
// MARK: - User interactions
@@ -902,6 +913,7 @@ open class EPUBNavigatorViewController: UIViewController,
902913
extension EPUBNavigatorViewController: EPUBNavigatorViewModelDelegate {
903914
func epubNavigatorViewModelInvalidatePaginationView(_ viewModel: EPUBNavigatorViewModel) {
904915
Task {
916+
paginationView.isScrollEnabled = isPaginationViewScrollingEnabled
905917
await reloadSpreads(force: true)
906918
}
907919
}

Sources/Navigator/Toolkit/PaginationView.swift

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,20 @@ final class PaginationView: UIView, Loggable {
9393

9494
private let scrollView = UIScrollView()
9595

96-
init(frame: CGRect, preloadPreviousPositionCount: Int, preloadNextPositionCount: Int) {
96+
/// Allows the scroll view to scroll.
97+
var isScrollEnabled: Bool {
98+
didSet { scrollView.isScrollEnabled = isScrollEnabled }
99+
}
100+
101+
init(
102+
frame: CGRect,
103+
preloadPreviousPositionCount: Int,
104+
preloadNextPositionCount: Int,
105+
isScrollEnabled: Bool
106+
) {
97107
self.preloadPreviousPositionCount = preloadPreviousPositionCount
98108
self.preloadNextPositionCount = preloadNextPositionCount
109+
self.isScrollEnabled = isScrollEnabled
99110

100111
super.init(frame: frame)
101112

@@ -105,6 +116,7 @@ final class PaginationView: UIView, Loggable {
105116
scrollView.isPagingEnabled = true
106117
scrollView.bounces = false
107118
scrollView.showsHorizontalScrollIndicator = false
119+
scrollView.isScrollEnabled = isScrollEnabled
108120
addSubview(scrollView)
109121

110122
// Adds an empty view before the scroll view to have a consistent behavior on all iOS
@@ -316,7 +328,7 @@ final class PaginationView: UIView, Loggable {
316328
return
317329
}
318330

319-
scrollView.isScrollEnabled = true
331+
scrollView.isScrollEnabled = isScrollEnabled
320332
await setCurrentIndex(index, location: location)
321333

322334
scrollView.scrollRectToVisible(CGRect(
@@ -341,17 +353,17 @@ extension PaginationView: UIScrollViewDelegate {
341353
}
342354

343355
func scrollViewDidEndScrollingAnimation(_ scrollView: UIScrollView) {
344-
scrollView.isScrollEnabled = true
356+
scrollView.isScrollEnabled = isScrollEnabled
345357
}
346358

347359
func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
348360
if !decelerate {
349-
scrollView.isScrollEnabled = true
361+
scrollView.isScrollEnabled = isScrollEnabled
350362
}
351363
}
352364

353365
public func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
354-
scrollView.isScrollEnabled = true
366+
scrollView.isScrollEnabled = isScrollEnabled
355367

356368
let currentOffset = (readingProgression == .rtl)
357369
? scrollView.contentSize.width - (scrollView.contentOffset.x + scrollView.frame.width)

Sources/Shared/Toolkit/HTTP/DefaultHTTPClient.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@ public final class DefaultHTTPClient: HTTPClient, Loggable {
429429
return
430430
}
431431

432-
var response = HTTPResponse(request: request, response: urlResponse, url: url)
432+
let response = HTTPResponse(request: request, response: urlResponse, url: url)
433433

434434
guard response.status.isSuccess else {
435435
state = .failure(continuation: continuation, error: .errorResponse(response))

TestApp/Sources/Reader/EPUB/EPUBViewController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class EPUBViewController: VisualReaderViewController<EPUBNavigatorViewController
3737
let navigator = try EPUBNavigatorViewController(
3838
publication: publication,
3939
initialLocation: locator,
40-
config: .init(
40+
config: EPUBNavigatorViewController.Configuration(
4141
preferences: initialPreferences,
4242
editingActions: EditingAction.defaultActions
4343
.appending(EditingAction(

0 commit comments

Comments
 (0)