Posts

Post not yet marked as solved
2 Replies
1.2k Views
Hi guys. I have a question related to the next behavior. I have ContentView with a list of views where the corresponding view models are passed. The user can click by some view. At the moment full-screen modal dialog will be shown according to the passed type. It's fine. At some time my view models are being updated and the whole ContentView will be reloaded. The problem is: fullScreenCover is called and ChildEventView is recreated. How to prevent recreating ChildEventView? struct ContentView: View { 		@ObservedObject private var eventListViewModel = EventListViewModel() 		@State private var fullScreenType: FullScreenType? 		/* some stuff */ 		var body: some View { 				ScrollView { 						LazyVStack { 								ForEach(eventListViewModel.cardStates.indices, id: \.self) { index in 										let eventVM = eventListViewModel.eventVMs[index] 										EventCardView(eventViewModel: eventVM, eventId: $selectedEvent.eventId) { 												self.fullScreenType = .type1 										} 										/* some other views */ 								} 						} 				}	 				.fullScreenCover(item: $fullScreenType, onDismiss: { 						self.fullScreenType = nil 				}, content: { fullScreenType in 						switch fullScreenType { 								case .type1: 								return ChildEventView(selectedEvent.eventId).eraseToAnyView() 								/* some other cases */ 						} 				}) 		} }
Posted Last updated
.
Post not yet marked as solved
2 Replies
1.4k Views
Hi guys. I'm new here. Unfortunately, I couldn't find an answer related to present different views as modal in fullScreenCover based on the view type. And I hope we will find the right way quickly =). So the goal: I would like to show different views using the single fullScreenCover. What I have: I defined enum with possible view types. enum FullScreenType: Int, Identifiable { 		var id: Int { rawValue } 		case none 		case seasonDetails 		case topTimes } 2. Defined method that returns a required view func makeFullScreenContent(_ fullScreenType: FullScreenType) -> some View { 				return Group { 						switch fullScreenType { 						case .seasonDetails: 								SeasonDetailsView(selectedSeason: self.$season, showModal: $showModal) 						case .topTimes: 								TimesView(eventId: $eventId, showModal: $showModal) 						case .none: 								EmptyView() 						} 				} 		} 3. My ContentView looks as: struct ContentView: View { 		// some code 		// ... 		@State private var showModal = false 		@State private var fullScreenType: FullScreenType? = Optional.none 		var body: some View { 				VStack { 				// some stuff 				// ... 						if card.flipped { 										self.fullScreenType = .topTimes 										self.showModal.toggle() 						} else { 										self.fullScreenType = .seasonDetails 										self.showModal.toggle() 								} 						} 				} 				.fullScreenCover(item: $fullScreenType, content: { fullScreenType in 						makeFullScreenContent(fullScreenType) 				}) 		} } Here I would like to keep Binding property showModal to close my custom views. But the main problem is -> my custom view isn't open. I debugged code and I made sure the corresponding views return. So what is the problem?
Posted Last updated
.