IceCubesApp/IceCubesApp/App/SafariRouter.swift

153 lines
4.5 KiB
Swift
Raw Normal View History

import DesignSystem
2023-01-17 10:36:01 +00:00
import Env
import Observation
2023-01-17 10:36:01 +00:00
import SafariServices
import SwiftUI
extension View {
@MainActor func withSafariRouter() -> some View {
modifier(SafariRouter())
2023-01-17 10:36:01 +00:00
}
}
@MainActor
private struct SafariRouter: ViewModifier {
2023-09-18 19:03:52 +00:00
@Environment(Theme.self) private var theme
2023-09-19 07:18:20 +00:00
@Environment(UserPreferences.self) private var preferences
@Environment(RouterPath.self) private var routerPath
2023-01-17 10:36:01 +00:00
@State private var safariManager = InAppSafariManager()
2023-02-21 06:23:42 +00:00
2023-01-17 10:36:01 +00:00
func body(content: Content) -> some View {
content
.environment(\.openURL, OpenURLAction { url in
// Open internal URL.
routerPath.handle(url: url)
2023-01-17 10:36:01 +00:00
})
.onOpenURL { url in
// Open external URL (from icecubesapp://)
let urlString = url.absoluteString.replacingOccurrences(of: "icecubesapp://", with: "https://")
guard let url = URL(string: urlString), url.host != nil else { return }
_ = routerPath.handle(url: url)
}
2023-01-17 10:36:01 +00:00
.onAppear {
routerPath.urlHandler = { url in
2023-01-23 20:34:45 +00: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-10-23 17:12:25 +00:00
guard preferences.preferredBrowser == .inAppSafari, !ProcessInfo.processInfo.isMacCatalystApp else { return .systemAction }
2023-01-17 10:36:01 +00:00
// SFSafariViewController only supports initial URLs with http:// or https:// schemes.
guard let scheme = url.scheme, ["https", "http"].contains(scheme.lowercased()) else {
return .systemAction
}
return safariManager.open(url)
}
}
.background {
WindowReader { window in
2023-09-16 12:15:03 +00:00
safariManager.windowScene = window.windowScene
}
2023-01-17 10:36:01 +00:00
}
}
}
2023-01-17 10:36:01 +00:00
@MainActor
@Observable private class InAppSafariManager: NSObject, SFSafariViewControllerDelegate {
var windowScene: UIWindowScene?
2023-02-21 06:23:42 +00:00
let viewController: UIViewController = .init()
var window: UIWindow?
2023-02-04 16:17:38 +00:00
@MainActor
func open(_ url: URL) -> OpenURLAction.Result {
2023-09-16 12:15:03 +00:00
guard let windowScene else { return .systemAction }
2023-02-21 06:23:42 +00:00
window = setupWindow(windowScene: windowScene)
let configuration = SFSafariViewController.Configuration()
configuration.entersReaderIfAvailable = UserPreferences.shared.inAppBrowserReaderView
2023-02-21 06:23:42 +00:00
let safari = SFSafariViewController(url: url, configuration: configuration)
safari.preferredBarTintColor = UIColor(Theme.shared.primaryBackgroundColor)
safari.preferredControlTintColor = UIColor(Theme.shared.tintColor)
safari.delegate = self
2023-02-21 06:23:42 +00:00
DispatchQueue.main.async { [weak self] in
self?.viewController.present(safari, animated: true)
}
2023-02-21 06:23:42 +00:00
return .handled
}
func setupWindow(windowScene: UIWindowScene) -> UIWindow {
2023-09-16 12:15:03 +00:00
let window = window ?? UIWindow(windowScene: windowScene)
2023-02-21 06:23:42 +00:00
window.rootViewController = viewController
window.makeKeyAndVisible()
2023-02-21 06:23:42 +00:00
switch Theme.shared.selectedScheme {
case .dark:
window.overrideUserInterfaceStyle = .dark
case .light:
window.overrideUserInterfaceStyle = .light
}
2023-02-21 06:23:42 +00:00
self.window = window
return window
}
nonisolated func safariViewControllerDidFinish(_: SFSafariViewController) {
Task { @MainActor in
window?.resignKey()
window?.isHidden = false
window = nil
}
}
}
private struct WindowReader: UIViewRepresentable {
var onUpdate: (UIWindow) -> Void
2023-02-21 06:23:42 +00:00
func makeUIView(context _: Context) -> InjectView {
InjectView(onUpdate: onUpdate)
}
2023-02-21 06:23:42 +00:00
func updateUIView(_: InjectView, context _: Context) {}
class InjectView: UIView {
var onUpdate: (UIWindow) -> Void
2023-02-21 06:23:42 +00:00
init(onUpdate: @escaping (UIWindow) -> Void) {
self.onUpdate = onUpdate
super.init(frame: .zero)
isHidden = true
isUserInteractionEnabled = false
}
2023-02-21 06:23:42 +00:00
@available(*, unavailable)
2023-02-21 06:23:42 +00:00
required init?(coder _: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
2023-02-21 06:23:42 +00:00
override func willMove(toWindow newWindow: UIWindow?) {
super.willMove(toWindow: newWindow)
2023-02-21 06:23:42 +00:00
if let window = newWindow {
onUpdate(window)
} else {
DispatchQueue.main.async { [weak self] in
if let window = self?.window {
self?.onUpdate(window)
}
}
}
}
2023-01-17 10:36:01 +00:00
}
}