Move multi-url opening to an extension on Browser

Also reword the alert a bit and add the number of pages to the "Open" button
This commit is contained in:
Nate Weaver
2022-10-08 01:23:24 -05:00
parent 49e8b921ba
commit 7f59f94542
4 changed files with 42 additions and 35 deletions

View File

@@ -73,3 +73,42 @@ extension Browser {
NSLocalizedString("Open in Browser in Background", comment: "Open in Browser in Background menu item title")
}
}
extension Browser {
static func open(_ urlStrings: [String], alertingInWindow window: NSWindow?, invertPreference: Bool = false) {
if urlStrings.count > 500 {
return
}
func doOpenURLs() {
for urlString in urlStrings {
Browser.open(urlString, invertPreference: invertPreference)
}
}
if urlStrings.count > 20 {
let alert = NSAlert()
let messageFormat = NSLocalizedString("Are you sure you want to open %ld articles in your browser?", comment: "")
alert.messageText = String.localizedStringWithFormat(messageFormat, urlStrings.count)
let confirmButtonTitleFormat = NSLocalizedString("Open %ld Articles", comment: "")
alert.addButton(withTitle: String.localizedStringWithFormat(confirmButtonTitleFormat, urlStrings.count))
alert.addButton(withTitle: NSLocalizedString("Cancel", comment: ""))
if let window {
alert.beginSheetModal(for: window) { response in
if response == .alertFirstButtonReturn {
doOpenURLs()
}
}
} else {
if alert.runModal() == .alertFirstButtonReturn {
doOpenURLs()
}
}
} else {
doOpenURLs()
}
}
}

View File

@@ -340,40 +340,10 @@ class MainWindowController : NSWindowController, NSUserInterfaceValidations {
}
}
func openArticleURLs(_ urlStrings: [String], invertPreference: Bool = false) {
if urlStrings.count > 500 {
return
}
func doOpenURLs() {
for urlString in urlStrings {
Browser.open(urlString, invertPreference: invertPreference)
}
}
if urlStrings.count > 20 {
let alert = NSAlert()
let messageFormat = NSLocalizedString("Are you sure you want to open %ld articles in your browser?", comment: "")
alert.messageText = String.localizedStringWithFormat(messageFormat, urlStrings.count)
alert.addButton(withTitle: NSLocalizedString("Open", comment: ""))
alert.addButton(withTitle: NSLocalizedString("Cancel", comment: ""))
guard let window = self.window else { return }
alert.beginSheetModal(for: window) { response in
if response == .alertFirstButtonReturn {
doOpenURLs()
}
}
} else {
doOpenURLs()
}
}
@IBAction func openArticleInBrowser(_ sender: Any?) {
guard let selectedArticles else { return }
let urlStrings = selectedArticles.compactMap { $0.preferredLink }
openArticleURLs(urlStrings, invertPreference: NSApp.currentEvent?.modifierFlags.contains(.shift) ?? false)
Browser.open(urlStrings, alertingInWindow: window, invertPreference: NSApp.currentEvent?.modifierFlags.contains(.shift) ?? false)
}
@IBAction func openInBrowser(_ sender: Any?) {

View File

@@ -93,8 +93,7 @@ extension TimelineViewController {
return
}
guard let windowController = self.view.window?.windowController as? MainWindowController else { return }
windowController.openArticleURLs(urlStrings)
Browser.open(urlStrings, alertingInWindow: self.view.window)
}
@objc func copyURLFromContextualMenu(_ sender: Any?) {

View File

@@ -315,9 +315,8 @@ final class TimelineViewController: NSViewController, UndoableCommandRunner, Unr
// MARK: - Actions
@objc func openArticleInBrowser(_ sender: Any?) {
guard let windowController = self.view.window?.windowController as? MainWindowController else { return }
let urlStrings = selectedArticles.compactMap { $0.preferredLink }
windowController.openArticleURLs(urlStrings)
Browser.open(urlStrings, alertingInWindow: self.view.window)
}
@IBAction func toggleStatusOfSelectedArticles(_ sender: Any?) {