forked from readium/swift-toolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVisualNavigator.swift
78 lines (62 loc) · 2.86 KB
/
VisualNavigator.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
//
// Copyright 2019 Readium Foundation. All rights reserved.
// Use of this source code is governed by the BSD-style license
// available in the top-level LICENSE file of the project.
//
import Foundation
import UIKit
import R2Shared
/// A navigator rendering the publication visually on-screen.
public protocol VisualNavigator: Navigator {
/// Viewport view.
var view: UIView! { get }
/// Current reading progression direction.
var readingProgression: ReadingProgression { get }
/// Moves to the left content portion (eg. page) relative to the reading progression direction.
/// - Parameter completion: Called when the transition is completed.
/// - Returns: Whether the navigator is able to move to the previous content portion. The completion block is only called if true was returned.
@discardableResult
func goLeft(animated: Bool, completion: @escaping () -> Void) -> Bool
/// Moves to the right content portion (eg. page) relative to the reading progression direction.
/// - Parameter completion: Called when the transition is completed.
/// - Returns: Whether the navigator is able to move to the previous content portion. The completion block is only called if true was returned.
@discardableResult
func goRight(animated: Bool, completion: @escaping () -> Void) -> Bool
/// Returns the `Locator` to the first content element that begins on the current screen.
func firstVisibleElementLocator(completion: @escaping (Locator?) -> Void)
}
public extension VisualNavigator {
func firstVisibleElementLocator(completion: @escaping (Locator?) -> ()) {
DispatchQueue.main.async {
completion(currentLocation)
}
}
@discardableResult
func goLeft(animated: Bool = false, completion: @escaping () -> Void = {}) -> Bool {
switch readingProgression {
case .ltr, .ttb, .auto:
return goBackward(animated: animated, completion: completion)
case .rtl, .btt:
return goForward(animated: animated, completion: completion)
}
}
@discardableResult
func goRight(animated: Bool = false, completion: @escaping () -> Void = {}) -> Bool {
switch readingProgression {
case .ltr, .ttb, .auto:
return goForward(animated: animated, completion: completion)
case .rtl, .btt:
return goBackward(animated: animated, completion: completion)
}
}
}
public protocol VisualNavigatorDelegate: NavigatorDelegate {
/// Called when the user tapped the publication, and it didn't trigger any internal action.
/// The point is relative to the navigator's view.
func navigator(_ navigator: VisualNavigator, didTapAt point: CGPoint)
}
public extension VisualNavigatorDelegate {
func navigator(_ navigator: VisualNavigator, didTapAt point: CGPoint) {
// Optional
}
}