Skip to content

Instantly share code, notes, and snippets.

@benigumocom
Created December 21, 2023 02:21
Show Gist options
  • Save benigumocom/ce2fa6a437559ddd336efd423c240d25 to your computer and use it in GitHub Desktop.
Save benigumocom/ce2fa6a437559ddd336efd423c240d25 to your computer and use it in GitHub Desktop.
struct BirdsSearchResults<Content: View>: View {
private var searchText: Binding<String>
private var content: (Bird) -> Content
@Query(sort: \Bird.creationDate) private var birds: [Bird]
init(searchText: Binding<String>, @ViewBuilder content: @escaping (Bird) -> Content) {
self.searchText = searchText
self.content = content
}
var body: some View {
let searchText = self.searchText.wrappedValue
let filtered = searchText.isEmpty ? birds : birds.filter { bird in
bird.speciesName.contains(searchText)
}
ForEach(filtered) { bird in
content(bird)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment