Open legal links with in app sheets instead of external browser

This commit is contained in:
Letro Bot
2026-05-04 23:01:00 +04:00
parent 392f10b2bc
commit e418357988

View File

@@ -8,28 +8,85 @@
import Compound import Compound
import SwiftUI import SwiftUI
import WebKit
struct LegalInformationScreen: View { struct LegalInformationScreen: View {
let context: LegalInformationScreenViewModel.Context let context: LegalInformationScreenViewModel.Context
@Environment(\.openURL) private var openURL @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 { var body: some View {
Form { Form {
Section { Section {
ListRow(label: .plain(title: L10n.commonCopyright), 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), 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), ListRow(label: .plain(title: L10n.commonPrivacyPolicy),
kind: .button { openURL(context.viewState.privacyURL) }) kind: .button { browserURL = BrowserURL(context.viewState.privacyURL) })
} }
} }
.compoundList() .compoundList()
.navigationTitle(L10n.commonAbout) .navigationTitle(L10n.commonAbout)
.navigationBarTitleDisplayMode(.inline) .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 // MARK: - Previews
struct LegalInformationScreen_Previews: PreviewProvider, TestablePreview { struct LegalInformationScreen_Previews: PreviewProvider, TestablePreview {