UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Refresh view when sheet dismisses

Forums > SwiftUI

I want to know how to refresh a view when a sheet is dismissed, either by swiping down the sheet or clicking the close button.

I have a custom TabBarView that has a MicButtonView (this shows a circle button that when clicked shows a sheet)

Heres all my code for the TabBarView code. I also included the code for the ViewRouter. I'm trying to give a complete picture of what my code looks like so someone could help me with this?

struct TabBarView: View {
    @StateObject var viewRouter: ViewRouter
    @EnvironmentObject var dataController: DataController

    @State var showPopUpMenu = false
    @State private var showWhatsNewView = false

    var body: some View {
        GeometryReader { geo in
            VStack {
                Spacer()

                // This is how we navigate
                switch viewRouter.currentPage {
                case .home:
                    ListOfRecordingsView(dataController: dataController)
                case .settings:
                    SettingsView(dataController: dataController)
                }

                Spacer()

                /// The TabBar
                HStack {
                    Spacer()

                    TabBarIcon(viewRouter: viewRouter,
                               assignedPage: .home,
                               proxy: geo,
                               systemIconName: "folder",
                               tabName: "Recordings"
                    )

                    Spacer()

                    MicButtonView(showPopUpMenu: $showPopUpMenu, proxy: geo)

                    Spacer()

                    TabBarIcon(viewRouter: viewRouter,
                               assignedPage: .settings,
                               proxy: geo,
                               systemIconName: "gearshape",
                               tabName: " Settings "
                    )

                    Spacer()
                }
                .frame(width: geo.size.width, height: geo.size.height/Constants.flt8)
                .background(Color.TabBarBackground.opacity(0.7))

            } // End of VStack
            /// This disables the sidebar
            .navigationViewStyle(StackNavigationViewStyle())
            .edgesIgnoringSafeArea(.bottom)
        } // End of Geo Reader
        .onAppear {
            showWhatsNewView = dataController.checkForUpdate()
        }
        .sheet(isPresented: $showWhatsNewView) {
            WelcomeScreenView(dataController: dataController)
        }
    }
}

/// This struct represents a single TabBar item
struct TabBarIcon: View {
    @EnvironmentObject var dataController: DataController
    @StateObject var viewRouter: ViewRouter
    let assignedPage: Page

    let proxy: GeometryProxy
    let systemIconName, tabName: String
    private let device = Device.current

    var body: some View {
        Spacer()

        if device.isPad {
            HStack {
                Image(systemName: systemIconName)
                    .resizable()
                    .aspectRatio(contentMode: .fit)
                    .frame(width: proxy.size.width/Constants.flt14, height: proxy.size.height/Constants.flt28)

                Text(tabName)
                    .genericTextModifier(
                        for: .callout,
                        color: viewRouter.currentPage == assignedPage ?
                        Color(dataController.themeAccentColor) : .DarkGray
                    )
            }
            .padding(.horizontal, -Constants.flt4)
            .onTapGesture {
                viewRouter.currentPage = assignedPage
            }
            .foregroundColor(viewRouter.currentPage == assignedPage ?
                Color(dataController.themeAccentColor) : .DarkGray
            )
            .offset(y: -proxy.size.height/Constants.flt16/Constants.flt2)
        } else {
            VStack {
                Image(systemName: systemIconName)
                    .resizable()
                    .aspectRatio(contentMode: .fit)
                    .frame(width: proxy.size.width/Constants.flt5, height: proxy.size.height/Constants.flt28)
                    .padding(.top, Constants.flt10)

                Text(tabName)
                    .genericTextModifier(
                        for: .footnote,
                        color: viewRouter.currentPage == assignedPage ?
                            Color(dataController.themeAccentColor) : .DarkGray
                    )
                Spacer()
            }
            .padding(.horizontal, -Constants.flt4)
            .onTapGesture {
                viewRouter.currentPage = assignedPage
            }
            .foregroundColor(viewRouter.currentPage == assignedPage ?
                Color(dataController.themeAccentColor) : .DarkGray
            )
        }

        Spacer()
    }
}

struct MicButtonView: View {
    @EnvironmentObject var dataController: DataController

    @Binding var showPopUpMenu: Bool
    let proxy: GeometryProxy

    private let device = Device.current

    var body: some View {
        Button(action: { showPopUpMenu.toggle() }, label: {
            ZStack {
                Circle()
                    .foregroundColor(Color(dataController.themeAccentColor))
                    .frame(
                        width:
                            device.isPhone || device.isPod ?
                                proxy.size.width/Constants.flt5 :
                            /// iPad config settings based on which orientation device is in
                            device.orientation == .portrait ?
                                proxy.size.width/Constants.flt6 :
                                proxy.size.width/Constants.flt10,
                        height:
                            device.isPhone || device.isPod ?
                                proxy.size.width/Constants.flt5 :
                            /// iPad config settings based on which orientation device is in
                            device.orientation == .portrait ?
                                proxy.size.width/Constants.flt6 :
                                proxy.size.width/Constants.flt10                )
                    .doubleShadow(radius: Constants.flt5)

                Image(systemName: "mic.fill")
                    .resizable()
                    .aspectRatio(contentMode: .fit)
                    .frame(
                        width:
                            device.isPhone || device.isPod ?
                                proxy.size.width/Constants.flt7-Constants.flt6 :
                            /// iPad config settings based on which orientation device is in
                            device.orientation == .portrait ?
                                proxy.size.width/Constants.flt8-Constants.flt9 :
                                proxy.size.width/Constants.flt12-Constants.flt13,
                        height:
                            device.isPhone || device.isPod ?
                                proxy.size.width/Constants.flt7-Constants.flt6 :
                            /// iPad config settings based on which orientation device is in
                            device.orientation == .portrait ?
                                proxy.size.width/Constants.flt8-Constants.flt9 :
                                proxy.size.width/Constants.flt12-Constants.flt13
                    )
                    .foregroundColor(.white)
//                    .rotationEffect(Angle(degrees: showPopUpMenu ? 45 : 0))
            }
            .sheet(isPresented: $showPopUpMenu) {
                MakeANewRecordingView(dataController: dataController, numberOfSamples: 10)
            }
        })
        .offset(
            x: device.isPad ? -Constants.flt27 : -Constants.flt0,
            y: -proxy.size.height/Constants.flt16/Constants.flt2)
        .buttonStyle(BounceButtonStyle())
    }
}

Here is the ViewRouter:

class ViewRouter: ObservableObject {
    @Published var currentPage: Page = .home
}

enum Page {
    case home
    case settings
}

3      

Hi @Appledad05

You can simply use the onDismiss parameter of your sheet

.sheet(isPresented: $showing, onDismiss: {
    // code to execute when sheet dismiss
  }, content: {
    YourView()
  })

3      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.