Open legal links with in app sheets instead of external browser
This commit is contained in:
@@ -8,27 +8,84 @@
|
||||
|
||||
import Compound
|
||||
import SwiftUI
|
||||
import WebKit
|
||||
|
||||
struct LegalInformationScreen: View {
|
||||
let context: LegalInformationScreenViewModel.Context
|
||||
@Environment(\.openURL) private var openURL
|
||||
|
||||
/*
|
||||
Letro: open legal info in a sheet instead of external browser
|
||||
*/
|
||||
@State private var browserURL: BrowserURL?
|
||||
|
||||
var body: some View {
|
||||
Form {
|
||||
Section {
|
||||
ListRow(label: .plain(title: L10n.commonCopyright),
|
||||
kind: .button { openURL(context.viewState.copyrightURL) })
|
||||
kind: .button { browserURL = BrowserURL(context.viewState.copyrightURL) })
|
||||
ListRow(label: .plain(title: L10n.commonAcceptableUsePolicy),
|
||||
kind: .button { openURL(context.viewState.acceptableUseURL) })
|
||||
kind: .button { browserURL = BrowserURL(context.viewState.acceptableUseURL) })
|
||||
ListRow(label: .plain(title: L10n.commonPrivacyPolicy),
|
||||
kind: .button { openURL(context.viewState.privacyURL) })
|
||||
kind: .button { browserURL = BrowserURL(context.viewState.privacyURL) })
|
||||
}
|
||||
}
|
||||
.compoundList()
|
||||
.navigationTitle(L10n.commonAbout)
|
||||
.navigationBarTitleDisplayMode(.inline)
|
||||
.sheet(item: $browserURL) { browserURL in
|
||||
ElementNavigationStack {
|
||||
LegalInformationWebView(url: browserURL.url)
|
||||
.ignoresSafeArea(edges: .bottom)
|
||||
.toolbar {
|
||||
ToolbarItem(placement: .confirmationAction) {
|
||||
Button(L10n.actionDone) {
|
||||
self.browserURL = nil
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
Letro: open legal info in a sheet instead of external browser
|
||||
*/
|
||||
|
||||
private struct BrowserURL: Identifiable {
|
||||
let url: URL
|
||||
var id: URL { url }
|
||||
|
||||
init(_ url: URL) {
|
||||
self.url = url
|
||||
}
|
||||
}
|
||||
|
||||
private struct LegalInformationWebView: UIViewControllerRepresentable {
|
||||
let url: URL
|
||||
|
||||
func makeUIViewController(context: Context) -> UIViewController {
|
||||
let viewController = UIViewController()
|
||||
let webView = WKWebView()
|
||||
webView.allowsBackForwardNavigationGestures = true
|
||||
webView.translatesAutoresizingMaskIntoConstraints = false
|
||||
|
||||
viewController.view.addSubview(webView)
|
||||
NSLayoutConstraint.activate([
|
||||
webView.topAnchor.constraint(equalTo: viewController.view.topAnchor),
|
||||
webView.leadingAnchor.constraint(equalTo: viewController.view.leadingAnchor),
|
||||
webView.trailingAnchor.constraint(equalTo: viewController.view.trailingAnchor),
|
||||
webView.bottomAnchor.constraint(equalTo: viewController.view.bottomAnchor)
|
||||
])
|
||||
|
||||
webView.load(URLRequest(url: url))
|
||||
|
||||
return viewController
|
||||
}
|
||||
|
||||
func updateUIViewController(_ uiViewController: UIViewController, context: Context) { }
|
||||
}
|
||||
|
||||
// MARK: - Previews
|
||||
|
||||
|
||||
Reference in New Issue
Block a user