diff --git a/iOS/SceneCoordinator.swift b/iOS/SceneCoordinator.swift index f84fe53e4..574fab784 100644 --- a/iOS/SceneCoordinator.swift +++ b/iOS/SceneCoordinator.swift @@ -705,8 +705,10 @@ struct SidebarItemNode: Hashable, Sendable { return } DispatchQueue.main.async { - let title = NSLocalizedString("Theme Error", comment: "Theme download error") - self.rootSplitViewController.presentError(title: title, message: ArticleThemesManager.importErrorMessage(for: error)) + self.withFrontmostController { controller in + let title = NSLocalizedString("Theme Error", comment: "Theme download error") + controller.presentError(title: title, message: ArticleThemesManager.importErrorMessage(for: error)) + } } } @@ -1778,12 +1780,13 @@ struct SidebarItemNode: Hashable, Sendable { } func importTheme(filename: String) { - do { - try ArticleThemeImporter.importTheme(controller: rootSplitViewController, url: URL(fileURLWithPath: filename)) - } catch { - NotificationCenter.default.post(name: .didFailToImportThemeWithError, object: nil, userInfo: ["error": error]) + withFrontmostController { controller in + do { + try ArticleThemeImporter.importTheme(controller: controller, url: URL(fileURLWithPath: filename)) + } catch { + NotificationCenter.default.post(name: .didFailToImportThemeWithError, object: nil, userInfo: ["error": error]) + } } - } /// This will dismiss the foremost view controller if the user @@ -1955,6 +1958,23 @@ private extension SceneCoordinator { } } + /// Calls `handler` with the frontmost presented controller (such as Settings), so + /// alerts can be presented over it. Waits for any presentation transition to finish. + func withFrontmostController(_ handler: @escaping @MainActor (UIViewController) -> Void) { + var controller: UIViewController = rootSplitViewController + while let presentedController = controller.presentedViewController, !presentedController.isBeingDismissed { + controller = presentedController + } + + if let transitionCoordinator = controller.transitionCoordinator { + transitionCoordinator.animate(alongsideTransition: nil) { _ in + handler(controller) + } + } else { + handler(controller) + } + } + // Focus once the navigation transition finishes, so becomeFirstResponder lands on the // revealed column rather than firing mid-animation. Falls back to the next runloop. func focusWhenTransitionCompletes(in navController: UINavigationController, thenFocus focus: @escaping @MainActor () -> Void) { diff --git a/iOS/SceneDelegate.swift b/iOS/SceneDelegate.swift index 64251d538..127003743 100644 --- a/iOS/SceneDelegate.swift +++ b/iOS/SceneDelegate.swift @@ -121,6 +121,21 @@ final class SceneDelegate: UIResponder, UIWindowSceneDelegate { DispatchQueue.main.async { + // Theme URLs leave Settings open so the install prompt can appear over it. + let filename = context.url.standardizedFileURL.path + if filename.hasSuffix(ArticleTheme.nnwThemeSuffix) { + self.coordinator.importTheme(filename: filename) + return + } + + // Handle theme URLs: netnewswire://theme/add?url={url} + if let urlComponents = URLComponents(url: context.url, resolvingAgainstBaseURL: false), urlComponents.scheme?.lowercased() == "netnewswire", urlComponents.host == "theme" { + if let providedThemeURL = urlComponents.queryItems?.first(where: { $0.name == "url" })?.value, let themeURL = URL(string: providedThemeURL) { + ArticleThemeDownloader.shared.downloadTheme(from: themeURL) + } + return + } + DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { self.coordinator.dismissIfLaunchingFromExternalAction() } @@ -192,25 +207,6 @@ final class SceneDelegate: UIResponder, UIWindowSceneDelegate { } return } - - let filename = context.url.standardizedFileURL.path - if filename.hasSuffix(ArticleTheme.nnwThemeSuffix) { - self.coordinator.importTheme(filename: filename) - return - } - - // Handle theme URLs: netnewswire://theme/add?url={url} - guard let comps = URLComponents(url: context.url, resolvingAgainstBaseURL: false), - comps.scheme?.lowercased() == "netnewswire", - "theme" == comps.host, - let queryItems = comps.queryItems else { - return - } - - if let providedThemeURL = queryItems.first(where: { $0.name == "url" })?.value, - let themeURL = URL(string: providedThemeURL) { - ArticleThemeDownloader.shared.downloadTheme(from: themeURL) - } } } } diff --git a/iOS/Settings/ArticleThemesView.swift b/iOS/Settings/ArticleThemesView.swift index 401d248e6..70a5f9597 100644 --- a/iOS/Settings/ArticleThemesView.swift +++ b/iOS/Settings/ArticleThemesView.swift @@ -57,6 +57,9 @@ struct ArticleThemesView: View { SafariView(url: themesGalleryURL) } } + .onReceive(NotificationCenter.default.publisher(for: .didBeginDownloadingTheme)) { _ in + isShowingThemesGallery = false + } .alert(String(localized: "Delete Theme?", comment: "Delete Theme"), isPresented: isShowingDeleteThemeConfirmation, presenting: themeNamePendingDeletion) { themeName in Button(String(localized: "Cancel", comment: "Cancel button"), role: .cancel) { } diff --git a/iOS/Settings/SettingsView.swift b/iOS/Settings/SettingsView.swift index 79b7267a7..a2d40ca66 100644 --- a/iOS/Settings/SettingsView.swift +++ b/iOS/Settings/SettingsView.swift @@ -123,6 +123,9 @@ struct SettingsView: View { SafariView(url: safariURL) } } + .onReceive(NotificationCenter.default.publisher(for: .didBeginDownloadingTheme)) { _ in + safariURL = nil + } .alert(alert?.title ?? "", isPresented: isShowingAlert, presenting: alert) { _ in Button(NSLocalizedString("OK", comment: "OK button")) { }