From ac6e691ed65dbc269468ef92c982414b3f5dda61 Mon Sep 17 00:00:00 2001
From: Tim <contact@bytim.eu>
Date: Mon, 24 Feb 2025 18:10:02 +0100
Subject: Add logic for countdown option

---
 src/distractionless/core.cljd             | 10 +++++++---
 src/distractionless/ui/apps.cljd          | 21 ++++++++++++++++-----
 src/distractionless/ui/apps/settings.cljd |  7 +++++--
 3 files changed, 28 insertions(+), 10 deletions(-)

(limited to 'src')

diff --git a/src/distractionless/core.cljd b/src/distractionless/core.cljd
index f8c0929..f40e4cd 100644
--- a/src/distractionless/core.cljd
+++ b/src/distractionless/core.cljd
@@ -23,7 +23,8 @@
            query (atom "") :as *query
            data-dir (await (path-provider/getApplicationDocumentsDirectory))
            _ (atom 0) :as reloader
-           show-all-apps? (atom false) :as *show-all-apps?]
+           show-all-apps? (atom false) :as *show-all-apps?
+           app-in-queue (atom nil) :as *app-in-queue]
    :managed [_ (duapps/load-installed-apps! *apps) ; loads apps on app startup 
              config-file (io/File. (str (.-path data-dir) "/config.edn"))
              _ (dconfig/init-config! config-file nil) ; Init config on app startup
@@ -69,11 +70,14 @@
                                                                                                  (cstr/lower-case query))
                                                                      show-all-apps? (fn [_] true)
                                                                      :else #(.contains favourites (.-packageName %))) apps)
-                                                           config-file ctx reloader))
+                                                           config-file ctx reloader *app-in-queue))
                                  (when (not (cstr/blank? query))
                                    (m/ListTile
                                     .title (m/Text (str "Nach \"" query "\" im Internet suchen")
                                                    .style duconstants/text-style)
                                     .onTap #(do (await (url-launcher-str/launchUrlString (str "https://google.com/search?q=" (cstr/replace query " " "+"))
                                                                                          .mode url-launcher/LaunchMode.externalApplication))
-                                                nil)))]))))))
+                                                nil)))
+                                 (when (some? app-in-queue)
+                                   (m/Text (str "Starte gleich " (.-name app-in-queue) "...")
+                                           .style duconstants/text-style))]))))))
diff --git a/src/distractionless/ui/apps.cljd b/src/distractionless/ui/apps.cljd
index b7cf61f..0341b6b 100644
--- a/src/distractionless/ui/apps.cljd
+++ b/src/distractionless/ui/apps.cljd
@@ -2,20 +2,31 @@
   (:require ["package:flutter/material.dart" :as m]
             ["package:installed_apps/installed_apps.dart" :as apps]
             [distractionless.ui.constants :as duconstants]
-            [distractionless.ui.apps.settings :as duasettings]))
+            [distractionless.ui.apps.settings :as duasettings]
+            ["dart:async" :as async]))
+
+(defn start-app [app config-file *app-in-queue]
+  (async/Future.delayed (Duration.
+                         .seconds (duasettings/countdown-setting-value app config-file))
+                        (fn []
+                          (reset! *app-in-queue nil)
+                          (await (apps/InstalledApps.startApp (.-packageName app))))))
 
 (defn load-installed-apps! [a]
   (reset! a (await (apps/InstalledApps.getInstalledApps false))))
 
-(defn- render-app [app config-file ctx reloader]
+(defn- render-app [app config-file ctx reloader *app-in-queue]
   (m/ListTile
    .title (m/Text (.-name app)
                   .style duconstants/text-style)
-   .onTap #(do (await (apps/InstalledApps.startApp (.-packageName app))) nil)
+   .onTap (fn []
+            (reset! *app-in-queue app)
+            (start-app app config-file *app-in-queue)
+            nil)
    .onLongPress #(duasettings/open app config-file ctx reloader)))
 
-(defn apps-list [apps config-file ctx reloader]
+(defn apps-list [apps config-file ctx reloader *app-in-queue]
   (m/ListView
    .padding m/EdgeInsets.zero
    .children (for [app apps]
-               (render-app app config-file ctx reloader))))
+               (render-app app config-file ctx reloader *app-in-queue))))
diff --git a/src/distractionless/ui/apps/settings.cljd b/src/distractionless/ui/apps/settings.cljd
index 8fa57ce..a2c1d4b 100644
--- a/src/distractionless/ui/apps/settings.cljd
+++ b/src/distractionless/ui/apps/settings.cljd
@@ -31,6 +31,10 @@
                                           reloader)))
    .activeColor duconstants/checkbox-active-color))
 
+(defn countdown-setting-value [app config-file]
+  (get-in (dconfig/read-from-file config-file)
+          ["countdowns" (.-packageName app)] 0))
+
 (defn- countdown-setting [app config-file reloader]
   (m/Row
    .children [(m/Text "Countdown, bevor App öffnet (in Sekunden): "
@@ -38,8 +42,7 @@
               (m/Expanded
                .child (m/TextFormField
                        .keyboardType m/TextInputType.number
-                       .initialValue (str (get-in (dconfig/read-from-file config-file)
-                                                  ["countdowns" (.-packageName app)] 0))
+                       .initialValue (str (countdown-setting-value app config-file))
                        .onChanged (fn [new-val]
                                     (dconfig/update-config! config-file
                                                             #(update % "countdowns"
-- 
cgit v1.2.3