Your first run
Let’s time a run without leaving your desk. TorkeKit ships a simulated street car: two seconds still, then past the quarter mile, with a tilted phone, sensor noise and a 1 Hz GPS, like a real one.
-
Create the session and arm it
arm()starts recording. In a standing start, the timer starts by itself when the car launches.import TorkeKitlet session = RunSession()session.arm()import today.torke.kit.session.RunSessionimport today.torke.kit.input.RecordedRunProviderval session = RunSession()session.arm() -
Feed it the simulated run
runreads the source to the end and closes the run. With the simulated run it takes milliseconds.await session.run(with: RecordedRunProvider.demo())session.run(RecordedRunProvider.demo()) // inside a coroutine -
Read the times
if let r = session.snapshot.result {print("60 ft: \(r.sixtyFeet!) s")print("0–100: \(r.zeroTo100!) s")print("1/8: \(r.eighthMile!) s at \(r.eighthMileKmh!) km/h")print("1/4: \(r.quarterMile!) s at \(r.quarterMileKmh!) km/h")}session.snapshot.value.result?.let { r ->println("60 ft: ${r.sixtyFeet} s")println("0–100: ${r.zeroTo100} s")println("1/8: ${r.eighthMile} s at ${r.eighthMileKmh} km/h")println("1/4: ${r.quarterMile} s at ${r.quarterMileKmh} km/h")}
You should see this, the same on iOS and Android:
60 ft: 2.100 s0–100: 5.292 s1/8: 8.486 s at 145.8 km/h1/4: 12.826 s at 184.3 km/hNow with the phone
Section titled “Now with the phone”Swap the source for the sensors and listen to the events. The session does the rest: waits for the car to stand still, arms, detects the launch and finishes by itself.
import SwiftUIimport TorkeKitimport TorkeKitSensors
struct RunView: View { @State private var session = RunSession() @State private var message = "Pull up to the line" private let phone = PhoneSensorProvider()
var body: some View { VStack(spacing: 12) { Text("\(Int(session.snapshot.speedKmh)) km/h") .font(.system(size: 64, weight: .black, design: .monospaced)) Text(String(format: "%.2f s", session.snapshot.elapsed)) Text(message).foregroundStyle(.secondary) Button("Arm") { session.arm() } } .task { await phone.requestPermission() session.start(with: phone) // GPS preview for await event in session.events() { switch event { case .armed: message = "Ready. Launch when you want." case .disarmed: message = "It moved: hold still." case .finished(let r): message = "1/4: \(r.quarterMile ?? 0) s" case .failed(let f): message = "No run: \(f)" default: break } } } .onAppear { UIApplication.shared.isIdleTimerDisabled = true } .onDisappear { session.stop() } }}@Composablefun RunScreen(phone: PhoneSensorProvider) { val scope = rememberCoroutineScope() val session = remember { RunSession() } val snapshot by session.snapshot.collectAsState() var message by remember { mutableStateOf("Pull up to the line") }
LaunchedEffect(Unit) { session.start(phone, scope) // GPS preview session.events.collect { event -> message = when (event) { RunEvent.Armed -> "Ready. Launch when you want." RunEvent.Disarmed -> "It moved: hold still." is RunEvent.Finished -> "1/4: ${event.result.quarterMile} s" is RunEvent.Failed -> "No run: ${event.failure}" else -> message } } } DisposableEffect(Unit) { onDispose { session.stop() } }
Column(horizontalAlignment = Alignment.CenterHorizontally) { Text("${snapshot.speedKmh.toInt()} km/h", fontSize = 64.sp, fontFamily = FontFamily.Monospace) Text("%.2f s".format(snapshot.elapsed)) Text(message) Button(onClick = { session.arm() }) { Text("Arm") } }}