Skip to content

Commit b8c67ad

Browse files
committed
Fix scroll direction being ignored on first run
Setting currentScrollDirection to .horizontal was ignored before the user changed it manually. Now, setting it like mentioned, the book scrolls horizontally as expected, and if the user changes it to vertical, it is respected by the app. Not setting the property still scrolls vertically by default, as before.
1 parent b18e2cf commit b8c67ad

File tree

7 files changed

+29
-13
lines changed

7 files changed

+29
-13
lines changed

Source/FolioReaderConfig.swift

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,18 @@ public enum FolioReaderScrollDirection: Int {
2323

2424
/// Sections scroll horizontal and content scroll on vertical
2525
case horizontalWithVerticalContent
26-
26+
27+
/// The default scroll direction, if not overridden; works as .vertical
28+
case defaultVertical
29+
2730
/**
2831
The current scroll direction
2932

3033
- returns: Returns `UICollectionViewScrollDirection`
3134
*/
3235
func collectionViewScrollDirection() -> UICollectionViewScrollDirection {
3336
switch self {
34-
case .vertical:
37+
case .vertical, .defaultVertical:
3538
return .vertical
3639
case .horizontal, .horizontalWithVerticalContent:
3740
return .horizontal
@@ -132,7 +135,7 @@ open class FolioReaderConfig: NSObject {
132135
open var hideBars = false
133136

134137
/// If `canChangeScrollDirection` is `true` it will be overrided by user's option.
135-
open var scrollDirection: FolioReaderScrollDirection = .vertical
138+
open var scrollDirection: FolioReaderScrollDirection = .defaultVertical
136139

137140
/// Enable or disable hability to user change scroll direction on menu.
138141
open var canChangeScrollDirection = true

Source/FolioReaderContainer.swift

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ open class FolioReaderContainer: UIViewController {
6969
kCurrentHighlightStyle: 0,
7070
kCurrentTOCMenu: 0,
7171
kCurrentMediaOverlayStyle: MediaOverlayStyle.default.rawValue,
72-
kCurrentScrollDirection: FolioReaderScrollDirection.vertical.rawValue
72+
kCurrentScrollDirection: FolioReaderScrollDirection.defaultVertical.rawValue
7373
])
7474
}
7575

@@ -95,8 +95,13 @@ open class FolioReaderContainer: UIViewController {
9595

9696
// If user can change scroll direction use the last saved
9797
if readerConfig.canChangeScrollDirection {
98-
let direction = FolioReaderScrollDirection(rawValue: FolioReader.currentScrollDirection) ?? .vertical
99-
readerConfig.scrollDirection = direction
98+
var scrollDirection = FolioReaderScrollDirection(rawValue: FolioReader.currentScrollDirection) ?? .vertical
99+
100+
if (scrollDirection == .defaultVertical && readerConfig.scrollDirection != .defaultVertical) {
101+
scrollDirection = readerConfig.scrollDirection
102+
}
103+
104+
readerConfig.scrollDirection = scrollDirection
100105
}
101106

102107
readerConfig.shouldHideNavigationOnTap = ((readerConfig.hideBars == true) ? true : readerConfig.shouldHideNavigationOnTap)

Source/FolioReaderFontsMenu.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,14 @@ class FolioReaderFontsMenu: UIViewController, SMSegmentViewDelegate, UIGestureRe
234234
layoutDirection.tag = 3
235235
layoutDirection.addSegmentWithTitle(readerConfig.localizedLayoutVertical, onSelectionImage: verticalSelected, offSelectionImage: verticalNormal)
236236
layoutDirection.addSegmentWithTitle(readerConfig.localizedLayoutHorizontal, onSelectionImage: horizontalSelected, offSelectionImage: horizontalNormal)
237-
layoutDirection.selectSegmentAtIndex(Int(FolioReader.currentScrollDirection))
237+
238+
var scrollDirection = FolioReaderScrollDirection(rawValue: FolioReader.currentScrollDirection)
239+
240+
if scrollDirection == .defaultVertical && readerConfig.scrollDirection != .defaultVertical {
241+
scrollDirection = readerConfig.scrollDirection
242+
}
243+
244+
layoutDirection.selectSegmentAtIndex(scrollDirection?.rawValue ?? 0)
238245
menuView.addSubview(layoutDirection)
239246
}
240247

Source/FolioReaderKit.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ open class FolioReader: NSObject {
163163
FolioReader.defaults.setValue(value, forKey: kCurrentScrollDirection)
164164

165165
if let _readerCenter = FolioReader.shared.readerCenter {
166-
let direction = FolioReaderScrollDirection(rawValue: currentScrollDirection) ?? .vertical
166+
let direction = FolioReaderScrollDirection(rawValue: currentScrollDirection) ?? .defaultVertical
167167
_readerCenter.setScrollDirection(direction)
168168
}
169169
}
@@ -266,7 +266,7 @@ func isNight<T> (_ f: T, _ l: T) -> T {
266266
*/
267267
func isDirection<T> (_ vertical: T, _ horizontal: T, _ horizontalContentVertical: T? = nil) -> T {
268268
switch readerConfig.scrollDirection {
269-
case .vertical: return vertical
269+
case .vertical, .defaultVertical: return vertical
270270
case .horizontal: return horizontal
271271
case .horizontalWithVerticalContent: return horizontalContentVertical ?? vertical
272272
}

Source/FolioReaderPage.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -396,15 +396,16 @@ open class FolioReaderPage: UICollectionViewCell, UIWebViewDelegate, UIGestureRe
396396
if !anchor.isEmpty {
397397
let offset = getAnchorOffset(anchor)
398398

399-
if readerConfig.scrollDirection == .vertical {
399+
switch readerConfig.scrollDirection {
400+
case .vertical, .defaultVertical:
400401
let isBeginning = offset < frame.forDirection()/2
401402

402403
if !avoidBeginningAnchors {
403404
scrollPageToOffset(offset, animated: animated)
404405
} else if avoidBeginningAnchors && !isBeginning {
405406
scrollPageToOffset(offset, animated: animated)
406407
}
407-
} else {
408+
case .horizontal, .horizontalWithVerticalContent:
408409
scrollPageToOffset(offset, animated: animated)
409410
}
410411
}

Source/FolioReaderWebView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ open class FolioReaderWebView: UIWebView {
277277

278278
func setupScrollDirection() {
279279
switch readerConfig.scrollDirection {
280-
case .vertical, .horizontalWithVerticalContent:
280+
case .vertical, .defaultVertical, .horizontalWithVerticalContent:
281281
scrollView.isPagingEnabled = false
282282
paginationMode = .unpaginated
283283
scrollView.bounces = true

Source/ScrollScrubber.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ class ScrollScrubber: NSObject, UIScrollViewDelegate {
174174
}
175175

176176
func scrollViewDidScroll(_ scrollView: UIScrollView) {
177-
guard readerConfig.scrollDirection == .vertical || readerConfig.scrollDirection == .horizontalWithVerticalContent else {
177+
guard readerConfig.scrollDirection == .vertical || readerConfig.scrollDirection == .defaultVertical || readerConfig.scrollDirection == .horizontalWithVerticalContent else {
178178
return
179179
}
180180

0 commit comments

Comments
 (0)