Skip to content

Commit 8994907

Browse files
committed
Fix for iOS 16
1 parent 0f0ff90 commit 8994907

2 files changed

Lines changed: 29 additions & 42 deletions

File tree

Sources/SupportDocs/Components/WebView.swift

Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -12,34 +12,31 @@ import WebKit
1212
The web view, with a progress bar at the top.
1313

1414
This is presented when a cell is tapped (in the main page).
15-
*/
15+
*/
1616
internal struct WebViewContainer: View {
17-
1817
/// URL to load.
1918
var url: URL
2019

2120
/// Stores the background and foreground color of the progress bar.
2221
var progressBarOptions: SupportOptions.ProgressBar
2322

2423
/**
25-
Callback from the `WKNavigationDelegate`.
26-
- `pageTitle` - the name of the page (supplied in the `Title` property at the top of your GitHub Pages).
27-
- `progress` - get how much the page is loaded.
28-
- `estimatedProgressObserver` - the observer that keeps track of the page load progress.
29-
- `nextUrl` - the url of the next page. Used when a link is pressed.
30-
- `presentNextPage` - determines whether a new page should be presented (when a link is pressed).
31-
*/
32-
@ObservedObject var webViewStateModel: WebViewStateModel = WebViewStateModel()
24+
Callback from the `WKNavigationDelegate`.
25+
- `pageTitle` - the name of the page (supplied in the `Title` property at the top of your GitHub Pages).
26+
- `progress` - get how much the page is loaded.
27+
- `estimatedProgressObserver` - the observer that keeps track of the page load progress.
28+
- `nextUrl` - the url of the next page. Used when a link is pressed.
29+
- `presentNextPage` - determines whether a new page should be presented (when a link is pressed).
30+
*/
31+
@ObservedObject var webViewStateModel: WebViewStateModel = .init()
3332

3433
var body: some View {
3534
ZStack(alignment: .center) {
36-
3735
/// First, the Web View at the bottom of everything.
3836
WebView(url: url, webViewStateModel: self.webViewStateModel)
3937

4038
/// Then, the progress bar at the top of the screen.
4139
VStack {
42-
4340
/// Reads the value inside `$webViewStateModel.progress`.
4441
/// This is set inside the `NSKeyValueObservation` as the page loads.
4542
ProgressBar(value: $webViewStateModel.progress, progressBarOptions: progressBarOptions)
@@ -53,15 +50,13 @@ internal struct WebViewContainer: View {
5350
The new page is pushed when `$webViewStateModel.presentNextPage` is true.
5451
*/
5552
NavigationLink(destination: WebViewContainer(url: webViewStateModel.nextUrl ?? URL(string: "https://aheze.github.io/SupportDocs/404")!, progressBarOptions: progressBarOptions), isActive: $webViewStateModel.presentNextPage) {
56-
5753
/// No need for a button (it's presented automatically based on `$webViewStateModel.presentNextPage`)
5854
EmptyView()
5955
}
6056
}
6157
}
6258
}
6359

64-
6560
/**
6661
Callback from the `WKNavigationDelegate`.
6762
- `pageTitle` - the name of the page (supplied in the `Title` property at the top of your GitHub Pages).
@@ -87,11 +82,11 @@ internal class WebViewStateModel: ObservableObject {
8782
Made possible with `UIViewRepresentable` and `WebViewStateModel: ObservableObject` for delegate callback.
8883
*/
8984
internal struct WebView: View {
90-
enum NavigationAction {
91-
case decidePolicy(WKNavigationAction, (WKNavigationActionPolicy) -> Void)
92-
case didStartProvisionalNavigation(WKNavigation)
93-
case didFinish(WKNavigation)
94-
}
85+
enum NavigationAction {
86+
case decidePolicy(WKNavigationAction, (WKNavigationActionPolicy) -> Void)
87+
case didStartProvisionalNavigation(WKNavigation)
88+
case didFinish(WKNavigation)
89+
}
9590

9691
/// Callback for the SwiftUI view.
9792
@ObservedObject var webViewStateModel: WebViewStateModel
@@ -100,7 +95,6 @@ internal struct WebView: View {
10095
let uRLRequest: URLRequest
10196

10297
var body: some View {
103-
10498
/// Return the `UIViewRepresentable`.
10599
WebViewWrapper(webViewStateModel: webViewStateModel,
106100
request: uRLRequest)
@@ -115,8 +109,7 @@ internal struct WebView: View {
115109
/**
116110
WKWebView ported over to SwiftUI with `UIViewRepresentable`.
117111
*/
118-
internal final class WebViewWrapper: UIViewRepresentable {
119-
112+
internal struct WebViewWrapper: UIViewRepresentable {
120113
/// Port the `WKNavigationDelegate` delegate over to SwiftUI.
121114
@ObservedObject var webViewStateModel: WebViewStateModel
122115

@@ -128,7 +121,7 @@ internal final class WebViewWrapper: UIViewRepresentable {
128121
}
129122

130123
/// `UIViewRepresentable` required function #1.
131-
func makeUIView(context: Context) -> WKWebView {
124+
func makeUIView(context: Context) -> WKWebView {
132125
let view = WKWebView()
133126
view.isOpaque = false
134127
view.navigationDelegate = context.coordinator
@@ -140,8 +133,7 @@ internal final class WebViewWrapper: UIViewRepresentable {
140133
}
141134

142135
/// `UIViewRepresentable` required function #2, but no need for this in our case.
143-
func updateUIView(_ uiView: WKWebView, context: Context) {
144-
}
136+
func updateUIView(_ uiView: WKWebView, context: Context) {}
145137

146138
/// `UIViewRepresentable` function for making the delegate.
147139
func makeCoordinator() -> Coordinator {
@@ -156,6 +148,7 @@ internal final class WebViewWrapper: UIViewRepresentable {
156148
setupEstimatedProgressObserver()
157149
}
158150
}
151+
159152
init(webViewStateModel: WebViewStateModel) {
160153
self.webViewStateModel = webViewStateModel
161154
}
@@ -167,31 +160,27 @@ internal final class WebViewWrapper: UIViewRepresentable {
167160
*/
168161
extension WebViewWrapper.Coordinator: WKNavigationDelegate {
169162
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
170-
171163
/**
172164
If the user presses a link, open it in a new page (push a new page onto the `NavigationView`.
173165
*/
174166
if navigationAction.navigationType == WKNavigationType.linkActivated {
175167
decisionHandler(WKNavigationActionPolicy.cancel)
176168
if let url = navigationAction.request.url {
177-
178169
/// Set the URL to present.
179170
webViewStateModel.nextUrl = url
180171

181172
/// Set `webViewStateModel.presentNextPage` to true so the `NavigationView` pushes the new page.
182173
webViewStateModel.presentNextPage = true
183-
184174
}
185175
} else {
186-
187176
/// Not likely to happen, but allow anyway.
188177
decisionHandler(.allow)
189178
}
190179
}
191180

192181
/// The page started loading, so set the progress bar's progress a bit.
193182
func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
194-
self.webViewStateModel.progress = Float(0.15)
183+
webViewStateModel.progress = Float(0.15)
195184
}
196185

197186
/// The page finished loading, and the `title` is available.

Sources/SupportDocs/SupportDocsView+Search.swift

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,16 @@ import SwiftUI
1111
View Modifier for applying the search bar.
1212
*/
1313
internal struct SearchBarModifier: ViewModifier {
14-
1514
var searchBarConfigurator: SearchBarConfigurator
1615

1716
func body(content: Content) -> some View {
1817
content
19-
.overlay(
20-
ViewControllerResolver { viewController in
21-
viewController.navigationItem.searchController = self.searchBarConfigurator.searchController
22-
}
23-
.frame(width: 0, height: 0)
24-
)
18+
.overlay(
19+
ViewControllerResolver { viewController in
20+
viewController.navigationItem.searchController = self.searchBarConfigurator.searchController
21+
}
22+
.frame(width: 0, height: 0)
23+
)
2524
}
2625
}
2726

@@ -30,15 +29,14 @@ internal struct SearchBarModifier: ViewModifier {
3029
*/
3130
extension View {
3231
func add(_ searchBarConfigurator: SearchBarConfigurator) -> some View {
33-
return self.modifier(SearchBarModifier(searchBarConfigurator: searchBarConfigurator))
32+
return modifier(SearchBarModifier(searchBarConfigurator: searchBarConfigurator))
3433
}
3534
}
3635

3736
/**
3837
Access the parent view controller of the SwiftUI View.
3938
*/
40-
internal final class ViewControllerResolver: UIViewControllerRepresentable {
41-
39+
internal struct ViewControllerResolver: UIViewControllerRepresentable {
4240
/// Closure to call when `didMove`
4341
let onResolve: (UIViewController) -> Void
4442

@@ -50,18 +48,18 @@ internal final class ViewControllerResolver: UIViewControllerRepresentable {
5048
ParentResolverViewController(onResolve: onResolve)
5149
}
5250

53-
func updateUIViewController(_ uiViewController: ParentResolverViewController, context: Context) { }
51+
func updateUIViewController(_ uiViewController: ParentResolverViewController, context: Context) {}
5452
}
5553

5654
internal class ParentResolverViewController: UIViewController {
57-
5855
let onResolve: (UIViewController) -> Void
5956

6057
init(onResolve: @escaping (UIViewController) -> Void) {
6158
self.onResolve = onResolve
6259
super.init(nibName: nil, bundle: nil)
6360
}
6461

62+
@available(*, unavailable)
6563
required init?(coder: NSCoder) {
6664
fatalError("Use init(onResolve:) to instantiate ParentResolverViewController.")
6765
}

0 commit comments

Comments
 (0)