Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open last valid downloads folder if no files downloaded to default #2264

Merged
merged 6 commits into from
Mar 1, 2024
30 changes: 25 additions & 5 deletions DuckDuckGo/FileDownload/View/DownloadsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -142,13 +142,33 @@ final class DownloadsViewController: NSViewController {
// MARK: User Actions

@IBAction func openDownloadsFolderAction(_ sender: Any) {
guard let url = DownloadsPreferences().effectiveDownloadLocation
?? FileManager.default.urls(for: .downloadsDirectory, in: .userDomainMask).first
else {
return
let prefs = DownloadsPreferences()
var url: URL?
var itemToSelect: URL?

if prefs.alwaysRequestDownloadLocation {
url = FileManager.default.urls(for: .downloadsDirectory, in: .userDomainMask).first

if let lastDownloaded = viewModel.items.first/* last added */(where: {
// should still exist
$0.localURL != nil && FileManager.default.fileExists(atPath: $0.localURL!.deletingLastPathComponent().path)
}),
let localURL = lastDownloaded.localURL {
// if no downloads are from the preferred Downloads folder - open the last downloaded item folder
if url == nil || !viewModel.items.contains(where: { $0.localURL?.deletingLastPathComponent().path == url?.path }) {
url = localURL.deletingLastPathComponent()
}
// select last downloaded item
itemToSelect = localURL
}
} else {
url = prefs.effectiveDownloadLocation ?? FileManager.default.urls(for: .downloadsDirectory, in: .userDomainMask).first
}

guard let url else { return }

self.dismiss()
NSWorkspace.shared.selectFile(nil, inFileViewerRootedAtPath: url.path)
NSWorkspace.shared.selectFile(itemToSelect?.path, inFileViewerRootedAtPath: url.path)
}

@IBAction func clearDownloadsAction(_ sender: Any) {
Expand Down
Loading