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

switch implementation from fileImporter to NSOpenPanel #23

Merged
merged 5 commits into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 27 additions & 13 deletions ClassDumper/Views/DatabaseButtons.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,45 @@ struct CreateFileButton: View {
@Environment(\.fileRepository) private var fileRepository
@EnvironmentObject var alertController: AlertController

@State private var importing = false
private var titleKey: LocalizedStringKey

init(_ titleKey: LocalizedStringKey = "Open…") {
self.titleKey = titleKey
}

private var panel: NSOpenPanel = {
let panel = NSOpenPanel()
panel.allowedContentTypes = [.application, .executable, .symbolicLink, .aliasFile]
panel.allowsMultipleSelection = false
panel.canChooseDirectories = true
panel.treatsFilePackagesAsDirectories = true
return panel
}()

private func openPanel() {
switch panel.runModal() {
case .OK:
guard let url = panel.url else {
print("Unable to read file url path from NSOpenPanel.")
return
}
success(with: url)
case .cancel:
break // noop, need something on this line with breaks being implicit
case .abort, .stop:
print("Abort or stop result from NSOpenPanel. No filepath to pass.")
default:
print("Something went really wrong with NSOpenPanel. Unable to read file contents")
}
}

var body: some View {
Button {
importing = true
openPanel()
} label: {
Label(titleKey, systemImage: "folder.badge.plus")
}
.keyboardShortcut("o", modifiers: [.command])
.fileImporter(
isPresented: $importing,
allowedContentTypes: [.application, .executable, .symbolicLink, .aliasFile]
) { result in
switch result {
case .success(let file):
success(with: file)
case .failure(let error):
print("Unable to read file contents: \(error.localizedDescription)")
}
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion ClassDumperUITests/ImportFlow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ struct ImportFlow: Screen {
func openApp(named appName: String) -> Self {
open(.filepicker)

app.sheets.firstMatch.outlineRows.staticTexts["Applications"].tap()
app.dialogs.firstMatch.outlineRows.staticTexts["Applications"].tap()

// taking advantage of finder directing keyboard input towards the middle column
app.typeText(appName)
Expand Down
Loading