Unified detail views, model
This commit is contained in:
@ -5,44 +5,29 @@ struct FileDetailView: View {
|
||||
@ObservedObject
|
||||
var file: FileResource
|
||||
|
||||
@State
|
||||
private var newId: String
|
||||
|
||||
init(file: FileResource) {
|
||||
self.file = file
|
||||
self.newId = file.id
|
||||
}
|
||||
|
||||
private let allowedCharactersInPostId = CharacterSet.alphanumerics.union(CharacterSet(charactersIn: "-.")).inverted
|
||||
|
||||
private var idExists: Bool {
|
||||
file.content.files.contains { $0.id == newId }
|
||||
}
|
||||
|
||||
private var containsInvalidCharacters: Bool {
|
||||
newId.rangeOfCharacter(from: allowedCharactersInPostId) != nil
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
VStack(alignment: .leading) {
|
||||
Text("File Name")
|
||||
.font(.headline)
|
||||
HStack {
|
||||
TextField("", text: $newId)
|
||||
.textFieldStyle(.roundedBorder)
|
||||
Button(action: setNewId) {
|
||||
Text("Update")
|
||||
}
|
||||
.disabled(newId.isEmpty || containsInvalidCharacters || idExists)
|
||||
}
|
||||
Text("German Description")
|
||||
.font(.headline)
|
||||
TextField("", text: $file.german)
|
||||
.textFieldStyle(.roundedBorder)
|
||||
Text("English Description")
|
||||
.font(.headline)
|
||||
TextField("", text: $file.english)
|
||||
.textFieldStyle(.roundedBorder)
|
||||
DetailTitle(
|
||||
title: "File",
|
||||
text: "A file that can be used in a post or page")
|
||||
|
||||
IdPropertyView(
|
||||
id: $file.id,
|
||||
title: "Name",
|
||||
footer: "The unique name of the file, which is also used to reference it in posts and pages.",
|
||||
validation: file.isValid,
|
||||
update: { file.update(id: $0) })
|
||||
|
||||
StringPropertyView(
|
||||
title: "German Description",
|
||||
text: $file.german,
|
||||
footer: "The description for the file in German. Descriptions are used for images and to explain the content of a file.")
|
||||
|
||||
StringPropertyView(
|
||||
title: "English Description",
|
||||
text: $file.english,
|
||||
footer: "The description for the file in English. Descriptions are used for images and to explain the content of a file.")
|
||||
|
||||
if file.type.isImage {
|
||||
Text("Image size")
|
||||
.font(.headline)
|
||||
@ -53,12 +38,6 @@ struct FileDetailView: View {
|
||||
Spacer()
|
||||
}.padding()
|
||||
}
|
||||
|
||||
private func setNewId() {
|
||||
if !file.update(id: newId) {
|
||||
newId = file.id
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension FileDetailView: MainContentView {
|
||||
|
Reference in New Issue
Block a user