Skip to content

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.

  1. Create the session and arm it

    arm() starts recording. In a standing start, the timer starts by itself when the car launches.

    import TorkeKit
    let session = RunSession()
    session.arm()
  2. Feed it the simulated run

    run reads the source to the end and closes the run. With the simulated run it takes milliseconds.

    await session.run(with: RecordedRunProvider.demo())
  3. 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")
    }

You should see this, the same on iOS and Android:

60 ft: 2.100 s
0–100: 5.292 s
1/8: 8.486 s at 145.8 km/h
1/4: 12.826 s at 184.3 km/h

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.

RunView.swift
import SwiftUI
import TorkeKit
import 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() }
}
}