2023-01-08 19:56:16 +01:00
|
|
|
import DesignSystem
|
2023-01-17 11:36:01 +01:00
|
|
|
import Env
|
|
|
|
import SafariServices
|
|
|
|
import SwiftUI
|
2023-01-08 19:56:16 +01:00
|
|
|
|
|
|
|
extension View {
|
2023-01-17 15:14:50 +01:00
|
|
|
func withSafariRouter() -> some View {
|
|
|
|
modifier(SafariRouter())
|
2023-01-17 11:36:01 +01:00
|
|
|
}
|
2023-01-08 19:56:16 +01:00
|
|
|
}
|
|
|
|
|
2023-01-17 15:14:50 +01:00
|
|
|
private struct SafariRouter: ViewModifier {
|
2023-01-17 11:36:01 +01:00
|
|
|
@EnvironmentObject private var theme: Theme
|
|
|
|
@EnvironmentObject private var preferences: UserPreferences
|
2023-01-17 15:14:50 +01:00
|
|
|
@EnvironmentObject private var routerPath: RouterPath
|
2023-01-17 11:36:01 +01:00
|
|
|
|
2023-02-02 07:04:51 +01:00
|
|
|
@State private var presentedURL: URL?
|
2023-01-17 11:36:01 +01:00
|
|
|
|
|
|
|
func body(content: Content) -> some View {
|
|
|
|
content
|
|
|
|
.environment(\.openURL, OpenURLAction { url in
|
2023-01-27 20:35:16 +01:00
|
|
|
// Open internal URL.
|
2023-01-17 15:14:50 +01:00
|
|
|
routerPath.handle(url: url)
|
2023-01-17 11:36:01 +01:00
|
|
|
})
|
2023-01-27 20:35:16 +01:00
|
|
|
.onOpenURL(perform: { url in
|
|
|
|
// Open external URL (from icecubesapp://)
|
|
|
|
let urlString = url.absoluteString.replacingOccurrences(of: "icecubesapp://", with: "https://")
|
2023-01-29 07:36:20 +01:00
|
|
|
guard let url = URL(string: urlString), url.host != nil else { return }
|
2023-01-27 20:35:16 +01:00
|
|
|
_ = routerPath.handle(url: url)
|
|
|
|
})
|
2023-01-17 11:36:01 +01:00
|
|
|
.onAppear {
|
2023-01-17 15:14:50 +01:00
|
|
|
routerPath.urlHandler = { url in
|
2023-01-23 21:34:45 +01:00
|
|
|
if url.absoluteString.contains("@twitter.com"), url.absoluteString.hasPrefix("mailto:") {
|
|
|
|
let username = url.absoluteString
|
|
|
|
.replacingOccurrences(of: "@twitter.com", with: "")
|
|
|
|
.replacingOccurrences(of: "mailto:", with: "")
|
|
|
|
let twitterLink = "https://twitter.com/\(username)"
|
|
|
|
if let url = URL(string: twitterLink) {
|
|
|
|
UIApplication.shared.open(url)
|
|
|
|
return .handled
|
|
|
|
}
|
|
|
|
}
|
2023-01-19 11:59:12 +01:00
|
|
|
guard preferences.preferredBrowser == .inAppSafari, !ProcessInfo.processInfo.isiOSAppOnMac else { return .systemAction }
|
2023-01-17 11:36:01 +01:00
|
|
|
// SFSafariViewController only supports initial URLs with http:// or https:// schemes.
|
|
|
|
guard let scheme = url.scheme, ["https", "http"].contains(scheme.lowercased()) else {
|
|
|
|
return .systemAction
|
|
|
|
}
|
|
|
|
|
2023-02-02 07:04:51 +01:00
|
|
|
presentedURL = url
|
2023-01-17 11:36:01 +01:00
|
|
|
return .handled
|
2023-01-08 19:56:16 +01:00
|
|
|
}
|
2023-01-17 11:36:01 +01:00
|
|
|
}
|
2023-02-02 07:04:51 +01:00
|
|
|
.sheet(item: $presentedURL, content: { url in
|
2023-02-03 07:04:00 +01:00
|
|
|
SafariView(url: url, inAppBrowserReaderView: preferences.inAppBrowserReaderView)
|
2023-02-02 07:04:51 +01:00
|
|
|
.edgesIgnoringSafeArea(.all)
|
|
|
|
})
|
2023-01-17 11:36:01 +01:00
|
|
|
}
|
|
|
|
|
2023-02-02 07:04:51 +01:00
|
|
|
struct SafariView: UIViewControllerRepresentable {
|
|
|
|
let url: URL
|
2023-02-03 07:04:00 +01:00
|
|
|
let inAppBrowserReaderView: Bool
|
|
|
|
|
2023-02-02 07:04:51 +01:00
|
|
|
func makeUIViewController(context _: UIViewControllerRepresentableContext<SafariView>) -> SFSafariViewController {
|
2023-02-03 07:04:00 +01:00
|
|
|
let configuration = SFSafariViewController.Configuration()
|
|
|
|
configuration.entersReaderIfAvailable = inAppBrowserReaderView
|
|
|
|
|
|
|
|
let safari = SFSafariViewController(url: url, configuration: configuration)
|
2023-02-02 07:04:51 +01:00
|
|
|
safari.preferredBarTintColor = UIColor(Theme.shared.primaryBackgroundColor)
|
|
|
|
safari.preferredControlTintColor = UIColor(Theme.shared.tintColor)
|
|
|
|
return safari
|
2023-01-08 19:56:16 +01:00
|
|
|
}
|
|
|
|
|
2023-02-02 07:04:51 +01:00
|
|
|
func updateUIViewController(_: SFSafariViewController, context _: UIViewControllerRepresentableContext<SafariView>) {}
|
2023-01-17 11:36:01 +01:00
|
|
|
}
|
2023-01-08 19:56:16 +01:00
|
|
|
}
|