Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ All notable changes to this project will be documented in this file. Take a look
* Support for the [`conformsTo` RWPM metadata](https://github.com/readium/webpub-manifest/issues/65), to identify the profile of a `Publication`.
* Support for right-to-left PDF documents by extracting the reading progression from the `ViewerPreferences/Direction` metadata.

#### Navigator

* The new `NavigatorDelegate.navigator(_:didJumpTo:)` API is called every time the navigator jumps to an explicit location, which might break the linear reading progression.
* For example, it is called when clicking on internal links or programmatically calling `Navigator.go(to:)`, but not when turning pages.
* You can use this callback to implement a navigation history by differentiating between continuous and discontinuous moves.

### Deprecated

#### Shared
Expand Down
6 changes: 6 additions & 0 deletions Sources/Navigator/Audiobook/AudioNavigator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,12 @@ open class _AudioNavigator: _MediaNavigator, _AudioSessionUser, Loggable {

play()

if let delegate = delegate, let location = currentLocation {
delegate.navigator(self, didJumpTo: location)
}

DispatchQueue.main.async(execute: completion)

return true

} catch {
Expand Down
28 changes: 12 additions & 16 deletions Sources/Navigator/CBZ/CBZNavigatorViewController.swift
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
//
// CBZNavigatorViewController.swift
// r2-navigator-swift
//
// Created by Alexandre Camilleri on 8/24/17.
//
// Copyright 2018 Readium Foundation. All rights reserved.
// Use of this source code is governed by a BSD-style license which is detailed
// in the LICENSE file present in the project repository where this source code is maintained.
// Use of this source code is governed by the BSD-style license
// available in the top-level LICENSE file of the project.
//

import UIKit
Expand Down Expand Up @@ -65,7 +60,7 @@ open class CBZNavigatorViewController: UIViewController, VisualNavigator, Loggab

view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(didTap)))

goToResourceAtIndex(initialIndex)
goToResourceAtIndex(initialIndex, animated: false, isJump: false)
}

private var currentResourceIndex: Int {
Expand All @@ -85,7 +80,7 @@ open class CBZNavigatorViewController: UIViewController, VisualNavigator, Loggab
}

@discardableResult
private func goToResourceAtIndex(_ index: Int, animated: Bool = false, completion: @escaping () -> Void = {}) -> Bool {
private func goToResourceAtIndex(_ index: Int, animated: Bool, isJump: Bool, completion: @escaping () -> Void = {}) -> Bool {
guard let imageViewController = imageViewController(at: index) else {
return false
}
Expand All @@ -105,6 +100,9 @@ open class CBZNavigatorViewController: UIViewController, VisualNavigator, Loggab
return
}
self.delegate?.navigator(self, locationDidChange: position)
if isJump {
self.delegate?.navigator(self, didJumpTo: position)
}
completion()
}
return true
Expand Down Expand Up @@ -140,22 +138,22 @@ open class CBZNavigatorViewController: UIViewController, VisualNavigator, Loggab
guard let index = publication.readingOrder.firstIndex(withHREF: locator.href) else {
return false
}
return goToResourceAtIndex(index, animated: animated, completion: completion)
return goToResourceAtIndex(index, animated: animated, isJump: true, completion: completion)
}

public func go(to link: Link, animated: Bool, completion: @escaping () -> Void) -> Bool {
guard let index = publication.readingOrder.firstIndex(withHREF: link.href) else {
return false
}
return goToResourceAtIndex(index, animated: animated, completion: completion)
return goToResourceAtIndex(index, animated: animated, isJump: true, completion: completion)
}

public func goForward(animated: Bool, completion: @escaping () -> Void) -> Bool {
return goToResourceAtIndex(currentResourceIndex + 1, animated: animated, completion: completion)
return goToResourceAtIndex(currentResourceIndex + 1, animated: animated, isJump: false, completion: completion)
}

public func goBackward(animated: Bool, completion: @escaping () -> Void) -> Bool {
return goToResourceAtIndex(currentResourceIndex - 1, animated: animated, completion: completion)
return goToResourceAtIndex(currentResourceIndex - 1, animated: animated, isJump: false, completion: completion)
}

}
Expand Down Expand Up @@ -228,9 +226,7 @@ extension CBZNavigatorViewController {
}

@available(*, unavailable, message: "Use `go(to:)` using the `readingOrder` instead")
public func load(at index: Int) {
goToResourceAtIndex(index, animated: true)
}
public func load(at index: Int) {}

