From 3bc1263294c12e5ffdefeafb085131a5e4e1294b Mon Sep 17 00:00:00 2001 From: ml Date: Sat, 18 Jul 2026 22:41:51 +0200 Subject: [PATCH] Add a standalone Android app with the same daily reading and config page android/ links engine/src/ and interpreter/src/ via JNI/CMake (real astronomy.c, not the watch's size-constrained substitute), rendering through Jetpack Compose with a burger-menu config page mirroring WatchConfig and a WorkManager-driven daily notification. Independent of the watch app - neither requires the other. build-image now bundles the Android SDK/NDK alongside the Pebble SDK (VERSION bumped to 2), and .gitea/workflows/build.yml/Makefile/ run-image.sh build and publish the APK the same way as the CLI tarball and .pbw. --- .gitea/workflows/build.yml | 29 +- .gitignore | 17 ++ CLAUDE.md | 92 ++++++- Makefile | 38 ++- README.md | 24 +- android/README.md | 43 +++ android/app/build.gradle.kts | 92 +++++++ android/app/proguard-rules.pro | 9 + android/app/src/main/AndroidManifest.xml | 26 ++ .../deckinadash/DeckInADashApplication.kt | 43 +++ .../de/ladkau/deckinadash/MainActivity.kt | 20 ++ .../deckinadash/data/ConfigRepository.kt | 75 +++++ .../de/ladkau/deckinadash/data/DeckConfig.kt | 32 +++ .../de/ladkau/deckinadash/data/ReadingDto.kt | 51 ++++ .../deckinadash/data/ReadingRepository.kt | 44 +++ .../deckinadash/nativebridge/DeckNative.kt | 37 +++ .../notify/NotificationScheduler.kt | 53 ++++ .../notify/ReadingNotificationWorker.kt | 116 ++++++++ .../java/de/ladkau/deckinadash/ui/CardArt.kt | 20 ++ .../ladkau/deckinadash/ui/DeckInADashApp.kt | 107 ++++++++ .../deckinadash/ui/about/AboutScreen.kt | 26 ++ .../deckinadash/ui/config/ConfigScreen.kt | 258 ++++++++++++++++++ .../deckinadash/ui/config/ConfigViewModel.kt | 46 ++++ .../deckinadash/ui/reading/ReadingScreen.kt | 156 +++++++++++ .../ui/reading/ReadingViewModel.kt | 58 ++++ .../de/ladkau/deckinadash/ui/theme/Theme.kt | 30 ++ android/app/src/main/jni/CMakeLists.txt | 34 +++ android/app/src/main/jni/deck_jni.c | 45 +++ android/app/src/main/jni/reading_json.c | 198 ++++++++++++++ android/app/src/main/jni/reading_json.h | 32 +++ .../src/main/res/drawable/ic_notification.xml | 17 ++ .../res/mipmap-anydpi-v26/ic_launcher.xml | 5 + .../mipmap-anydpi-v26/ic_launcher_round.xml | 5 + android/app/src/main/res/values/colors.xml | 5 + android/app/src/main/res/values/strings.xml | 37 +++ android/app/src/main/res/values/themes.xml | 4 + android/build.gradle.kts | 5 + android/gradle.properties | 3 + android/gradle/libs.versions.toml | 35 +++ android/gradle/wrapper/gradle-wrapper.jar | Bin 0 -> 48462 bytes .../gradle/wrapper/gradle-wrapper.properties | 9 + android/gradlew | 248 +++++++++++++++++ android/gradlew.bat | 82 ++++++ android/scripts/gen_card_images.py | 82 ++++++ android/scripts/gen_launcher_icon.py | 79 ++++++ android/settings.gradle.kts | 18 ++ build-image/Dockerfile | 55 +++- build-image/VERSION | 2 +- run-image.sh | 31 ++- 49 files changed, 2530 insertions(+), 43 deletions(-) create mode 100644 android/README.md create mode 100644 android/app/build.gradle.kts create mode 100644 android/app/proguard-rules.pro create mode 100644 android/app/src/main/AndroidManifest.xml create mode 100644 android/app/src/main/java/de/ladkau/deckinadash/DeckInADashApplication.kt create mode 100644 android/app/src/main/java/de/ladkau/deckinadash/MainActivity.kt create mode 100644 android/app/src/main/java/de/ladkau/deckinadash/data/ConfigRepository.kt create mode 100644 android/app/src/main/java/de/ladkau/deckinadash/data/DeckConfig.kt create mode 100644 android/app/src/main/java/de/ladkau/deckinadash/data/ReadingDto.kt create mode 100644 android/app/src/main/java/de/ladkau/deckinadash/data/ReadingRepository.kt create mode 100644 android/app/src/main/java/de/ladkau/deckinadash/nativebridge/DeckNative.kt create mode 100644 android/app/src/main/java/de/ladkau/deckinadash/notify/NotificationScheduler.kt create mode 100644 android/app/src/main/java/de/ladkau/deckinadash/notify/ReadingNotificationWorker.kt create mode 100644 android/app/src/main/java/de/ladkau/deckinadash/ui/CardArt.kt create mode 100644 android/app/src/main/java/de/ladkau/deckinadash/ui/DeckInADashApp.kt create mode 100644 android/app/src/main/java/de/ladkau/deckinadash/ui/about/AboutScreen.kt create mode 100644 android/app/src/main/java/de/ladkau/deckinadash/ui/config/ConfigScreen.kt create mode 100644 android/app/src/main/java/de/ladkau/deckinadash/ui/config/ConfigViewModel.kt create mode 100644 android/app/src/main/java/de/ladkau/deckinadash/ui/reading/ReadingScreen.kt create mode 100644 android/app/src/main/java/de/ladkau/deckinadash/ui/reading/ReadingViewModel.kt create mode 100644 android/app/src/main/java/de/ladkau/deckinadash/ui/theme/Theme.kt create mode 100644 android/app/src/main/jni/CMakeLists.txt create mode 100644 android/app/src/main/jni/deck_jni.c create mode 100644 android/app/src/main/jni/reading_json.c create mode 100644 android/app/src/main/jni/reading_json.h create mode 100644 android/app/src/main/res/drawable/ic_notification.xml create mode 100644 android/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml create mode 100644 android/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml create mode 100644 android/app/src/main/res/values/colors.xml create mode 100644 android/app/src/main/res/values/strings.xml create mode 100644 android/app/src/main/res/values/themes.xml create mode 100644 android/build.gradle.kts create mode 100644 android/gradle.properties create mode 100644 android/gradle/libs.versions.toml create mode 100644 android/gradle/wrapper/gradle-wrapper.jar create mode 100644 android/gradle/wrapper/gradle-wrapper.properties create mode 100755 android/gradlew create mode 100644 android/gradlew.bat create mode 100644 android/scripts/gen_card_images.py create mode 100644 android/scripts/gen_launcher_icon.py create mode 100644 android/settings.gradle.kts diff --git a/.gitea/workflows/build.yml b/.gitea/workflows/build.yml index 8201eee..560a639 100644 --- a/.gitea/workflows/build.yml +++ b/.gitea/workflows/build.yml @@ -1,11 +1,14 @@ name: build -# Build a versioned Linux CLI release tarball (see `make package`) and the -# Pebble watchapp (see `make watchapp`) on every push/PR, plus on-demand -# via the Gitea "Run workflow" button. Runs inside build-image (see -# ../../build-image/Dockerfile, built/pushed via ../../build-image.sh and -# ../../upload-image.sh), which bundles the Pebble SDK alongside the C -# toolchain. +# Build a versioned Linux CLI release tarball (see `make package`), the +# Pebble watchapp (see `make watchapp`), and the Android app (see `make +# android`) on every push/PR, plus on-demand via the Gitea "Run workflow" +# button. Runs inside build-image (see ../../build-image/Dockerfile, +# built/pushed via ../../build-image.sh and ../../upload-image.sh), which +# bundles the Pebble SDK and the Android SDK/NDK alongside the C +# toolchain. Unlike the CLI/Pebble build path, the Android build still +# needs network access at job runtime (Gradle/AGP/androidx dependency +# resolution) - see the root CLAUDE.md's Android section. on: push: pull_request: @@ -41,6 +44,9 @@ jobs: command -v cc >/dev/null 2>&1 || { echo "PREFLIGHT FAIL: no C compiler (cc) in PATH" >&2; exit 1; } command -v make >/dev/null 2>&1 || { echo "PREFLIGHT FAIL: make not in PATH" >&2; exit 1; } command -v pebble >/dev/null 2>&1 || { echo "PREFLIGHT FAIL: pebble not in PATH" >&2; exit 1; } + command -v java >/dev/null 2>&1 || { echo "PREFLIGHT FAIL: java not in PATH" >&2; exit 1; } + [ -n "${ANDROID_HOME:-}" ] || { echo "PREFLIGHT FAIL: ANDROID_HOME not set" >&2; exit 1; } + [ -n "${ANDROID_NDK_HOME:-}" ] || { echo "PREFLIGHT FAIL: ANDROID_NDK_HOME not set" >&2; exit 1; } - name: Build and test run: | @@ -48,6 +54,7 @@ jobs: make test make package make watchapp + make android - name: Upload build artifact # v4 uses the newer @actions/artifact backend, which this Gitea @@ -63,8 +70,14 @@ jobs: name: deck-in-a-dash-watchapp path: dist/deck-in-a-dash-*.pbw + - name: Upload android artifact + uses: actions/upload-artifact@v3 + with: + name: deck-in-a-dash-android + path: dist/deck-in-a-dash-*.apk + - name: Publish to dl.ladkau.de - # Uploads the tarball and .pbw over SFTP instead of using + # Uploads the tarball, .pbw, and .apk over SFTP instead of using # actions/upload-artifact (whose zip wrapping can't be disabled). # Only runs on push so PR builds don't publish. if: gitea.event_name == 'push' @@ -72,6 +85,7 @@ jobs: set -euo pipefail TARBALL="$(ls dist/deck-in-a-dash-*-linux-*.tar.gz)" PBW="$(ls dist/deck-in-a-dash-*.pbw)" + APK="$(ls dist/deck-in-a-dash-*.apk)" mkdir -p ~/.ssh echo "${{ secrets.DL_SFTP_KEY }}" > ~/.ssh/dl_sftp_key chmod 600 ~/.ssh/dl_sftp_key @@ -81,4 +95,5 @@ jobs: -mkdir files/deck-in-a-dash put $TARBALL files/deck-in-a-dash/$(basename "$TARBALL") put $PBW files/deck-in-a-dash/$(basename "$PBW") + put $APK files/deck-in-a-dash/$(basename "$APK") EOF diff --git a/.gitignore b/.gitignore index b873b0d..d17b504 100644 --- a/.gitignore +++ b/.gitignore @@ -13,6 +13,23 @@ # added this .gitignore line for the git rm --cached that untracked it. /watch/.lock-waf_linux_build +# Android app build output/local config and generated resources (card +# art, launcher icon, synced i18n files - see android/app/build.gradle.kts's +# generateCardArt/generateLauncherIcon/syncI18n tasks and their own +# source scripts' doc comments). +/android/local.properties +/android/.gradle +/android/build +/android/app/build +/android/app/.cxx +/android/app/src/main/assets/i18n +/android/app/src/main/res/drawable-nodpi +/android/app/src/main/res/mipmap-mdpi +/android/app/src/main/res/mipmap-hdpi +/android/app/src/main/res/mipmap-xhdpi +/android/app/src/main/res/mipmap-xxhdpi +/android/app/src/main/res/mipmap-xxxhdpi + # Durable backup of the same real birth data, outside dist/ so a dist/ # wipe doesn't lose it — see the Makefile's `scripts` target. Never # commit this either. diff --git a/CLAUDE.md b/CLAUDE.md index 51c251b..ba097e2 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -123,30 +123,94 @@ push/PR, alongside `make test`/`make package` - the whole job runs inside a container image (`build-image/`, built and pushed with `build-image.sh`/`upload-image.sh`, see "Build image" below) that has the Pebble SDK baked in, so the CI runner itself needs no toolchain -beyond Docker access to pull that image. Both the CLI tarball and the -`.pbw` are uploaded as build artifacts and published to +beyond Docker access to pull that image. The CLI tarball, the `.pbw`, +and the Android `.apk` (see "Android app packaging" below) are all +uploaded as build artifacts and published to `dl.ladkau.de/files/deck-in-a-dash/` on push. +### Android app packaging (`make android`) + +A separate target, deliberately **not** a prerequisite of `all` - it +shells out to `./gradlew assembleDebug` inside `android/` (via that +directory's own Gradle/AGP/NDK toolchain, not this Makefile's plain-`cc` +rules) and copies the resulting +`android/app/build/outputs/apk/debug/app-debug.apk` to +`dist/deck-in-a-dash-.apk`, using the exact same version +resolution as `make package`/`make watchapp` above (current git tag, or +`make android VERSION=1.2.3` to override). Requires the Android SDK/NDK +(`ANDROID_HOME`/`ANDROID_NDK_HOME` set, plus the Python 3 + Pillow +`android/app/build.gradle.kts`'s `preBuild` task needs for its own +resource-generation scripts - see `android/README.md`) - a machine +without it can still build/test/package the CLI and the watch app via +every other target in this file. + +See `android/README.md` for the app's own architecture summary; in +short, it's a Jetpack Compose Material3 app whose native layer links the +same `engine/src/*.c`/`interpreter/src/*.c` sources the watch links (via +JNI/CMake, `android/app/src/main/jni/CMakeLists.txt`) - **the real, +full-precision `engine/third_party/astronomy/astronomy.c`, not the +watch's `lowprec_ephemeris.c` substitute**, since a phone has no +equivalent of Pebble's 64KB code+data+bss budget forcing that +substitution. Like the watch, the interpreter's real `.c` implementations +are linked in directly (`significance.c`/`narrative.c`/`guidance.c`), +not `json.c`/`reading_io.c` - there's no JSON pipe between separate +binaries here either, just one process computing and rendering its own +reading. Unlike the watch (which walks the `DailyReading`/ +`DailyInterpretation` structs directly across the same C/UI boundary), +the JNI bridge (`android/app/src/main/jni/reading_json.c`, a structural +port of `interpreter/src/main.c`'s `interp_print_json()`) serializes one +complete reading to a JSON string per call - a single call, not +fine-grained per-field accessors, since the alternative would mean +either re-running real ephemeris trig per field access or pinning a live +native object across calls; Kotlin then decodes that JSON with +`kotlinx.serialization` rather than maintaining its own JNI accessor +surface. Every string in that JSON (titles, narrative, guidance, +position/card names and meanings) is already localized via `i18n_get()`, +so Kotlin never needs its own planet/aspect/sign display-name table - +only the `*_slug` fields exist for the UI's card-art drawable lookup. +Card art is regenerated by `android/scripts/gen_card_images.py` +(structurally a sibling of `watch/scripts/gen_card_images.py`, same slug +table) at 400px wide, lossy WebP - not the watch's 72px PNG, since a +phone has no hardware-decoder budget forcing that size, but 21MB of +source JPEGs would otherwise bloat the APK; config (birth data, gender, +language, notification settings - the same fields as `WatchConfig`) is +persisted via Jetpack Preferences DataStore instead of Pebble's +key-value persist API. The daily notification uses WorkManager (a +self-rescheduling one-shot chain, not `AlarmManager`, since a "roughly +this time daily" reminder doesn't need `AlarmManager`'s exact-alarm +permission burden) - the Android analog of `watch/src/c/notify.c`'s own +daily-wakeup contract. + +**CI needs network access for this target specifically** - unlike the +CLI/Pebble build path above (deliberately zero network access needed +once `build-image` is pulled), Gradle/AGP/androidx dependency resolution +against Google's Maven + Maven Central happens at build time, every +build. This is a deliberate, accepted deviation from this image's +otherwise-offline build philosophy, not an oversight. + ### Build image `build-image/Dockerfile` pins the exact toolchain versions (Pebble -Tool, Pebble SDK core, Node.js) needed for `make test`, `make package`, -and `make watchapp`, so builds are reproducible independent of whatever -happens to be installed on a given machine. Three root-level scripts -drive it, each reading registry settings from a gitignored -`registry.env` (copy `registry.env.example` to create it - never commit -the real file, since it holds registry credentials) and the image tag -from `build-image/VERSION`: +Tool, Pebble SDK core, Node.js, and the Android SDK/NDK) needed for +`make test`, `make package`, `make watchapp`, and `make android`, so +builds are reproducible independent of whatever happens to be installed +on a given machine. Three root-level scripts drive it, each reading +registry settings from a gitignored `registry.env` (copy +`registry.env.example` to create it - never commit the real file, since +it holds registry credentials) and the image tag from +`build-image/VERSION`: - `build-image.sh` - builds and locally tags the image (`:VERSION` and `:latest`), no push. -- `run-image.sh [VERSION]` - runs `make test package watchapp` inside - the locally-built image against a live bind-mount of the repo, wiping - generated build artifacts first for CI parity. Use this to validate a - `build-image/Dockerfile` change before pushing the image anywhere. +- `run-image.sh [VERSION]` - runs `make test package watchapp android` + inside the locally-built image against a live bind-mount of the repo, + wiping generated build artifacts first for CI parity. Use this to + validate a `build-image/Dockerfile` change before pushing the image + anywhere. - `upload-image.sh` - logs in and pushes the image (`:VERSION` and `:latest`) to the registry; `.gitea/workflows/build.yml` pulls - `:latest` to run its build job (see "Watch app packaging" above). + `:latest` to run its build job (see "Watch app packaging"/"Android app + packaging" above). ## Architecture diff --git a/Makefile b/Makefile index d430942..09e7e47 100644 --- a/Makefile +++ b/Makefile @@ -92,7 +92,12 @@ PACKAGE_DIR := $(BUILD_DIR)/package WATCH_DIR := watch WATCH_PBW := $(WATCH_DIR)/build/watch.pbw -.PHONY: all test clean images i18n scripts package watchapp +# ---- Android app ---- + +ANDROID_DIR := android +ANDROID_APK := $(ANDROID_DIR)/app/build/outputs/apk/debug/app-debug.apk + +.PHONY: all test clean images i18n scripts package watchapp android all: $(ENGINE_BINARY) $(INTERP_BINARY) images i18n scripts @@ -242,3 +247,34 @@ watchapp: PBW_NAME="deck-in-a-dash-$$V.pbw"; \ cp $(WATCH_PBW) "$(DIST_DIR)/$$PBW_NAME"; \ echo "Wrote $(DIST_DIR)/$$PBW_NAME" + +# Builds the Android app (`./gradlew assembleDebug` in android/, via its +# own Gradle/AGP/NDK toolchain - not a plain C toolchain, so this is a +# separate target from `all` rather than one of its prerequisites) and +# copies the resulting debug APK into dist/, versioned the same way +# `package`/`watchapp` above version their own artifacts (current git +# tag, or `make android VERSION=1.2.3` to override). Ships the +# auto-generated debug keystore, not a release signing config - see the +# root CLAUDE.md's Android section for why. Requires the Android SDK/NDK +# (ANDROID_HOME/ANDROID_NDK_HOME set, see android/README.md) - this is +# NOT checked by the root `make`/`make test`, so a machine without them +# can still build/test the CLI (and, separately, the watch app). +android: + cd $(ANDROID_DIR) && ./gradlew assembleDebug + @mkdir -p $(DIST_DIR) + @V="$(VERSION)"; \ + if [ -z "$$V" ]; then \ + if TAG=$$(git describe --tags --exact-match --match 'v[0-9]*.[0-9]*.[0-9]*' 2>/dev/null); then \ + V=$${TAG#v}; \ + else \ + V="0.0.0-dev+$$(git rev-parse --short HEAD)"; \ + echo "WARNING: HEAD is not on a vX.Y.Z tag - building placeholder version $$V (push a tag to drive a real release version)" >&2; \ + fi; \ + fi; \ + case "$$V" in \ + [0-9]*.[0-9]*.[0-9]*) ;; \ + *) echo "PREFLIGHT FAIL: VERSION '$$V' is not a semantic version (expected X.Y.Z, optionally with a -pre+meta suffix)" >&2; exit 1;; \ + esac; \ + APK_NAME="deck-in-a-dash-$$V.apk"; \ + cp $(ANDROID_APK) "$(DIST_DIR)/$$APK_NAME"; \ + echo "Wrote $(DIST_DIR)/$$APK_NAME" diff --git a/README.md b/README.md index e869785..307d4a7 100644 --- a/README.md +++ b/README.md @@ -59,11 +59,19 @@ watch/ The actual Pebble watchapp - links engine/src/ notification, and config page. Built separately from the rest of this repo (`make watchapp`, below) - requires the Pebble SDK. +android/ A standalone Android app (independent of the + watch app - neither requires the other) with the + same daily reading + config page, Jetpack Compose + UI, links engine/src/ and interpreter/src/ via + JNI/CMake (see CLAUDE.md's "Android app + packaging"). Built separately (`make android`, + below) - requires the Android SDK/NDK. docs/ Architecture/dataflow diagrams, input/output format reference. build-image/ Dockerfile for the reproducible build environment - (C toolchain + Pebble SDK) used by CI and by the - build-image.sh/run-image.sh/upload-image.sh scripts - at the repo root (see "Building" below). + (C toolchain + Pebble SDK + Android SDK/NDK) used + by CI and by the build-image.sh/run-image.sh/ + upload-image.sh scripts at the repo root (see + "Building" below). Makefile Single root Makefile, builds both engine/ and interpreter/, and packages a release (`make package`). ``` @@ -98,6 +106,16 @@ make watchapp # -> dist/deck-in-a-dash-.pbw, same version # resolution as `make package` ``` +**Building the Android app** (`android/`, a standalone app independent +of the watch app) needs the separate Android SDK/NDK +(`ANDROID_HOME`/`ANDROID_NDK_HOME` set - see `android/README.md`), so +it's likewise a separate target: + +```bash +make android # -> dist/deck-in-a-dash-.apk, same version + # resolution as `make package` +``` + **CI runs `make watchapp` too** — `.gitea/workflows/build.yml` builds and tests everything inside a container built from `build-image/`, which bundles the Pebble SDK alongside the C toolchain so the runner diff --git a/android/README.md b/android/README.md new file mode 100644 index 0000000..6584a1b --- /dev/null +++ b/android/README.md @@ -0,0 +1,43 @@ +# Deck in a Dash — Android app + +A standalone Android app showing the same daily tarot (Celtic Cross) + +astrology reading as the Pebble watch app (`../watch/`), with a burger +menu leading to a config page. Independent of the watch app - neither +requires the other to be installed. See the root +[`CLAUDE.md`](../CLAUDE.md)'s "Android app" section for the full +architecture (JNI-linked engine/interpreter, JSON bridge schema, +WorkManager notification, card art pipeline). + +## Building + +Requires: + +- A JDK (17+; the project's own `compileOptions`/toolchain target Java + 17). `JAVA_HOME` should point at it, or a `java` matching that version + must be first on `PATH`. +- The Android SDK, with `ANDROID_HOME` (or `ANDROID_SDK_ROOT`) set - + needs at minimum `platforms;android-36`, `build-tools;36.0.0`, + `ndk;30.0.14904198`, and `cmake;3.22.1` installed (see + `app/build.gradle.kts`'s `compileSdk`/`ndkVersion` and + `app/src/main/jni/CMakeLists.txt`'s `cmake_minimum_required` for the + exact versions this project pins - `../build-image/Dockerfile` + installs the same ones for CI). +- `ANDROID_NDK_HOME` set to that installed NDK's directory. +- Python 3 + Pillow (`pip install Pillow`) - needed by + `scripts/gen_card_images.py`/`gen_launcher_icon.py`, which Gradle's + `preBuild` task runs automatically (see `app/build.gradle.kts`). + +```bash +./gradlew assembleDebug # -> app/build/outputs/apk/debug/app-debug.apk +``` + +Or, from the repo root: `make android`, which wraps the above and +copies the APK into `dist/deck-in-a-dash-.pbw`'s sibling, +`dist/deck-in-a-dash-.apk` (same version resolution as `make +package`/`make watchapp` - see the root `Makefile`). + +The Gradle wrapper (`./gradlew`) resolves its own pinned Gradle version +on first run - no separate Gradle install is needed. + +Ships the Android auto-generated debug keystore, not a release-signed +build - see the root `CLAUDE.md`'s Android section for why. diff --git a/android/app/build.gradle.kts b/android/app/build.gradle.kts new file mode 100644 index 0000000..abe432c --- /dev/null +++ b/android/app/build.gradle.kts @@ -0,0 +1,92 @@ +plugins { + alias(libs.plugins.android.application) + alias(libs.plugins.kotlin.serialization) + alias(libs.plugins.kotlin.compose) +} + +android { + namespace = "de.ladkau.deckinadash" + compileSdk = 36 + ndkVersion = "30.0.14904198" + + defaultConfig { + applicationId = "de.ladkau.deckinadash" + minSdk = 24 + targetSdk = 36 + versionCode = 1 + versionName = "0.0.0-dev" + + ndk { + abiFilters += listOf("arm64-v8a", "x86_64") + } + } + + buildTypes { + release { + isMinifyEnabled = false + proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro") + } + } + + externalNativeBuild { + cmake { + path = file("src/main/jni/CMakeLists.txt") + version = "3.22.1" + } + } + + compileOptions { + sourceCompatibility = JavaVersion.VERSION_17 + targetCompatibility = JavaVersion.VERSION_17 + } + + buildFeatures { + compose = true + } +} + +// Regenerates app/src/main/res/drawable-nodpi/.webp from +// ../../res/img/*.jpeg (see ../../scripts/gen_card_images.py) and syncs +// engine/i18n/*.lang into app/src/main/assets/i18n/ - both are gitignored +// generated output, so a fresh checkout needs them produced before any +// resource-merge/asset-packaging task runs. Mirrors watch/wscript's own +// "generate before the SDK tooling packs resources" fix - see that +// file's own comment for the ordering bug this exact pattern avoids. +val generateCardArt = tasks.register("generateCardArt") { + workingDir = rootDir + commandLine("python3", "scripts/gen_card_images.py") +} + +val generateLauncherIcon = tasks.register("generateLauncherIcon") { + workingDir = rootDir + commandLine("python3", "scripts/gen_launcher_icon.py") +} + +val syncI18n = tasks.register("syncI18n") { + from("${rootDir}/../engine/i18n") + include("*.lang") + into("src/main/assets/i18n") +} + +tasks.named("preBuild") { + dependsOn(generateCardArt, generateLauncherIcon, syncI18n) +} + +dependencies { + implementation(libs.androidx.core.ktx) + implementation(libs.androidx.lifecycle.runtime.ktx) + implementation(libs.androidx.lifecycle.viewmodel.compose) + implementation(libs.androidx.activity.compose) + implementation(platform(libs.androidx.compose.bom)) + implementation(libs.androidx.ui) + implementation(libs.androidx.ui.graphics) + implementation(libs.androidx.ui.tooling.preview) + implementation(libs.androidx.material3) + implementation(libs.androidx.material.icons.core) + implementation(libs.androidx.navigation.compose) + implementation(libs.androidx.datastore.preferences) + implementation(libs.androidx.work.runtime.ktx) + implementation(libs.kotlinx.serialization.json) + implementation(libs.kotlinx.coroutines.android) + debugImplementation(libs.androidx.ui.tooling) +} diff --git a/android/app/proguard-rules.pro b/android/app/proguard-rules.pro new file mode 100644 index 0000000..28ee178 --- /dev/null +++ b/android/app/proguard-rules.pro @@ -0,0 +1,9 @@ +# kotlinx.serialization needs its generated serializer classes kept. +-keepattributes *Annotation*, InnerClasses +-dontnote kotlinx.serialization.AnnotationsKt +-keepclasseswithmembers class de.ladkau.deckinadash.data.** { + *** Companion; +} +-keepclasseswithmembers class de.ladkau.deckinadash.data.** { + kotlinx.serialization.KSerializer serializer(...); +} diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml new file mode 100644 index 0000000..94367f5 --- /dev/null +++ b/android/app/src/main/AndroidManifest.xml @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + + + diff --git a/android/app/src/main/java/de/ladkau/deckinadash/DeckInADashApplication.kt b/android/app/src/main/java/de/ladkau/deckinadash/DeckInADashApplication.kt new file mode 100644 index 0000000..14d23a9 --- /dev/null +++ b/android/app/src/main/java/de/ladkau/deckinadash/DeckInADashApplication.kt @@ -0,0 +1,43 @@ +package de.ladkau.deckinadash + +import android.app.Application +import de.ladkau.deckinadash.data.ConfigRepository +import de.ladkau.deckinadash.notify.NotificationScheduler +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch +import kotlinx.coroutines.flow.first +import java.io.File + +/** + * Copies assets/i18n's .lang files (synced at build time from engine/i18n/ - + * see app/build.gradle.kts's syncI18n task) into filesDir/i18n/ once + * per process start, since i18n_load() (linked into the native library + * unchanged, see reading_json.c) needs real filesystem paths, not APK + * assets. Also reschedules the daily notification on launch - one of + * the two required call sites for NotificationScheduler.reschedule(), + * see its own doc comment for the other (every config save). + */ +class DeckInADashApplication : Application() { + override fun onCreate() { + super.onCreate() + syncI18nAssets() + + CoroutineScope(Dispatchers.Default).launch { + val config = ConfigRepository(this@DeckInADashApplication).config.first() + if (config.configured) { + NotificationScheduler.reschedule(this@DeckInADashApplication, config) + } + } + } + + private fun syncI18nAssets() { + val outDir = File(filesDir, "i18n").apply { mkdirs() } + val files = assets.list("i18n") ?: return + for (name in files) { + assets.open("i18n/$name").use { input -> + File(outDir, name).outputStream().use { output -> input.copyTo(output) } + } + } + } +} diff --git a/android/app/src/main/java/de/ladkau/deckinadash/MainActivity.kt b/android/app/src/main/java/de/ladkau/deckinadash/MainActivity.kt new file mode 100644 index 0000000..d8592e9 --- /dev/null +++ b/android/app/src/main/java/de/ladkau/deckinadash/MainActivity.kt @@ -0,0 +1,20 @@ +package de.ladkau.deckinadash + +import android.os.Bundle +import androidx.activity.ComponentActivity +import androidx.activity.compose.setContent +import androidx.activity.enableEdgeToEdge +import de.ladkau.deckinadash.ui.DeckInADashApp +import de.ladkau.deckinadash.ui.theme.DeckInADashTheme + +class MainActivity : ComponentActivity() { + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + enableEdgeToEdge() + setContent { + DeckInADashTheme { + DeckInADashApp() + } + } + } +} diff --git a/android/app/src/main/java/de/ladkau/deckinadash/data/ConfigRepository.kt b/android/app/src/main/java/de/ladkau/deckinadash/data/ConfigRepository.kt new file mode 100644 index 0000000..947cd59 --- /dev/null +++ b/android/app/src/main/java/de/ladkau/deckinadash/data/ConfigRepository.kt @@ -0,0 +1,75 @@ +package de.ladkau.deckinadash.data + +import android.content.Context +import androidx.datastore.core.DataStore +import androidx.datastore.preferences.core.Preferences +import androidx.datastore.preferences.core.booleanPreferencesKey +import androidx.datastore.preferences.core.doublePreferencesKey +import androidx.datastore.preferences.core.edit +import androidx.datastore.preferences.core.intPreferencesKey +import androidx.datastore.preferences.core.stringPreferencesKey +import androidx.datastore.preferences.preferencesDataStore +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.map + +private val Context.configDataStore: DataStore by preferencesDataStore(name = "deck_config") + +/** Preferences DataStore-backed persistence for [DeckConfig] - see that + * class's own doc comment for why DataStore over SharedPreferences/Proto + * DataStore. */ +class ConfigRepository(private val context: Context) { + private object Keys { + val CONFIGURED = booleanPreferencesKey("configured") + val BIRTH_YEAR = intPreferencesKey("birth_year") + val BIRTH_MONTH = intPreferencesKey("birth_month") + val BIRTH_DAY = intPreferencesKey("birth_day") + val BIRTH_HOUR = intPreferencesKey("birth_hour") + val BIRTH_MINUTE = intPreferencesKey("birth_minute") + val UTC_OFFSET_HOURS = doublePreferencesKey("utc_offset_hours") + val LATITUDE = doublePreferencesKey("latitude") + val LONGITUDE = doublePreferencesKey("longitude") + val GENDER = stringPreferencesKey("gender") + val LANG = stringPreferencesKey("lang") + val NOTIFY_ENABLED = booleanPreferencesKey("notify_enabled") + val NOTIFY_HOUR = intPreferencesKey("notify_hour") + val NOTIFY_MINUTE = intPreferencesKey("notify_minute") + } + + val config: Flow = context.configDataStore.data.map { prefs -> + DeckConfig( + configured = prefs[Keys.CONFIGURED] ?: false, + birthYear = prefs[Keys.BIRTH_YEAR] ?: 0, + birthMonth = prefs[Keys.BIRTH_MONTH] ?: 0, + birthDay = prefs[Keys.BIRTH_DAY] ?: 0, + birthHour = prefs[Keys.BIRTH_HOUR] ?: 0, + birthMinute = prefs[Keys.BIRTH_MINUTE] ?: 0, + utcOffsetHours = prefs[Keys.UTC_OFFSET_HOURS] ?: 0.0, + latitude = prefs[Keys.LATITUDE] ?: 0.0, + longitude = prefs[Keys.LONGITUDE] ?: 0.0, + gender = prefs[Keys.GENDER]?.let { Gender.valueOf(it) } ?: Gender.UNSPECIFIED, + lang = prefs[Keys.LANG] ?: "en", + notifyEnabled = prefs[Keys.NOTIFY_ENABLED] ?: false, + notifyHour = prefs[Keys.NOTIFY_HOUR] ?: 8, + notifyMinute = prefs[Keys.NOTIFY_MINUTE] ?: 0, + ) + } + + suspend fun save(config: DeckConfig) { + context.configDataStore.edit { prefs -> + prefs[Keys.CONFIGURED] = true + prefs[Keys.BIRTH_YEAR] = config.birthYear + prefs[Keys.BIRTH_MONTH] = config.birthMonth + prefs[Keys.BIRTH_DAY] = config.birthDay + prefs[Keys.BIRTH_HOUR] = config.birthHour + prefs[Keys.BIRTH_MINUTE] = config.birthMinute + prefs[Keys.UTC_OFFSET_HOURS] = config.utcOffsetHours + prefs[Keys.LATITUDE] = config.latitude + prefs[Keys.LONGITUDE] = config.longitude + prefs[Keys.GENDER] = config.gender.name + prefs[Keys.LANG] = config.lang + prefs[Keys.NOTIFY_ENABLED] = config.notifyEnabled + prefs[Keys.NOTIFY_HOUR] = config.notifyHour + prefs[Keys.NOTIFY_MINUTE] = config.notifyMinute + } + } +} diff --git a/android/app/src/main/java/de/ladkau/deckinadash/data/DeckConfig.kt b/android/app/src/main/java/de/ladkau/deckinadash/data/DeckConfig.kt new file mode 100644 index 0000000..09d6327 --- /dev/null +++ b/android/app/src/main/java/de/ladkau/deckinadash/data/DeckConfig.kt @@ -0,0 +1,32 @@ +package de.ladkau.deckinadash.data + +/** Mirrors engine/src/tarot.h's Gender enum - ordinal must match. */ +enum class Gender { + UNSPECIFIED, + MALE, + FEMALE, +} + +/** + * Everything the app needs that isn't computed fresh each day: birth + * data, display language, and daily notification settings. Mirrors + * watch/src/c/config.h's WatchConfig field-for-field - same data, same + * defaults, persisted here via Preferences DataStore (see + * ConfigRepository) instead of Pebble's key-value persist API. + */ +data class DeckConfig( + val configured: Boolean = false, + val birthYear: Int = 0, + val birthMonth: Int = 0, + val birthDay: Int = 0, + val birthHour: Int = 0, + val birthMinute: Int = 0, + val utcOffsetHours: Double = 0.0, + val latitude: Double = 0.0, + val longitude: Double = 0.0, + val gender: Gender = Gender.UNSPECIFIED, + val lang: String = "en", + val notifyEnabled: Boolean = false, + val notifyHour: Int = 8, + val notifyMinute: Int = 0, +) diff --git a/android/app/src/main/java/de/ladkau/deckinadash/data/ReadingDto.kt b/android/app/src/main/java/de/ladkau/deckinadash/data/ReadingDto.kt new file mode 100644 index 0000000..a9a6b49 --- /dev/null +++ b/android/app/src/main/java/de/ladkau/deckinadash/data/ReadingDto.kt @@ -0,0 +1,51 @@ +package de.ladkau.deckinadash.data + +import kotlinx.serialization.Serializable + +/** + * Mirrors the JSON schema app/src/main/jni/reading_json.c emits - see + * that file's own header comment. Deserialized with a snake_case naming + * strategy (see ReadingRepository's Json instance), so these property + * names stay idiomatic Kotlin camelCase without needing @SerialName on + * every field. + */ +@Serializable +data class ReadingDto( + val date: String, + val gender: String, + val daySignificance: DaySignificanceDto, + val transits: List, + val guidance: String, + val spread: List, +) + +@Serializable +data class DaySignificanceDto( + val levelSlug: String, + val levelLabel: String, + val rank: Int, + val count: Int, + val deservesFraming: Boolean, +) + +@Serializable +data class TransitDto( + val title: String, + val transitingPlanetSlug: String, + val natalPlanetSlug: String, + val aspectSlug: String, + val orb: Double, + val score: Double, + val narrative: String, +) + +@Serializable +data class SpreadPositionDto( + val positionSlug: String, + val positionName: String, + val positionDescription: String, + val cardSlug: String, + val cardName: String, + val reversed: Boolean, + val cardMeaning: String, +) diff --git a/android/app/src/main/java/de/ladkau/deckinadash/data/ReadingRepository.kt b/android/app/src/main/java/de/ladkau/deckinadash/data/ReadingRepository.kt new file mode 100644 index 0000000..07bcc12 --- /dev/null +++ b/android/app/src/main/java/de/ladkau/deckinadash/data/ReadingRepository.kt @@ -0,0 +1,44 @@ +package de.ladkau.deckinadash.data + +import android.content.Context +import de.ladkau.deckinadash.nativebridge.DeckNative +import kotlinx.serialization.ExperimentalSerializationApi +import kotlinx.serialization.json.Json +import kotlinx.serialization.json.JsonNamingStrategy + +/** + * Computes a [ReadingDto] via [DeckNative], the single entry point both + * the reading screen (ReadingViewModel) and the daily notification + * (ReadingNotificationWorker) call - see NotificationScheduler's own + * comment on why the notification must reuse this rather than + * duplicating reading logic. + */ +class ReadingRepository(private val context: Context) { + @OptIn(ExperimentalSerializationApi::class) + private val json = Json { + ignoreUnknownKeys = true + namingStrategy = JsonNamingStrategy.SnakeCase + } + + /** Directory DeckInADashApplication.onCreate() synced engine/i18n's .lang files into. */ + private val i18nDir: String + get() = "${context.filesDir}/i18n" + + fun compute(config: DeckConfig, utcMomentEpochSeconds: Long): ReadingDto { + val jsonText = DeckNative.generateReadingJson( + birthYear = config.birthYear, + birthMonth = config.birthMonth, + birthDay = config.birthDay, + birthHour = config.birthHour, + birthMinute = config.birthMinute, + utcOffsetHours = config.utcOffsetHours, + latitude = config.latitude, + longitude = config.longitude, + gender = config.gender.ordinal, + utcMomentEpochSeconds = utcMomentEpochSeconds, + i18nDir = i18nDir, + lang = config.lang, + ) + return json.decodeFromString(ReadingDto.serializer(), jsonText) + } +} diff --git a/android/app/src/main/java/de/ladkau/deckinadash/nativebridge/DeckNative.kt b/android/app/src/main/java/de/ladkau/deckinadash/nativebridge/DeckNative.kt new file mode 100644 index 0000000..48e8781 --- /dev/null +++ b/android/app/src/main/java/de/ladkau/deckinadash/nativebridge/DeckNative.kt @@ -0,0 +1,37 @@ +package de.ladkau.deckinadash.nativebridge + +/** + * JNI bridge to the native `deckengine` shared library (built from + * app/src/main/jni/CMakeLists.txt, which links the same engine + + * interpreter C sources the watch app links - see that CMakeLists.txt's + * own comment). One call returns one complete JSON snapshot of a day's + * reading; see app/src/main/jni/reading_json.h for the exact schema and + * why this is a single call rather than many fine-grained accessors. + */ +object DeckNative { + init { + System.loadLibrary("deckengine") + } + + /** + * @param gender 0 = unspecified, 1 = male, 2 = female - matches + * engine/src/tarot.h's Gender enum order. + * @param i18nDir directory containing `.lang` files (see + * engine/i18n/), synced from assets by DeckInADashApp.onCreate(). + * @throws IllegalStateException if native JSON generation fails. + */ + external fun generateReadingJson( + birthYear: Int, + birthMonth: Int, + birthDay: Int, + birthHour: Int, + birthMinute: Int, + utcOffsetHours: Double, + latitude: Double, + longitude: Double, + gender: Int, + utcMomentEpochSeconds: Long, + i18nDir: String, + lang: String, + ): String +} diff --git a/android/app/src/main/java/de/ladkau/deckinadash/notify/NotificationScheduler.kt b/android/app/src/main/java/de/ladkau/deckinadash/notify/NotificationScheduler.kt new file mode 100644 index 0000000..c645b62 --- /dev/null +++ b/android/app/src/main/java/de/ladkau/deckinadash/notify/NotificationScheduler.kt @@ -0,0 +1,53 @@ +package de.ladkau.deckinadash.notify + +import android.content.Context +import androidx.work.ExistingWorkPolicy +import androidx.work.OneTimeWorkRequestBuilder +import androidx.work.WorkManager +import de.ladkau.deckinadash.data.DeckConfig +import java.util.Calendar +import java.util.concurrent.TimeUnit + +/** + * WorkManager analog of watch/src/c/notify.c's notify_reschedule(): (re)schedules + * a one-shot wakeup for the next occurrence of `config.notifyHour:notifyMinute` + * in the device's local time (today if not yet passed, tomorrow otherwise), + * or only cancels if notifications are disabled. See ReadingNotificationWorker + * for the self-rescheduling chain this kicks off. + * + * Call once per app launch (DeckInADashApplication.onCreate()) and on every + * config save (ConfigViewModel.save()) - same two call sites notify_reschedule() + * itself requires, for the same reason (settings may have changed). + */ +object NotificationScheduler { + private const val WORK_NAME = "daily_reading_notification" + + fun reschedule(context: Context, config: DeckConfig) { + val workManager = WorkManager.getInstance(context) + if (!config.notifyEnabled) { + workManager.cancelUniqueWork(WORK_NAME) + return + } + val delayMillis = millisUntilNext(config.notifyHour, config.notifyMinute) + val request = OneTimeWorkRequestBuilder() + .setInitialDelay(delayMillis, TimeUnit.MILLISECONDS) + .build() + // REPLACE is the WorkManager analog of notify_reschedule()'s own + // cancel-then-reschedule - guarantees no duplicate chains. + workManager.enqueueUniqueWork(WORK_NAME, ExistingWorkPolicy.REPLACE, request) + } + + private fun millisUntilNext(hour: Int, minute: Int): Long { + val now = Calendar.getInstance() + val target = (now.clone() as Calendar).apply { + set(Calendar.HOUR_OF_DAY, hour) + set(Calendar.MINUTE, minute) + set(Calendar.SECOND, 0) + set(Calendar.MILLISECOND, 0) + } + if (!target.after(now)) { + target.add(Calendar.DAY_OF_YEAR, 1) + } + return target.timeInMillis - now.timeInMillis + } +} diff --git a/android/app/src/main/java/de/ladkau/deckinadash/notify/ReadingNotificationWorker.kt b/android/app/src/main/java/de/ladkau/deckinadash/notify/ReadingNotificationWorker.kt new file mode 100644 index 0000000..f286cc4 --- /dev/null +++ b/android/app/src/main/java/de/ladkau/deckinadash/notify/ReadingNotificationWorker.kt @@ -0,0 +1,116 @@ +package de.ladkau.deckinadash.notify + +import android.Manifest +import android.app.NotificationChannel +import android.app.NotificationManager +import android.app.PendingIntent +import android.content.Context +import android.content.Intent +import android.content.pm.PackageManager +import android.os.Build +import androidx.core.app.ActivityCompat +import androidx.core.app.NotificationCompat +import androidx.core.app.NotificationManagerCompat +import androidx.work.CoroutineWorker +import androidx.work.WorkerParameters +import de.ladkau.deckinadash.MainActivity +import de.ladkau.deckinadash.R +import de.ladkau.deckinadash.data.ConfigRepository +import de.ladkau.deckinadash.data.ReadingDto +import de.ladkau.deckinadash.data.ReadingRepository +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.flow.first +import kotlinx.coroutines.withContext +import java.time.Instant + +private const val CHANNEL_ID = "daily_reading" +private const val NOTIFICATION_ID = 1 + +/** + * Fires once (scheduled by NotificationScheduler.reschedule()), computes + * today's reading via the same ReadingRepository the UI uses (no + * duplicated reading logic - see that class's own comment), posts a + * "quick digest" notification from the day's significance + top + * transit (the same lead-in watch/src/c/ui_report_window.c's own + * comment describes the watch's wakeup-vibrate leading into), then + * reschedules itself for tomorrow - WorkManager has no "repeat forever + * at an exact wall-clock time" primitive, so this self-chaining step is + * how notify_reschedule()'s own daily-repeat contract is reproduced. + */ +class ReadingNotificationWorker( + appContext: Context, + params: WorkerParameters, +) : CoroutineWorker(appContext, params) { + + override suspend fun doWork(): Result { + val configRepository = ConfigRepository(applicationContext) + val config = configRepository.config.first() + + // Mirrors notify_reschedule()'s own "if disabled, only cancel" branch - + // a defensive no-op, since reschedule() should already have cancelled + // this work item when notifications were turned off. + if (!config.notifyEnabled || !config.configured) { + return Result.success() + } + + val reading = try { + withContext(Dispatchers.Default) { + ReadingRepository(applicationContext).compute(config, Instant.now().epochSecond) + } + } catch (t: Throwable) { + NotificationScheduler.reschedule(applicationContext, config) + return Result.failure() + } + + postNotification(reading) + NotificationScheduler.reschedule(applicationContext, config) + return Result.success() + } + + private fun postNotification(reading: ReadingDto) { + ensureChannel() + + val body = reading.transits.firstOrNull()?.let { "${it.title}. ${it.narrative}" } + ?: reading.guidance + + val openAppIntent = PendingIntent.getActivity( + applicationContext, + 0, + Intent(applicationContext, MainActivity::class.java), + PendingIntent.FLAG_IMMUTABLE, + ) + + val notification = NotificationCompat.Builder(applicationContext, CHANNEL_ID) + .setSmallIcon(R.drawable.ic_notification) + .setContentTitle( + applicationContext.getString(R.string.notification_title, reading.daySignificance.levelLabel), + ) + .setContentText(body) + .setStyle(NotificationCompat.BigTextStyle().bigText(body)) + .setAutoCancel(true) + .setContentIntent(openAppIntent) + .build() + + if (ActivityCompat.checkSelfPermission( + applicationContext, + Manifest.permission.POST_NOTIFICATIONS, + ) != PackageManager.PERMISSION_GRANTED + ) { + return + } + NotificationManagerCompat.from(applicationContext).notify(NOTIFICATION_ID, notification) + } + + private fun ensureChannel() { + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) return + val manager = applicationContext.getSystemService(NotificationManager::class.java) + val channel = NotificationChannel( + CHANNEL_ID, + applicationContext.getString(R.string.notification_channel_name), + NotificationManager.IMPORTANCE_DEFAULT, + ).apply { + description = applicationContext.getString(R.string.notification_channel_description) + } + manager.createNotificationChannel(channel) + } +} diff --git a/android/app/src/main/java/de/ladkau/deckinadash/ui/CardArt.kt b/android/app/src/main/java/de/ladkau/deckinadash/ui/CardArt.kt new file mode 100644 index 0000000..b8d2e98 --- /dev/null +++ b/android/app/src/main/java/de/ladkau/deckinadash/ui/CardArt.kt @@ -0,0 +1,20 @@ +package de.ladkau.deckinadash.ui + +import android.content.Context +import androidx.annotation.DrawableRes + +/** + * Resolves a card slug (e.g. "fool", "wheel_of_fortune" - the same + * strings engine/src/tarot_data.c's tarot_card_slug() returns) to its + * generated drawable resource, produced by ../../scripts/gen_card_images.py + * into res/drawable-nodpi/.webp at build time - see that script's + * own comment. Slugs are already valid Android resource-name identifiers, + * so no separate mapping table is needed, only a runtime resource lookup + * (the resource ID isn't known at compile time from this module, since + * the drawables are generated, not committed). + */ +@DrawableRes +fun cardDrawableRes(context: Context, cardSlug: String): Int { + val id = context.resources.getIdentifier(cardSlug, "drawable", context.packageName) + return if (id != 0) id else android.R.drawable.ic_menu_report_image +} diff --git a/android/app/src/main/java/de/ladkau/deckinadash/ui/DeckInADashApp.kt b/android/app/src/main/java/de/ladkau/deckinadash/ui/DeckInADashApp.kt new file mode 100644 index 0000000..e34d695 --- /dev/null +++ b/android/app/src/main/java/de/ladkau/deckinadash/ui/DeckInADashApp.kt @@ -0,0 +1,107 @@ +package de.ladkau.deckinadash.ui + +import androidx.compose.foundation.layout.padding +import androidx.compose.material.icons.Icons +import androidx.compose.material.icons.filled.Menu +import androidx.compose.material3.DrawerValue +import androidx.compose.material3.ExperimentalMaterial3Api +import androidx.compose.material3.Icon +import androidx.compose.material3.IconButton +import androidx.compose.material3.ModalDrawerSheet +import androidx.compose.material3.ModalNavigationDrawer +import androidx.compose.material3.NavigationDrawerItem +import androidx.compose.material3.Scaffold +import androidx.compose.material3.Text +import androidx.compose.material3.TopAppBar +import androidx.compose.material3.rememberDrawerState +import androidx.compose.runtime.Composable +import androidx.compose.runtime.rememberCoroutineScope +import androidx.compose.ui.Modifier +import androidx.compose.ui.res.stringResource +import androidx.compose.ui.unit.dp +import androidx.navigation.NavHostController +import androidx.navigation.compose.NavHost +import androidx.navigation.compose.composable +import androidx.navigation.compose.currentBackStackEntryAsState +import androidx.navigation.compose.rememberNavController +import de.ladkau.deckinadash.R +import de.ladkau.deckinadash.ui.about.AboutScreen +import de.ladkau.deckinadash.ui.config.ConfigScreen +import de.ladkau.deckinadash.ui.reading.ReadingScreen +import kotlinx.coroutines.launch + +private object Destinations { + const val READING = "reading" + const val SETTINGS = "settings" + const val ABOUT = "about" +} + +/** Burger menu = ModalNavigationDrawer, leading to Reading/Settings/About - + * the app's single navigation host, per the plan's UI decision. */ +@OptIn(ExperimentalMaterial3Api::class) +@Composable +fun DeckInADashApp() { + val navController = rememberNavController() + val drawerState = rememberDrawerState(initialValue = DrawerValue.Closed) + val scope = rememberCoroutineScope() + val currentRoute = navController.currentBackStackEntryAsState().value?.destination?.route + + ModalNavigationDrawer( + drawerState = drawerState, + drawerContent = { + ModalDrawerSheet { + NavigationDrawerItem( + label = { Text(stringResource(R.string.nav_reading)) }, + selected = currentRoute == Destinations.READING, + onClick = { navigateTo(navController, Destinations.READING); scope.launch { drawerState.close() } }, + modifier = Modifier.padding(horizontal = 12.dp), + ) + NavigationDrawerItem( + label = { Text(stringResource(R.string.nav_settings)) }, + selected = currentRoute == Destinations.SETTINGS, + onClick = { navigateTo(navController, Destinations.SETTINGS); scope.launch { drawerState.close() } }, + modifier = Modifier.padding(horizontal = 12.dp), + ) + NavigationDrawerItem( + label = { Text(stringResource(R.string.nav_about)) }, + selected = currentRoute == Destinations.ABOUT, + onClick = { navigateTo(navController, Destinations.ABOUT); scope.launch { drawerState.close() } }, + modifier = Modifier.padding(horizontal = 12.dp), + ) + } + }, + ) { + Scaffold( + topBar = { + TopAppBar( + title = { Text(stringResource(R.string.app_name)) }, + navigationIcon = { + IconButton(onClick = { scope.launch { drawerState.open() } }) { + Icon(Icons.Filled.Menu, contentDescription = stringResource(R.string.nav_settings)) + } + }, + ) + }, + ) { innerPadding -> + NavHost( + navController = navController, + startDestination = Destinations.READING, + modifier = Modifier.padding(innerPadding), + ) { + composable(Destinations.READING) { + ReadingScreen(onNavigateToSettings = { navigateTo(navController, Destinations.SETTINGS) }) + } + composable(Destinations.SETTINGS) { ConfigScreen() } + composable(Destinations.ABOUT) { AboutScreen() } + } + } + } +} + +private fun navigateTo(navController: NavHostController, route: String) { + navController.navigate(route) { + popUpTo(navController.graph.startDestinationId) { saveState = true } + launchSingleTop = true + restoreState = true + } +} diff --git a/android/app/src/main/java/de/ladkau/deckinadash/ui/about/AboutScreen.kt b/android/app/src/main/java/de/ladkau/deckinadash/ui/about/AboutScreen.kt new file mode 100644 index 0000000..a66b683 --- /dev/null +++ b/android/app/src/main/java/de/ladkau/deckinadash/ui/about/AboutScreen.kt @@ -0,0 +1,26 @@ +package de.ladkau.deckinadash.ui.about + +import androidx.compose.foundation.layout.fillMaxSize +import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.rememberScrollState +import androidx.compose.foundation.verticalScroll +import androidx.compose.material3.Text +import androidx.compose.runtime.Composable +import androidx.compose.ui.Modifier +import androidx.compose.ui.res.stringResource +import androidx.compose.ui.unit.dp +import de.ladkau.deckinadash.R + +/** Static Waite/Sepharial public-domain attribution, carried forward + * from the root CLAUDE.md's own "Licensing" section, since this app's + * reading screen displays that same card/narrative text verbatim. */ +@Composable +fun AboutScreen(modifier: Modifier = Modifier) { + Text( + text = stringResource(R.string.about_body), + modifier = modifier + .fillMaxSize() + .verticalScroll(rememberScrollState()) + .padding(16.dp), + ) +} diff --git a/android/app/src/main/java/de/ladkau/deckinadash/ui/config/ConfigScreen.kt b/android/app/src/main/java/de/ladkau/deckinadash/ui/config/ConfigScreen.kt new file mode 100644 index 0000000..0864c51 --- /dev/null +++ b/android/app/src/main/java/de/ladkau/deckinadash/ui/config/ConfigScreen.kt @@ -0,0 +1,258 @@ +package de.ladkau.deckinadash.ui.config + +import android.Manifest +import android.os.Build +import androidx.activity.compose.rememberLauncherForActivityResult +import androidx.activity.result.contract.ActivityResultContracts +import androidx.compose.foundation.layout.Arrangement +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.Row +import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.rememberScrollState +import androidx.compose.foundation.verticalScroll +import androidx.compose.material3.Button +import androidx.compose.material3.DatePicker +import androidx.compose.material3.DatePickerDialog +import androidx.compose.material3.DropdownMenuItem +import androidx.compose.material3.ExperimentalMaterial3Api +import androidx.compose.material3.ExposedDropdownMenuAnchorType +import androidx.compose.material3.ExposedDropdownMenuBox +import androidx.compose.material3.ExposedDropdownMenuDefaults +import androidx.compose.material3.MaterialTheme +import androidx.compose.material3.OutlinedTextField +import androidx.compose.material3.SegmentedButton +import androidx.compose.material3.SegmentedButtonDefaults +import androidx.compose.material3.SingleChoiceSegmentedButtonRow +import androidx.compose.material3.Switch +import androidx.compose.material3.Text +import androidx.compose.material3.TextButton +import androidx.compose.material3.TimePicker +import androidx.compose.material3.rememberDatePickerState +import androidx.compose.material3.rememberTimePickerState +import androidx.compose.runtime.Composable +import androidx.compose.runtime.collectAsState +import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.remember +import androidx.compose.runtime.setValue +import androidx.compose.ui.Modifier +import androidx.compose.ui.platform.LocalContext +import androidx.compose.ui.res.stringResource +import androidx.compose.ui.text.input.KeyboardType +import androidx.compose.ui.unit.dp +import androidx.compose.material3.AlertDialog +import androidx.lifecycle.viewmodel.compose.viewModel +import de.ladkau.deckinadash.R +import de.ladkau.deckinadash.data.Gender +import java.time.Instant +import java.time.ZoneOffset + +@OptIn(ExperimentalMaterial3Api::class) +@Composable +fun ConfigScreen( + modifier: Modifier = Modifier, + viewModel: ConfigViewModel = viewModel(), +) { + val config by viewModel.config.collectAsState() + val context = LocalContext.current + + var showDatePicker by remember { mutableStateOf(false) } + var showBirthTimePicker by remember { mutableStateOf(false) } + var showNotifyTimePicker by remember { mutableStateOf(false) } + + val notificationPermissionLauncher = rememberLauncherForActivityResult( + ActivityResultContracts.RequestPermission(), + ) { granted -> + viewModel.update { it.copy(notifyEnabled = granted) } + } + + Column( + modifier = modifier + .fillMaxWidth() + .verticalScroll(rememberScrollState()) + .padding(16.dp), + verticalArrangement = Arrangement.spacedBy(16.dp), + ) { + Text(stringResource(R.string.config_birth_date), style = MaterialTheme.typography.titleSmall) + Button(onClick = { showDatePicker = true }) { + val label = if (config.birthYear > 0) { + "%04d-%02d-%02d".format(config.birthYear, config.birthMonth, config.birthDay) + } else { + stringResource(R.string.config_birth_date) + } + Text(label) + } + + Text(stringResource(R.string.config_birth_time), style = MaterialTheme.typography.titleSmall) + Button(onClick = { showBirthTimePicker = true }) { + Text("%02d:%02d".format(config.birthHour, config.birthMinute)) + } + + OutlinedTextField( + value = config.utcOffsetHours.toString(), + onValueChange = { text -> + text.toDoubleOrNull()?.let { value -> viewModel.update { it.copy(utcOffsetHours = value) } } + }, + label = { Text(stringResource(R.string.config_utc_offset)) }, + keyboardOptions = androidx.compose.foundation.text.KeyboardOptions(keyboardType = KeyboardType.Number), + ) + OutlinedTextField( + value = config.latitude.toString(), + onValueChange = { text -> + text.toDoubleOrNull()?.let { value -> viewModel.update { it.copy(latitude = value) } } + }, + label = { Text(stringResource(R.string.config_latitude)) }, + keyboardOptions = androidx.compose.foundation.text.KeyboardOptions(keyboardType = KeyboardType.Number), + ) + OutlinedTextField( + value = config.longitude.toString(), + onValueChange = { text -> + text.toDoubleOrNull()?.let { value -> viewModel.update { it.copy(longitude = value) } } + }, + label = { Text(stringResource(R.string.config_longitude)) }, + keyboardOptions = androidx.compose.foundation.text.KeyboardOptions(keyboardType = KeyboardType.Number), + ) + + Text(stringResource(R.string.config_gender), style = MaterialTheme.typography.titleSmall) + val genderOptions = listOf( + Gender.UNSPECIFIED to stringResource(R.string.config_gender_unspecified), + Gender.MALE to stringResource(R.string.config_gender_male), + Gender.FEMALE to stringResource(R.string.config_gender_female), + ) + SingleChoiceSegmentedButtonRow { + genderOptions.forEachIndexed { index, (gender, label) -> + SegmentedButton( + shape = SegmentedButtonDefaults.itemShape(index = index, count = genderOptions.size), + selected = config.gender == gender, + onClick = { viewModel.update { it.copy(gender = gender) } }, + ) { Text(label) } + } + } + + LanguageDropdown( + selected = config.lang, + onSelected = { lang -> viewModel.update { it.copy(lang = lang) } }, + ) + + Row { + Text(stringResource(R.string.config_notify_enabled), style = MaterialTheme.typography.titleSmall) + Switch( + checked = config.notifyEnabled, + onCheckedChange = { enabled -> + if (enabled && Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { + notificationPermissionLauncher.launch(Manifest.permission.POST_NOTIFICATIONS) + } else { + viewModel.update { it.copy(notifyEnabled = enabled) } + } + }, + ) + } + if (config.notifyEnabled) { + Button(onClick = { showNotifyTimePicker = true }) { + Text("%02d:%02d".format(config.notifyHour, config.notifyMinute)) + } + } + + Button(onClick = { viewModel.save {} }) { + Text(stringResource(R.string.config_save)) + } + } + + if (showDatePicker) { + val initialMillis = runCatching { + java.time.LocalDate.of( + config.birthYear.takeIf { it > 0 } ?: 2000, + config.birthMonth.takeIf { it in 1..12 } ?: 1, + config.birthDay.takeIf { it in 1..31 } ?: 1, + ).atStartOfDay(ZoneOffset.UTC).toInstant().toEpochMilli() + }.getOrDefault(Instant.now().toEpochMilli()) + val datePickerState = rememberDatePickerState(initialSelectedDateMillis = initialMillis) + DatePickerDialog( + onDismissRequest = { showDatePicker = false }, + confirmButton = { + TextButton(onClick = { + datePickerState.selectedDateMillis?.let { millis -> + val date = Instant.ofEpochMilli(millis).atZone(ZoneOffset.UTC).toLocalDate() + viewModel.update { + it.copy(birthYear = date.year, birthMonth = date.monthValue, birthDay = date.dayOfMonth) + } + } + showDatePicker = false + }) { Text(stringResource(R.string.config_save)) } + }, + ) { DatePicker(state = datePickerState) } + } + + if (showBirthTimePicker) { + TimePickerDialogContent( + initialHour = config.birthHour, + initialMinute = config.birthMinute, + onDismiss = { showBirthTimePicker = false }, + onConfirm = { hour, minute -> + viewModel.update { it.copy(birthHour = hour, birthMinute = minute) } + showBirthTimePicker = false + }, + ) + } + + if (showNotifyTimePicker) { + TimePickerDialogContent( + initialHour = config.notifyHour, + initialMinute = config.notifyMinute, + onDismiss = { showNotifyTimePicker = false }, + onConfirm = { hour, minute -> + viewModel.update { it.copy(notifyHour = hour, notifyMinute = minute) } + showNotifyTimePicker = false + }, + ) + } +} + +@OptIn(ExperimentalMaterial3Api::class) +@Composable +private fun TimePickerDialogContent( + initialHour: Int, + initialMinute: Int, + onDismiss: () -> Unit, + onConfirm: (hour: Int, minute: Int) -> Unit, +) { + val state = rememberTimePickerState(initialHour = initialHour, initialMinute = initialMinute, is24Hour = true) + AlertDialog( + onDismissRequest = onDismiss, + confirmButton = { + TextButton(onClick = { onConfirm(state.hour, state.minute) }) { + Text(stringResource(R.string.config_save)) + } + }, + text = { TimePicker(state = state) }, + ) +} + +@OptIn(ExperimentalMaterial3Api::class) +@Composable +private fun LanguageDropdown(selected: String, onSelected: (String) -> Unit) { + var expanded by remember { mutableStateOf(false) } + val languages = listOf("en", "de") + ExposedDropdownMenuBox(expanded = expanded, onExpandedChange = { expanded = it }) { + OutlinedTextField( + value = selected, + onValueChange = {}, + readOnly = true, + label = { Text(stringResource(R.string.config_language)) }, + trailingIcon = { ExposedDropdownMenuDefaults.TrailingIcon(expanded = expanded) }, + modifier = Modifier.fillMaxWidth().menuAnchor(ExposedDropdownMenuAnchorType.PrimaryNotEditable), + ) + ExposedDropdownMenu(expanded = expanded, onDismissRequest = { expanded = false }) { + languages.forEach { lang -> + DropdownMenuItem( + text = { Text(lang) }, + onClick = { + onSelected(lang) + expanded = false + }, + ) + } + } + } +} diff --git a/android/app/src/main/java/de/ladkau/deckinadash/ui/config/ConfigViewModel.kt b/android/app/src/main/java/de/ladkau/deckinadash/ui/config/ConfigViewModel.kt new file mode 100644 index 0000000..5988828 --- /dev/null +++ b/android/app/src/main/java/de/ladkau/deckinadash/ui/config/ConfigViewModel.kt @@ -0,0 +1,46 @@ +package de.ladkau.deckinadash.ui.config + +import android.app.Application +import androidx.lifecycle.AndroidViewModel +import androidx.lifecycle.viewModelScope +import de.ladkau.deckinadash.data.ConfigRepository +import de.ladkau.deckinadash.data.DeckConfig +import de.ladkau.deckinadash.notify.NotificationScheduler +import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.flow.StateFlow +import kotlinx.coroutines.flow.asStateFlow +import kotlinx.coroutines.flow.first +import kotlinx.coroutines.launch + +class ConfigViewModel(application: Application) : AndroidViewModel(application) { + private val configRepository = ConfigRepository(application) + + // A local editable draft, seeded once from the persisted config below - + // deliberately not a live collection of configRepository.config, so + // in-progress edits aren't clobbered by DataStore's own emission of the + // write this same screen is about to make on save(). + private val _config = MutableStateFlow(DeckConfig()) + val config: StateFlow = _config.asStateFlow() + + init { + viewModelScope.launch { + _config.value = configRepository.config.first() + } + } + + fun update(transform: (DeckConfig) -> DeckConfig) { + _config.value = transform(_config.value) + } + + /** Persists the current draft and reschedules the daily notification - + * the other of NotificationScheduler.reschedule()'s two required call + * sites (see its own doc comment; the first is app launch). */ + fun save(onSaved: () -> Unit) { + viewModelScope.launch { + val toSave = _config.value.copy(configured = true) + configRepository.save(toSave) + NotificationScheduler.reschedule(getApplication(), toSave) + onSaved() + } + } +} diff --git a/android/app/src/main/java/de/ladkau/deckinadash/ui/reading/ReadingScreen.kt b/android/app/src/main/java/de/ladkau/deckinadash/ui/reading/ReadingScreen.kt new file mode 100644 index 0000000..ccbd78a --- /dev/null +++ b/android/app/src/main/java/de/ladkau/deckinadash/ui/reading/ReadingScreen.kt @@ -0,0 +1,156 @@ +package de.ladkau.deckinadash.ui.reading + +import androidx.compose.foundation.Image +import androidx.compose.foundation.layout.Arrangement +import androidx.compose.foundation.layout.Box +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.fillMaxSize +import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.layout.width +import androidx.compose.foundation.layout.wrapContentSize +import androidx.compose.foundation.lazy.LazyColumn +import androidx.compose.foundation.lazy.items +import androidx.compose.material3.Button +import androidx.compose.material3.CircularProgressIndicator +import androidx.compose.material3.HorizontalDivider +import androidx.compose.material3.MaterialTheme +import androidx.compose.material3.Text +import androidx.compose.runtime.Composable +import androidx.compose.runtime.collectAsState +import androidx.compose.runtime.getValue +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.graphics.graphicsLayer +import androidx.compose.ui.platform.LocalContext +import androidx.compose.ui.res.painterResource +import androidx.compose.ui.res.stringResource +import androidx.compose.ui.unit.dp +import androidx.lifecycle.viewmodel.compose.viewModel +import de.ladkau.deckinadash.R +import de.ladkau.deckinadash.data.ReadingDto +import de.ladkau.deckinadash.data.SpreadPositionDto +import de.ladkau.deckinadash.data.TransitDto +import de.ladkau.deckinadash.ui.cardDrawableRes + +@Composable +fun ReadingScreen( + onNavigateToSettings: () -> Unit, + modifier: Modifier = Modifier, + viewModel: ReadingViewModel = viewModel(), +) { + val uiState by viewModel.uiState.collectAsState() + + Box(modifier = modifier.fillMaxSize()) { + when (val state = uiState) { + is ReadingUiState.Loading -> CenteredMessage(stringResource(R.string.reading_loading)) + is ReadingUiState.NotConfigured -> Column( + modifier = Modifier.wrapContentSize(Alignment.Center).padding(24.dp), + ) { + Text(stringResource(R.string.reading_not_configured)) + Button(onClick = onNavigateToSettings, modifier = Modifier.padding(top = 16.dp)) { + Text(stringResource(R.string.nav_settings)) + } + } + is ReadingUiState.Error -> CenteredMessage(stringResource(R.string.reading_error)) + is ReadingUiState.Content -> ReadingContent(state.reading) + } + } +} + +@Composable +private fun CenteredMessage(text: String) { + Column( + modifier = Modifier.fillMaxSize(), + verticalArrangement = Arrangement.Center, + horizontalAlignment = Alignment.CenterHorizontally, + ) { + if (text == stringResource(R.string.reading_loading)) { + CircularProgressIndicator(modifier = Modifier.padding(bottom = 16.dp)) + } + Text(text) + } +} + +/** Single scrollable page, same content order as + * watch/src/c/ui_report_window.c's build_content(): date, day + * significance, top transits+narratives, full Celtic Cross spread, + * guidance last. */ +@Composable +private fun ReadingContent(reading: ReadingDto) { + LazyColumn( + modifier = Modifier.fillMaxSize().padding(16.dp), + verticalArrangement = Arrangement.spacedBy(12.dp), + ) { + item { + Text(reading.date, style = MaterialTheme.typography.labelLarge) + Text( + reading.daySignificance.levelLabel, + style = MaterialTheme.typography.headlineSmall, + ) + } + + if (reading.transits.isNotEmpty()) { + item { + Text( + stringResource(R.string.reading_transits_heading), + style = MaterialTheme.typography.titleMedium, + ) + } + items(reading.transits) { transit -> TransitItem(transit) } + } + + item { HorizontalDivider() } + + item { + Text( + stringResource(R.string.reading_spread_heading), + style = MaterialTheme.typography.titleMedium, + ) + } + items(reading.spread) { position -> SpreadItem(position) } + + item { HorizontalDivider() } + + item { + Text( + stringResource(R.string.reading_guidance_heading), + style = MaterialTheme.typography.titleMedium, + ) + Text(reading.guidance, style = MaterialTheme.typography.bodyMedium) + } + } +} + +@Composable +private fun TransitItem(transit: TransitDto) { + Column { + Text(transit.title, style = MaterialTheme.typography.bodyLarge) + if (transit.narrative.isNotBlank()) { + Text(transit.narrative, style = MaterialTheme.typography.bodyMedium) + } + } +} + +@Composable +private fun SpreadItem(position: SpreadPositionDto) { + val context = LocalContext.current + Column { + Text(position.positionName, style = MaterialTheme.typography.titleSmall) + Image( + painter = painterResource(cardDrawableRes(context, position.cardSlug)), + contentDescription = position.cardName, + modifier = Modifier + .width(160.dp) + .padding(vertical = 4.dp) + .graphicsLayer { rotationZ = if (position.reversed) 180f else 0f }, + ) + val cardTitle = if (position.reversed) { + "${position.cardName} ${stringResource(R.string.reading_reversed)}" + } else { + position.cardName + } + Text(cardTitle, style = MaterialTheme.typography.bodyLarge) + Text(position.positionDescription, style = MaterialTheme.typography.bodySmall) + Text(position.cardMeaning, style = MaterialTheme.typography.bodyMedium) + } +} diff --git a/android/app/src/main/java/de/ladkau/deckinadash/ui/reading/ReadingViewModel.kt b/android/app/src/main/java/de/ladkau/deckinadash/ui/reading/ReadingViewModel.kt new file mode 100644 index 0000000..9e103f4 --- /dev/null +++ b/android/app/src/main/java/de/ladkau/deckinadash/ui/reading/ReadingViewModel.kt @@ -0,0 +1,58 @@ +package de.ladkau.deckinadash.ui.reading + +import android.app.Application +import androidx.lifecycle.AndroidViewModel +import androidx.lifecycle.viewModelScope +import de.ladkau.deckinadash.data.ConfigRepository +import de.ladkau.deckinadash.data.ReadingDto +import de.ladkau.deckinadash.data.ReadingRepository +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.flow.StateFlow +import kotlinx.coroutines.flow.asStateFlow +import kotlinx.coroutines.flow.collectLatest +import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext +import java.time.Instant + +sealed interface ReadingUiState { + data object Loading : ReadingUiState + data object NotConfigured : ReadingUiState + data class Content(val reading: ReadingDto) : ReadingUiState + data object Error : ReadingUiState +} + +/** + * Recomputes today's reading whenever the persisted config changes + * (including the very first emission) - the Compose analog of + * watch/src/c/reading_state.c's reading_state_recompute() own doc + * comment ("safe to call again later... overwrites the previous result + * in place"), just reactive instead of imperatively re-invoked. + */ +class ReadingViewModel(application: Application) : AndroidViewModel(application) { + private val configRepository = ConfigRepository(application) + private val readingRepository = ReadingRepository(application) + + private val _uiState = MutableStateFlow(ReadingUiState.Loading) + val uiState: StateFlow = _uiState.asStateFlow() + + init { + viewModelScope.launch { + configRepository.config.collectLatest { config -> + if (!config.configured) { + _uiState.value = ReadingUiState.NotConfigured + return@collectLatest + } + _uiState.value = ReadingUiState.Loading + _uiState.value = try { + val reading = withContext(Dispatchers.Default) { + readingRepository.compute(config, Instant.now().epochSecond) + } + ReadingUiState.Content(reading) + } catch (t: Throwable) { + ReadingUiState.Error + } + } + } + } +} diff --git a/android/app/src/main/java/de/ladkau/deckinadash/ui/theme/Theme.kt b/android/app/src/main/java/de/ladkau/deckinadash/ui/theme/Theme.kt new file mode 100644 index 0000000..4bd8b95 --- /dev/null +++ b/android/app/src/main/java/de/ladkau/deckinadash/ui/theme/Theme.kt @@ -0,0 +1,30 @@ +package de.ladkau.deckinadash.ui.theme + +import android.os.Build +import androidx.compose.foundation.isSystemInDarkTheme +import androidx.compose.material3.MaterialTheme +import androidx.compose.material3.dynamicDarkColorScheme +import androidx.compose.material3.dynamicLightColorScheme +import androidx.compose.material3.darkColorScheme +import androidx.compose.material3.lightColorScheme +import androidx.compose.runtime.Composable +import androidx.compose.ui.platform.LocalContext + +private val LightColors = lightColorScheme() +private val DarkColors = darkColorScheme() + +@Composable +fun DeckInADashTheme( + darkTheme: Boolean = isSystemInDarkTheme(), + dynamicColor: Boolean = true, + content: @Composable () -> Unit, +) { + val context = LocalContext.current + val colorScheme = when { + dynamicColor && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S -> + if (darkTheme) dynamicDarkColorScheme(context) else dynamicLightColorScheme(context) + darkTheme -> DarkColors + else -> LightColors + } + MaterialTheme(colorScheme = colorScheme, content = content) +} diff --git a/android/app/src/main/jni/CMakeLists.txt b/android/app/src/main/jni/CMakeLists.txt new file mode 100644 index 0000000..f9b9383 --- /dev/null +++ b/android/app/src/main/jni/CMakeLists.txt @@ -0,0 +1,34 @@ +cmake_minimum_required(VERSION 3.22.1) +project(deckengine C) + +set(CMAKE_C_STANDARD 99) +set(CMAKE_C_STANDARD_REQUIRED ON) + +# Repo root, three levels up from android/app/src/main/jni/. +get_filename_component(REPO_ROOT "${CMAKE_CURRENT_SOURCE_DIR}/../../../../.." ABSOLUTE) + +# Same engine + interpreter source files the watch app links (see +# watch/wscript's ENGINE_SRCS/INTERP_SRCS and its own comment on why - +# except astro.c is paired with the real, full-precision +# engine/third_party/astronomy/astronomy.c here, not the watch's +# lowprec_ephemeris.c substitute: that substitute exists solely for +# Pebble's 64KB code+data+bss budget, which Android has no equivalent +# of. json.c/reading_io.c are not linked - like the watch, this is one +# process computing and rendering its own reading, no JSON pipe between +# separate binaries. +add_library(deckengine SHARED + deck_jni.c + reading_json.c + ${REPO_ROOT}/engine/src/rng.c + ${REPO_ROOT}/engine/src/tarot.c + ${REPO_ROOT}/engine/src/tarot_data.c + ${REPO_ROOT}/engine/src/astro.c + ${REPO_ROOT}/engine/src/i18n.c + ${REPO_ROOT}/engine/src/reading.c + ${REPO_ROOT}/engine/third_party/astronomy/astronomy.c + ${REPO_ROOT}/interpreter/src/significance.c + ${REPO_ROOT}/interpreter/src/narrative.c + ${REPO_ROOT}/interpreter/src/guidance.c +) + +target_link_libraries(deckengine m) diff --git a/android/app/src/main/jni/deck_jni.c b/android/app/src/main/jni/deck_jni.c new file mode 100644 index 0000000..e4cfccf --- /dev/null +++ b/android/app/src/main/jni/deck_jni.c @@ -0,0 +1,45 @@ +/* Thin JNIEXPORT shim over reading_json.c - keeps all real logic (and + * every engine/interpreter header include) in plain, JNI-free C, same + * separation watch/src/c/notify.c keeps from reading_state.c. */ +#include +#include + +#include "reading_json.h" + +JNIEXPORT jstring JNICALL +Java_de_ladkau_deckinadash_nativebridge_DeckNative_generateReadingJson( + JNIEnv *env, jobject thiz, + jint birth_year, jint birth_month, jint birth_day, + jint birth_hour, jint birth_minute, jdouble utc_offset_hours, + jdouble latitude, jdouble longitude, + jint gender, jlong utc_moment_epoch_seconds, + jstring i18n_dir, jstring lang) { + (void)thiz; + + BirthData birth = { + .year = birth_year, .month = birth_month, .day = birth_day, + .hour = birth_hour, .minute = birth_minute, + .utc_offset_hours = utc_offset_hours, + .latitude = latitude, .longitude = longitude, + }; + + const char *i18n_dir_chars = (*env)->GetStringUTFChars(env, i18n_dir, NULL); + const char *lang_chars = (*env)->GetStringUTFChars(env, lang, NULL); + + char *json = NULL; + int rc = reading_json_generate(&birth, (Gender)gender, (time_t)utc_moment_epoch_seconds, + i18n_dir_chars, lang_chars, &json); + + (*env)->ReleaseStringUTFChars(env, i18n_dir, i18n_dir_chars); + (*env)->ReleaseStringUTFChars(env, lang, lang_chars); + + if (rc != 0) { + jclass exc = (*env)->FindClass(env, "java/lang/IllegalStateException"); + (*env)->ThrowNew(env, exc, "reading_json_generate failed"); + return NULL; + } + + jstring result = (*env)->NewStringUTF(env, json); + free(json); + return result; +} diff --git a/android/app/src/main/jni/reading_json.c b/android/app/src/main/jni/reading_json.c new file mode 100644 index 0000000..9ba4f3f --- /dev/null +++ b/android/app/src/main/jni/reading_json.c @@ -0,0 +1,198 @@ +/* open_memstream() is POSIX (2008), not C99 - needs this feature-test + * macro under -std=c99 (glibc and Android's bionic both support it from + * a plain #define, no libc version gate below Android API 23, well + * under this app's minSdk 24). */ +#define _POSIX_C_SOURCE 200809L + +#include "reading_json.h" + +#include +#include +#include + +#include "../../../../../engine/src/i18n.h" +#include "../../../../../engine/src/reading.h" +#include "../../../../../interpreter/src/guidance.h" +#include "../../../../../interpreter/src/narrative.h" +#include "../../../../../interpreter/src/significance.h" + +/* Same convention as engine/src/main.c's own default_i18n_path() - + * duplicated rather than shared, same reasoning CLAUDE.md gives for why + * engine/src/main.c and interpreter/src/main.c each keep their own copy + * (the binaries - here, shared libraries - are never linked together). */ +static void i18n_path(const char *dir, const char *lang, char *out, size_t out_size) { + snprintf(out, out_size, "%s/%s.lang", dir, lang); +} + +static const char *const k_day_level_slug[DAY_SIGNIFICANCE_COUNT] = { + "quiet", "notable", "significant", "major", +}; + +static const char *day_level_name(DaySignificance level) { + switch (level) { + case DAY_QUIET: return i18n_get("interp.day_level.quiet", "Quiet"); + case DAY_NOTABLE: return i18n_get("interp.day_level.notable", "Notable"); + case DAY_SIGNIFICANT: return i18n_get("interp.day_level.significant", "Significant"); + case DAY_MAJOR: return i18n_get("interp.day_level.major", "Major"); + default: return "Unknown"; + } +} + +/* "Transiting Saturn Square natal Sun" - the same title text + * interpreter/src/main.c's --format text prints per transit item, built + * here (rather than left to Kotlin) since it's fully localized via + * astro_body_name()/astro_aspect_name() - Kotlin never needs its own + * planet/aspect display-name table. */ +static char *build_transit_title(const Aspect *a) { + char buf[160]; + snprintf(buf, sizeof buf, "%s %s %s %s %s", i18n_get("ui.transiting", "Transiting"), + astro_body_name(a->transiting_planet), astro_aspect_name(a->type), + i18n_get("ui.natal", "natal"), astro_body_name(a->natal_planet)); + size_t len = strlen(buf); + char *out = malloc(len + 1); + memcpy(out, buf, len + 1); + return out; +} + +static char *capture_narrative(const Aspect *aspect, int house) { + char stack_buf[NARRATIVE_TEXT_MAX]; + narrative_write(stack_buf, sizeof stack_buf, "", aspect, house); + size_t len = strlen(stack_buf); + if (len > 0 && stack_buf[len - 1] == '\n') stack_buf[--len] = '\0'; + char *buf = malloc(len + 1); + memcpy(buf, stack_buf, len + 1); + return buf; +} + +static char *capture_guidance(const DailyInterpretation *interp, const CelticCrossSpread *spread) { + char stack_buf[GUIDANCE_TEXT_MAX]; + guidance_write(stack_buf, sizeof stack_buf, "", interp, spread); + size_t len = strlen(stack_buf); + if (len > 0 && stack_buf[len - 1] == '\n') stack_buf[--len] = '\0'; + char *buf = malloc(len + 1); + memcpy(buf, stack_buf, len + 1); + return buf; +} + +static void json_string(FILE *out, const char *s) { + fputc('"', out); + for (; *s; s++) { + unsigned char c = (unsigned char)*s; + switch (c) { + case '"': fputs("\\\"", out); break; + case '\\': fputs("\\\\", out); break; + case '\n': fputs("\\n", out); break; + case '\r': fputs("\\r", out); break; + case '\t': fputs("\\t", out); break; + default: + if (c < 0x20) fprintf(out, "\\u%04x", c); + else fputc((char)c, out); + } + } + fputc('"', out); +} + +static void format_date(time_t utc_moment, char *out, size_t out_size) { + struct tm *utc = gmtime(&utc_moment); + snprintf(out, out_size, "%04d-%02d-%02d", utc->tm_year + 1900, utc->tm_mon + 1, utc->tm_mday); +} + +int reading_json_generate(const BirthData *birth, Gender gender, time_t utc_moment, + const char *i18n_dir, const char *lang, char **out_json) { + char path[512]; + i18n_path(i18n_dir, lang, path, sizeof path); + i18n_load(path); /* non-fatal on failure - falls back to built-in English, same as engine/src/main.c */ + + /* The tarot seed is today's UTC date, same convention + * dist/run-engine.sh and watch/src/c/reading_state.c use. */ + char seed[16]; + format_date(utc_moment, seed, sizeof seed); + + DailyReading reading; + reading_generate(seed, birth, gender, utc_moment, &reading); + + DailyInterpretation interp; + interpret_daily_reading(&reading, &interp); + + size_t json_len = 0; + char *json_buf = NULL; + FILE *out = open_memstream(&json_buf, &json_len); + if (!out) return 1; + + fprintf(out, "{\n"); + + char date_buf[32]; + format_date(reading.utc_moment, date_buf, sizeof date_buf); + fprintf(out, " \"date\": "); + json_string(out, date_buf); + fprintf(out, ",\n \"gender\": "); + json_string(out, tarot_gender_slug(reading.gender)); + fprintf(out, ",\n"); + + fprintf(out, " \"day_significance\": {\n"); + fprintf(out, " \"level_slug\": \"%s\",\n", k_day_level_slug[interp.day_level]); + fprintf(out, " \"level_label\": "); + json_string(out, day_level_name(interp.day_level)); + fprintf(out, ",\n \"rank\": %d,\n \"count\": %d,\n \"deserves_framing\": %s\n", + interp.day_level + 1, DAY_SIGNIFICANCE_COUNT, + interpretation_deserves_framing(&interp) ? "true" : "false"); + fprintf(out, " },\n"); + + fprintf(out, " \"transits\": [\n"); + for (int i = 0; i < interp.top_item_count; i++) { + const SignificantItem *item = &interp.top_items[i]; + const Aspect *a = &item->aspect; + const PlanetPosition *tp = &reading.transits.bodies[a->transiting_planet]; + char *title = build_transit_title(a); + char *narrative = capture_narrative(a, tp->house); + + fprintf(out, " {\n"); + fprintf(out, " \"title\": "); + json_string(out, title); + fprintf(out, ",\n \"transiting_planet_slug\": \"%s\",\n", astro_body_slug(a->transiting_planet)); + fprintf(out, " \"natal_planet_slug\": \"%s\",\n", astro_body_slug(a->natal_planet)); + fprintf(out, " \"aspect_slug\": \"%s\",\n", astro_aspect_slug(a->type)); + fprintf(out, " \"orb\": %.4f,\n", a->orb); + fprintf(out, " \"score\": %.4f,\n", item->score); + fprintf(out, " \"narrative\": "); + json_string(out, narrative); + fprintf(out, "\n }%s\n", i + 1 < interp.top_item_count ? "," : ""); + + free(title); + free(narrative); + } + fprintf(out, " ],\n"); + + char *guidance = capture_guidance(&interp, &reading.spread); + fprintf(out, " \"guidance\": "); + json_string(out, guidance); + fprintf(out, ",\n"); + free(guidance); + + fprintf(out, " \"spread\": [\n"); + for (int i = 0; i < TAROT_SPREAD_SIZE; i++) { + CelticCrossPosition position = (CelticCrossPosition)i; + const TarotDraw *draw = &reading.spread.positions[i]; + + fprintf(out, " {\n"); + fprintf(out, " \"position_slug\": \"%s\",\n", tarot_position_slug(position)); + fprintf(out, " \"position_name\": "); + json_string(out, tarot_position_name(position, reading.gender)); + fprintf(out, ",\n \"position_description\": "); + json_string(out, tarot_position_description(position, reading.gender)); + fprintf(out, ",\n \"card_slug\": \"%s\",\n", tarot_card_slug(draw->card)); + fprintf(out, " \"card_name\": "); + json_string(out, tarot_card_name(draw->card)); + fprintf(out, ",\n \"reversed\": %s,\n", draw->reversed ? "true" : "false"); + fprintf(out, " \"card_meaning\": "); + json_string(out, tarot_card_meaning(draw->card, draw->reversed)); + fprintf(out, "\n }%s\n", i + 1 < TAROT_SPREAD_SIZE ? "," : ""); + } + fprintf(out, " ]\n"); + + fprintf(out, "}\n"); + fclose(out); + + *out_json = json_buf; + return 0; +} diff --git a/android/app/src/main/jni/reading_json.h b/android/app/src/main/jni/reading_json.h new file mode 100644 index 0000000..0042d23 --- /dev/null +++ b/android/app/src/main/jni/reading_json.h @@ -0,0 +1,32 @@ +#ifndef DECKINADASH_READING_JSON_H +#define DECKINADASH_READING_JSON_H + +#include + +#include "../../../../../engine/src/astro.h" +#include "../../../../../engine/src/tarot.h" + +/* Recomputes a full day's reading (astro + tarot + interpretation) from + * `birth`/`gender`/`utc_moment` - same inputs and same + * reading_generate()/interpret_daily_reading() calls + * watch/src/c/reading_state.c makes - and serializes everything a UI + * needs to render it (in watch/src/c/ui_report_window.c's own section + * order: date, day significance, top transits+narratives, full Celtic + * Cross spread, guidance last) as one JSON string, structurally a port + * of interpreter/src/main.c's interp_print_json() targeting a heap + * buffer instead of stdout. + * + * `i18n_dir`/`lang` locate `/.lang` (same convention as + * engine/src/main.c's default_i18n_path()); a missing/unreadable file is + * a non-fatal fallback to built-in English (i18n_load()'s own contract), + * not an error returned here. + * + * On success, returns a malloc'd, null-terminated JSON string in + * `*out_json` (caller must free()) and returns 0. Returns non-zero and + * leaves `*out_json` untouched only if the underlying open_memstream() + * allocation itself fails - not a condition this codebase's other + * callers (deck-engine, interpreter-cli) treat as reachable in practice. */ +int reading_json_generate(const BirthData *birth, Gender gender, time_t utc_moment, + const char *i18n_dir, const char *lang, char **out_json); + +#endif diff --git a/android/app/src/main/res/drawable/ic_notification.xml b/android/app/src/main/res/drawable/ic_notification.xml new file mode 100644 index 0000000..0414f74 --- /dev/null +++ b/android/app/src/main/res/drawable/ic_notification.xml @@ -0,0 +1,17 @@ + + + + + + diff --git a/android/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml b/android/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml new file mode 100644 index 0000000..be43858 --- /dev/null +++ b/android/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/android/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml b/android/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml new file mode 100644 index 0000000..be43858 --- /dev/null +++ b/android/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/android/app/src/main/res/values/colors.xml b/android/app/src/main/res/values/colors.xml new file mode 100644 index 0000000..0b206c1 --- /dev/null +++ b/android/app/src/main/res/values/colors.xml @@ -0,0 +1,5 @@ + + + #1B1B2F + #6750A4 + diff --git a/android/app/src/main/res/values/strings.xml b/android/app/src/main/res/values/strings.xml new file mode 100644 index 0000000..b5dc786 --- /dev/null +++ b/android/app/src/main/res/values/strings.xml @@ -0,0 +1,37 @@ + + + Deck in a Dash + + Today\'s Reading + Settings + About + + Daily reading + A daily notification with your tarot and astrology reading + Today\'s reading: %1$s + + Settings + Birth date + Birth time + UTC offset (hours) + Latitude + Longitude + Reading for + Unspecified + Male + Female + Language + Daily notification + Notification time + Save + + Set your birth details in Settings to see today\'s reading. + Computing today\'s reading… + Could not compute today\'s reading. + Guidance + Significant transits + Celtic Cross + (Reversed) + + Deck in a Dash shows a daily personalized tarot (Celtic Cross) and astrology (natal chart + transits) reading.\n\nCard and spread text is condensed from A. E. Waite\'s The Pictorial Key to the Tarot (1911, public domain). Transit narrative text is condensed from Sepharial\'s Transits and Planetary Periods (1920, public domain). + diff --git a/android/app/src/main/res/values/themes.xml b/android/app/src/main/res/values/themes.xml new file mode 100644 index 0000000..5146f46 --- /dev/null +++ b/android/app/src/main/res/values/themes.xml @@ -0,0 +1,4 @@ + + +