aboutsummaryrefslogtreecommitdiff
path: root/src/distractionless/ui/apps/settings.cljd
blob: bf786b87a55b63444b4f9b0edcbead1c72f29206 (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
(ns distractionless.ui.apps.settings
  (:require ["package:flutter/material.dart" :as m]
            ["package:installed_apps/installed_apps.dart" :as apps]
            [cljd.flutter :as f]
            [distractionless.ui.constants :as duconstants]
            [distractionless.config :as dconfig]))

(defn- header [app ctx]
  (m/Row
   .mainAxisAlignment m/MainAxisAlignment.spaceBetween
   .children [(m/Text (.-name app)
                      .style duconstants/title-style)
              (m/IconButton .icon (m/Icon m/Icons.clear_rounded)
                            .color duconstants/text-color
                            .onPressed #(m/Navigator.pop ctx)
                            .iconSize 30.0)]))

(defn- favourite-setting [app config-file reloader]
  (m/CheckboxListTile
   .title (m/Text "Favorit"
                  .style duconstants/text-style)
   .value (contains? (get (dconfig/read-from-file config-file) "favourites")
                     (.-packageName app))
   .onChanged (fn [new-val]
                (if new-val
                  (dconfig/update-config! config-file
                                          #(update % "favourites" conj (.-packageName app))
                                          reloader)
                  (dconfig/update-config! config-file
                                          (fn [config]
                                            (update config "favourites" disj (.-packageName app)))
                                          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): "
                      .style duconstants/text-style)
              (m/Expanded
               .child (m/TextFormField
                       .keyboardType m/TextInputType.number
                       .initialValue (str (countdown-setting-value app config-file))
                       .onChanged (fn [new-val]
                                    (dconfig/update-config! config-file
                                                            #(update % "countdowns"
                                                                     conj [(.-packageName app) (or (int/tryParse new-val) 0)])
                                                            reloader))))]))

(defn- open-in-system-settings [app]
  (m/ListTile
   .title (m/Text "In Systemeinstellungen öffnen"
                  .style duconstants/text-style)
   .onTap #(do (apps/InstalledApps.openSettings (.-packageName app)) nil)))

(defn open [app config-file ctx reloader]
  (m/showDialog
   .context ctx
   .builder (f/build :watch [_ reloader]
                     (m/Dialog.fullscreen
                      .backgroundColor duconstants/background-color
                      .child (m/Column
                              .children (list (header app ctx)
                                              (favourite-setting app config-file reloader)
                                              (countdown-setting app config-file reloader)
                                              (open-in-system-settings app)))))))