From c85836eda6b8c02a7b5e92c3782f9e62e1bcb8f0 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Thu, 14 Nov 2019 19:53:27 -0500 Subject: [PATCH] Add preferences for using in-app vs out of app Safari and using Reader Mode for in-app --- Tusker/Preferences/Preferences.swift | 8 ++++++++ Tusker/Screens/Preferences/BehaviorPrefsView.swift | 6 ++++++ Tusker/TuskerNavigationDelegate.swift | 14 ++++++++++++-- 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/Tusker/Preferences/Preferences.swift b/Tusker/Preferences/Preferences.swift index e3673cfc..5135ff46 100644 --- a/Tusker/Preferences/Preferences.swift +++ b/Tusker/Preferences/Preferences.swift @@ -46,6 +46,8 @@ class Preferences: Codable, ObservableObject { self.automaticallySaveDrafts = try container.decode(Bool.self, forKey: .automaticallySaveDrafts) self.contentWarningCopyMode = try container.decode(ContentWarningCopyMode.self, forKey: .contentWarningCopyMode) self.openLinksInApps = try container.decode(Bool.self, forKey: .openLinksInApps) + self.useInAppSafari = try container.decode(Bool.self, forKey: .useInAppSafari) + self.inAppSafariAutomaticReaderMode = try container.decode(Bool.self, forKey: .inAppSafariAutomaticReaderMode) self.showFavoriteAndReblogCounts = try container.decode(Bool.self, forKey: .showFavoriteAndReblogCounts) self.defaultNotificationsMode = try container.decode(NotificationsMode.self, forKey: .defaultNotificationsType) @@ -65,6 +67,8 @@ class Preferences: Codable, ObservableObject { try container.encode(automaticallySaveDrafts, forKey: .automaticallySaveDrafts) try container.encode(contentWarningCopyMode, forKey: .contentWarningCopyMode) try container.encode(openLinksInApps, forKey: .openLinksInApps) + try container.encode(useInAppSafari, forKey: .useInAppSafari) + try container.encode(inAppSafariAutomaticReaderMode, forKey: .inAppSafariAutomaticReaderMode) try container.encode(showFavoriteAndReblogCounts, forKey: .showFavoriteAndReblogCounts) try container.encode(defaultNotificationsMode, forKey: .defaultNotificationsType) @@ -83,6 +87,8 @@ class Preferences: Codable, ObservableObject { @Published var automaticallySaveDrafts = true @Published var contentWarningCopyMode = ContentWarningCopyMode.asIs @Published var openLinksInApps = true + @Published var useInAppSafari = true + @Published var inAppSafariAutomaticReaderMode = false // MARK: - Digital Wellness @Published var showFavoriteAndReblogCounts = true @@ -101,6 +107,8 @@ class Preferences: Codable, ObservableObject { case automaticallySaveDrafts case contentWarningCopyMode case openLinksInApps + case useInAppSafari + case inAppSafariAutomaticReaderMode case showFavoriteAndReblogCounts case defaultNotificationsType diff --git a/Tusker/Screens/Preferences/BehaviorPrefsView.swift b/Tusker/Screens/Preferences/BehaviorPrefsView.swift index 486377ce..c062583d 100644 --- a/Tusker/Screens/Preferences/BehaviorPrefsView.swift +++ b/Tusker/Screens/Preferences/BehaviorPrefsView.swift @@ -47,6 +47,12 @@ struct BehaviorPrefsView: View { Toggle(isOn: $preferences.openLinksInApps) { Text("Open Links in Apps") } + Toggle(isOn: $preferences.useInAppSafari) { + Text("Use In-App Safari") + } + Toggle(isOn: $preferences.inAppSafariAutomaticReaderMode) { + Text("Always Use Reader Mode in In-App Safari") + }.disabled(!preferences.useInAppSafari) } } } diff --git a/Tusker/TuskerNavigationDelegate.swift b/Tusker/TuskerNavigationDelegate.swift index 40d0b2c3..e7707eba 100644 --- a/Tusker/TuskerNavigationDelegate.swift +++ b/Tusker/TuskerNavigationDelegate.swift @@ -74,14 +74,24 @@ extension TuskerNavigationDelegate where Self: UIViewController { } func selected(url: URL) { + func openSafari() { + if Preferences.shared.useInAppSafari { + let config = SFSafariViewController.Configuration() + config.entersReaderIfAvailable = Preferences.shared.inAppSafariAutomaticReaderMode + present(SFSafariViewController(url: url, configuration: config), animated: true) + } else { + UIApplication.shared.open(url, options: [:]) + } + } + if (Preferences.shared.openLinksInApps) { UIApplication.shared.open(url, options: [.universalLinksOnly: true]) { (success) in if (!success) { - self.present(SFSafariViewController(url: url), animated: true) + openSafari() } } } else { - present(SFSafariViewController(url: url), animated: true) + openSafari() } }