aboutsummaryrefslogtreecommitdiff
path: root/src/distractionless/core.cljd
blob: f40e4cd9cf25903529dccc8cb7aad0d19eab4658 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
(ns distractionless.core
  (:require ["package:flutter/material.dart" :as m]
            [cljd.flutter :as f]
            ["package:path_provider/path_provider.dart" :as path-provider]
            [distractionless.config :as dconfig]
            ["dart:io" :as io]
            [clojure.string :as cstr]
            ["package:url_launcher/url_launcher.dart" :as url-launcher]
            ["package:url_launcher/url_launcher_string.dart" :as url-launcher-str]

            [distractionless.ui.constants :as duconstants]
            [distractionless.ui.apps :as duapps]))

(defn ^dynamic main []
  (f/run
   (m/MaterialApp
    .theme (m/ThemeData .primarySwatch m.Colors/pink))
   .home
   (m/Scaffold .backgroundColor duconstants/background-color)
   .body
   :context ctx
   :watch [apps (atom nil) :as *apps
           query (atom "") :as *query
           data-dir (await (path-provider/getApplicationDocumentsDirectory))
           _ (atom 0) :as reloader
           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
             search-bar-controller (m/TextEditingController.)
             _ (m/AppLifecycleListener .onShow #(do (dconfig/init-config! config-file nil)
                                                    (duapps/load-installed-apps! *apps)
                                                    (reset! *query "")
                                                    (.clear search-bar-controller)))]
   :let [favourites (get (dconfig/read-from-file config-file) "favourites")]
   (m/Container
    .padding (m/EdgeInsets.only
              .top 50)
    .child (if (nil? apps)
             (m/Text "Lade Apps..."
                     .style (m/TextStyle
                             .color duconstants/text-color
                             .fontStyle m/FontStyle.italic))
             (m/Column
              .children (filter some?
                                [(m/SearchBar
                                  .onChanged (fn [new-val]
                                               (reset! *query new-val)
                                               nil)
                                  .hintText "Suche"
                                  .controller search-bar-controller
                                  .backgroundColor (#/(.resolveWith m/Color?) m/MaterialStateProperty (fn [^Set _states] duconstants/background-color))
                                  .trailing (when-not (cstr/blank? query)
                                              [(m/IconButton .icon (m/Icon m/Icons.clear_rounded)
                                                             .color duconstants/text-color
                                                             .onPressed #(do (reset! *query "")
                                                                             (.clear search-bar-controller))
                                                             .iconSize 30.0)]))
                                 (m/CheckboxListTile
                                  .title (m/Text "Alle Apps anzeigen"
                                                 .style duconstants/text-style)
                                  .value show-all-apps?
                                  .onChanged #(reset! *show-all-apps? %)
                                  .activeColor duconstants/checkbox-active-color)
                                 (m/Expanded
                                  .child (duapps/apps-list (filter (cond
                                                                     (not (cstr/blank? query)) #(cstr/includes?
                                                                                                 (cstr/lower-case (.-name %))
                                                                                                 (cstr/lower-case query))
                                                                     show-all-apps? (fn [_] true)
                                                                     :else #(.contains favourites (.-packageName %))) apps)
                                                           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)))
                                 (when (some? app-in-queue)
                                   (m/Text (str "Starte gleich " (.-name app-in-queue) "...")
                                           .style duconstants/text-style))]))))))