-
Notifications
You must be signed in to change notification settings - Fork 28
/
ReadiumView.swift
167 lines (141 loc) · 4.67 KB
/
ReadiumView.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import Combine
import Foundation
import R2Shared
import R2Streamer
import UIKit
import R2Navigator
class ReadiumView : UIView, Loggable {
var readerService: ReaderService = ReaderService()
var readerViewController: ReaderViewController?
var viewController: UIViewController? {
let viewController = sequence(first: self, next: { $0.next }).first(where: { $0 is UIViewController })
return viewController as? UIViewController
}
private var subscriptions = Set<AnyCancellable>()
@objc var file: NSDictionary? = nil {
didSet {
let initialLocation = file?["initialLocation"] as? NSDictionary
if let url = file?["url"] as? String {
self.loadBook(url: url, location: initialLocation)
}
}
}
@objc var location: NSDictionary? = nil {
didSet {
self.updateLocation()
}
}
@objc var settings: NSDictionary? = nil {
didSet {
self.updateUserSettings(settings)
}
}
@objc var onLocationChange: RCTDirectEventBlock?
@objc var onTableOfContents: RCTDirectEventBlock?
func loadBook(
url: String,
location: NSDictionary?
) {
guard let rootViewController = UIApplication.shared.delegate?.window??.rootViewController else { return }
self.readerService.buildViewController(
url: url,
bookId: url,
location: location,
sender: rootViewController,
completion: { vc in
self.addViewControllerAsSubview(vc)
self.location = location
}
)
}
func getLocator() -> Locator? {
return ReaderService.locatorFromLocation(location, readerViewController?.publication)
}
func updateLocation() {
guard let navigator = readerViewController?.navigator else {
return;
}
guard let locator = self.getLocator() else {
return;
}
let cur = navigator.currentLocation
if (cur != nil && locator.hashValue == cur?.hashValue) {
return;
}
navigator.go(
to: locator,
animated: true
)
}
func updateUserSettings(_ settings: NSDictionary?) {
if (readerViewController == nil) {
// defer setting update as view isn't initialized yet
return;
}
if let navigator = readerViewController!.navigator as? EPUBNavigatorViewController {
let userProperties = navigator.userSettings.userProperties
for property in userProperties.properties {
let value = settings?[property.reference]
if (value == nil) {
continue
}
if let e = property as? Enumerable {
e.index = value as! Int
// synchronize background color
if property.reference == ReadiumCSSReference.appearance.rawValue {
if let vc = readerViewController as? EPUBViewController {
vc.setUIColor(for: property)
}
}
} else if let i = property as? Incrementable {
i.value = value as! Float
} else if let s = property as? Switchable {
s.on = value as! Bool
}
}
navigator.updateUserSettingStyle()
}
}
override func removeFromSuperview() {
readerViewController?.willMove(toParent: nil)
readerViewController?.view.removeFromSuperview()
readerViewController?.removeFromParent()
// cancel all current subscriptions
for subscription in subscriptions {
subscription.cancel()
}
subscriptions = Set<AnyCancellable>()
readerViewController = nil
super.removeFromSuperview()
}
private func addViewControllerAsSubview(_ vc: ReaderViewController) {
vc.publisher.sink(
receiveValue: { locator in
self.onLocationChange?(locator.json)
}
)
.store(in: &self.subscriptions)
readerViewController = vc
// if the controller was just instantiated then apply any existing settings
if (settings != nil) {
self.updateUserSettings(settings)
}
readerViewController!.view.frame = self.superview!.frame
self.viewController!.addChild(readerViewController!)
let rootView = self.readerViewController!.view!
self.addSubview(rootView)
self.viewController!.addChild(readerViewController!)
self.readerViewController!.didMove(toParent: self.viewController!)
// bind the reader's view to be constrained to its parent
rootView.translatesAutoresizingMaskIntoConstraints = false
rootView.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
rootView.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true
rootView.leftAnchor.constraint(equalTo: self.leftAnchor).isActive = true
rootView.rightAnchor.constraint(equalTo: self.rightAnchor).isActive = true
self.onTableOfContents?([
"toc": vc.publication.tableOfContents.map({ link in
return link.json
})
])
}
}