Skip to content

Instantly share code, notes, and snippets.

@doganaysahins
Created June 17, 2022 22:21
Show Gist options
  • Select an option

  • Save doganaysahins/95d01786a1671b7b54b14db4abacce24 to your computer and use it in GitHub Desktop.

Select an option

Save doganaysahins/95d01786a1671b7b54b14db4abacce24 to your computer and use it in GitHub Desktop.
Empty list view extension
List{
ForEach(medViewModel.medicals, id : \.id) { medicalInfo in
MedicalCardView()
}.onDelete(perform: deletePet)
.listRowSeparator(.hidden)
}.emptyPlaceholder(medViewModel.medicals) {
EmptyMedicalView()
.padding()
}
struct EmptyPlaceholderModifier<Items: Collection>: ViewModifier {
let items: Items
let placeholder: AnyView
@ViewBuilder func body(content: Content) -> some View {
if !items.isEmpty {
content
} else {
placeholder
}
}
}
extension View {
func emptyPlaceholder<Items: Collection, PlaceholderView: View>(_ items: Items, _ placeholder: @escaping () -> PlaceholderView) -> some View {
modifier(EmptyPlaceholderModifier(items: items, placeholder: AnyView(placeholder())))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment