External files, improve page generation
This commit is contained in:
@ -34,6 +34,7 @@ struct AddFileView: View {
|
||||
HStack {
|
||||
Button("Cancel", role: .cancel) { dismiss() }
|
||||
Button("Select more files", action: openFilePanel)
|
||||
Button("Add placeholder", action: addPlaceholderFile)
|
||||
Button("Add selected", action: importSelectedFiles)
|
||||
.disabled(filesToAdd.isEmpty)
|
||||
}
|
||||
@ -68,6 +69,11 @@ struct AddFileView: View {
|
||||
}
|
||||
}
|
||||
|
||||
private func addPlaceholderFile() {
|
||||
let newFile = FileToAdd(content: content, externalFile: "placeholder")
|
||||
filesToAdd.append(newFile)
|
||||
}
|
||||
|
||||
private func delete(file: FileToAdd) {
|
||||
guard let index = filesToAdd.firstIndex(of: file) else {
|
||||
return
|
||||
@ -85,16 +91,19 @@ struct AddFileView: View {
|
||||
print("Skipping existing file \(file.uniqueId)")
|
||||
continue
|
||||
}
|
||||
do {
|
||||
try content.storage.copyFile(at: file.url, fileId: file.uniqueId)
|
||||
} catch {
|
||||
print("Failed to import file '\(file.uniqueId)' at \(file.url.path()): \(error)")
|
||||
return
|
||||
if let url = file.url {
|
||||
do {
|
||||
try content.storage.copyFile(at: url, fileId: file.uniqueId)
|
||||
} catch {
|
||||
print("Failed to import file '\(file.uniqueId)' at \(url.path()): \(error)")
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
let resource = FileResource(
|
||||
content: content,
|
||||
id: file.uniqueId,
|
||||
isExternallyStored: file.url == nil,
|
||||
en: "", de: "")
|
||||
// TODO: Insert at correct index?
|
||||
content.files.insert(resource, at: 0)
|
||||
|
@ -13,44 +13,56 @@ struct FileContentView: View {
|
||||
|
||||
var body: some View {
|
||||
VStack {
|
||||
switch file.type {
|
||||
case .image:
|
||||
file.imageToDisplay
|
||||
.resizable()
|
||||
.aspectRatio(contentMode: .fit)
|
||||
case .model:
|
||||
if file.isExternallyStored {
|
||||
VStack {
|
||||
Image(systemSymbol: .cubeTransparent)
|
||||
Image(systemSymbol: .squareDashed)
|
||||
.resizable()
|
||||
.aspectRatio(contentMode: .fit)
|
||||
.frame(width: iconSize)
|
||||
Text("No preview available")
|
||||
Text("External file")
|
||||
.font(.title)
|
||||
}
|
||||
.foregroundStyle(.secondary)
|
||||
case .text, .code:
|
||||
TextFileContentView(file: file)
|
||||
.id(file.id)
|
||||
case .video:
|
||||
VStack {
|
||||
Image(systemSymbol: .film)
|
||||
} else {
|
||||
switch file.type {
|
||||
case .image:
|
||||
file.imageToDisplay
|
||||
.resizable()
|
||||
.aspectRatio(contentMode: .fit)
|
||||
.frame(width: iconSize)
|
||||
Text("No preview available")
|
||||
.font(.title)
|
||||
case .model:
|
||||
VStack {
|
||||
Image(systemSymbol: .cubeTransparent)
|
||||
.resizable()
|
||||
.aspectRatio(contentMode: .fit)
|
||||
.frame(width: iconSize)
|
||||
Text("No preview available")
|
||||
.font(.title)
|
||||
}
|
||||
.foregroundStyle(.secondary)
|
||||
case .text, .code:
|
||||
TextFileContentView(file: file)
|
||||
.id(file.id)
|
||||
case .video:
|
||||
VStack {
|
||||
Image(systemSymbol: .film)
|
||||
.resizable()
|
||||
.aspectRatio(contentMode: .fit)
|
||||
.frame(width: iconSize)
|
||||
Text("No preview available")
|
||||
.font(.title)
|
||||
}
|
||||
.foregroundStyle(.secondary)
|
||||
case .other:
|
||||
VStack {
|
||||
Image(systemSymbol: .docQuestionmark)
|
||||
.resizable()
|
||||
.aspectRatio(contentMode: .fit)
|
||||
.frame(width: iconSize)
|
||||
Text("No preview available")
|
||||
.font(.title)
|
||||
}
|
||||
.foregroundStyle(.secondary)
|
||||
}
|
||||
.foregroundStyle(.secondary)
|
||||
case .other:
|
||||
VStack {
|
||||
Image(systemSymbol: .docQuestionmark)
|
||||
.resizable()
|
||||
.aspectRatio(contentMode: .fit)
|
||||
.frame(width: iconSize)
|
||||
Text("No preview available")
|
||||
.font(.title)
|
||||
}
|
||||
.foregroundStyle(.secondary)
|
||||
}
|
||||
}.padding()
|
||||
}
|
||||
|
@ -2,9 +2,12 @@ import Foundation
|
||||
|
||||
final class FileToAdd: ObservableObject {
|
||||
|
||||
let id: Int
|
||||
|
||||
unowned let content: Content
|
||||
|
||||
let url: URL
|
||||
// The external path to the file, or nil if the file is just a placeholder
|
||||
let url: URL?
|
||||
|
||||
@Published
|
||||
var uniqueId: String
|
||||
@ -13,11 +16,19 @@ final class FileToAdd: ObservableObject {
|
||||
var isSelected: Bool = true
|
||||
|
||||
init(content: Content, url: URL) {
|
||||
self.id = .random()
|
||||
self.content = content
|
||||
self.url = url
|
||||
self.uniqueId = url.lastPathComponent
|
||||
}
|
||||
|
||||
init(content: Content, externalFile: String) {
|
||||
self.id = .random()
|
||||
self.content = content
|
||||
self.url = nil
|
||||
self.uniqueId = externalFile
|
||||
}
|
||||
|
||||
var idAlreadyExists: Bool {
|
||||
content.files.contains { $0.id == uniqueId }
|
||||
}
|
||||
@ -25,9 +36,6 @@ final class FileToAdd: ObservableObject {
|
||||
|
||||
extension FileToAdd: Identifiable {
|
||||
|
||||
var id: URL {
|
||||
url
|
||||
}
|
||||
}
|
||||
|
||||
extension FileToAdd: Equatable {
|
||||
|
@ -30,7 +30,7 @@ struct FileToAddView: View {
|
||||
.frame(maxWidth: 200)
|
||||
|
||||
}
|
||||
Text(file.url.path())
|
||||
Text(file.url?.path() ?? "Placeholder file")
|
||||
.foregroundStyle(.secondary)
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user