From adfdc5b31a96b2ebe8fde64ea68b1c2377c3bda0 Mon Sep 17 00:00:00 2001 From: "s0wlz (Matthias Puchstein)" Date: Fri, 27 Feb 2026 22:15:21 +0100 Subject: [PATCH] [refactor] updated the API TODO: implement the countdown.store.ts Signed-off-by: s0wlz (Matthias Puchstein) --- src-tauri/src/lib.rs | 2 +- .../countdown/api/countdown.client.ts | 69 ++++++++++--------- .../components/CountdownManager.svelte | 0 .../countdown/model/countdown.mapper.ts | 14 ++++ .../countdown/model/countdown.types.ts | 27 +++++++- .../countdown/state/countdown.store.ts | 49 +------------ src/shared/payloads/empty.ts | 1 + src/shared/time/duration.ts | 4 ++ 8 files changed, 85 insertions(+), 81 deletions(-) delete mode 100644 src/features/countdown/components/CountdownManager.svelte create mode 100644 src/features/countdown/model/countdown.mapper.ts create mode 100644 src/shared/payloads/empty.ts diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index e927521..cab9a9a 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -21,8 +21,8 @@ pub fn run() { countdown_delete, countdown_start, countdown_reset, - countdown_pause, countdown_resume, + countdown_pause, countdown_snapshot, ]) .run(tauri::generate_context!()) diff --git a/src/features/countdown/api/countdown.client.ts b/src/features/countdown/api/countdown.client.ts index 93948e9..ac93352 100644 --- a/src/features/countdown/api/countdown.client.ts +++ b/src/features/countdown/api/countdown.client.ts @@ -1,45 +1,52 @@ import {invokeCommand} from "../../../shared/tauri/invoke"; -import type {CountdownCommand, CountdownSnapshot, CountdownState} from "../model/countdown.types"; -import {type Duration, millisToDuration} from "../../../shared/time/duration"; +import type { + CountdownCommand, + CountdownPayload, + CountdownSnapshot, + CountdownSnapshotDto +} from "../model/countdown.types"; +import {mapSnapshotDtoToSnapshot} from "../model/countdown.mapper"; +import {type Duration, durationToMillis} from "../../../shared/time/duration"; -type CountdownSnapshotDto = { - id: number; - label: string; - duration: number; - state: CountdownState; - start_epoch_ms: number | null; - target_epoch_ms: number | null; -}; -export async function fetchCountdownSnapshot(): Promise { - let temp = await invokeCommand("countdown_snapshot"); - const duration: Duration = millisToDuration(temp.duration); - return { - id: temp.id, - label: temp.label, - duration: duration, - state: temp.state, - start_epoch: temp.start_epoch_ms !== null ? new Date(temp.start_epoch_ms) : null, - target_epoch: temp.target_epoch_ms !== null ? new Date(temp.target_epoch_ms) : null, - }; +async function invokeCountdownCommand(command: CountdownCommand, payload: CountdownPayload): Promise { + await invokeCommand(command, payload); } -async function invokeCountdownCommand(command: CountdownCommand): Promise { - await invokeCommand(command); +export async function createCountdown(label: string, duration: Duration): Promise { + return await invokeCommand("countdown_create", {label, duration: durationToMillis(duration)}); } -export async function startCountdown(): Promise { - await invokeCountdownCommand("countdown_start"); +export async function listCountdowns(): Promise { + let snapshotsDto = await invokeCommand("countdown_list", {}); + let snapshots: CountdownSnapshot[] = []; + let snapshot: CountdownSnapshotDto; + for (snapshot of snapshotsDto) { + snapshots.push(mapSnapshotDtoToSnapshot(snapshot)); + } + return snapshots; } -export async function pauseCountdown(): Promise { - await invokeCountdownCommand("countdown_pause"); +export async function deleteCountdown(id: number): Promise { + await invokeCountdownCommand("countdown_delete", {id}); } -export async function resumeCountdown(): Promise { - await invokeCountdownCommand("countdown_resume"); +export async function startCountdown(id: number): Promise { + await invokeCountdownCommand("countdown_start", {id}); } -export async function resetCountdown(): Promise { - await invokeCountdownCommand("countdown_reset"); +export async function resumeCountdown(id: number): Promise { + await invokeCountdownCommand("countdown_resume", {id}); +} + +export async function pauseCountdown(id: number): Promise { + await invokeCountdownCommand("countdown_pause", {id}); +} + +export async function resetCountdown(id: number): Promise { + await invokeCountdownCommand("countdown_reset", {id}); +} + +export async function fetchCountdownSnapshot(id: number): Promise { + return mapSnapshotDtoToSnapshot(await invokeCommand("countdown_snapshot", {id})); } diff --git a/src/features/countdown/components/CountdownManager.svelte b/src/features/countdown/components/CountdownManager.svelte deleted file mode 100644 index e69de29..0000000 diff --git a/src/features/countdown/model/countdown.mapper.ts b/src/features/countdown/model/countdown.mapper.ts new file mode 100644 index 0000000..51b5c8d --- /dev/null +++ b/src/features/countdown/model/countdown.mapper.ts @@ -0,0 +1,14 @@ +import type {CountdownSnapshot, CountdownSnapshotDto} from "./countdown.types"; +import {type Duration, millisToDuration} from "../../../shared/time/duration"; + +export function mapSnapshotDtoToSnapshot(snapshotDto: CountdownSnapshotDto): CountdownSnapshot { + const duration: Duration = millisToDuration(snapshotDto.duration); + return { + id: snapshotDto.id, + label: snapshotDto.label, + duration: duration, + state: snapshotDto.state, + start_epoch: snapshotDto.start_epoch_ms !== null ? new Date(snapshotDto.start_epoch_ms) : null, + target_epoch: snapshotDto.target_epoch_ms !== null ? new Date(snapshotDto.target_epoch_ms) : null, + }; +} \ No newline at end of file diff --git a/src/features/countdown/model/countdown.types.ts b/src/features/countdown/model/countdown.types.ts index f64b343..2b049a4 100644 --- a/src/features/countdown/model/countdown.types.ts +++ b/src/features/countdown/model/countdown.types.ts @@ -1,7 +1,17 @@ import type {Duration} from "../../../shared/time/duration"; +import type {EmptyPayload} from "../../../shared/payloads/empty"; export type CountdownState = "Idle" | "Running" | "Paused" | "Finished"; +export type CountdownSnapshotDto = { + id: number; + label: string; + duration: number; + state: CountdownState; + start_epoch_ms: number | null; + target_epoch_ms: number | null; +}; + export type CountdownSnapshot = { id: number; label: string; @@ -12,7 +22,22 @@ export type CountdownSnapshot = { } export type CountdownCommand = + | "countdown_create" + | "countdown_delete" + | "countdown_list" | "countdown_start" | "countdown_pause" | "countdown_resume" - | "countdown_reset"; + | "countdown_reset" + | "countdown_snapshot"; + +export type CountdownPayload = EmptyPayload | CountdownIdPayload | CountdownCreatePayload; + +export type CountdownIdPayload = { + id: number; +}; + +export type CountdownCreatePayload = { + label: string; + duration: number; +} \ No newline at end of file diff --git a/src/features/countdown/state/countdown.store.ts b/src/features/countdown/state/countdown.store.ts index d194d48..7e987ec 100644 --- a/src/features/countdown/state/countdown.store.ts +++ b/src/features/countdown/state/countdown.store.ts @@ -1,48 +1 @@ -import { - fetchCountdownSnapshot, - pauseCountdown, - resetCountdown, - resumeCountdown, - startCountdown, -} from "../api/countdown.client"; -import {createCountdownView} from "../view"; - -export async function initCountdownController(container: HTMLElement): Promise { - const view = createCountdownView(container); - - const refreshSnapshot = async (): Promise => { - try { - const snapshot = await fetchCountdownSnapshot(); - view.setSnapshot(snapshot); - } catch (error) { - view.setError(`snapshot error: ${String(error)}`); - } - }; - - const runAction = async (action: () => Promise): Promise => { - try { - await action(); - await refreshSnapshot(); - } catch (error) { - view.setError(`command error: ${String(error)}`); - } - }; - - view.onStart(() => { - void runAction(startCountdown); - }); - view.onPause(() => { - void runAction(pauseCountdown); - }); - view.onResume(() => { - void runAction(resumeCountdown); - }); - view.onReset(() => { - void runAction(resetCountdown); - }); - view.onRefresh(() => { - void refreshSnapshot(); - }); - - await refreshSnapshot(); -} +//TODO: Implement countdown store \ No newline at end of file diff --git a/src/shared/payloads/empty.ts b/src/shared/payloads/empty.ts new file mode 100644 index 0000000..eaa0c74 --- /dev/null +++ b/src/shared/payloads/empty.ts @@ -0,0 +1 @@ +export type EmptyPayload = {} \ No newline at end of file diff --git a/src/shared/time/duration.ts b/src/shared/time/duration.ts index 466eb35..e3ca7ef 100644 --- a/src/shared/time/duration.ts +++ b/src/shared/time/duration.ts @@ -17,3 +17,7 @@ export function millisToDuration(millis: number): Duration { millis: millis % 1000, } } + +export function durationToMillis(duration: Duration): number { + return duration.hours * 3600000 + duration.minutes * 60000 + duration.seconds * 1000 + duration.millis; +}