From 0f2b9370ca599407b332d68ffa89966dd39800b1 Mon Sep 17 00:00:00 2001 From: Jackson Harper Date: Tue, 26 Mar 2024 18:00:40 +0800 Subject: [PATCH] Add open in system browser iOS setting Also fixes issue where the incorrect URL could be used when doing open operation. --- .../App/Views/Profile/ProfileView.swift | 3 +++ .../Views/Profile/ReaderSettingsView.swift | 27 +++++++++++++++++++ .../Views/WebReader/WebReaderContainer.swift | 22 ++++++++++----- .../Sources/Utils/UserDefaultKeys.swift | 1 + .../OmnivoreKit/Sources/Views/LocalText.swift | 1 + .../Resources/en.lproj/Localizable.strings | 5 ++-- 6 files changed, 50 insertions(+), 9 deletions(-) create mode 100644 apple/OmnivoreKit/Sources/App/Views/Profile/ReaderSettingsView.swift diff --git a/apple/OmnivoreKit/Sources/App/Views/Profile/ProfileView.swift b/apple/OmnivoreKit/Sources/App/Views/Profile/ProfileView.swift index 1dda93273..d6b16d333 100644 --- a/apple/OmnivoreKit/Sources/App/Views/Profile/ProfileView.swift +++ b/apple/OmnivoreKit/Sources/App/Views/Profile/ProfileView.swift @@ -136,6 +136,9 @@ struct ProfileView: View { #if os(iOS) Section { + NavigationLink(destination: ReaderSettingsView()) { + Text(LocalText.readerSettingsGeneric) + } NavigationLink(destination: PushNotificationSettingsView()) { Text(LocalText.pushNotificationsGeneric) } diff --git a/apple/OmnivoreKit/Sources/App/Views/Profile/ReaderSettingsView.swift b/apple/OmnivoreKit/Sources/App/Views/Profile/ReaderSettingsView.swift new file mode 100644 index 000000000..7c7943a15 --- /dev/null +++ b/apple/OmnivoreKit/Sources/App/Views/Profile/ReaderSettingsView.swift @@ -0,0 +1,27 @@ +import Services +import SwiftUI +import Views +import Utils + +enum OpenLinkIn: String { + case insideApp + case systemBrowser +} + +struct ReaderSettingsView: View { + @Environment(\.dismiss) private var dismiss + @AppStorage(UserDefaultKey.openExternalLinksIn.rawValue) var openExternalLinksIn = OpenLinkIn.insideApp.rawValue + + var body: some View { + List { + Picker(selection: $openExternalLinksIn, content: { + Text("Inside app").tag(OpenLinkIn.insideApp.rawValue) + Text("Use system browser").tag(OpenLinkIn.systemBrowser.rawValue) + }, label: { Text("Open links:") }) + .pickerStyle(MenuPickerStyle()) + }.navigationTitle(LocalText.readerSettingsGeneric) + .onReceive(NotificationCenter.default.publisher(for: Notification.Name("ScrollToTop"))) { _ in + dismiss() + } + } +} diff --git a/apple/OmnivoreKit/Sources/App/Views/WebReader/WebReaderContainer.swift b/apple/OmnivoreKit/Sources/App/Views/WebReader/WebReaderContainer.swift index 639bb7c90..b816f8987 100644 --- a/apple/OmnivoreKit/Sources/App/Views/WebReader/WebReaderContainer.swift +++ b/apple/OmnivoreKit/Sources/App/Views/WebReader/WebReaderContainer.swift @@ -414,16 +414,24 @@ struct WebReaderContainerView: View { titleVisibility: .visible) { Button(action: { if let linkToOpen = linkToOpen { - safariWebLink = SafariWebLink(id: UUID(), url: linkToOpen) + if UserDefaults.standard.string(forKey: UserDefaultKey.openExternalLinksIn.rawValue) == OpenLinkIn.systemBrowser.rawValue, UIApplication.shared.canOpenURL(linkToOpen) { + UIApplication.shared.open(linkToOpen) + } else { + safariWebLink = SafariWebLink(id: UUID(), url: linkToOpen) + } } }, label: { Text(LocalText.genericOpen) }) Button(action: { - #if os(iOS) - UIPasteboard.general.string = item.unwrappedPageURLString - #else - // Pasteboard.general.string = item.unwrappedPageURLString TODO: fix for mac - #endif - Snackbar.show(message: "Link copied", dismissAfter: 2000) + if let linkToOpen = linkToOpen?.absoluteString { +#if os(iOS) + UIPasteboard.general.string = linkToOpen +#else + // Pasteboard.general.string = item.unwrappedPageURLString TODO: fix for mac +#endif + Snackbar.show(message: "Link copied", dismissAfter: 2000) + } else { + Snackbar.show(message: "Error copying link", dismissAfter: 2000) + } }, label: { Text(LocalText.readerCopyLink) }) Button(action: { if let linkToOpen = linkToOpen { diff --git a/apple/OmnivoreKit/Sources/Utils/UserDefaultKeys.swift b/apple/OmnivoreKit/Sources/Utils/UserDefaultKeys.swift index 8e8953575..ee3758a02 100644 --- a/apple/OmnivoreKit/Sources/Utils/UserDefaultKeys.swift +++ b/apple/OmnivoreKit/Sources/Utils/UserDefaultKeys.swift @@ -35,6 +35,7 @@ public enum UserDefaultKey: String { case hideFeatureSection case hideSystemLabels case justifyText + case openExternalLinksIn case prefersHideStatusBarInReader case visibleShareExtensionTab } diff --git a/apple/OmnivoreKit/Sources/Views/LocalText.swift b/apple/OmnivoreKit/Sources/Views/LocalText.swift index 42625397e..a6739d66a 100644 --- a/apple/OmnivoreKit/Sources/Views/LocalText.swift +++ b/apple/OmnivoreKit/Sources/Views/LocalText.swift @@ -201,4 +201,5 @@ public enum LocalText { public static let dismissButton = localText(key: "dismissButton") public static let errorNetwork = localText(key: "errorNetwork") public static let documentationGeneric = localText(key: "documentationGeneric") + public static let readerSettingsGeneric = localText(key: "readerSettingsGeneric") } diff --git a/apple/OmnivoreKit/Sources/Views/Resources/en.lproj/Localizable.strings b/apple/OmnivoreKit/Sources/Views/Resources/en.lproj/Localizable.strings index e0566b829..8638b6849 100644 --- a/apple/OmnivoreKit/Sources/Views/Resources/en.lproj/Localizable.strings +++ b/apple/OmnivoreKit/Sources/Views/Resources/en.lproj/Localizable.strings @@ -171,7 +171,7 @@ "labelsGeneric" = "Labels"; "emailsGeneric" = "Emails"; "subscriptionsGeneric" = "Subscriptions"; -"textToSpeechGeneric" = "Text to Speech"; +"textToSpeechGeneric" = "Text to speech"; "privacyPolicyGeneric" = "Privacy Policy"; "termsAndConditionsGeneric" = "Terms and Conditions"; "feedbackGeneric" = "Feedback"; @@ -196,7 +196,8 @@ "clubsGeneric" = "Clubs"; "filterGeneric" = "Filters"; "errorGeneric" = "Something went wrong, please try again."; -"pushNotificationsGeneric" = "Push Notifications"; +"readerSettingsGeneric" = "Reader settings"; +"pushNotificationsGeneric" = "Push notifications"; "dismissButton" = "Dismiss"; "errorNetwork" = "We are having trouble connecting to the internet."; "documentationGeneric" = "Documentation";