Set new post ids

This commit is contained in:
Christoph Hagen
2024-11-24 20:32:07 +01:00
parent 64b6d88a41
commit 5b29ee65fb
3 changed files with 70 additions and 3 deletions

View File

@ -5,6 +5,12 @@ struct PostList: View {
@EnvironmentObject
private var content: Content
@State
private var showNewPostIdSheet = false
@State
private var newPostId = ""
var body: some View {
List {
if content.posts.isEmpty {
@ -15,7 +21,7 @@ struct PostList: View {
.listRowSeparator(.hidden)
}
HorizontalCenter {
Button(action: addNewPost) {
Button(action: { showNewPostIdSheet = true }) {
Text("Add post")
}
.padding()
@ -31,12 +37,24 @@ struct PostList: View {
}
}
.listStyle(.plain)
//.scrollContentBackground(.hidden)
.sheet(isPresented: $showNewPostIdSheet, onDismiss: addNewPost) {
TextEntrySheet(title: "Enter the new post id", text: $newPostId)
}
}
private func addNewPost() {
let id = newPostId.trimmingCharacters(in: .whitespacesAndNewlines)
guard id != "" else {
return
}
guard !content.posts.contains(where: { $0.id == id }) else {
print("ID \(id) already exists")
return
}
let post = Post(
id: "new",
id: id,
isDraft: true,
createdDate: .now,
startDate: .now,

View File

@ -0,0 +1,45 @@
import SwiftUI
struct TextEntrySheet: View {
let title: String
@Binding
var text: String
@Environment(\.dismiss)
var dismiss: DismissAction
@State
private var enteredText: String = ""
var body: some View {
VStack {
Text(title)
.foregroundStyle(.secondary)
TextField("Text", text: $enteredText)
HStack {
Button(action: submit) {
Text("Submit")
}
Button(role: .cancel, action: cancel) {
Text("Cancel")
}
}
}
.padding()
}
private func submit() {
text = enteredText
dismiss()
}
private func cancel() {
dismiss()
}
}
#Preview {
TextEntrySheet(title: "Enter the id for the new post", text: .constant("new"))
}