import SwiftUI 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 { HorizontalCenter { Text("No posts yet.") .padding() } .listRowSeparator(.hidden) } HorizontalCenter { Button(action: { showNewPostIdSheet = true }) { Text("Add post") } .padding() .listRowSeparator(.hidden) } ForEach(content.posts) { post in HorizontalCenter { PostView(post: post) .frame(maxWidth: 600) } .listRowSeparator(.hidden) .listRowInsets(.init(top: 0, leading: 0, bottom: 30, trailing: 0)) } } .listStyle(.plain) .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: id, isDraft: true, createdDate: .now, startDate: .now, endDate: nil, tags: [], german: .init(title: "Titel", content: "Text"), english: .init(title: "Title", content: "Text")) content.posts.insert(post, at: 0) } } #Preview { PostList() .environmentObject(Content()) }