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,