Huzaifa Ameen
Huzaifa Ameen

Reputation: 23

How to navigate another view in onReceive timer closure SwiftUI iOS

I am trying to achieve a navigation to another view when timer hits a specific time. For example I want to navigate to another view after 5 minutes. In swift i can easily achieve this but I am new to SwiftUI and little help will be highly appreciated.

My code:

struct TwitterWebView: View {
    
    @State var timerTime : Float
    @State var minute: Float = 0.0
    @State private var showLinkTarget = false
    let timer = Timer.publish(every: 60.0, on: .main, in: .common).autoconnect()
    
    var body: some View {
        
        WebView(url: "https://twitter.com/")
        .navigationBarTitle("")
        .navigationBarHidden(true)
        
        .onReceive(timer) { _ in
            if self.minute == self.timerTime {
                print("Timer completed navigate to Break view")
                NavigationLink(destination: BreakView()) {
                    Text("Test")
                }
                self.timer.upstream.connect().cancel()
            } else {
                self.minute += 1.0
            }
        }
    }
}

Upvotes: 2

Views: 1689

Answers (2)

komara
komara

Reputation: 382

Navigation between views using timer. Background color while using NavigationView.

struct IntroView: View {
@State var currentTimer = 0
var totalCountDown = 2
let timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect()
@State var shouldNavigate = false

var body: some View {
    
    NavigationView {
        ZStack {
            Color.appBackColor.edgesIgnoringSafeArea(.all)
            
            NavigationLink(destination: LoginView(), isActive: $shouldNavigate, label: {
                Text("")
            })
                .onReceive(timer) { _ in
                    if currentTimer == totalCountDown {
                        self.timer.upstream.connect().cancel()
                        shouldNavigate = true
                    } else {
                        self.currentTimer += 1
                    }
                }
        }
        
    }
}
}

Upvotes: 0

Asperi
Asperi

Reputation: 258247

Here is possible approach (of course assuming that TwitterWebView has NavigationView somewhere in parents)

struct TwitterWebView: View {

    @State var timerTime : Float
    @State var minute: Float = 0.0
    @State private var showLinkTarget = false
    let timer = Timer.publish(every: 60.0, on: .main, in: .common).autoconnect()

    @State private var shouldNavigate = false

    var body: some View {

        WebView(url: "https://twitter.com/")
        .navigationBarTitle("")
        .navigationBarHidden(true)

        .background(
            NavigationLink(destination: BreakView(), 
                              isActive: $shouldNavigate) { EmptyView() }
        )

        .onReceive(timer) { _ in
            if self.minute == self.timerTime {
                print("Timer completed navigate to Break view")
                self.timer.upstream.connect().cancel()

                self.shouldNavigate = true      // << here
            } else {
                self.minute += 1.0
            }
        }
    }
}

Upvotes: 4

Related Questions