@@ -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+ */
1616internal 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 */
8984internal 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 */
168161extension 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.
0 commit comments