fullScreenCover dissmissed after swipe of tabView | SwiftUI | iOS

185 Views Asked by At

My application has a splash screen with the following code, It redirects to ApplicationSwitcher()

HStack {
Spacer()
VStack(alignment: .center) {
    Spacer()
    // Text("Value: \(vm.isNavigating == true ? "True" : "False")")
    Image(systemName: "apple.logo")
        .resizable()
        .scaledToFit()
        .foregroundColor(.LabelRev)
        .frame(maxHeight: .infinity, alignment: .center)
        .aspectRatio(contentMode: .fit)
        .clipped()
    
    Spacer()
    
    if appUpdateChecker.isLoading {
        ProgressView("Checking for Updates...")
            .progressViewStyle(CircularProgressViewStyle())
    } else {
        Text(appUpdateChecker.needsUpdate ? "New Version is Available. Please Update to Proceed" : "You are Using the Latest Version")
            .font(.system(size: showingUpdateAlert ? Sizes.MESSAGE : Sizes.LABEL))
            .multilineTextAlignment(.center)
            .foregroundColor(Color.Dim)
            .underline(!showingUpdateAlert && appUpdateChecker.needsUpdate)
            .onTapGesture {
                if appUpdateChecker.needsUpdate {
                    appUpdateChecker.initiateAppUpdate()
                }
            }
    }
}
.onReceive(appUpdateChecker.$goodToGo) { value in
    if value {
        DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
            vm.isNavigating = true
        }
    }
}
.fullScreenCover(isPresented: $vm.isNavigating, content: {
    NavigationStack {
        ApplicationSwitcher()
    }
    .frame(maxWidth: .infinity, maxHeight: .infinity)
    .environmentObject(vm)
    .environmentObject(sbVM)
    .environmentObject(orderBookViewModel)
})
Spacer()
}

In ApplicationSwitcher() I have a code :

VStack(spacing: 0) {
        TabView(selection: $orderBookViewModel.activeTab) {
            WatchlistView()
                .applyBackground()
                .tag(MainTab.Watchlist)
                .id(MainTab.Watchlist)
            
            Orders()
                .applyBackground()
                .tag(MainTab.Orders)
                .id(MainTab.Orders)
            
            Positions()
                .applyBackground()
                .tag(MainTab.Positions)
                .id(MainTab.Positions)
            
            OverviewView()
                .applyBackground()
                .tag(MainTab.Overview)
                .id(MainTab.Overview)

            AdvisoryView()
                .applyBackground()
                .tag(MainTab.Advisory)
                .id(MainTab.Advisory)
        }
     }

In Watchlist I have code:

TabView (selection: $activeTab) {
    ForEach(watchlist.indices, id: \.self) { index in
        if activeTab == index {
            ScrollView{
                getList(index: index)
                
            }
        }                                
    }
}
.tabViewStyle(.page(indexDisplayMode: .never))

with getList function as -

func getList(index: Int) -> some View {
    
    return LazyVStack (spacing: 0) {
        
        ForEach(watchlist[index].scrips, id: \.self) { scrip in
            if scrip.pInstType != nil {
                    
                    watchListItem(scrip: scrip, advisoryFlag: true)
                        .environmentObject(commonViewModel)
                    Divider().background(Color.gray)
            }
        }
    }
    .environmentObject(accountViewModel)
}

Edit I am using a extension to bottom sheet

extension View {
@ViewBuilder
func bottomSheet<Content: View>(
    presentationDetents: Set<PresentationDetent>,
    isPresented: Binding<Bool>,
    selectedDetent: Binding<PresentationDetent>,
    dragIndicator: Visibility = .visible,
    sheetCornerRadius: CGFloat? = CGFloat(Sizes.CORNER_RADIUS),
    largestUndimmedIdentifier: UISheetPresentationController.Detent.Identifier = .large,
    isTransparent: Bool = false,
    interactiveDisabled: Bool = false,
    @ViewBuilder content: @escaping () -> Content,
    onDismiss: @escaping () -> ()
) -> some View {
    self
        .sheet(isPresented: isPresented) {
            onDismiss()
        } content: {
            content()
                .presentationDetents(presentationDetents, selection: selectedDetent)
                .presentationDragIndicator(dragIndicator)
                .interactiveDismissDisabled(interactiveDisabled)
                .onAppear {
                    guard let windows = UIApplication.shared.connectedScenes.first as? UIWindowScene else {
                        return
                    }
                    
                    if let controller = windows.windows.first?.rootViewController?.presentedViewController,
                       let sheet = controller.presentationController as? UISheetPresentationController {
                        if isTransparent {
                            controller.view.backgroundColor = .clear
                        }
                        controller.presentingViewController?.view.tintAdjustmentMode = .normal
                        sheet.largestUndimmedDetentIdentifier = largestUndimmedIdentifier
                        sheet.preferredCornerRadius = sheetCornerRadius
                    } else {
                        print("NO CONTROLLER FOUND")
                    }
                }
        }
}

}

After I click on the watchlist Item and close the pop-up and try to switch the tab of watchlist

It gets redirected to the splash screen.

and this happens only if I change the tap before the execution of onDismiss function's content

Check the issue here

0

There are 0 best solutions below