102 lines
3.0 KiB
Swift
102 lines
3.0 KiB
Swift
import SwiftUI
|
|
import SFSafeSymbols
|
|
|
|
struct LocalizedPageDetailView: View {
|
|
|
|
@ObservedObject
|
|
private var page: LocalizedPage
|
|
|
|
init(page: LocalizedPage, showImagePicker: Bool = false) {
|
|
self.page = page
|
|
self.showImagePicker = showImagePicker
|
|
self.newUrlString = page.urlString
|
|
}
|
|
|
|
@State
|
|
private var showImagePicker = false
|
|
|
|
@State
|
|
private var newUrlString: String
|
|
|
|
private let allowedCharactersInPostId = CharacterSet.alphanumerics.union(CharacterSet(charactersIn: "-")).inverted
|
|
|
|
private var idExists: Bool {
|
|
page.content.pages.contains {
|
|
$0.german.urlString == newUrlString
|
|
|| $0.english.urlString == newUrlString
|
|
}
|
|
}
|
|
|
|
private var containsInvalidCharacters: Bool {
|
|
newUrlString.rangeOfCharacter(from: allowedCharactersInPostId) != nil
|
|
}
|
|
|
|
var body: some View {
|
|
VStack(alignment: .leading) {
|
|
HStack {
|
|
TextField("", text: $newUrlString)
|
|
.textFieldStyle(.roundedBorder)
|
|
Button("Update", action: setNewId)
|
|
.disabled(newUrlString.isEmpty || containsInvalidCharacters || idExists)
|
|
}
|
|
.padding(.bottom)
|
|
|
|
Text("Link Preview Title")
|
|
.font(.headline)
|
|
OptionalTextField("", text: $page.linkPreviewTitle,
|
|
prompt: page.title)
|
|
.textFieldStyle(.roundedBorder)
|
|
.padding(.bottom)
|
|
|
|
HStack {
|
|
Text("Link Preview Image")
|
|
.font(.headline)
|
|
IconButton(symbol: .squareAndPencilCircleFill,
|
|
size: 22,
|
|
color: .blue) {
|
|
showImagePicker = true
|
|
}
|
|
|
|
IconButton(symbol: .trashCircleFill,
|
|
size: 22,
|
|
color: .red) {
|
|
page.linkPreviewImage = nil
|
|
}.disabled(page.linkPreviewImage == nil)
|
|
Spacer()
|
|
}
|
|
|
|
.buttonStyle(.plain)
|
|
if let image = page.linkPreviewImage {
|
|
image.imageToDisplay
|
|
.resizable()
|
|
.aspectRatio(contentMode: .fit)
|
|
.frame(maxWidth: 400, maxHeight: 300)
|
|
.cornerRadius(8)
|
|
Text(image.id)
|
|
.font(.headline)
|
|
}
|
|
|
|
Text("Link Preview Description")
|
|
.font(.headline)
|
|
.padding(.top)
|
|
OptionalDescriptionField(text: $page.linkPreviewDescription)
|
|
.textFieldStyle(.roundedBorder)
|
|
.padding(.bottom)
|
|
}
|
|
.sheet(isPresented: $showImagePicker) {
|
|
ImagePickerView(showImagePicker: $showImagePicker) { image in
|
|
page.linkPreviewImage = image
|
|
}
|
|
}
|
|
}
|
|
|
|
private func setNewId() {
|
|
page.urlString = newUrlString
|
|
}
|
|
}
|
|
|
|
#Preview {
|
|
LocalizedPageDetailView(page: .english)
|
|
.environmentObject(Content.mock)
|
|
}
|