Skip to content

Commit

Permalink
Added option to open links in external browser
Browse files Browse the repository at this point in the history
  • Loading branch information
michalrentka committed Jun 25, 2024
1 parent 78ce944 commit e6df82c
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 30 deletions.
3 changes: 3 additions & 0 deletions Zotero/Models/Defaults.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ final class Defaults {
@UserDefault(key: "ShowCollectionItemCounts", defaultValue: true, defaults: .standard)
var showCollectionItemCounts: Bool

@UserDefault(key: "OpenLinksInExternalBrowser", defaultValue: false, defaults: .standard)
var openLinksInExternalBrowser: Bool

@UserDefault(key: "QuickCopyStyleId", defaultValue: "http://www.zotero.org/styles/chicago-note-bibliography", defaults: .standard)
var quickCopyStyleId: String

Expand Down
8 changes: 4 additions & 4 deletions Zotero/Scenes/Detail/DetailCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -327,8 +327,8 @@ final class DetailCoordinator: Coordinator {
func show(url: URL) {
DDLogInfo("DetailCoordinator: show url \(url.absoluteString)")

if !controllers.urlDetector.isUrl(string: url.absoluteString) {
self.showWeb(url: url)
if !Defaults.shared.openLinksInExternalBrowser || !controllers.urlDetector.isUrl(string: url.absoluteString) {
showWeb(url: url)
return
}

Expand All @@ -341,7 +341,7 @@ final class DetailCoordinator: Coordinator {
UIApplication.shared.open(fixedUrl)
}

func showWeb(url: URL) {
private func showWeb(url: URL) {
let controller = SFSafariViewController(url: url.withHttpSchemeIfMissing)
controller.modalPresentationStyle = .fullScreen
// Changes transition to normal modal transition instead of push from right.
Expand Down Expand Up @@ -722,7 +722,7 @@ extension DetailCoordinator: DetailItemDetailCoordinatorDelegate {
navigationController?.present(controller, animated: true, completion: nil)

func attachmentMessageAndActions(for error: Error) -> (String, [UIAlertAction]) {
var actions: [UIAlertAction] = [UIAlertAction(title: L10n.ok, style: .cancel)]
let actions: [UIAlertAction] = [UIAlertAction(title: L10n.ok, style: .cancel)]
if let error = error as? AttachmentDownloader.Error {
switch error {
case .incompatibleAttachment:
Expand Down
20 changes: 2 additions & 18 deletions Zotero/Scenes/General/NoteEditorCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ typealias NoteEditorSaveResult = NoteEditorCoordinator.SaveResult
typealias NoteEditorSaveCallback = NoteEditorCoordinator.SaveCallback

protocol NoteEditorCoordinatorDelegate: AnyObject {
func showWeb(url: URL)
func show(url: URL)
func showTagPicker(libraryId: LibraryIdentifier, selected: Set<String>, picked: @escaping ([Tag]) -> Void)
}
Expand Down Expand Up @@ -94,22 +93,7 @@ extension NoteEditorCoordinator: NoteEditorCoordinatorDelegate {
}

func show(url: URL) {
if let scheme = url.scheme, scheme != "http" && scheme != "https" {
UIApplication.shared.open(url)
} else {
showWeb(url: url)
}
}

func showWeb(url: URL) {
guard let navigationController else { return }

let controller = SFSafariViewController(url: url.withHttpSchemeIfMissing)
controller.modalPresentationStyle = .fullScreen
// Changes transition to normal modal transition instead of push from right.
transitionDelegate = EmptyTransitioningDelegate()
controller.transitioningDelegate = self.transitionDelegate
transitionDelegate = nil
navigationController.present(controller, animated: true, completion: nil)
guard let detailCoordinator = parentCoordinator as? DetailCoordinator else { return }
detailCoordinator.show(url: url)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ import Foundation
enum GeneralSettingsAction {
case setShowSubcollectionItems(Bool)
case setShowCollectionItemCounts(Bool)
case setOpenLinksInExternalBrowser(Bool)
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,15 @@ struct GeneralSettingsState: ViewModelState {
}
}

var openLinksInExternalBrowser: Bool {
get {
return Defaults.shared.openLinksInExternalBrowser
}

set {
Defaults.shared.openLinksInExternalBrowser = newValue
}
}

func cleanup() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,19 @@ struct GeneralSettingsActionHandler: ViewModelActionHandler {
func process(action: GeneralSettingsAction, in viewModel: ViewModel<GeneralSettingsActionHandler>) {
switch action {
case .setShowSubcollectionItems(let value):
self.update(viewModel: viewModel) { state in
update(viewModel: viewModel) { state in
state.showSubcollectionItems = value
}

case .setShowCollectionItemCounts(let value):
self.update(viewModel: viewModel) { state in
update(viewModel: viewModel) { state in
state.showCollectionItemCounts = value
}

case .setOpenLinksInExternalBrowser(let value):
update(viewModel: viewModel) { state in
state.openLinksInExternalBrowser = value
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,23 @@ struct GeneralSettingsView: View {

var body: some View {
Form {
SettingsToggleRow(title: L10n.Settings.General.showSubcollectionsTitle,
subtitle: nil,
value: self.viewModel.binding(keyPath: \.showSubcollectionItems, action: { .setShowSubcollectionItems($0) }))
SettingsToggleRow(
title: L10n.Settings.General.showSubcollectionsTitle,
subtitle: nil,
value: self.viewModel.binding(keyPath: \.showSubcollectionItems, action: { .setShowSubcollectionItems($0) })
)

SettingsToggleRow(title: L10n.Settings.General.showCollectionItemCounts,
subtitle: nil,
value: self.viewModel.binding(keyPath: \.showCollectionItemCounts, action: { .setShowCollectionItemCounts($0) }))
SettingsToggleRow(
title: L10n.Settings.General.showCollectionItemCounts,
subtitle: nil,
value: self.viewModel.binding(keyPath: \.showCollectionItemCounts, action: { .setShowCollectionItemCounts($0) })
)

SettingsToggleRow(
title: "Open links in external browser",
subtitle: nil,
value: self.viewModel.binding(keyPath: \.openLinksInExternalBrowser, action: { .setOpenLinksInExternalBrowser($0) })
)
}
.navigationBarTitle(L10n.Settings.General.title)
}
Expand Down

0 comments on commit e6df82c

Please sign in to comment.