bjomarks-android: the Android client of bjomarks

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Linus Björnstam 2026-11-21 21:06:11 +01:00
commit bf114513a5
11 changed files with 2526 additions and 0 deletions

1166
src/app.bjo Normal file

File diff suppressed because it is too large Load diff

30
src/main.bjo Normal file
View file

@ -0,0 +1,30 @@
;; This Source Code Form is subject to the terms of the Mozilla Public
;; License, v. 2.0. If a copy of the MPL was not distributed with this
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
;; (bjomarks-android) — bookmarks on an Android phone.
;;
;; bjo run --android
;;
;; - The shared folder is chosen with the system's folder picker, and opened
;; again at each start: see (bjomarks-android settings).
;; - This device's own folder is "bjomarks" in the app's folder for data.
;; - See (bjomarks-android app) for the screens.
;;
;; It runs on the desktop too, in a window, for trying it out: the picker is
;; then the desktop's.
(import (std prelude))
(import (std os))
(import (bjogui app))
(import (bjomarks-android app))
(import (bjomarks-android settings))
(: (-> (Vec string) int))
(defun (main args)
(def settings (settings-folder))
(def local (path-combine (data-directory) "bjomarks"))
(run-app (initial-model local settings (read-folder-mark settings) #:vaults (read-vaults settings)) gui-update view
#:windows windows #:subscriptions subscriptions #:on-back back-message
#:title "Bjomarks" #:width 420.0 #:height 760.0)
0)

120
src/settings.bjo Normal file
View file

@ -0,0 +1,120 @@
;; This Source Code Form is subject to the terms of the Mozilla Public
;; License, v. 2.0. If a copy of the MPL was not distributed with this
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
;; bjomarks-android/src/settings.bjo — the module (bjomarks-android
;; settings): what the app keeps between runs, in settings.json in a folder
;; of its own.
;;
;; {"folder": "<bookmark of the shared folder>",
;; "vaults": [{"name": "Bookmarks", "folder": "<bookmark>"}, ...]}
;;
;; - The folder is "bjomarks" in the app's folder for configuration.
;; - "folder" is the bookmark of the shared folder used last, from bjogui's
;; `folder-bookmark`. It opens the folder again, and on Android it keeps
;; the app's access to it.
;; - "vaults" is the shared folders that the user added, each with the name
;; of its folder, in the order they were added. Settings of an earlier
;; version, with "folder" only, have that one.
;; - The fields that this version does not know are kept when it writes.
;; - The file is written whole and then renamed over the old one, so that it
;; is never half written.
(export settings-folder read-folder-mark save-folder-mark!
VaultEntry vault-entry-name vault-entry-mark read-vaults save-vaults!)
(import (std prelude))
(import (std os))
(import (text json))
(: string)
(def file-name "settings.json")
;; The folder of the settings: "bjomarks" in the app's folder for
;; configuration.
(: (-> string))
(defun (settings-folder) (path-combine (config-directory) "bjomarks"))
;; The fields of the settings in `folder`, in their order. None when there
;; is no settings file, or it is not a JSON object.
(: (-> string (Option (IArray (Tuple string Json)))))
(defun (read-fields folder)
(def file (path-combine folder file-name))
(if (not (file-exists? file))
None
(match (parse-json (file-read-text file) #:objects 'fields)
((Ok (JsonFields fields)) (Some fields))
(_ None))))
;; The bookmark of the shared folder saved in the settings in `folder`. None
;; when none is saved, or the file cannot be read as settings.
(: (-> string (Option string)))
(defun (read-folder-mark folder)
(match (read-fields folder)
((Some fields)
(match (json-object-ref (JsonFields fields) "folder")
((Some (JsonStr s)) (if (string-empty? (string-trim s)) None (Some s)))
(_ None)))
(None None)))
;; Saves `mark` as the bookmark of the shared folder in the settings in
;; `folder`, and keeps their other fields. Makes `folder` when it is not
;; there. Raises what the file system raises.
(: (-> string string void))
(defun (save-folder-mark! folder mark)
(save-fields! folder [(Tuple "folder" (JsonStr mark))]))
;; A shared folder that the user added: the name to show, and its bookmark.
(type (: VaultEntry (Record (: name string) (: mark string))))
(: (-> VaultEntry string))
(defun (vault-entry-name e) (.name e))
(: (-> VaultEntry string))
(defun (vault-entry-mark e) (.mark e))
;; The shared folders saved in the settings in `folder`, in their order.
;; Settings with a "folder" and no "vaults" have that one, with no name yet.
(: (-> string (Vec VaultEntry)))
(defun (read-vaults folder)
(match (read-fields folder)
((Some fields)
(match (json-object-ref (JsonFields fields) "vaults")
((Some (JsonArr items))
(vec-fold (fun (j acc)
(match (Tuple (json-object-ref j "name") (json-object-ref j "folder"))
((Tuple (Some (JsonStr name)) (Some (JsonStr mark)))
(if (string-empty? mark) acc (vec-add acc (VaultEntry (name name) (mark mark)))))
(_ acc)))
[] (iarray->vec items)))
(_ (match (read-folder-mark folder)
((Some mark) [(VaultEntry (name "") (mark mark))])
(None [])))))
(None [])))
;; Saves `vaults` in the settings in `folder`, and `last` as the one used
;; last, or none, and keeps their other fields.
(: (-> string (Vec VaultEntry) (Option string) void))
(defun (save-vaults! folder vaults last)
(def items (vec-map (fun (e) (JsonFields (vec->iarray [(Tuple "name" (JsonStr (.name e)))
(Tuple "folder" (JsonStr (.mark e)))])))
vaults))
(def list-field (Tuple "vaults" (JsonArr (vec->iarray items))))
;; "" is no folder: `read-folder-mark` reads it as none.
(save-fields! folder [(Tuple "folder" (JsonStr (option-ref-or last ""))) list-field]))
;; Saves `fields` in the settings in `folder`, in place of those of their
;; names, and keeps the others. Makes `folder` when it is not there. Raises
;; what the file system raises.
(: (-> string (Vec (Tuple string Json)) void))
(defun (save-fields! folder fields)
(def names (vec-map fst fields))
(def others (match (read-fields folder)
((Some old) (vec-filter (fun (f) (not (vec-contains? names (fst f)))) (iarray->vec old)))
(None [])))
(def text (json->string (JsonFields (vec->iarray (vec-append others fields)))))
(directory-create folder)
(def file (path-combine folder file-name))
(def partial (str file ".new"))
(file-write-text partial text)
(file-move partial file #:mode MoveReplace))