Reputation: 5329
I have a NavigationStack
, inside which there is a button. Tapping on the button presents Apple's SubscriptionStoreView
inside fullScreenCover
. I was surprised that when I tap on "Subscribe" inside the view, and Apple's controller for completing the purchase is presented/dismissed, it cause the fullScreenCover
to be redrawn causing a flicker and to reset the state of the store view.
import SwiftUI
import StoreKit
struct TestView: View {
@State private var isShowingSubscriptionScreen: Bool = false
var body: some View {
NavigationStack {
List {
Section {
HStack {
Label("Subscribe", systemImage:"crown.fill")
.fullScreenCover(isPresented: $isShowingSubscriptionScreen) {
SubscriptionStoreView(groupID: "4CD3E8A1")
}
.onTapGesture {
isShowingSubscriptionScreen = true
}
}
}
}
}
}
}
Further I was surprised that removing the HStack
section solved the issue.
I can solve this easily by attaching the fullScreenCover
to a different view outside of the navigation stack, however I would like to know the explanation of why it is happening. How does Apple's controller can affect the state and cause a redrawing of my views even if there is no state.
Upvotes: 0
Views: 60
Reputation: 30549
The state problem is because you haven't called the isShowingSubscriptionScreen
getter anywhere. This is because your source of truth is wrong, it shouldnt be a boolean but be the groupID you want to show, e.g. something like this:
struct TestView: View {
@State private var showGroupID: Group.ID? = nil
var body: some View {
NavigationStack {
List {
Section {
HStack {
Label("Subscribe", systemImage:"crown.fill")
.fullScreenCover(item: $showGroupID) { groupID in
SubscriptionStoreView(groupID: groupID)
}
.onTapGesture {
showGroupID = "4CD3E8A1"
}
}
}
}
}
}
}
Upvotes: 0