Created
June 17, 2022 22:21
-
-
Save doganaysahins/95d01786a1671b7b54b14db4abacce24 to your computer and use it in GitHub Desktop.
Empty list view extension
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| List{ | |
| ForEach(medViewModel.medicals, id : \.id) { medicalInfo in | |
| MedicalCardView() | |
| }.onDelete(perform: deletePet) | |
| .listRowSeparator(.hidden) | |
| }.emptyPlaceholder(medViewModel.medicals) { | |
| EmptyMedicalView() | |
| .padding() | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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