Skip to content

Commit cfed75c

Browse files
authored
CA-5858: Implement TODO that checks the url before reloading a web page (#281)
* Implement todo around reloading the webpage only when the url has changed. * Fix a small bug where in-site navigation was not updating the model's url.
1 parent 97ae122 commit cfed75c

2 files changed

Lines changed: 11 additions & 1 deletion

File tree

lib/Sources/WebView/Coordinator.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public class Coordinator: NSObject, NavigationEngineDelegate {
1212
private var contextId: JsContextIdentifier!
1313
private var scriptHandler: ScriptHandler?
1414
private var viewModel: WebPageModel?
15+
private var lastLoadedURL: URL?
1516
private var navigationEngine: NavigationEngine?
1617
private var authorize: () async -> Bool = { false }
1718

@@ -46,13 +47,15 @@ public class Coordinator: NSObject, NavigationEngineDelegate {
4647
webView.navigationDelegate = nil
4748
webView.uiDelegate = nil
4849
viewModel = nil
50+
lastLoadedURL = nil
4951
detachOldHandler(from: webView)
5052
authorize = { false }
5153
}
5254

5355
func update(webView: WKWebView, model: WebPageModel) {
5456
webView.customUserAgent = model.customUserAgent
55-
// TODO: load when observed model url changes only
57+
guard model.url != lastLoadedURL else { return }
58+
lastLoadedURL = model.url
5659
webView.load(URLRequest(url: model.url))
5760
}
5861

@@ -111,6 +114,9 @@ public class Coordinator: NSObject, NavigationEngineDelegate {
111114
}
112115

113116
public func didEndLoading(_ navigation: NavigationItem, in webView: WKWebView) {
117+
guard let currentURL = webView.url else { return }
118+
lastLoadedURL = currentURL
119+
viewModel?.didFinishLoading(url: currentURL)
114120
// TODO: detect if the webpage has `overflow: hidden;` and `height: 100%` and set viewModel?.isFullScreenNonScrollable accordingly
115121
}
116122

lib/Sources/WebView/WebPageModel.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,10 @@ public class WebPageModel: Identifiable {
132132
}
133133
}
134134

135+
func didFinishLoading(url: URL) {
136+
self.url = url
137+
}
138+
135139
func requestAuthorization() async -> Bool {
136140
guard let webOrigin else {
137141
return false

0 commit comments

Comments
 (0)