Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open external links in the browser #30

Merged
merged 1 commit into from
May 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Core/Core/View/Base/WebBrowser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public struct WebBrowser: View {
VStack {
ZStack(alignment: .top) {
NavigationView {
WebView(viewModel: .init(url: url), isLoading: $isShowProgress, refreshCookies: {})
WebView(viewModel: .init(url: url, baseURL: ""), isLoading: $isShowProgress, refreshCookies: {})
.navigationBarTitle(Text("")) // Needed for hide navBar on ios 14, 15
.navigationBarHidden(true)
.ignoresSafeArea()
Expand Down
8 changes: 5 additions & 3 deletions Core/Core/View/Base/WebUnitView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@ public struct WebUnitView: View {
GeometryReader { reader in
ScrollView {
if viewModel.cookiesReady {
WebView(viewModel: .init(url: url), isLoading: $isWebViewLoading, refreshCookies: {
await viewModel.updateCookies(force: true)
})
WebView(
viewModel: .init(url: url, baseURL: viewModel.config.baseURL.absoluteString),
isLoading: $isWebViewLoading, refreshCookies: {
await viewModel.updateCookies(force: true)
})
.introspectScrollView(customize: { scrollView in
scrollView.isScrollEnabled = false
scrollView.alwaysBounceVertical = false
Expand Down
6 changes: 5 additions & 1 deletion Core/Core/View/Base/WebUnitViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ import Foundation
import SwiftUI

public class WebUnitViewModel: ObservableObject {

let authInteractor: AuthInteractorProtocol
let config: Config

@Published var updatingCookies: Bool = false
@Published var cookiesReady: Bool = false
@Published var showError: Bool = false
Expand All @@ -23,8 +26,9 @@ public class WebUnitViewModel: ObservableObject {
}
}

public init(authInteractor: AuthInteractorProtocol) {
public init(authInteractor: AuthInteractorProtocol, config: Config) {
self.authInteractor = authInteractor
self.config = config
}

@MainActor
Expand Down
23 changes: 22 additions & 1 deletion Core/Core/View/Base/WebView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@ import SwiftUI
public struct WebView: UIViewRepresentable {

public class ViewModel: ObservableObject {

@Published var url: String
let baseURL: String

public init(url: String) {
public init(url: String, baseURL: String) {
self.url = url
self.baseURL = baseURL
}
}

Expand All @@ -42,6 +45,24 @@ public struct WebView: UIViewRepresentable {
}
}

public func webView(_ webView: WKWebView,
decidePolicyFor navigationAction: WKNavigationAction) async -> WKNavigationActionPolicy {

guard let url = navigationAction.request.url else {
return .cancel
}

let baseURL = await parent.viewModel.baseURL
if !baseURL.isEmpty, !url.absoluteString.starts(with: baseURL) {
await MainActor.run {
UIApplication.shared.open(url, options: [:])
}
return .cancel
}

return .allow
}

public func webView(_ webView: WKWebView,
decidePolicyFor navigationResponse: WKNavigationResponse) async -> WKNavigationResponsePolicy {
guard let statusCode = (navigationResponse.response as? HTTPURLResponse)?.statusCode else {
Expand Down
3 changes: 2 additions & 1 deletion NewEdX/DI/ScreenAssembly.swift
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,8 @@ class ScreenAssembly: Assembly {
}

container.register(WebUnitViewModel.self) { r in
WebUnitViewModel(authInteractor: r.resolve(AuthInteractorProtocol.self)!)
WebUnitViewModel(authInteractor: r.resolve(AuthInteractorProtocol.self)!,
config: r.resolve(Config.self)!)
}

container.register(VideoPlayerViewModel.self) { r in
Expand Down