Show file list and contents
This commit is contained in:
39
CHDataManagement/Views/Files/FileContentView.swift
Normal file
39
CHDataManagement/Views/Files/FileContentView.swift
Normal file
@ -0,0 +1,39 @@
|
||||
import SwiftUI
|
||||
|
||||
struct FileContentView: View {
|
||||
|
||||
@ObservedObject
|
||||
var file: FileResource
|
||||
|
||||
@EnvironmentObject
|
||||
private var content: Content
|
||||
|
||||
@State
|
||||
private var fileContent: String = ""
|
||||
|
||||
var body: some View {
|
||||
VStack {
|
||||
if fileContent != "" {
|
||||
TextEditor(text: $fileContent)
|
||||
.font(.body.monospaced())
|
||||
.textEditorStyle(.plain)
|
||||
} else {
|
||||
Text("The file is not a text file")
|
||||
.onAppear(perform: loadFileContent)
|
||||
}
|
||||
}.padding()
|
||||
}
|
||||
|
||||
private func loadFileContent() {
|
||||
do {
|
||||
fileContent = try content.storage.fileContent(for: file.uniqueId)
|
||||
} catch {
|
||||
print(error)
|
||||
fileContent = ""
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#Preview {
|
||||
FileContentView(file: .mock)
|
||||
}
|
26
CHDataManagement/Views/Files/FileDetailView.swift
Normal file
26
CHDataManagement/Views/Files/FileDetailView.swift
Normal file
@ -0,0 +1,26 @@
|
||||
import SwiftUI
|
||||
|
||||
struct FileDetailView: View {
|
||||
|
||||
@ObservedObject
|
||||
var file: FileResource
|
||||
|
||||
var body: some View {
|
||||
VStack(alignment: .leading) {
|
||||
Text("File Name")
|
||||
.font(.headline)
|
||||
TextField("", text: $file.uniqueId)
|
||||
.textFieldStyle(.roundedBorder)
|
||||
.padding(.bottom)
|
||||
.disabled(true)
|
||||
Text("Description")
|
||||
.font(.headline)
|
||||
TextField("", text: $file.description)
|
||||
.textFieldStyle(.roundedBorder)
|
||||
}.padding()
|
||||
}
|
||||
}
|
||||
|
||||
#Preview {
|
||||
FileDetailView(file: .mock)
|
||||
}
|
@ -3,18 +3,76 @@ import SwiftUI
|
||||
struct FilesView: View {
|
||||
|
||||
@EnvironmentObject
|
||||
var content: Content
|
||||
private var content: Content
|
||||
|
||||
@State
|
||||
private var selected: FileResource? = nil
|
||||
|
||||
var body: some View {
|
||||
ScrollView {
|
||||
VStack {
|
||||
|
||||
NavigationSplitView {
|
||||
List(content.files, selection: $selected) { file in
|
||||
Text(file.uniqueId)
|
||||
.tag(file)
|
||||
}
|
||||
.toolbar {
|
||||
ToolbarItem(placement: .primaryAction) {
|
||||
Button(action: openFilePanel) {
|
||||
Label("Add file", systemSymbol: .plus)
|
||||
}
|
||||
}
|
||||
}
|
||||
.navigationSplitViewColumnWidth(min: 250, ideal: 250, max: 250)
|
||||
} content: {
|
||||
if let selected {
|
||||
FileContentView(file: selected)
|
||||
.id(selected.uniqueId)
|
||||
} else {
|
||||
Text("Select a file")
|
||||
}
|
||||
} detail: {
|
||||
if let selected {
|
||||
FileDetailView(file: selected)
|
||||
} else {
|
||||
EmptyView()
|
||||
}
|
||||
}
|
||||
Text(/*@START_MENU_TOKEN@*/"Hello, World!"/*@END_MENU_TOKEN@*/)
|
||||
}
|
||||
|
||||
private func openFilePanel() {
|
||||
let panel = NSOpenPanel()
|
||||
// Sets up so user can only select a single directory
|
||||
panel.canChooseFiles = true
|
||||
panel.canChooseDirectories = false
|
||||
panel.allowsMultipleSelection = true
|
||||
panel.showsHiddenFiles = false
|
||||
panel.title = "Select files to add"
|
||||
panel.prompt = ""
|
||||
|
||||
let response = panel.runModal()
|
||||
guard response == .OK else {
|
||||
print("Failed to select files to import")
|
||||
return
|
||||
}
|
||||
|
||||
for url in panel.urls {
|
||||
let fileId = url.lastPathComponent
|
||||
guard !content.files.contains(where: { $0.uniqueId == fileId }) else {
|
||||
print("A file '\(fileId)' already exists")
|
||||
continue
|
||||
}
|
||||
let file = FileResource(uniqueId: fileId, description: "")
|
||||
guard content.storage.copyFile(at: url, fileId: fileId) else {
|
||||
print("Failed to import file '\(fileId)'")
|
||||
continue
|
||||
}
|
||||
content.files.insert(file, at: 0)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
#Preview {
|
||||
FilesView()
|
||||
.environmentObject(Content.mock)
|
||||
}
|
||||
|
Reference in New Issue
Block a user