(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))]))))))