Skip to content
Navigation Menu
{{ message }}
forked from altstoreio/AltStore
-
-
Notifications
You must be signed in to change notification settings - Fork 390
Expand file tree
/
Copy pathWebViewController.swift
More file actions
356 lines (297 loc) · 11.6 KB
/
Copy pathWebViewController.swift
File metadata and controls
356 lines (297 loc) · 11.6 KB
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
//
// WebViewController.swift
// AltStoreCore
//
// Created by Riley Testut on 10/31/23.
// Copyright © 2023 Riley Testut. All rights reserved.
//
import UIKit
import WebKit
import Combine
public protocol WebViewControllerDelegate: NSObject
{
func webViewControllerDidFinish(_ webViewController: WebViewController)
}
public class WebViewController: UIViewController
{
//MARK: Public Properties
public weak var delegate: WebViewControllerDelegate?
// WKWebView used to display webpages
public private(set) var webView: WKWebView!
public private(set) lazy var backButton: UIBarButtonItem = UIBarButtonItem(image: UIImage(systemName: "chevron.backward"), style: .plain, target: self, action: #selector(WebViewController.goBack(_:)))
public private(set) lazy var forwardButton: UIBarButtonItem = UIBarButtonItem(image: UIImage(systemName: "chevron.forward"), style: .plain, target: self, action: #selector(WebViewController.goForward(_:)))
public private(set) lazy var shareButton: UIBarButtonItem = UIBarButtonItem(barButtonSystemItem: .action, target: self, action: #selector(WebViewController.shareLink(_:)))
public private(set) lazy var reloadButton: UIBarButtonItem = UIBarButtonItem(barButtonSystemItem: .refresh, target: self, action: #selector(WebViewController.refresh(_:)))
public private(set) lazy var stopLoadingButton: UIBarButtonItem = UIBarButtonItem(barButtonSystemItem: .stop, target: self, action: #selector(WebViewController.refresh(_:)))
public private(set) lazy var doneButton: UIBarButtonItem = UIBarButtonItem(barButtonSystemItem: .cancel, target: self, action: #selector(WebViewController.dismissWebViewController(_:)))
//MARK: Private Properties
private let progressView = UIProgressView()
private lazy var refreshButton: UIBarButtonItem = self.reloadButton
private let initialReqest: URLRequest?
private var ignoreUpdateProgress: Bool = false
private var cancellables: Set<AnyCancellable> = []
public required init(request: URLRequest?, configuration: WKWebViewConfiguration = WKWebViewConfiguration())
{
self.initialReqest = request
super.init(nibName: nil, bundle: nil)
self.webView = WKWebView(frame: CGRectZero, configuration: configuration)
self.webView.allowsBackForwardNavigationGestures = true
self.progressView.progressViewStyle = .bar
self.progressView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
self.progressView.progress = 0.5
self.progressView.alpha = 0.0
self.progressView.isHidden = true
}
public convenience init(url: URL?, configuration: WKWebViewConfiguration = WKWebViewConfiguration())
{
if let url
{
let request = URLRequest(url: url)
self.init(request: request, configuration: configuration)
}
else
{
self.init(request: nil, configuration: configuration)
}
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
//MARK: UIViewController
public override func loadView()
{
self.preparePipeline()
if let request = self.initialReqest
{
self.webView.load(request)
}
self.view = self.webView
}
public override func viewDidLoad()
{
super.viewDidLoad()
self.navigationController?.isModalInPresentation = true
self.navigationController?.view.tintColor = .altPrimary
if let navigationBar = self.navigationController?.navigationBar
{
navigationBar.scrollEdgeAppearance = navigationBar.standardAppearance
}
if let toolbar = self.navigationController?.toolbar, #available(iOS 15, *)
{
toolbar.scrollEdgeAppearance = toolbar.standardAppearance
}
}
public override func viewIsAppearing(_ animated: Bool)
{
super.viewIsAppearing(animated)
if self.webView.estimatedProgress < 1.0
{
self.transitionCoordinator?.animate(alongsideTransition: { context in
self.showProgressBar(animated: true)
}) { context in
if context.isCancelled
{
self.hideProgressBar(animated: false)
}
}
}
self.navigationController?.setToolbarHidden(false, animated: false)
self.update()
}
public override func viewWillDisappear(_ animated: Bool)
{
super.viewWillDisappear(animated)
var shouldHideToolbarItems = true
if let toolbarItems = self.navigationController?.topViewController?.toolbarItems
{
if toolbarItems.count > 0
{
shouldHideToolbarItems = false
}
}
if shouldHideToolbarItems
{
self.navigationController?.setToolbarHidden(true, animated: false)
}
self.transitionCoordinator?.animate(alongsideTransition: { context in
self.hideProgressBar(animated: true)
}) { (context) in
if context.isCancelled && self.webView.estimatedProgress < 1.0
{
self.showProgressBar(animated: false)
}
}
}
public override func didMove(toParent parent: UIViewController?)
{
super.didMove(toParent: parent)
if parent == nil
{
self.webView.stopLoading()
}
}
deinit
{
self.webView.stopLoading()
}
}
private extension WebViewController
{
func preparePipeline()
{
self.webView.publisher(for: \.title, options: [.initial, .new])
.sink { [weak self] title in
self?.title = title
}
.store(in: &self.cancellables)
self.webView.publisher(for: \.estimatedProgress, options: [.new])
.sink { [weak self] progress in
self?.updateProgress(progress)
}
.store(in: &self.cancellables)
Publishers.Merge3(
self.webView.publisher(for: \.isLoading, options: [.new]),
self.webView.publisher(for: \.canGoBack, options: [.new]),
self.webView.publisher(for: \.canGoForward, options: [.new])
)
.sink { [weak self] _ in
self?.update()
}
.store(in: &self.cancellables)
}
func update()
{
if self.webView.isLoading
{
self.refreshButton = self.stopLoadingButton
}
else
{
self.refreshButton = self.reloadButton
}
self.backButton.isEnabled = self.webView.canGoBack
self.forwardButton.isEnabled = self.webView.canGoForward
self.navigationItem.leftBarButtonItem = self.doneButton
self.navigationItem.rightBarButtonItem = self.refreshButton
self.toolbarItems = [self.backButton, .fixedSpace(70), self.forwardButton, .flexibleSpace(), self.shareButton]
}
func updateProgress(_ progress: Double)
{
if self.progressView.isHidden
{
self.showProgressBar(animated: true)
}
if self.ignoreUpdateProgress
{
self.ignoreUpdateProgress = false
self.hideProgressBar(animated: true)
}
else if progress < Double(self.progressView.progress)
{
// If progress is less than self.progressView.progress, another webpage began to load before the first one completed
// In this case, we set the progress back to 0.0, and then wait until the next updateProgress, because it results in a much better animation
self.progressView.setProgress(0.0, animated: false)
}
else
{
UIView.animate(withDuration: 0.4, animations: {
self.progressView.setProgress(Float(progress), animated: true)
}, completion: { (finished) in
if progress == 1.0
{
// This delay serves two purposes. One, it keeps the progress bar on screen just a bit longer so it doesn't appear to disappear too quickly.
// Two, it allows us to prevent the progress bar from disappearing if the user actually started loading another webpage before the current one finished loading.
DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) {
if self.webView.estimatedProgress == 1.0
{
self.hideProgressBar(animated: true)
}
}
}
})
}
}
func showProgressBar(animated: Bool)
{
let navigationBarBounds = self.navigationController?.navigationBar.bounds ?? .zero
self.progressView.frame = CGRect(x: 0, y: navigationBarBounds.height - self.progressView.bounds.height, width: navigationBarBounds.width, height: self.progressView.bounds.height)
self.navigationController?.navigationBar.addSubview(self.progressView)
self.progressView.setProgress(Float(self.webView.estimatedProgress), animated: false)
self.progressView.isHidden = false
if animated
{
UIView.animate(withDuration: 0.4) {
self.progressView.alpha = 1.0
}
}
else
{
self.progressView.alpha = 1.0
}
}
func hideProgressBar(animated: Bool)
{
if animated
{
UIView.animate(withDuration: 0.4, animations: {
self.progressView.alpha = 0.0
}, completion: { (finished) in
self.progressView.setProgress(0.0, animated: false)
self.progressView.isHidden = true
self.progressView.removeFromSuperview()
})
}
else
{
self.progressView.alpha = 0.0
// Completion
self.progressView.setProgress(0.0, animated: false)
self.progressView.isHidden = true
self.progressView.removeFromSuperview()
}
}
}
@objc
private extension WebViewController
{
func goBack(_ sender: UIBarButtonItem)
{
self.webView.goBack()
}
func goForward(_ sender: UIBarButtonItem)
{
self.webView.goForward()
}
func refresh(_ sender: UIBarButtonItem)
{
if self.webView.isLoading
{
self.ignoreUpdateProgress = true
self.webView.stopLoading()
}
else
{
if let initialRequest = self.initialReqest, self.webView.url == nil && self.webView.backForwardList.backList.count == 0
{
self.webView.load(initialRequest)
}
else
{
self.webView.reload()
}
}
}
func shareLink(_ sender: UIBarButtonItem)
{
let url = self.webView.url ?? (NSURL() as URL)
let activityViewController = UIActivityViewController(activityItems: [url], applicationActivities: nil)
activityViewController.modalPresentationStyle = .popover
activityViewController.popoverPresentationController?.barButtonItem = sender
self.present(activityViewController, animated: true)
}
func dismissWebViewController(_ sender: UIBarButtonItem)
{
self.delegate?.webViewControllerDidFinish(self)
self.parent?.dismiss(animated: true)
}
}
You can’t perform that action at this time.
