| 1 | import SwiftUI |
| 2 | import Observation |
| 3 | #if os(iOS) |
| 4 | import SafariServices |
| 5 | #else |
| 6 | import AppKit |
| 7 | #endif |
| 8 | |
| 9 | /// Fixed task/settings routes and checked forge links share the same opener. |
| 10 | enum SettingsNavigation { |
| 11 | static func openChanges(origin: URL, url: URL, opener: (URL) -> Void) { |
| 12 | guard url.user == nil, url.password == nil else { return } |
| 13 | let sameOrigin = url.scheme == origin.scheme && url.host == origin.host && url.port == origin.port |
| 14 | let forge = url.scheme == "https" && url.host == "amadan.net" && (url.port == nil || url.port == 443) |
| 15 | guard sameOrigin || forge, ["http", "https"].contains(url.scheme) else { return } |
| 16 | opener(url) |
| 17 | } |
| 18 | static func open(origin: URL, path: String, opener: (URL) -> Void) { |
| 19 | let fixedSettings = ["/conversation/settings/machines", "/conversation/settings/connections", "/conversation/settings/connections/add"].contains(path) |
| 20 | let taskPage = path.range(of: #"^/tasks/[A-Za-z0-9_-]+(?:/move|/preview)?\z"#, options: .regularExpression) != nil |
| 21 | guard fixedSettings || taskPage, |
| 22 | var url = URLComponents(url: origin, resolvingAgainstBaseURL: false) else { return } |
| 23 | url.path = path; url.query = nil; url.fragment = nil |
| 24 | if let destination = url.url { opener(destination) } |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | @MainActor @Observable final class SettingsPresentation { |
| 29 | struct Page: Identifiable { let url: URL; var id: URL { url } } |
| 30 | var page: Page? |
| 31 | func open(origin: URL, path: String) { |
| 32 | SettingsNavigation.open(origin: origin, path: path, opener: present) |
| 33 | } |
| 34 | func openChanges(origin: URL, url: URL) { |
| 35 | SettingsNavigation.openChanges(origin: origin, url: url, opener: present) |
| 36 | } |
| 37 | private func present(_ url: URL) { |
| 38 | #if os(macOS) |
| 39 | NSWorkspace.shared.open(url) |
| 40 | #else |
| 41 | page = Page(url: url) |
| 42 | #endif |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | /// Owned by the signed-in tab container, which survives changing task pauses. |
| 47 | struct SettingsSheetPresenter: ViewModifier { |
| 48 | let session: EquipoSession |
| 49 | func body(content: Content) -> some View { |
| 50 | #if os(iOS) |
| 51 | @Bindable var presentation = session.settings |
| 52 | content.sheet(item: $presentation.page, onDismiss: { Task { await session.refresh() } }) { page in |
| 53 | SafariSettingsView(url: page.url).ignoresSafeArea() |
| 54 | } |
| 55 | #else |
| 56 | content |
| 57 | #endif |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | #if os(iOS) |
| 62 | private struct SafariSettingsView: UIViewControllerRepresentable { |
| 63 | let url: URL |
| 64 | func makeUIViewController(context: Context) -> SFSafariViewController { SFSafariViewController(url: url) } |
| 65 | func updateUIViewController(_ controller: SFSafariViewController, context: Context) {} |
| 66 | } |
| 67 | #endif |
| 68 | |