Add button command, fix list selection

This commit is contained in:
Christoph Hagen
2025-01-29 09:16:29 +01:00
parent eaad0a4bff
commit e291fbec52
11 changed files with 305 additions and 68 deletions

View File

@ -48,15 +48,13 @@ struct PostListView: View {
@EnvironmentObject
private var content: Content
@Binding
private var selectedPost: Post?
@EnvironmentObject
private var selection: SelectedContent
@State
private var searchString = ""
init(selectedPost: Binding<Post?>) {
self._selectedPost = selectedPost
}
init() { }
private var filteredPosts: [Post] {
guard !searchString.isEmpty else {
@ -74,13 +72,20 @@ struct PostListView: View {
TextField("", text: $searchString, prompt: Text("Search"))
.textFieldStyle(.roundedBorder)
.padding(.horizontal, 8)
List(filteredAndSortedPosts, selection: $selectedPost) { post in
List(filteredAndSortedPosts) { post in
PostListItem(post: post)
.tag(post)
.listRowBackground(RoundedRectangle(cornerRadius: 5)
.fill(selection.post == post ? Color.blue : Color.clear)
.padding(.horizontal, 10)
)
.contentShape(Rectangle())
.onTapGesture {
selection.post = post
}
}
}.onAppear {
if selectedPost == nil {
selectedPost = content.posts.first
if selection.post == nil, let first = content.posts.first {
selection.post = first
}
}
}