@available(*, unavailable, message: "Use init(publication:initialLocation:) instead")
public convenience init(for publication: Publication, initialIndex: Int = 0) {
Expand Down
1 change: 1 addition & 0 deletions Sources/Navigator/EPUB/EPUBNavigatorViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,7 @@ open class EPUBNavigatorViewController: UIViewController, VisualNavigator, Selec

return paginationView.goToIndex(spreadIndex, location: .locator(locator), animated: animated) {
self.on(.jumped)
self.delegate?.navigator(self, didJumpTo: locator)
completion()
}
}
Expand Down
19 changes: 15 additions & 4 deletions Sources/Navigator/Navigator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,19 @@ public extension Navigator {

public protocol NavigatorDelegate: AnyObject {

/// Called when the current position in the publication changed. You should save the locator here to restore the last read page.
/// Called when the current position in the publication changed. You should save the locator here to restore the
/// last read page.
func navigator(_ navigator: Navigator, locationDidChange locator: Locator)


/// Called when the navigator jumps to an explicit location, which might break the linear reading progression.
///
/// For example, it is called when clicking on internal links or programmatically calling `go()`, but not when
/// turning pages.
///
/// You can use this callback to implement a navigation history by differentiating between continuous and
/// discontinuous moves.
func navigator(_ navigator: Navigator, didJumpTo locator: Locator)

/// Called when an error must be reported to the user.
func navigator(_ navigator: Navigator, presentError error: NavigatorError)

Expand All @@ -97,7 +107,9 @@ public protocol NavigatorDelegate: AnyObject {


public extension NavigatorDelegate {


func navigator(_ navigator: Navigator, didJumpTo locator: Locator) {}

func navigator(_ navigator: Navigator, presentExternalURL url: URL) {
if UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
Expand All @@ -107,7 +119,6 @@ public extension NavigatorDelegate {
func navigator(_ navigator: Navigator, shouldNavigateToNoteAt link: Link, content: String, referrer: String?) -> Bool {
return true
}

}


Expand Down
46 changes: 26 additions & 20 deletions Sources/Navigator/PDF/PDFNavigatorViewController.swift
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
//
// PDFNavigatorViewController.swift
// r2-navigator-swift
//
// Created by Mickaël Menu on 05.03.19.
//
// Copyright 2019 Readium Foundation. All rights reserved.
// Use of this source code is governed by a BSD-style license which is detailed
// in the LICENSE file present in the project repository where this source code is maintained.
// Use of this source code is governed by the BSD-style license
// available in the top-level LICENSE file of the project.
//

import Foundation
Expand Down Expand Up @@ -93,9 +88,9 @@ open class PDFNavigatorViewController: UIViewController, VisualNavigator, Select
editingActions.updateSharedMenuController()

if let locator = initialLocation {
go(to: locator)
go(to: locator, isJump: false)
} else if let link = publication.readingOrder.first {
go(to: link)
go(to: link, pageNumber: 0, isJump: false)
} else {
log(.error, "No initial location and empty reading order")
}
Expand Down Expand Up @@ -146,7 +141,22 @@ open class PDFNavigatorViewController: UIViewController, VisualNavigator, Select
delegate?.navigator(self, locationDidChange: locator)
}

private func go(to link: Link, pageNumber: Int? = nil, completion: @escaping () -> Void) -> Bool {
@discardableResult
private func go(to locator: Locator, isJump: Bool, completion: @escaping () -> Void = {}) -> Bool {
guard let index = publication.readingOrder.firstIndex(withHREF: locator.href) else {
return false
}

return go(
to: publication.readingOrder[index],
pageNumber: pageNumber(for: locator),
isJump: isJump,
completion: completion
)
}

@discardableResult
private func go(to link: Link, pageNumber: Int?, isJump: Bool, completion: @escaping () -> Void = {}) -> Bool {
guard let index = publication.readingOrder.firstIndex(of: link) else {
return false
}
Expand Down Expand Up @@ -175,6 +185,10 @@ open class PDFNavigatorViewController: UIViewController, VisualNavigator, Select
}
pdfView.go(to: page)
}
if isJump, let delegate = delegate, let location = currentPosition {
delegate.navigator(self, didJumpTo: location)
}

DispatchQueue.main.async(execute: completion)
return true
}
Expand Down Expand Up @@ -284,19 +298,11 @@ open class PDFNavigatorViewController: UIViewController, VisualNavigator, Select
}

public func go(to locator: Locator, animated: Bool, completion: @escaping () -> Void) -> Bool {
guard let index = publication.readingOrder.firstIndex(withHREF: locator.href) else {
return false
}

return go(
to: publication.readingOrder[index],
pageNumber: pageNumber(for: locator),
completion: completion
)
return go(to: locator, isJump: true, completion: completion)
}

public func go(to link: Link, animated: Bool, completion: @escaping () -> Void) -> Bool {
return go(to: Locator(link: link), animated: animated, completion: completion)
return go(to: link, pageNumber: nil, isJump: true, completion: completion)
}

public func goForward(animated: Bool, completion: @escaping () -> Void) -> Bool {
Expand Down
9 changes: 2 additions & 7 deletions Sources/Streamer/Parser/Audio/AudioParser.swift
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
//
// AudioParser.swift
// r2-streamer-swift
//
// Created by Mickaël Menu on 15/07/2020.
//
// Copyright 2020 Readium Foundation. All rights reserved.
// Use of this source code is governed by a BSD-style license which is detailed
// in the LICENSE file present in the project repository where this source code is maintained.
// Use of this source code is governed by the BSD-style license
// available in the top-level LICENSE file of the project.
//

import Foundation
Expand Down