You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
66 lines
1.8 KiB
66 lines
1.8 KiB
import {Program} from "./Programs";
|
|
import {Device} from "./Devices";
|
|
import {ColorName, formatValue, Values, ValuesWithTime} from "./Shared";
|
|
|
|
interface Workout<D> {
|
|
id: string
|
|
createdAt: string
|
|
device: Device | null
|
|
program: Program | null
|
|
status: WorkoutStatus
|
|
message: string
|
|
test: string
|
|
}
|
|
export type CurrentWorkout = Workout<Device>;
|
|
export type PastWorkout = Workout<Device | null>;
|
|
|
|
export type WorkoutState = ValuesWithTime;
|
|
|
|
export enum WorkoutStatus {
|
|
Created = "Created",
|
|
Connected = "Connected",
|
|
Started = "Started",
|
|
Stopped = "Stopped",
|
|
Disconnected = "Disconnected",
|
|
}
|
|
|
|
export function colorOf(workout: CurrentWorkout | PastWorkout): ColorName {
|
|
if (workout.message) {
|
|
return "red";
|
|
}
|
|
|
|
else return colorByStatus[workout.status];
|
|
}
|
|
|
|
const colorByStatus: Record<WorkoutStatus, ColorName> = {
|
|
[WorkoutStatus.Created]: "indigo",
|
|
[WorkoutStatus.Connected]: "blue",
|
|
[WorkoutStatus.Started]: "green",
|
|
[WorkoutStatus.Stopped]: "yellow",
|
|
[WorkoutStatus.Disconnected]: "gray",
|
|
}
|
|
|
|
export function firstKey(state: WorkoutState): (keyof WorkoutState) | null {
|
|
if (state.time !== undefined) return "time";
|
|
if (state.calories !== undefined) return "calories";
|
|
if (state.distance !== undefined) return "distance";
|
|
if (state.level !== undefined) return "level";
|
|
return null;
|
|
}
|
|
|
|
export function stateString(state: Values, type: keyof WorkoutState): string | null {
|
|
if (type === "time" && state.time !== undefined) {
|
|
return formatValue(state.time, type);
|
|
}
|
|
if (type === "calories" && state.calories !== undefined) {
|
|
return formatValue(state.calories, type);
|
|
}
|
|
if (type === "distance" && state.distance !== undefined) {
|
|
return formatValue(state.distance, type);
|
|
}
|
|
if (type === "level" && state.level !== undefined) {
|
|
return formatValue(state.level, type);
|
|
}
|
|
|
|
return null;
|
|
}
|