From cad91d9415fa22de72c906f3a2bc6fe4c8fdcb21 Mon Sep 17 00:00:00 2001 From: Maurice Parker Date: Sun, 12 Sep 2021 14:46:15 -0500 Subject: [PATCH] Add the ability to import themes on iOS --- iOS/SceneCoordinator.swift | 80 ++++++++++++++++++++++++++++++++++++++ iOS/SceneDelegate.swift | 5 +++ 2 files changed, 85 insertions(+) diff --git a/iOS/SceneCoordinator.swift b/iOS/SceneCoordinator.swift index ef9cb07de..08d0de214 100644 --- a/iOS/SceneCoordinator.swift +++ b/iOS/SceneCoordinator.swift @@ -1282,6 +1282,72 @@ class SceneCoordinator: NSObject, UndoableCommandRunner, UnreadCountProvider { self.selectArticle(article) } } + + func importTheme(filename: String) { + let theme = ArticleTheme(path: filename) + + let localizedTitleText = NSLocalizedString("Install theme “%@” by %@?", comment: "Theme message text") + let title = NSString.localizedStringWithFormat(localizedTitleText as NSString, theme.name, theme.creatorName) as String + + let localizedMessageText = NSLocalizedString("Author's Website:\n%@", comment: "Authors website") + let message = NSString.localizedStringWithFormat(localizedMessageText as NSString, theme.creatorHomePage) as String + + let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert) + + let cancelTitle = NSLocalizedString("Cancel", comment: "Cancel") + alertController.addAction(UIAlertAction(title: cancelTitle, style: .cancel)) + + if let url = URL(string: theme.creatorHomePage) { + let visitSiteTitle = NSLocalizedString("Show Website", comment: "Show Website") + let visitSiteAction = UIAlertAction(title: visitSiteTitle, style: .default) { [weak self] action in + UIApplication.shared.open(url) + self?.importTheme(filename: filename) + } + alertController.addAction(visitSiteAction) + } + + func importTheme() { + do { + try ArticleThemesManager.shared.importTheme(filename: filename) + confirmImportSuccess(themeName: theme.name) + } catch { + rootSplitViewController.presentError(error) + } + } + + let installThemeTitle = NSLocalizedString("Install Theme", comment: "Install Theme") + let installThemeAction = UIAlertAction(title: installThemeTitle, style: .default) { [weak self] action in + + if ArticleThemesManager.shared.themeExists(filename: filename) { + let title = NSLocalizedString("Duplicate Theme", comment: "Duplicate Theme") + let localizedMessageText = NSLocalizedString("The theme “%@” already exists. Overwrite it?", comment: "Overwrite theme") + let message = NSString.localizedStringWithFormat(localizedMessageText as NSString, theme.name) as String + + let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert) + + let cancelTitle = NSLocalizedString("Cancel", comment: "Cancel") + alertController.addAction(UIAlertAction(title: cancelTitle, style: .cancel)) + + let overwriteAction = UIAlertAction(title: NSLocalizedString("Overwrite", comment: "Overwrite"), style: .default) { action in + importTheme() + } + alertController.addAction(overwriteAction) + alertController.preferredAction = overwriteAction + + self?.rootSplitViewController.present(alertController, animated: true) + } else { + importTheme() + } + + } + + alertController.addAction(installThemeAction) + alertController.preferredAction = installThemeAction + + rootSplitViewController.present(alertController, animated: true) + + } + } // MARK: UISplitViewControllerDelegate @@ -2296,4 +2362,18 @@ private extension SceneCoordinator { return false } + func confirmImportSuccess(themeName: String) { + let title = NSLocalizedString("Theme installed", comment: "Theme installed") + + let localizedMessageText = NSLocalizedString("The theme “%@” has been installed.", comment: "Theme installed") + let message = NSString.localizedStringWithFormat(localizedMessageText as NSString, themeName) as String + + let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert) + + let cancelTitle = NSLocalizedString("Done", comment: "Done") + alertController.addAction(UIAlertAction(title: cancelTitle, style: .default)) + + rootSplitViewController.present(alertController, animated: true) + } + } diff --git a/iOS/SceneDelegate.swift b/iOS/SceneDelegate.swift index 39a26aa2d..7ae1674e3 100644 --- a/iOS/SceneDelegate.swift +++ b/iOS/SceneDelegate.swift @@ -162,6 +162,11 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate { } } + let filename = context.url.standardizedFileURL.path + if filename.hasSuffix(ArticleTheme.nnwThemeSuffix) { + self.coordinator.importTheme(filename: filename) + } + } }