@@ -30,32 +30,35 @@ final class JetpackConnectionWebViewModel: AuthenticatedWebViewModel {
3030 guard let path = url? . absoluteString else {
3131 return
3232 }
33- // When the web view navigates to the site address or Jetpack plans page,
34- // we can assume that the setup has completed.
35- if path. hasPrefix ( siteURL) || path. hasPrefix ( Constants . plansPage) {
36- handleSetupCompletion ( )
37- }
33+ handleCompletionIfPossible ( path)
3834 }
3935
4036 func decidePolicy( for navigationURL: URL ) async -> WKNavigationActionPolicy {
4137 let url = navigationURL. absoluteString
42- switch url {
43- // When the web view navigates to the site address or Jetpack plans page,
44- // we can assume that the setup has completed.
45- case let url where url. hasPrefix ( siteURL) || url. hasPrefix ( Constants . plansPage) :
46- await MainActor . run { [ weak self] in
47- self ? . handleSetupCompletion ( )
48- }
38+ if handleCompletionIfPossible ( url) {
4939 return . cancel
50- default :
51- return . allow
5240 }
41+ return . allow
5342 }
5443
5544 private func handleSetupCompletion( ) {
5645 analytics. track ( . loginJetpackConnectCompleted)
5746 completionHandler ( )
5847 }
48+
49+ @discardableResult
50+ func handleCompletionIfPossible( _ url: String ) -> Bool {
51+ // When the web view navigates to the site address or Jetpack plans page,
52+ // we can assume that the setup has completed.
53+ if url. hasPrefix ( siteURL) || url. hasPrefix ( Constants . plansPage) {
54+ // Running on the main thread is necessary if this method is triggered from `decidePolicy`.
55+ DispatchQueue . main. async { [ weak self] in
56+ self ? . handleSetupCompletion ( )
57+ }
58+ return true
59+ }
60+ return false
61+ }
5962}
6063
6164private extension JetpackConnectionWebViewModel {
0 commit comments