Files
workoutsplus/WorkoutsPlus/Components/TimerView.swift
T

33 lines
549 B
Swift

//
// TimerView.swift
// WorkoutsPlus
//
// Created by Felix Förtsch on 12.09.24.
//
import SwiftUI
struct TimerView: View {
@Binding var isActive: Bool
@State private var time = 0
let timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect()
var body: some View {
Text("\(time)")
.onReceive(timer) { _ in
if isActive {
self.time += 1
}
}
.onDisappear {
self.timer.upstream.connect().cancel()
}
}
}
#Preview {
TimerView(isActive: .constant(true))
}