Skip to content

About

Live NUS shuttle tracking and trip planning. Flutter app with a caching Spring Boot backend.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

30 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

NUS Motion

A Flutter app for catching NUS shuttle buses: every active bus on one map, live arrivals for nearby stops, and a trip planner that handles transfers. A small Spring Boot backend sits between the app and NUS's data feeds.

Route planner Alerts
Suggested routes to University Town Alerts tab with weather card and announcements

How it works

flowchart LR
  UI["Flutter screens"] --> RP["Riverpod providers"]
  RP --> HV[("Hive: saved stops, recents, settings")]
  RP -- "HTTPS, polled" --> CADDY["Caddy (auto-TLS)"]
  CADDY --> API["Spring Boot /api/*"]
  API <--> CACHE[("Caffeine caches")]
  API --> NB["NUS NextBus API"]
  API --> DT["NUS buildings API"]
  API --> OM["Open-Meteo"]
Loading

The app never talks to NUS directly. It polls one Spring Boot 4 / Java 21 service, which holds the NextBus credential, cleans up the data and caches it: 5 s for arrivals and bus positions, minutes to a day for stops, buildings and route geometry. Saved stops, recent searches and settings stay on the device in Hive.

Interesting bits

  • The upstream API is awkward. NextBus needs a credential that can't ship in an app, returns JSON labelled text/html, and uses inconsistent PascalCase fields. WebClientConfig adds the auth header plus an interceptor that rewrites the content type so Jackson can parse it, and CacheConfig sets up about 15 Caffeine caches with their own TTLs. However many clients are polling, the backend makes at most one upstream call per stop or route per TTL window. Broken upstream endpoints return a structured 502 instead of a raw 500.
  • NUS publishes arrivals, not journeys, so RoutingService.java builds them. It takes up to five stops within 800 m of each end, builds a route graph from every service's pickup points (fetched in parallel on Java 21 virtual threads), and enumerates direct and one-transfer paths that move forward on each route. Each path costs walk + wait + ride plus a 3-minute transfer penalty. The wait uses live ETAs offset by when you'll actually reach the stop, so if you'd miss the next bus it uses the one after. The five fastest come back as WALK / WAIT / BUS legs with coordinates.
  • Native Google Maps markers jump on every poll, and regenerating bitmaps every 5 s is expensive. Buses are Flutter widgets (BusOverlayMarker) laid over the map and projected to screen pixels with Web Mercator, corrected for the bottom sheet's padding. AnimatedPositioned eases them over 500 ms between polls but snaps when the camera moves, so they don't lag behind a pan. Planned bus legs are sliced out of the real route polyline (clipCheckpointSegment) instead of drawn as straight lines.
  • Nothing polls in the background. Screens refresh with ref.invalidate on a timer (5 s on the map and Saved tabs, 15 s on Alerts), and didChangeAppLifecycleState cancels the timer and the GPS stream when the app is paused or hidden.
  • It runs on a free-tier VPS with 1 GB of RAM: the fat JAR uses -Xmx400m and SerialGC under a hardened systemd unit (MemoryMax=600M), with Caddy doing Let's Encrypt TLS. Setup is in docs/deploy-oracle-cloud.md.

Running it

Backend (JDK 21):

cd backend
cp .env.example .env                    # fill in NUS_NEXTBUS_AUTH; .env is gitignored
set -a; source .env; set +a
export SERVER_TOMCAT_BASEDIR=/tmp/nusmotion   # default is the production path
./mvnw spring-boot:run                  # http://localhost:8080/api/stops
./mvnw test                             # no credential needed

curl "http://localhost:8080/api/shuttles?stop=UTOWN"
curl "http://localhost:8080/api/route?from=PGP&to=UTown"

NUS_NEXTBUS_AUTH is the full Authorization header value, e.g. "Basic <base64 credentials>". Anything else in application.properties can be overridden with an env var (e.g. NUS_API_BASE_URL).

App (Flutter, Dart ≥ 3.11, plus Xcode and CocoaPods for iOS):

cd frontend
cp secrets.properties.example secrets.properties   # add your GOOGLE_MAPS_API_KEY; gitignored
flutter pub get
(cd ios && pod install)    # iOS only; also generates ios/Flutter/Secrets.xcconfig
flutter run

The app builds without a Maps key, but the map won't render. It calls the hosted API by default. For a local backend, set ApiConstants.baseUrl in lib/core/constants/api_constants.dart to http://10.0.2.2:8080/api (Android emulator) or http://localhost:8080/api (iOS simulator).

Stack: Flutter, Riverpod, go_router, Dio, google_maps_flutter, Hive, Spring Boot 4, Java 21, Caffeine, Caddy, systemd.

The backend is deployed and the app builds for Android and iOS. Turn-by-turn navigation is scaffolded (BusLegState) but switched off for now.

Not affiliated with or endorsed by NUS.

About

Live NUS shuttle tracking and trip planning. Flutter app with a caching Spring Boot backend.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages