-
Notifications
You must be signed in to change notification settings - Fork 406
/
WebContainerViewController.swift
105 lines (84 loc) · 2.98 KB
/
WebContainerViewController.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
//
// WebContainerViewController.swift
// DuckDuckGo
//
// Copyright © 2019 DuckDuckGo. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import Common
import Core
import UIKit
import WebKit
import Networking
/// Use title property to set the displayed title
class WebContainerViewController: UIViewController {
@IBOutlet weak var progressView: UIProgressView!
weak var webView: WKWebView?
var url: URL? {
didSet {
if let url = url {
load(url: url)
}
}
}
var progress: Float = 0.0 {
didSet {
progressView.progress = progress
progressView.isHidden = progress >= 1.0
}
}
override func viewDidLoad() {
super.viewDidLoad()
applyTheme(ThemeManager.shared.currentTheme)
let webView = WKWebView(frame: view.frame)
webView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
webView.addObserver(self, forKeyPath: #keyPath(WKWebView.estimatedProgress), options: .new, context: nil)
view.addSubview(webView)
self.webView = webView
if let url = url {
load(url: url)
}
view.bringSubviewToFront(progressView)
}
@IBAction func dismiss() {
dismiss(animated: true)
}
private func load(url: URL) {
var request = URLRequest.userInitiated(url)
request.addValue(DefaultUserAgentManager.duckDuckGoUserAgent, forHTTPHeaderField: "User-Agent")
webView?.load(request)
}
// swiftlint:disable block_based_kvo
open override func observeValue(forKeyPath keyPath: String?,
of object: Any?,
change: [NSKeyValueChangeKey: Any]?,
context: UnsafeMutableRawPointer?) {
// swiftlint:enable block_based_kvo
guard let keyPath = keyPath else { return }
switch keyPath {
case #keyPath(WKWebView.estimatedProgress):
progress = Float(webView?.estimatedProgress ?? 0.0)
default:
os_log("Unhandled keyPath %s", log: .generalLog, type: .debug, keyPath)
}
}
private func removeObservers() {
webView?.removeObserver(self, forKeyPath: #keyPath(WKWebView.estimatedProgress))
}
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
removeObservers()
}
}
extension WebContainerViewController: Themable { }