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

SOLVED: How to pass a value from 2 steppers to countdown timer

Forums > Swift

Hi community,

Writing an Exercise app and I am having issues to pass a value from 2 steppers to the timer. The timeRemaining initial value has to be equal to practiceTime / numberOfExercise When the START button os pressed the timer will start.

import SwiftUI

struct CounterView: View {

    @State private var practiceTime: Double = 30
    @State private var numberOfExercise = 5
    @State private var timeRemaining = 30
    @State private var isPracticeRunning = false

    let timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect()

    var body: some View {
        NavigationView {
            VStack {

                Stepper("Number of Exercises:    \(numberOfExercise)", value: $numberOfExercise, in: 1...12, step: 1)
                    .padding(.horizontal)

                Stepper("Practice Time:    \(practiceTime.formatted()) min",value: $practiceTime, in: 5...60, step: 5)
                    .padding()

                Spacer()

                Text("Time: \(timeRemaining.formatted())")
                    .font(.title)
                    .foregroundColor(.secondary)
                Spacer()
                Button(action: {isPracticeRunning.toggle()}) {
                    Text(isPracticeRunning ? "Stop" : "Start")
                        .padding(20)
                        .foregroundColor(.primary)
                        .background(
                        RoundedRectangle(cornerRadius: 10)
                            .stroke(Color.primary, lineWidth: 2))
                }

                Spacer()
            }
            .navigationTitle(Text("Exercise Timer"))
        }
        .onReceive(timer) { time in
            guard self.isPracticeRunning else { return }
            if timeRemaining > 0 {
                timeRemaining -= 1
            }
        }
    }
}

Thanks,

3      

Put this before the onReceive handler:

.onChange(of: numberOfExercise) { _ in
    timeRemaining = Int(practiceTime) / numberOfExercise
}
.onChange(of: practiceTime) { _ in
    timeRemaining = Int(practiceTime) / numberOfExercise
}

And if "The timeRemaining initial value has to be equal to practiceTime / numberOfExercise", shouldn't the initial value of timeRemaining be 6?

3      

Thanks @roosterboy!

It's working now and yes it was supposed to be 6. Actually 360, because I need to change to seconds. Thanks again.

3      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

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.