From ccf06386c6b153fe3c420e9200f481dc085e064a Mon Sep 17 00:00:00 2001
From: ivarlovlie <git@ivarlovlie.no>
Date: Thu, 2 Jun 2022 23:02:47 +0200
Subject: refactor: Rename accounts to portal

---
 apps/web-shared/src/components/stopwatch.svelte | 30 +++++++++++++++++++++----
 1 file changed, 26 insertions(+), 4 deletions(-)

(limited to 'apps/web-shared/src/components/stopwatch.svelte')

diff --git a/apps/web-shared/src/components/stopwatch.svelte b/apps/web-shared/src/components/stopwatch.svelte
index 8287e31..3ed0d0e 100644
--- a/apps/web-shared/src/components/stopwatch.svelte
+++ b/apps/web-shared/src/components/stopwatch.svelte
@@ -1,8 +1,8 @@
 <script lang="ts">
-    import {writable_persistent} from "$shared/lib/persistent-store";
     import Button from "$shared/components/button.svelte";
     import {Textarea} from "$shared/components/form";
     import {StorageKeys} from "$shared/lib/configuration";
+    import {StoreType, writable_persistent} from "$shared/lib/persistent-store";
     import {Temporal} from "@js-temporal/polyfill";
     import {createEventDispatcher, onMount} from "svelte";
 
@@ -11,11 +11,15 @@
             hours: 0,
             minutes: 0,
             seconds: 0,
-            startTime: null as Temporal.PlainTime,
+            startTime: null as Temporal.PlainDateTime,
+            lastStep: null as Temporal.PlainDateTime,
             isRunning: false,
             intervalId: 0,
             note: "",
         },
+        options: {
+            store: StoreType.LOCAL
+        },
         name: StorageKeys.stopwatch,
     });
 
@@ -30,10 +34,27 @@
     }
 
     onMount(() => {
+        start_if_running();
+    });
+
+    function start_if_running() {
         if ($state.isRunning) {
+            if (Temporal.PlainDateTime.compare($state.lastStep, Temporal.Now.plainDateTimeISO()) == -1) {
+                const duration = Temporal.Now.plainDateTimeISO().since($state.lastStep, {smallestUnit: "second"});
+                console.log("lastStep",$state.lastStep.toString());
+                console.log("duration", duration.toString());
+                console.log(duration.seconds);
+                // for (let i = 0; i < steps; i++) {
+                //     step();
+                // }
+            }
             clearInterval($state.intervalId);
             $state.intervalId = setInterval(step, 1000);
         }
+    }
+
+    window.addEventListener("focus", () => {
+        start_if_running();
     });
 
     const dispatch = createEventDispatcher();
@@ -52,7 +73,8 @@
             $state.seconds = 0;
         }
 
-        if (!$state.startTime) $state.startTime = Temporal.Now.plainTimeISO();
+        if (!$state.startTime) $state.startTime = Temporal.Now.plainDateTimeISO();
+        $state.lastStep = Temporal.Now.plainDateTimeISO();
     }
 
     function reset() {
@@ -108,7 +130,7 @@
 
     function on_create_entry() {
         if (!$state.startTime) return;
-        const plainStartTime = Temporal.PlainTime.from($state.startTime);
+        const plainStartTime = Temporal.PlainDateTime.from($state.startTime);
         dispatch("create", {
             from: plainStartTime,
             to: plainStartTime.add({hours: $state.hours, minutes: $state.minutes, seconds: $state.seconds}),
-- 
cgit v1.3

