new changes maps
This commit is contained in:
100
.gitignore
vendored
100
.gitignore
vendored
@@ -1,49 +1,51 @@
|
||||
# Miscellaneous
|
||||
*.class
|
||||
*.log
|
||||
*.pyc
|
||||
*.swp
|
||||
.DS_Store
|
||||
.atom/
|
||||
.build/
|
||||
.buildlog/
|
||||
.history
|
||||
.svn/
|
||||
.swiftpm/
|
||||
migrate_working_dir/
|
||||
|
||||
# IntelliJ related
|
||||
*.iml
|
||||
*.ipr
|
||||
*.iws
|
||||
.idea/
|
||||
|
||||
# The .vscode folder contains launch configuration and tasks you configure in
|
||||
# VS Code which you may wish to be included in version control, so this line
|
||||
# is commented out by default.
|
||||
#.vscode/
|
||||
|
||||
# Flutter/Dart/Pub related
|
||||
**/doc/api/
|
||||
**/ios/Flutter/.last_build_id
|
||||
.dart_tool/
|
||||
.flutter-plugins-dependencies
|
||||
.pub-cache/
|
||||
.pub/
|
||||
/build/
|
||||
/coverage/
|
||||
|
||||
# Symbolication related
|
||||
app.*.symbols
|
||||
|
||||
# Obfuscation related
|
||||
app.*.map.json
|
||||
|
||||
# Android Studio will place build artifacts here
|
||||
/android/app/debug
|
||||
/android/app/profile
|
||||
/android/app/release
|
||||
|
||||
# Claude AI settings
|
||||
.claude/
|
||||
|
||||
# Miscellaneous
|
||||
*.class
|
||||
*.log
|
||||
*.pyc
|
||||
*.swp
|
||||
.DS_Store
|
||||
.atom/
|
||||
.build/
|
||||
.buildlog/
|
||||
.history
|
||||
.svn/
|
||||
.swiftpm/
|
||||
migrate_working_dir/
|
||||
|
||||
# IntelliJ related
|
||||
*.iml
|
||||
*.ipr
|
||||
*.iws
|
||||
.idea/
|
||||
|
||||
# The .vscode folder contains launch configuration and tasks you configure in
|
||||
# VS Code which you may wish to be included in version control, so this line
|
||||
# is commented out by default.
|
||||
#.vscode/
|
||||
|
||||
# Flutter/Dart/Pub related
|
||||
**/doc/api/
|
||||
**/ios/Flutter/.last_build_id
|
||||
.dart_tool/
|
||||
.flutter-plugins-dependencies
|
||||
.pub-cache/
|
||||
.pub/
|
||||
/build/
|
||||
/coverage/
|
||||
|
||||
# Symbolication related
|
||||
app.*.symbols
|
||||
|
||||
# Obfuscation related
|
||||
app.*.map.json
|
||||
|
||||
# Android Studio will place build artifacts here
|
||||
/android/app/debug
|
||||
/android/app/profile
|
||||
/android/app/release
|
||||
|
||||
# Claude AI settings
|
||||
.claude/
|
||||
|
||||
# Self-hosted OSRM graph data (large, regenerated from OSM)
|
||||
/deploy/osrm/data/
|
||||
|
||||
451
API_ENDPOINTS.md
Normal file
451
API_ENDPOINTS.md
Normal file
@@ -0,0 +1,451 @@
|
||||
# Nearle Rider — API Reference
|
||||
|
||||
Generated from a source sweep of `lib/`. Every endpoint the app talks to, with
|
||||
request payloads for all write operations.
|
||||
|
||||
## Base hosts
|
||||
|
||||
| Host | Used for | Notes |
|
||||
|---|---|---|
|
||||
| `jupiter.nearle.app` | all **reads** | env segment from `ApiConstants.mainRoute` (`live`) / `mainDev` (`dev`) |
|
||||
| `queue.workolik.com` | all **writes** (status updates + logs) | hardcoded `live`, DNS pinned to `66.116.225.226`, TLS cert validation bypassed (`lib/providers/deliverylog/deliverylog_provider.dart:20`) |
|
||||
| `msg.lionsms.com` | OTP SMS | |
|
||||
| `maps.googleapis.com` | route distance | |
|
||||
| `sgp1.digitaloceanspaces.com` → `images.nearle.app` | proof / ticket images (Minio SDK, not REST) | |
|
||||
| `66.116.225.226:1883` | MQTT telemetry | |
|
||||
|
||||
**No auth header on any call.** Every request sends only
|
||||
`Content-Type: application/json` / `Accept: application/json`. Identity is
|
||||
carried as a `userid` field in the query string or body.
|
||||
|
||||
Endpoint constants live in `lib/views/helpers/constants/apiconstants.dart`.
|
||||
|
||||
---
|
||||
|
||||
## 1. Auth
|
||||
|
||||
### POST `https://jupiter.nearle.app/live/api/v2/users/rider/login`
|
||||
`lib/providers/auth/auth_provider.dart:19`
|
||||
|
||||
```json
|
||||
{
|
||||
"contactno": "9876543210",
|
||||
"devicetype": "android",
|
||||
"configid": 1,
|
||||
"deviceid": "<device id>",
|
||||
"userfcmtoken": "<fcm token>",
|
||||
"pin": 1234
|
||||
}
|
||||
```
|
||||
`pin` is only included when non-null.
|
||||
|
||||
Response `details` is persisted to SharedPreferences: `userid`, `shiftid`,
|
||||
`logid`, `riderid`, `partnerid`, `configid`, `logseconds`, `locationid`,
|
||||
`tenantid`, `applocationid`, `userfcmtoken`, `firstname`, `lastname`,
|
||||
`username`, `onduty`, `fuelcharge`, `firstmilecharge`, `starttime`, `endtime`,
|
||||
`deliveryradius` (default 100 m).
|
||||
|
||||
### PUT `https://jupiter.nearle.app/live/api/v2/users/update`
|
||||
Set / change MPIN — `lib/providers/auth/auth_provider.dart:186`
|
||||
|
||||
```json
|
||||
{ "userid": 1242, "pin": 1234 }
|
||||
```
|
||||
|
||||
### GET `https://jupiter.nearle.app/live/api/v1/platform/getsmsprovider?templatetypeid=1`
|
||||
Fetches `templateid` + SMS `content` template. Cached 1 hour.
|
||||
`lib/controllers/auth.dart:367`
|
||||
|
||||
### GET `https://msg.lionsms.com/api/smsapi`
|
||||
`lib/controllers/auth.dart:407`
|
||||
|
||||
| Param | Value |
|
||||
|---|---|
|
||||
| `key` | `e57f5c9679af26077be1a7eadabb1b2a` |
|
||||
| `route` | `7` |
|
||||
| `sender` | `NEARLE` |
|
||||
| `number` | `+91<phone>` |
|
||||
| `templateid` | from `getsmsprovider` |
|
||||
| `sms` | URL-encoded body with OTP substituted for `{#var#}` |
|
||||
|
||||
The OTP is **generated on-device** and stored in `SharedPreferences('lastOtp')`.
|
||||
|
||||
---
|
||||
|
||||
## 2. Deliveries — reads (all GET)
|
||||
|
||||
| Endpoint | Query | Caller |
|
||||
|---|---|---|
|
||||
| `…/{env}/api/v2/deliveries/getdeliveryqueues` | `userid, fromdate, todate, t` (+ optional `orderstatus`) | `lib/providers/delivery/delivery_provider.dart:34` |
|
||||
| `…/{env}/api/v1/deliveries/getdeliveries` | `userid, fromdate, todate, t` | `lib/background/backgroundservice.dart:68` |
|
||||
| `…/{env}/api/v2/deliveries/getdeliveries` | `userid, fromdate, todate, t` | hardcoded in `homepage.dart:1074`, `cartpage.dart:79`, `deliveries.dart:563` |
|
||||
| `…/{env}/api/v3/deliveries/getdeliveries` | `userid, fromdate, todate, t` | picked orders — `delivery_provider.dart:92` |
|
||||
|
||||
- `fromdate` / `todate` are always **today** in `YYYY-MM-DD`.
|
||||
- `t` is a cache-buster (epoch ms).
|
||||
- Response shape: `{ "code": 200, "status": true, "message": "Success", "details": [ … ] }`.
|
||||
Readers fall back to `data` then the raw body.
|
||||
|
||||
---
|
||||
|
||||
## 3. Delivery status updates
|
||||
|
||||
### PUT `https://queue.workolik.com/live/api/v1/deliveries/updatedelivery`
|
||||
|
||||
One endpoint, eight payload shapes. All from `lib/controllers/deliveries_controller.dart`.
|
||||
|
||||
Most variants share this GPS block:
|
||||
`riderslat`, `riderslon`, `raw_latitude`, `raw_longitude`, `velocity_lat`,
|
||||
`velocity_lng`, `speed`, `heading`.
|
||||
|
||||
#### accepted — `:695`
|
||||
```json
|
||||
{
|
||||
"deliveryid": 0,
|
||||
"orderheaderid": 0,
|
||||
"orderstatus": "accepted",
|
||||
"acceptedtime": "YYYY-MM-DD HH:MM:SS",
|
||||
"riderslat": "", "riderslon": "",
|
||||
"raw_latitude": "", "raw_longitude": "",
|
||||
"velocity_lat": "", "velocity_lng": "",
|
||||
"speed": "", "heading": "",
|
||||
"deliverylat": "", "deliverylong": "",
|
||||
"actualkms": "",
|
||||
"deliveryamt": 0.0,
|
||||
"notes": ""
|
||||
}
|
||||
```
|
||||
|
||||
#### active (start navigation) — `:1317`
|
||||
Same as accepted, plus:
|
||||
```json
|
||||
{
|
||||
"orderstatus": "active",
|
||||
"activetime": "YYYY-MM-DD HH:MM:SS",
|
||||
"starttime": "YYYY-MM-DD HH:MM:SS",
|
||||
"activelat": "", "activelon": ""
|
||||
}
|
||||
```
|
||||
Aborts if rider coordinates resolve to `0`.
|
||||
|
||||
#### arrived — `:1429`
|
||||
Same as accepted, with `"orderstatus": "arrived"` and
|
||||
`"arrivaltime": "YYYY-MM-DD HH:MM:SS"`. Geofence-checked against the pickup
|
||||
location before sending.
|
||||
|
||||
#### picked — `:280`
|
||||
No `raw_*` / velocity block on this one.
|
||||
```json
|
||||
{
|
||||
"deliveryid": 0,
|
||||
"orderheaderid": 0,
|
||||
"pickuplocationid": 0,
|
||||
"orderstatus": "picked",
|
||||
"pickuptime": "YYYY-MM-DD HH:MM:SS",
|
||||
"riderslat": "", "riderslon": "",
|
||||
"deliverylat": "", "deliverylong": "",
|
||||
"actualkms": "0.00",
|
||||
"deliveryamt": 0.0,
|
||||
"deliverytype": "",
|
||||
"address": "", "city": "", "state": "", "suburb": "", "postcode": "",
|
||||
"notes": "",
|
||||
"pickupimage": "",
|
||||
"proofimage": ""
|
||||
}
|
||||
```
|
||||
`actualkms` is computed rider→pickup when not supplied. Geofence-checked.
|
||||
|
||||
#### delivered — `:1229`
|
||||
```json
|
||||
{
|
||||
"deliveryid": 0,
|
||||
"orderheaderid": 0,
|
||||
"deliverylocationid": 0,
|
||||
"orderstatus": "delivered",
|
||||
"deliveredtime": "YYYY-MM-DD HH:MM:SS",
|
||||
"deliverytime": "YYYY-MM-DD HH:MM:SS",
|
||||
"smsdelivery": 0,
|
||||
"riderslat": "", "riderslon": "",
|
||||
"raw_latitude": "", "raw_longitude": "",
|
||||
"velocity_lat": "", "velocity_lng": "",
|
||||
"speed": "", "heading": "",
|
||||
"pickuplat": "", "pickuplong": "",
|
||||
"deliverylat": "", "deliverylong": "",
|
||||
"riderkms": "",
|
||||
"ridercharges": 0,
|
||||
"deliveryamt": 0,
|
||||
"collectionamt": 0,
|
||||
"collectedamt": 0,
|
||||
"collectionstatus": "",
|
||||
"ridertime": 0,
|
||||
"notes": "",
|
||||
"wasskipped": false,
|
||||
"bonuspts": 0,
|
||||
"dropimage": ""
|
||||
}
|
||||
```
|
||||
Returns `false` without sending if rider **or** drop coordinates are `0` or
|
||||
out of range (`|lat| > 90`, `|lng| > 180`).
|
||||
|
||||
#### skipped — `:1758`
|
||||
```json
|
||||
{
|
||||
"deliveryid": 0,
|
||||
"orderheaderid": 0,
|
||||
"orderstatus": "skipped",
|
||||
"skippedtime": "YYYY-MM-DD HH:MM:SS",
|
||||
"riderslat": "", "riderslon": "",
|
||||
"deliverylat": "", "deliverylong": "",
|
||||
"actualkms": "",
|
||||
"deliveryamt": 0.0,
|
||||
"notes": "",
|
||||
"riderkms": "0.00"
|
||||
}
|
||||
```
|
||||
`riderkms` is added only when the computed distance is > 0 (Google Directions,
|
||||
falling back to straight-line).
|
||||
|
||||
#### rejected — `:1477`
|
||||
Status block only, no timestamp: `"orderstatus": "rejected"` plus the shared
|
||||
GPS block, `deliverylat`/`deliverylong`, `actualkms`, `deliveryamt`, `notes`.
|
||||
|
||||
#### cancelled — `:1813`
|
||||
```json
|
||||
{
|
||||
"deliveryid": 0,
|
||||
"orderheaderid": 0,
|
||||
"orderstatus": "cancelled",
|
||||
"canceltime": "YYYY-MM-DD HH:MM:SS",
|
||||
"riderslat": "", "riderslon": "",
|
||||
"raw_latitude": "", "raw_longitude": "",
|
||||
"velocity_lat": "", "velocity_lng": "",
|
||||
"speed": "", "heading": "",
|
||||
"deliverylat": "", "deliverylong": "",
|
||||
"riderkms": "0.00",
|
||||
"ridercharges": 0,
|
||||
"deliveryamt": 0.0,
|
||||
"notes": ""
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 4. Delivery log (background heartbeat)
|
||||
|
||||
### POST `https://queue.workolik.com/live/api/v2/deliveries/createdeliverylog`
|
||||
|
||||
**The body is wrapped in an array** — `json.encode([data])`
|
||||
(`lib/providers/deliverylog/deliverylog_provider.dart:73`).
|
||||
|
||||
```json
|
||||
[
|
||||
{
|
||||
"logid": 0,
|
||||
"tenantid": 0,
|
||||
"partnerid": 0,
|
||||
"locationid": 0,
|
||||
"orderheaderid": 0,
|
||||
"deliveryid": 0,
|
||||
"userid": 1242,
|
||||
"orderid": "ORD123",
|
||||
"orderstatus": "active",
|
||||
"logdate": "YYYY-MM-DD HH:MM:SS",
|
||||
"latitude": "", "longitude": "",
|
||||
"raw_latitude": "", "raw_longitude": "",
|
||||
"velocity_lat": "", "velocity_lng": "",
|
||||
"speed": "", "heading": "",
|
||||
"riderkms": 0.0,
|
||||
"logstatus": 0
|
||||
}
|
||||
]
|
||||
```
|
||||
|
||||
Base fields come from `_createPayload` (`backgroundservice.dart:236`); GPS,
|
||||
`logdate`, `riderkms` and `logstatus` are merged in `_postDeliveryLog` (`:180`).
|
||||
Posted roughly every 30 s per active delivery. 10 s timeout; failures are
|
||||
queued in SharedPreferences and retried.
|
||||
|
||||
---
|
||||
|
||||
## 5. Rider log
|
||||
|
||||
### GET `…/{env}/api/v1/partners/getriderlog?userid=<id>`
|
||||
Response `details` is cached as `riderlog_template` and reused as the base for
|
||||
every subsequent create-log payload.
|
||||
|
||||
### GET `…/{env}/api/v1/partners/getridercount?userid=<id>`
|
||||
|
||||
### POST `https://queue.workolik.com/live/api/v2/partners/createriderlog`
|
||||
Periodic on-duty ping — `lib/controllers/riderlog.dart:289`,
|
||||
`lib/background/foreground_service.dart:289`
|
||||
|
||||
```json
|
||||
{
|
||||
"logid": 0,
|
||||
"userid": 1242,
|
||||
"partnerid": 0,
|
||||
"shiftid": 0,
|
||||
"logdate": "YYYY-MM-DD HH:MM:SS",
|
||||
"login": "HH:MM:SS",
|
||||
"latitude": "", "longitude": "",
|
||||
"raw_latitude": "", "raw_longitude": "",
|
||||
"velocity_lat": "", "velocity_lng": "",
|
||||
"speed": "", "heading": "",
|
||||
"onduty": 1,
|
||||
"status": "active",
|
||||
"contactno": "",
|
||||
"username": "",
|
||||
"firstname": "",
|
||||
"lastname": "",
|
||||
"tenantid": 0,
|
||||
"locationid": 0,
|
||||
"applocationid": 0,
|
||||
"userfcmtoken": "",
|
||||
"orderid": ""
|
||||
}
|
||||
```
|
||||
|
||||
- Built by spreading the cached `riderlog_template`, then overriding time, GPS
|
||||
and identity fields.
|
||||
- `status` is `"active"` when deliveries are live, otherwise `"idle"`.
|
||||
- `firstname` / `lastname` keys are **removed** when empty; `username` is always
|
||||
sent even if blank.
|
||||
- Interval = `logseconds` from login, forced to **30 s** while deliveries are
|
||||
active. 3 retries with 1 s backoff, then the offline queue (capped at 50).
|
||||
|
||||
### PUT `…/{env}/api/v1/partners/updateriderlog`
|
||||
Duty toggle — `lib/controllers/riderlog.dart:520`, model at
|
||||
`lib/Models/riders/riders_models.dart:275`
|
||||
|
||||
```json
|
||||
{
|
||||
"userid": 1242,
|
||||
"logstatus": null,
|
||||
"Logout": null,
|
||||
"workhours": null,
|
||||
"shorthours": null,
|
||||
"latitude": "",
|
||||
"longitude": "",
|
||||
"onduty": 1
|
||||
}
|
||||
```
|
||||
Note the capitalized `Logout` key (and `Login` in `RiderLogin.toJson`).
|
||||
|
||||
---
|
||||
|
||||
## 6. Breaks
|
||||
|
||||
### POST `https://queue.workolik.com/live/api/v2/partners/createbreaklog`
|
||||
`lib/controllers/riderlog.dart:925`
|
||||
|
||||
```json
|
||||
{
|
||||
"breakid": 427,
|
||||
"logid": 0,
|
||||
"breakdate": "YYYY-MM-DD HH:MM:SS",
|
||||
"userid": 1242,
|
||||
"partnerid": 0,
|
||||
"shiftid": 0,
|
||||
"breakstart": "HH:MM:SS",
|
||||
"breakend": "",
|
||||
"breakhours": 0.0,
|
||||
"latitude": "",
|
||||
"longitude": ""
|
||||
}
|
||||
```
|
||||
`breakid` is a **client-generated random 3-digit** value (100–999), replaced by
|
||||
the server id when one comes back.
|
||||
|
||||
Also emitted by the auto-shift-end flow at `backgroundservice.dart:765`.
|
||||
|
||||
### PUT `https://queue.workolik.com/live/api/v2/partners/updatebreaklog`
|
||||
`lib/controllers/riderlog.dart:196`
|
||||
|
||||
Same shape, with `breakend` filled and `breakhours` as fractional hours
|
||||
(`duration.inSeconds / 3600`).
|
||||
|
||||
---
|
||||
|
||||
## 7. Stats & rewards (GET)
|
||||
|
||||
| Endpoint | Source |
|
||||
|---|---|
|
||||
| `…/live/api/v2/partners/getdeliverystats?userid=<id>` | `lib/providers/summary/summary.dart:10` |
|
||||
| `…/live/api/v1/partners/getriderweeklykms?userid=<id>` | `lib/views/Dashboard/summary/summary.dart:318` |
|
||||
| `…/live/api/v1/utils/getuserbonussummary/?userid=<id>` | `lib/controllers/rewards_controller.dart:16` |
|
||||
|
||||
---
|
||||
|
||||
## 8. Support tickets
|
||||
|
||||
### GET `…/live/api/v1/partners/getridersupport/?userid=<id>`
|
||||
|
||||
### POST `…/live/api/v1/partners/createridersupport/`
|
||||
`lib/controllers/support_ticket.dart:186`
|
||||
|
||||
```json
|
||||
{
|
||||
"userid": 1242,
|
||||
"category": "",
|
||||
"priority": "",
|
||||
"subject": "",
|
||||
"issue": "",
|
||||
"image": "https://images.nearle.app/support/ticket-1234-1242.jpg"
|
||||
}
|
||||
```
|
||||
|
||||
Images are uploaded directly to DigitalOcean Spaces with the Minio SDK
|
||||
(bucket `nearle`, folder `support`, region `sgp1`) — not through the API. The
|
||||
returned CDN URL is what goes in `image`.
|
||||
|
||||
---
|
||||
|
||||
## 9. External services
|
||||
|
||||
- **GET** `https://maps.googleapis.com/maps/api/directions/json?origin=<lat,lng>&destination=<lat,lng>&key=<key>&units=metric&mode=driving`
|
||||
— road distance for km calculation, 5 s timeout (`deliveries_controller.dart:1115`)
|
||||
- `launchUrl` only (no HTTP from the app):
|
||||
- `https://www.google.com/maps/dir/?api=1&destination=<lat>,<lng>` (`nav.dart:444`)
|
||||
- `https://nearle.in/terms`, `https://nearle.in/privacy`, `https://nearle.in/faq`
|
||||
- `https://play.google.com/store/apps/details?id=com.nearle.partner`
|
||||
- `https://apps.apple.com/us/app/nearle/id1596895375ls=1`
|
||||
|
||||
### MQTT
|
||||
`66.116.225.226:1883`, user `admin` (`lib/views/helpers/constants/mqtt_constants.dart`)
|
||||
|
||||
| Topic | Payload |
|
||||
|---|---|
|
||||
| `nearle/riders/{riderId}/status` | `Online` / `Offline` / `Idle` / `Active` |
|
||||
| `nearle/riders/{riderId}/profile` | `userid, username, firstname, lastname, contactno, userfcmtoken, app_version, device_info` |
|
||||
| `nearle/riders/{riderId}/location` | `userid, orderid, lat, lng, raw_lat, raw_lng, speed, heading, velocity_lat, velocity_lng, timestamp` |
|
||||
| `nearle/riders/{riderId}/telemetry` | battery, charging, speed, connection, accuracy |
|
||||
| `nearle/riders/{riderId}/logs` | `rider_periodic_log`, `delivery_picked`, `critical_battery`, alerts |
|
||||
|
||||
---
|
||||
|
||||
## Known issues found during the sweep
|
||||
|
||||
1. **`lib/controllers/riderkm.dart:11` builds a broken URL.** `baseUrl` is
|
||||
already `…/v1/partners/getriderweeklykms`, and the code appends
|
||||
`/getriderweeklykms?userid=` → `…/getriderweeklykms/getriderweeklykms?userid=`.
|
||||
The summary *view* calls it correctly, so this controller path is likely dead
|
||||
or silently failing.
|
||||
2. **`lib/controllers/support_ticket.dart:66` hardcodes `userId = 1242`** in
|
||||
`fetchTickets()` — every rider sees user 1242's ticket list.
|
||||
3. **Secrets shipped in the APK**: DO Spaces access key + secret
|
||||
(`support_ticket.dart`, `deliveries_controller.dart`), LionSMS API key,
|
||||
MQTT `admin` / `Package@321#`, Google Maps key.
|
||||
4. **OTP is generated and verified on-device**, stored in
|
||||
`SharedPreferences('lastOtp')` — trivially bypassable.
|
||||
5. **TLS verification disabled** for `queue.workolik.com` plus a hardcoded IP
|
||||
pin (`66.116.225.226`). If that VPS IP changes, every write endpoint breaks
|
||||
at once.
|
||||
6. **`backgroundservice.dart:845` POSTs to `updateriderlog`** while every other
|
||||
caller PUTs it. If the server is method-strict, auto-shift-end never marks
|
||||
the rider offline.
|
||||
7. `updateDeliveryDev`, `createRiderLogDev`, `createBreakRiderLogDev`,
|
||||
`updateBreakRiderLogDev` and `createDeliveryLogDev` all point at **live** —
|
||||
there is no working dev environment for writes.
|
||||
8. `lib/providers/support/support_ticket.dart` is a dead duplicate of the
|
||||
controller and references an `…/partners/uploadimage/` endpoint nothing else
|
||||
uses.
|
||||
@@ -22,14 +22,13 @@ if (keystorePropertiesFile.exists()) {
|
||||
keystoreProperties.load(new FileInputStream(keystorePropertiesFile))
|
||||
}
|
||||
|
||||
def googleMapsApiKey = localProperties.getProperty("GOOGLE_MAPS_API_KEY") ?: ""
|
||||
|
||||
android {
|
||||
namespace = "com.nearle.partner"
|
||||
compileSdk = 36
|
||||
|
||||
// Correct NDK version
|
||||
ndkVersion "27.0.12077973"
|
||||
// Must be >= the highest NDK required by any plugin (maplibre_gl needs
|
||||
// 28.1.13356709). NDK releases are backward compatible, so the highest wins.
|
||||
ndkVersion "28.1.13356709"
|
||||
|
||||
packagingOptions {
|
||||
jniLibs {
|
||||
@@ -58,7 +57,6 @@ android {
|
||||
versionName flutterVersionName
|
||||
|
||||
manifestPlaceholders = [
|
||||
GOOGLE_MAPS_API_KEY: googleMapsApiKey,
|
||||
applicationName: "android.app.Application"
|
||||
]
|
||||
|
||||
@@ -120,3 +118,57 @@ tasks.register("copyNotificationSounds", Copy) {
|
||||
tasks.named("preBuild").configure {
|
||||
dependsOn("copyNotificationSounds")
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Release artifact naming: nearlepartner<DDMMYY>v<versionName>.aab / .apk
|
||||
//
|
||||
// The file is RENAMED rather than copied, and any previous nearlepartner*
|
||||
// artifact is removed first, so exactly one bundle ever sits in the output
|
||||
// directory. That matters: when two .aab files are present, Flutter's
|
||||
// "✓ Built ..." line can name the wrong one, which is an easy way to upload a
|
||||
// stale build to the Play Store.
|
||||
// ---------------------------------------------------------------------------
|
||||
// `versionName` is passed in explicitly: script-level `def` variables such as
|
||||
// flutterVersionName are not in scope inside a `def` method in Groovy.
|
||||
def renameReleaseArtifact(File outputDir, String defaultName, String extension, String versionName) {
|
||||
def source = new File(outputDir, defaultName)
|
||||
if (!source.exists()) {
|
||||
return
|
||||
}
|
||||
|
||||
outputDir.listFiles({ File f ->
|
||||
f.name.startsWith("nearlepartner") && f.name.endsWith(extension)
|
||||
} as FileFilter)?.each { it.delete() }
|
||||
|
||||
def stamp = new Date().format("ddMMyy")
|
||||
def target = new File(outputDir, "nearlepartner${stamp}v${versionName}${extension}")
|
||||
if (source.renameTo(target)) {
|
||||
println "Release artifact renamed -> ${target.name}"
|
||||
} else {
|
||||
println "WARNING: could not rename ${source.name}; leaving it in place"
|
||||
}
|
||||
}
|
||||
|
||||
def releaseArtifactVersion = flutterVersionName
|
||||
|
||||
tasks.matching { it.name == "bundleRelease" }.configureEach {
|
||||
doLast {
|
||||
renameReleaseArtifact(
|
||||
layout.buildDirectory.dir("outputs/bundle/release").get().asFile,
|
||||
"app-release.aab",
|
||||
".aab",
|
||||
releaseArtifactVersion
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
tasks.matching { it.name == "assembleRelease" }.configureEach {
|
||||
doLast {
|
||||
renameReleaseArtifact(
|
||||
layout.buildDirectory.dir("outputs/apk/release").get().asFile,
|
||||
"app-release.apk",
|
||||
".apk",
|
||||
releaseArtifactVersion
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,9 +3,6 @@
|
||||
|
||||
<!-- ===== Permissions ===== -->
|
||||
<uses-permission android:name="android.permission.CAMERA" />
|
||||
<uses-permission android:name="android.permission.READ_MEDIA_IMAGES" />
|
||||
<!-- For older Android versions -->
|
||||
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
|
||||
<uses-permission android:name="android.permission.PICTURE_IN_PICTURE" />
|
||||
<!-- Android 13+ notifications -->
|
||||
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
|
||||
@@ -37,11 +34,6 @@
|
||||
android:icon="@mipmap/ic_launcher"
|
||||
android:usesCleartextTraffic="true"> <!-- allows http if any -->
|
||||
|
||||
<!-- Google Maps API Key -->
|
||||
<meta-data
|
||||
android:name="com.google.android.geo.API_KEY"
|
||||
android:value="AIzaSyBhkGfnq27sN0wV5y_S-M2KojpFTk_by-Q" />
|
||||
|
||||
<!-- Firebase notification channel -->
|
||||
<meta-data
|
||||
android:name="com.google.firebase.messaging.default_notification_channel_id"
|
||||
|
||||
81
deploy/osrm/README.md
Normal file
81
deploy/osrm/README.md
Normal file
@@ -0,0 +1,81 @@
|
||||
# Self-hosted OSRM
|
||||
|
||||
The app defaults to the FOSSGIS community routing server. That server is free
|
||||
and keyless, but it is donation-funded infrastructure with a fair-use policy —
|
||||
fine for development, **not** appropriate for a production delivery fleet.
|
||||
Run your own before shipping.
|
||||
|
||||
Everything below is open source and costs nothing but the machine it runs on.
|
||||
|
||||
## 1. Pick a region
|
||||
|
||||
Nearle operates around Coimbatore, so the **southern zone** extract is enough —
|
||||
it covers Tamil Nadu, Kerala, Karnataka, Andhra Pradesh and Telangana.
|
||||
|
||||
| Extract | Download size |
|
||||
| --- | --- |
|
||||
| `asia/india/southern-zone` | **0.52 GB** ← use this |
|
||||
| `asia/india` (all of India) | 1.58 GB |
|
||||
|
||||
Using the regional extract rather than all-India cuts preprocessing time and
|
||||
roughly a third of the memory.
|
||||
|
||||
## 2. Preprocess (one-off, ~15–40 min)
|
||||
|
||||
```sh
|
||||
mkdir -p data && cd data
|
||||
wget https://download.geofabrik.de/asia/india/southern-zone-latest.osm.pbf
|
||||
|
||||
REGION=southern-zone-latest
|
||||
IMG=ghcr.io/project-osrm/osrm-backend:latest
|
||||
|
||||
docker run -t -v "$PWD:/data" $IMG osrm-extract -p /opt/car.lua /data/$REGION.osm.pbf
|
||||
docker run -t -v "$PWD:/data" $IMG osrm-partition /data/$REGION.osrm
|
||||
docker run -t -v "$PWD:/data" $IMG osrm-customize /data/$REGION.osrm
|
||||
```
|
||||
|
||||
`osrm-extract` is the memory-hungry step — give the machine **~8 GB RAM** for
|
||||
the southern zone. Serving afterwards needs far less (the compose file caps at
|
||||
4 GB). If extraction is killed, that is OOM, not corruption: retry with more RAM
|
||||
or a smaller extract.
|
||||
|
||||
Use `/opt/bicycle.lua` instead of `car.lua` if riders are mostly on two-wheelers
|
||||
in pedestrianised areas — but `car.lua` is the right default for motorbike
|
||||
delivery on Indian roads.
|
||||
|
||||
## 3. Serve
|
||||
|
||||
```sh
|
||||
cd .. && docker compose up -d
|
||||
curl "http://localhost:5000/route/v1/driving/76.9558,11.0168;76.9776,10.9984?overview=full"
|
||||
```
|
||||
|
||||
A healthy response starts with `{"code":"Ok"`.
|
||||
|
||||
## 4. Point the app at it
|
||||
|
||||
```sh
|
||||
flutter build apk --dart-define=OSRM_BASE_URL=https://osrm.your-domain.in
|
||||
```
|
||||
|
||||
Put it behind TLS and restrict it to your own backend or app — OSRM has **no
|
||||
authentication of its own**. An open OSRM instance is a free compute service for
|
||||
anyone who finds it. If you already proxy other services, route this through the
|
||||
same gateway.
|
||||
|
||||
## 5. Keep it current
|
||||
|
||||
OSM data drifts as roads change. Re-run step 2 monthly-ish and restart the
|
||||
container. There is no live-update path in OSRM — reprocessing is the only way.
|
||||
|
||||
## Optional: also self-host tiles
|
||||
|
||||
The app's default tile source, [OpenFreeMap](https://openfreemap.org), is a free
|
||||
public instance with no key and no request limits, so self-hosting tiles is not
|
||||
required. If you ever want to, the options are OpenFreeMap's own self-host
|
||||
instructions or serving a [Protomaps](https://protomaps.com) `.pmtiles` extract
|
||||
from the MinIO instance this project already uses. Then:
|
||||
|
||||
```sh
|
||||
--dart-define=MAP_STYLE_URL=https://tiles.your-domain.in/styles/liberty
|
||||
```
|
||||
40
deploy/osrm/docker-compose.yml
Normal file
40
deploy/osrm/docker-compose.yml
Normal file
@@ -0,0 +1,40 @@
|
||||
# Self-hosted OSRM routing backend.
|
||||
#
|
||||
# Run the one-off preprocessing in ./README.md FIRST — this file only serves
|
||||
# the already-built graph.
|
||||
#
|
||||
# docker compose up -d
|
||||
#
|
||||
# Then point the app at it:
|
||||
# flutter build apk --dart-define=OSRM_BASE_URL=http://your-host:5000
|
||||
|
||||
services:
|
||||
osrm:
|
||||
image: ghcr.io/project-osrm/osrm-backend:latest
|
||||
container_name: nearle-osrm
|
||||
restart: unless-stopped
|
||||
command: >
|
||||
osrm-routed
|
||||
--algorithm mld
|
||||
--max-table-size 8000
|
||||
/data/${OSRM_REGION:-southern-zone-latest}.osrm
|
||||
volumes:
|
||||
- ./data:/data:ro
|
||||
ports:
|
||||
- "5000:5000"
|
||||
healthcheck:
|
||||
# Coimbatore -> a nearby point; fails loudly if the graph did not load.
|
||||
test:
|
||||
- CMD-SHELL
|
||||
- >-
|
||||
wget -qO- "http://localhost:5000/route/v1/driving/76.9558,11.0168;76.9776,10.9984"
|
||||
| grep -q '"code":"Ok"' || exit 1
|
||||
interval: 30s
|
||||
timeout: 10s
|
||||
retries: 3
|
||||
start_period: 40s
|
||||
deploy:
|
||||
resources:
|
||||
limits:
|
||||
# Southern-zone MLD needs well under this; raise for all-India.
|
||||
memory: 4G
|
||||
99
docs/MAPS.md
Normal file
99
docs/MAPS.md
Normal file
@@ -0,0 +1,99 @@
|
||||
# Maps & routing
|
||||
|
||||
Fully open source, no API key, no account, no card.
|
||||
|
||||
| Concern | Technology | Cost |
|
||||
| --- | --- | --- |
|
||||
| Rendering | [MapLibre GL](https://maplibre.org) (BSD) | free |
|
||||
| Tiles | [OpenFreeMap](https://openfreemap.org) — OSM data, MIT | free, no key, no limits |
|
||||
| Routing | [OSRM](https://project-osrm.org) (BSD) | free, self-hostable |
|
||||
|
||||
There is no dependency on Google Maps: no Maps SDK, no Directions API, no
|
||||
`com.google.android.geo.API_KEY` in the manifest, and no credential of any kind
|
||||
shipped in the app.
|
||||
|
||||
## Why
|
||||
|
||||
Google's mobile Maps *SDK* is free and unlimited — the bill came from the
|
||||
**Directions API** at $5 per 1,000 requests above a 10,000/month free tier. Two
|
||||
things made it worse: the multi-stop screen issued one Directions call *per
|
||||
route segment*, and the API key was hardcoded in five places (including
|
||||
`AndroidManifest.xml`), unrestricted because the routing calls went out from the
|
||||
device as web-service requests.
|
||||
|
||||
Commercial alternatives were considered and rejected because they all want an
|
||||
account and a payment method. The open-source stack has neither.
|
||||
|
||||
## Configuration
|
||||
|
||||
Nothing is required to run the app — the defaults work out of the box:
|
||||
|
||||
```sh
|
||||
flutter run
|
||||
```
|
||||
|
||||
Both endpoints are overridable at build time, so production can point at
|
||||
self-hosted infrastructure without a code change:
|
||||
|
||||
```sh
|
||||
flutter build apk \
|
||||
--dart-define=OSRM_BASE_URL=https://osrm.your-domain.in \
|
||||
--dart-define=MAP_STYLE_URL=https://tiles.your-domain.in/styles/liberty
|
||||
```
|
||||
|
||||
> **Before production**, stand up your own OSRM. The default
|
||||
> (`routing.openstreetmap.de`) is the FOSSGIS community server — free and
|
||||
> keyless, but donation-funded with a fair-use policy that a delivery fleet
|
||||
> would abuse. See [`deploy/osrm/README.md`](../deploy/osrm/README.md); the
|
||||
> Coimbatore region needs only a 0.52 GB extract.
|
||||
>
|
||||
> Tiles do **not** need self-hosting — OpenFreeMap's public instance has no key
|
||||
> and no request limits, and permits commercial use under MIT with attribution
|
||||
> (which MapLibre renders automatically).
|
||||
|
||||
## Layout
|
||||
|
||||
| File | Role |
|
||||
| --- | --- |
|
||||
| `lib/services/maps/map_config.dart` | Tile style + routing endpoints |
|
||||
| `lib/services/maps/routing_service.dart` | OSRM client, polyline decoding, response cache |
|
||||
| `lib/services/maps/map_marker_icons.dart` | Canvas-drawn marker bitmaps (MapLibre has no built-in pins) |
|
||||
| `lib/services/maps/nearle_map.dart` | The `NearleMap` widget — declarative markers/polylines over MapLibre |
|
||||
|
||||
Provider choice is confined to the first two files. The four map screens talk
|
||||
only to `NearleMap` and `RoutingService`, so swapping backends again would not
|
||||
touch UI code.
|
||||
|
||||
### OSRM notes
|
||||
|
||||
- Coordinates go in the URL path as **`lon,lat`** — the reverse of most APIs.
|
||||
- `overview=full&geometries=polyline` returns an encoded polyline at
|
||||
**precision 5**. OSRM's `polyline6` option is precision 6; pass
|
||||
`precision: 6` to `decodePolyline` if you ever switch.
|
||||
- `_parse()` is deliberately tolerant — plain-number *or* `{"value": n}`
|
||||
distances, several geometry key names, GeoJSON `LineString` — so a proxy or an
|
||||
OSRM-compatible alternative (Valhalla, GraphHopper) drops in without changes.
|
||||
|
||||
## Cost controls
|
||||
|
||||
These mattered when routing was billed and still cut latency and load:
|
||||
|
||||
- **One call per route, not per leg.** The multi-stop screen passes intermediate
|
||||
stops as additional coordinates in a single OSRM request. It previously issued
|
||||
one request per consecutive pair, so an N-stop route cost N calls every time
|
||||
the screen opened.
|
||||
- **Response cache.** `RoutingService` caches by origin/destination rounded to
|
||||
~11 m for 30 minutes, so repeated quotes for the same delivery reuse one call.
|
||||
- **Rendering makes no routing calls at all.**
|
||||
|
||||
## Known gaps vs. the old Google implementation
|
||||
|
||||
- **Info windows.** MapLibre has no info-window concept. Marker taps still fire
|
||||
(`NearleMapMarker.onTap` drives the customer sheet on the multi-stop screen),
|
||||
but the old tap-to-show-title bubbles are gone.
|
||||
- **Traffic and 3D buildings.** `trafficEnabled` / `buildingsEnabled` on the
|
||||
navigation screen had no MapLibre equivalent and were dropped. OSRM has no
|
||||
live-traffic model either — ETAs are free-flow estimates.
|
||||
- **Turn-by-turn navigation is unchanged.** It still hands off to whatever
|
||||
navigation app is installed via a `google.navigation:` / maps URL intent. That
|
||||
is an OS intent, not an API — no key, no SDK, no billing.
|
||||
620
docs/PORTFOLIO.md
Normal file
620
docs/PORTFOLIO.md
Normal file
@@ -0,0 +1,620 @@
|
||||
# Nearle Rider (`com.nearle.partner`) — Portfolio Technical Summary
|
||||
|
||||
A production Android delivery-rider app for a last-mile logistics operation in
|
||||
Coimbatore, India. ~30,000 lines of Dart plus native Kotlin. It's the app a
|
||||
delivery rider has open on the handlebar for an entire shift.
|
||||
|
||||
There are really **six** distinct systems in here, not one app. Taken in order of
|
||||
engineering weight.
|
||||
|
||||
---
|
||||
|
||||
## 1. The GPS Pipeline — Trusted Distance Measurement
|
||||
|
||||
### What it is
|
||||
|
||||
Riders are paid by the kilometre. The formula lives in `deliveries_controller.dart`:
|
||||
|
||||
```
|
||||
ridercharges = distance_travelled_km × fuelcharge_rate
|
||||
```
|
||||
|
||||
That single line is why this subsystem exists. Consumer phone GPS is noisy: it
|
||||
drifts several metres while stationary, jumps hundreds of metres when switching
|
||||
between cell-tower and satellite fixes, degrades under power-saving mode, and can
|
||||
be trivially faked with a mock-location app. Every one of those failure modes is
|
||||
*money* — noise inflates payouts, jumps inflate payouts spectacularly, and
|
||||
spoofing is outright fraud.
|
||||
|
||||
So the problem isn't "track the rider." It's: **produce a distance number that is
|
||||
defensible enough to pay against, from a sensor that lies.**
|
||||
|
||||
### How it's built
|
||||
|
||||
```
|
||||
Android GPS (bestForNavigation, distanceFilter: 0)
|
||||
│ raw Position stream, ~1 Hz
|
||||
▼
|
||||
┌──────────────────────────────────────────┐
|
||||
│ Gate 1: isMocked? → drop │ anti-fraud
|
||||
│ Gate 2: accuracy > 50 m? → drop │ quality
|
||||
│ Gate 3: <3 s since last? → drop │ rate limit
|
||||
│ Gate 4: _isProcessing? → drop │ re-entrancy mutex
|
||||
└──────────────────────────────────────────┘
|
||||
▼
|
||||
┌──────────────────────────────────────────┐
|
||||
│ 4-D Kalman filter │
|
||||
│ state = [lat, lng, v_lat, v_lng] │
|
||||
│ predict(dt) → update(measurement) │
|
||||
└──────────────────────────────────────────┘
|
||||
│ smoothed (lat, lng)
|
||||
├──────────────────────────────► MQTT nearle/riders/{id}/location
|
||||
│ (QoS 0 — fire and forget)
|
||||
▼
|
||||
┌──────────────────────────────────────────┐
|
||||
│ Per-delivery odometer, in SharedPrefs │
|
||||
│ Haversine(anchor → now) │
|
||||
│ Gate 5: implied speed > 120 km/h │
|
||||
│ AND jump > 200 m → move anchor,│
|
||||
│ DON'T count the metres │
|
||||
│ Gate 6: delta < 5 m → skip, DON'T move │
|
||||
│ anchor (noise floor) │
|
||||
│ else: cumulativeKm += delta │
|
||||
└──────────────────────────────────────────┘
|
||||
```
|
||||
|
||||
### The approach, step by step
|
||||
|
||||
**The Kalman filter** (`lib/utils/kalman_filter.dart`) is hand-implemented — no
|
||||
linear-algebra dependency, just nested `List<List<double>>` and explicitly
|
||||
unrolled matrix helpers (`_multiply2x4_4x2`, `_inverse2x2`, and so on). State is
|
||||
4-dimensional: position *and* velocity in each axis. The measurement matrix `H`
|
||||
is 2×4 because GPS only observes position; velocity is inferred from how position
|
||||
evolves. Measurement noise `R` is set to `1e-5` degrees² (~1 m), encoding "GPS is
|
||||
accurate to roughly 5–10 m." `dt` is fed in live from wall-clock deltas, so the
|
||||
filter behaves correctly whether samples arrive 3 seconds apart (foreground) or 30
|
||||
seconds apart (background).
|
||||
|
||||
The payload sends **both** the filtered and the raw coordinates (`lat`/`lng`
|
||||
alongside `raw_lat`/`raw_lng`). That's a good call: dispatch sees the smooth
|
||||
track, but the unfiltered truth is preserved server-side for audit or for
|
||||
re-deriving with different parameters later.
|
||||
|
||||
**The two gates are asymmetric on purpose, and this is the cleverest bit.** When
|
||||
GPS jumps implausibly far, the code moves the anchor point but does *not* add the
|
||||
distance — the phantom kilometres are discarded. When movement is *below* 5 m, it
|
||||
does the opposite: it skips *without* moving the anchor. If it moved the anchor on
|
||||
tiny deltas, a phone sitting still would accumulate GPS jitter forever and
|
||||
silently print money. Getting both directions right requires actually reasoning
|
||||
about the failure mode rather than just clamping values.
|
||||
|
||||
The jump threshold is also velocity-based rather than fixed:
|
||||
`max_realistic = elapsed_seconds × 33.3 m/s` (120 km/h). A 200 m step in 3 seconds
|
||||
is fraud; the same 200 m step across a 60-second background interval is a normal
|
||||
motorcycle. A fixed distance threshold would have to choose one and be wrong for
|
||||
the other.
|
||||
|
||||
### The tricky case: two isolates, one odometer
|
||||
|
||||
Flutter's foreground service runs in a **separate Dart isolate** with its own
|
||||
memory. So there are two independent copies of this pipeline —
|
||||
`LiveTrackingService` (main isolate, 3 s cadence) and `_BackgroundRiderLog`
|
||||
(service isolate, 30 s cadence) — and both want to increment the same odometer.
|
||||
Naively, every metre gets counted twice and every rider gets paid double.
|
||||
|
||||
The coordination is a **heartbeat in shared storage**:
|
||||
|
||||
```
|
||||
main isolate ──writes──► live_tracking_last_update_ms ──reads──► service isolate
|
||||
│
|
||||
if (now − heartbeat < 10 s) │
|
||||
→ main is alive, stand down
|
||||
else │
|
||||
→ main is dormant, take over
|
||||
```
|
||||
|
||||
The main isolate stamps a timestamp on every update. The background isolate reads
|
||||
it and accumulates *only* if the stamp is stale by 10 seconds — meaning the app is
|
||||
backgrounded, screen-off, or the OS has frozen the main isolate. Handover in both
|
||||
directions is automatic and requires no signalling. Since `SharedPreferences`
|
||||
caches in memory per isolate, the background side calls `prefs.reload()` before
|
||||
reading to force a disk re-read.
|
||||
|
||||
*This is a lease/lock pattern implemented over the only shared medium two Dart
|
||||
isolates reliably have.* It's the single most non-obvious piece of engineering in
|
||||
the codebase.
|
||||
|
||||
### Trade-offs
|
||||
|
||||
- **`SharedPreferences` as the IPC channel.** It's key–value storage, not a
|
||||
database — no transactions, no atomic increments. Two writers to the same key
|
||||
could theoretically interleave. The 10-second lease makes that window vanishingly
|
||||
small in practice, and it buys zero extra dependencies plus survival across
|
||||
process death. A proper answer would be SQLite with a write-ahead log; this is
|
||||
the pragmatic one.
|
||||
- **Cumulative distance beats point-to-point.** At delivery confirmation the code
|
||||
prefers the tracked odometer over any recomputed origin→destination distance,
|
||||
precisely because the odometer reflects the path actually ridden — detours,
|
||||
one-ways, wrong turns and all.
|
||||
- Reading the code honestly: the process-noise matrix `Q` has entries at `[2][0]`
|
||||
and `[3][1]` that make it non-symmetric, which is unusual for a covariance
|
||||
matrix. It doesn't destabilise the filter at these magnitudes, but it looks like
|
||||
a transcription slip rather than a deliberate choice.
|
||||
|
||||
---
|
||||
|
||||
## 2. The Maps & Routing Migration — Removing Google Entirely
|
||||
|
||||
### What it is
|
||||
|
||||
The app originally used Google Maps SDK for rendering and the Google Directions
|
||||
API for road routes. It was billing painfully. The rebuild replaced the entire
|
||||
stack with open-source components — **no API key of any kind ships in the app
|
||||
anymore.**
|
||||
|
||||
### The diagnosis (this is the interesting part)
|
||||
|
||||
The obvious assumption — "map rendering is expensive" — is wrong. Google's mobile
|
||||
Maps *SDK* is free and unlimited. The bill came from **Directions**, at $5 per
|
||||
1,000 calls above a 10,000/month free tier. Two things amplified it:
|
||||
|
||||
1. The multi-stop screen issued **one Directions request per consecutive pair of
|
||||
stops**. Opening a 6-stop route cost 5 billable requests — *every time the
|
||||
screen opened*, and riders open it constantly.
|
||||
2. The API key was hardcoded in five places including `AndroidManifest.xml`.
|
||||
Because `flutter_polyline_points` called the Directions **web service** from the
|
||||
device, the key could not carry an Android-app restriction — Google only
|
||||
supports that for SDK calls. Anyone who unzipped the APK had an unrestricted,
|
||||
billable key.
|
||||
|
||||
So the fix that actually saves money is replacing *routing*, not rendering. That
|
||||
reframing is what made the migration tractable.
|
||||
|
||||
### The replacement stack
|
||||
|
||||
| Concern | Was | Now | Cost |
|
||||
| --- | --- | --- | --- |
|
||||
| Rendering | Google Maps SDK | **MapLibre GL** (BSD) | free |
|
||||
| Tiles | Google | **OpenFreeMap** (OSM data, MIT) | free, keyless, no limits |
|
||||
| Routing | Directions API | **OSRM** (BSD, self-hostable) | free |
|
||||
| Turn-by-turn | — | OS intent handoff (`google.navigation:`) | free — it's an intent, not an API |
|
||||
|
||||
### Architecture
|
||||
|
||||
```
|
||||
4 map screens (map, multi_map, nav, homepage)
|
||||
│
|
||||
┌─────────────────┴─────────────────┐
|
||||
▼ ▼
|
||||
NearleMap widget RoutingService
|
||||
(declarative markers/lines) (OSRM client + cache)
|
||||
│ │
|
||||
├── MapMarkerIcons ──┐ │
|
||||
│ (Canvas → PNG) │ │
|
||||
▼ ▼ ▼
|
||||
maplibre_gl MapConfig ──── endpoint URLs
|
||||
│ (String.fromEnvironment)
|
||||
▼ ▼
|
||||
OpenFreeMap tiles OSRM /route/v1/driving/…
|
||||
```
|
||||
|
||||
The provider choice is confined to **two files** (`map_config.dart`,
|
||||
`routing_service.dart`). No UI file names a vendor. Swapping backends again
|
||||
wouldn't touch a screen.
|
||||
|
||||
### How each hard part was solved
|
||||
|
||||
**One call, not N.** OSRM accepts every stop as a coordinate in a single request
|
||||
path: `lon,lat;lon,lat;lon,lat`. The multi-stop screen now passes intermediate
|
||||
stops as `waypoints` in one call. N requests → 1.
|
||||
|
||||
**Response caching.** `RoutingService` keys its cache on coordinates rounded to 4
|
||||
decimal places (~11 m), with a 30-minute TTL and a 120-entry cap evicted
|
||||
oldest-first. Repeated quotes for the same delivery reuse one response.
|
||||
|
||||
**A deliberately tolerant parser.** `_parse()` accepts distances as plain numbers
|
||||
*or* as `{"value": n}` objects, falls back from route-level totals to summing legs,
|
||||
and hunts for geometry under `geometry` / `overview_polyline` / `polyline`, nested
|
||||
`{"points": …}`, GeoJSON `coordinates`, and finally by stitching per-step
|
||||
geometries. This is defensive work that lets a proxy, a self-hosted build, or an
|
||||
OSRM-compatible alternative (Valhalla, GraphHopper) drop in without a code change.
|
||||
|
||||
**The polyline codec.** Both Google and OSRM encode geometry using Google's
|
||||
Encoded Polyline Algorithm — a variable-length, zigzag-encoded, base-64-offset
|
||||
delta format. It's implemented from scratch (`decodePolyline`) with the precision
|
||||
parameterised, because OSRM's default `geometries=polyline` is precision 5 while
|
||||
its `polyline6` option is precision 6. Get that wrong and routes render as a tiny
|
||||
smudge near the equator.
|
||||
|
||||
**The lon,lat trap.** OSRM takes coordinates as `lon,lat` in the URL path —
|
||||
reversed from nearly every other geo API. Swapping them silently returns routes in
|
||||
the wrong hemisphere rather than erroring. The smoke test asserts route endpoints
|
||||
land within 0.02° of the requested points specifically to catch this.
|
||||
|
||||
**Markers had to be redrawn from nothing.** MapLibre has no `BitmapDescriptor`
|
||||
equivalent. `map_marker_icons.dart` draws teardrop pins, rider dots, and numbered
|
||||
step circles directly onto a `dart:ui` Canvas and rasterises them to PNG bytes,
|
||||
cached by a composite key so identical icons register once.
|
||||
|
||||
**Diffing instead of clear-and-re-add.** On the navigation screen the rider marker
|
||||
moves every few metres. Tearing down and re-adding every annotation would flicker
|
||||
visibly. `_NearleMapState` keeps `Map<String, Symbol>` / `Map<String, Line>`
|
||||
registries and calls `updateSymbol`/`updateLine` on changed items only. Two
|
||||
supporting details make this work:
|
||||
|
||||
- `NearleMapMarker`'s `==` **deliberately excludes `onTap`**. A new closure on
|
||||
every rebuild would otherwise mark every marker dirty. Taps are instead resolved
|
||||
by looking up the ID against the *current* widget's marker list, so the handler
|
||||
still closes over fresh state.
|
||||
- Native annotation calls aren't re-entrant, so `_syncing`/`_syncQueued` flags
|
||||
coalesce overlapping syncs into one trailing run.
|
||||
|
||||
**Degenerate camera bounds.** Fitting the camera to a single point (or two points
|
||||
on an exact horizontal line) makes MapLibre's native fit routine fail. `fitBounds`
|
||||
nudges any span below `0.0015°` (~165 m) outward around its midpoint first.
|
||||
|
||||
### Trade-offs accepted
|
||||
|
||||
- **No info windows.** MapLibre has no equivalent. Marker taps still fire and
|
||||
drive the customer sheet; tap-to-show-title bubbles are gone.
|
||||
- **No traffic layer, no 3D buildings.** No MapLibre equivalent, and OSRM has no
|
||||
live-traffic model either — ETAs are free-flow estimates.
|
||||
- **The public FOSSGIS OSRM server is the default.** It's donation-funded with a
|
||||
fair-use policy a delivery fleet would abuse. `deploy/osrm/` ships a Docker
|
||||
Compose stack and preprocessing guide for self-hosting; the whole southern-India
|
||||
extract is only 0.52 GB. The endpoint is overridable via
|
||||
`--dart-define=OSRM_BASE_URL=…` with no code change.
|
||||
|
||||
### Verification
|
||||
|
||||
The migration was checked at the **artifact** level, not the build-log level — the
|
||||
release AAB was unzipped and inspected: `libmaplibre.so` present for 3 ABIs,
|
||||
`com/google/android/gms/maps` references **0**, `org/maplibre` references 341.
|
||||
`test/maps_smoke_test.dart` runs 8 tests including live hits against both
|
||||
endpoints, validating the polyline decoder against Google's canonical published
|
||||
example and asserting no `api_key=` appears in the fetched style document.
|
||||
|
||||
---
|
||||
|
||||
## 3. Delivery Lifecycle & Compensation Engine
|
||||
|
||||
### What it is
|
||||
|
||||
The state machine a delivery moves through, and the money that falls out of it.
|
||||
|
||||
```
|
||||
accepted → active → arrived → picked → delivered
|
||||
│ │ │
|
||||
├─ rejected └─ (or skipped / cancelled)
|
||||
```
|
||||
|
||||
Each transition is a method on `DeliveriesController` and each does far more than
|
||||
flip a status.
|
||||
|
||||
### The delivery-confirmation path
|
||||
|
||||
`updateDeliveredStatus()` is ~400 lines and reads like a checklist of everything
|
||||
that can go wrong in the field:
|
||||
|
||||
**Step 1 — Geofence gate.** Compute Haversine distance from the rider to the drop
|
||||
point. If it exceeds a server-configured radius, show the exact distance and
|
||||
required radius, and **refuse the update**. This is the anti-fraud gate that stops
|
||||
"delivered" being tapped from home.
|
||||
|
||||
**Step 2 — Distance resolution, four-tier cascade.** This is a
|
||||
graceful-degradation ladder, best source first:
|
||||
|
||||
1. Cumulative GPS odometer (§1) — the actual path ridden
|
||||
2. Caller-supplied `actualKms`
|
||||
3. OSRM road distance from a recorded origin — tried against the saved start
|
||||
location, then the provided pickup, then the cached pickup, then the drop point
|
||||
4. Straight-line Haversine — if routing is unreachable
|
||||
|
||||
Then a floor: if everything failed, use 0.01 km. Never zero, never null. The
|
||||
reasoning is visible in the code — a null propagating into `ridercharges` means an
|
||||
unpaid delivery and an angry rider, which is worse than a slightly wrong number.
|
||||
|
||||
**Step 3 — Bonus points.** An ETA deadline is stamped at `eta_endtime_{orderId}`
|
||||
when the delivery goes active. Delivered at or before it → bonus points equal to
|
||||
`round(distance_km)`.
|
||||
|
||||
**Step 4 — Skip penalty.** A 3-hour rolling window tracks skip count. If the rider
|
||||
is over the limit, bonus points are **forfeited to zero** — the base per-km pay is
|
||||
untouched. Penalising the incentive rather than the wage is a deliberate, and
|
||||
fairly humane, design choice.
|
||||
|
||||
**Step 5 — Coordinate validation.** Reject `0`, reject `|lat| > 90`, reject
|
||||
`|lng| > 180`, and abort the whole update rather than post garbage. Compare with
|
||||
the geofence check, which *fails open* on missing coordinates (warn and proceed) —
|
||||
the two policies are opposite, and correctly so. A geofence failure shouldn't
|
||||
strand a rider who genuinely delivered; a malformed coordinate write would corrupt
|
||||
the record permanently.
|
||||
|
||||
**Step 6 — Money.** `ridercharges = km × fuelcharge`, rounded to 2 decimals to
|
||||
match a `DECIMAL(10,2)` column server-side.
|
||||
|
||||
### Route sequencing
|
||||
|
||||
Stop ordering (`step`) is assigned **server-side** — the app doesn't solve a
|
||||
travelling-salesman problem. What it does handle is *display* sequencing:
|
||||
`preservedStepNumbers` keeps numbering stable as deliveries complete or get
|
||||
skipped, so a stop the rider has been calling "number 3" doesn't silently renumber
|
||||
itself mid-shift. Orders arriving without a step get synthesised display numbers
|
||||
appended after the numbered ones. Small feature, real usability value.
|
||||
|
||||
---
|
||||
|
||||
## 4. Background Execution & Shift Automation
|
||||
|
||||
### What it is
|
||||
|
||||
Android aggressively kills background work — Doze mode, app standby, and
|
||||
OEM-specific battery managers (particularly aggressive on the Chinese-brand
|
||||
handsets common in this market). But a rider's shift must be logged, and their
|
||||
shift must end on time, whether or not the app is alive.
|
||||
|
||||
This system is a **layered survival strategy**: each layer covers the failure of
|
||||
the one above.
|
||||
|
||||
```
|
||||
Layer 1 — App foregrounded
|
||||
LiveTrackingService, 3 s cadence, full fidelity
|
||||
|
||||
Layer 2 — App backgrounded
|
||||
flutter_foreground_task, 30 s repeat, persistent notification,
|
||||
foregroundServiceType="dataSync|location", wake + WiFi locks
|
||||
|
||||
Layer 3 — App process killed
|
||||
Android AlarmManager, setExactAndAllowWhileIdle (pierces Doze)
|
||||
→ ShiftEndReceiver.kt, pure Kotlin, no Flutter engine
|
||||
→ reads Flutter's SharedPreferences directly ("flutter." prefix)
|
||||
→ raw HttpURLConnection POST, under a 60 s PARTIAL_WAKE_LOCK
|
||||
|
||||
Layer 4 — Phone rebooted / alarm lost
|
||||
On next app launch, main.dart replays the shift-end check
|
||||
and creates the break log retroactively
|
||||
```
|
||||
|
||||
Layer 3 is the interesting one. The receiver deliberately **does not** boot the
|
||||
Flutter engine — starting a Dart VM from a broadcast receiver is slow and
|
||||
unreliable. Instead it reaches into `FlutterSharedPreferences` (Flutter prefixes
|
||||
every key with `flutter.`, which the Kotlin reads verbatim), hand-builds the JSON
|
||||
payload as a string, and POSTs it with `HttpURLConnection`. The break log is
|
||||
created correctly even from a cold-killed app.
|
||||
|
||||
### Overnight shift arithmetic
|
||||
|
||||
Shifts are stored as bare `HH:mm:ss` strings with no date, which makes "has the
|
||||
shift ended?" genuinely ambiguous. The logic compares start and end as fractional
|
||||
hours; if `start > end`, the shift crosses midnight, and "over" means *within the
|
||||
gap between end and start*, not simply "after end":
|
||||
|
||||
```
|
||||
22:00 start, 06:00 end
|
||||
now 23:00 → after start → ACTIVE
|
||||
now 05:00 → before end → ACTIVE (yesterday's shift)
|
||||
now 10:00 → after end AND before start → OVER
|
||||
```
|
||||
|
||||
That check is implemented three times — Dart foreground, Dart background isolate,
|
||||
and Kotlin — because each layer may be the only one running. Triplicated logic is a
|
||||
maintenance liability, and it's the right call anyway: a single shared
|
||||
implementation isn't reachable from a receiver that can't start Dart.
|
||||
|
||||
---
|
||||
|
||||
## 5. Offline Resilience
|
||||
|
||||
Riders lose signal in basements, lifts, and rural stretches constantly. Every
|
||||
write path has a queue behind it.
|
||||
|
||||
```
|
||||
POST attempt (8–10 s timeout)
|
||||
│
|
||||
success ──► persist "last known good" anchor, done
|
||||
│
|
||||
failure ──► append {orderId, payload, timestamp} to a
|
||||
SharedPreferences string-list queue
|
||||
│
|
||||
▼
|
||||
on next successful cycle / app start / reconnect:
|
||||
drain queue, retry each, keep failures, rewrite remainder
|
||||
```
|
||||
|
||||
Two independent queues exist — `offline_delivery_logs` and `offline_rider_logs` —
|
||||
each with an `_isFlushing` re-entrancy guard so overlapping timers can't
|
||||
double-post. Requests carry explicit timeouts throughout, so a hung socket
|
||||
degrades to a queued write instead of freezing a UI thread.
|
||||
|
||||
Payloads are persisted per order (`delivery_payload_{orderId}`) so a delivery in
|
||||
progress survives a process kill and resumes with the same identity rather than
|
||||
starting a fresh log.
|
||||
|
||||
---
|
||||
|
||||
## 6. Real-Time Telemetry Bus (MQTT)
|
||||
|
||||
Location, status, and device health stream to dispatch over MQTT rather than HTTP
|
||||
polling.
|
||||
|
||||
```
|
||||
nearle/riders/{riderId}/status retained + Last Will (QoS 1)
|
||||
nearle/riders/{riderId}/location QoS 0, ~every 3 s
|
||||
nearle/riders/{riderId}/profile retained (QoS 1)
|
||||
nearle/riders/{riderId}/telemetry every 5 min (QoS 1)
|
||||
nearle/riders/{riderId}/logs/{event} (QoS 1)
|
||||
nearle/riders/{riderId}/alerts (QoS 1)
|
||||
```
|
||||
|
||||
The QoS choices are thought through, not defaults. Location is **QoS 0** — a
|
||||
dropped fix is replaced by a fresher one 3 seconds later, so guaranteed delivery
|
||||
would be pure overhead. Status is **QoS 1 and retained**, so a dispatcher opening
|
||||
the console sees current state immediately rather than waiting for the next
|
||||
publish.
|
||||
|
||||
**Last Will and Testament** is the sharp detail: the client registers an `Offline`
|
||||
message with the broker at connect time. If the rider's phone dies, loses signal,
|
||||
or force-quits, the *broker* publishes that message on their behalf. Dispatch
|
||||
learns the rider is gone without any cooperation from the dead client.
|
||||
|
||||
Client IDs are salted per isolate startup (`rider_{id}_{base36 timestamp}`) —
|
||||
otherwise the main and background isolates would connect with identical IDs and
|
||||
MQTT brokers evict the older session, causing the two to kick each other offline in
|
||||
a loop.
|
||||
|
||||
Alerts fire on device conditions dispatch actually cares about: location services
|
||||
disabled, battery under 15% and not charging, GPS accuracy worse than 30 m.
|
||||
|
||||
---
|
||||
|
||||
## Supporting Features
|
||||
|
||||
Smaller but real:
|
||||
|
||||
- **Auth** — phone precheck → OTP (with `sms_autofill`) → MPIN, with server-side
|
||||
PIN verification and session refresh
|
||||
- **Picture-in-Picture** — native Android PiP via `MethodChannel`, so an ETA
|
||||
countdown stays visible while the rider is in Google Maps
|
||||
- **Proximity alerts** — within 50 m of the drop, fire a local notification plus a
|
||||
bundled audio clip, falling back to TTS if playback fails. A per-delivery flag
|
||||
ensures once only
|
||||
- **Proof-of-delivery upload** — camera capture → DigitalOcean Spaces
|
||||
(S3-compatible, via the `minio` client) → CDN URL returned to the API
|
||||
- **Push** — FCM with per-rider selectable alert sounds; the Android notification
|
||||
channel is *recreated* under a new ID when the sound changes, because channel
|
||||
settings are immutable after creation
|
||||
- **Summary & rewards** — `fl_chart` dashboards, weekly km, scratch-card rewards
|
||||
(`scratcher` + `confetti`)
|
||||
- **Support tickets**, FAQ, saved addresses, forced-update gate against the Play
|
||||
Store version
|
||||
|
||||
---
|
||||
|
||||
## The Stack
|
||||
|
||||
**Language / runtime**
|
||||
- Dart (SDK ^3.9.2) · Flutter · Kotlin (native Android layer)
|
||||
|
||||
**State & architecture**
|
||||
- GetX (`get`) — DI, reactive state, routing · Provider · MVC-ish split:
|
||||
`views/` → `controllers/` → `providers/` → `Models/`
|
||||
|
||||
**Geo**
|
||||
- `maplibre_gl` (rendering) · OpenFreeMap (OSM vector tiles) · OSRM (routing) ·
|
||||
`geolocator` · `geocoding` · custom Kalman filter · custom polyline codec
|
||||
|
||||
**Background & lifecycle**
|
||||
- `flutter_foreground_task` · Android AlarmManager (native) · `wakelock_plus` ·
|
||||
`battery_plus` · `connectivity_plus`
|
||||
|
||||
**Networking & messaging**
|
||||
- `http` · `mqtt_client` · Firebase Core + Messaging (FCM) · `minio`
|
||||
(S3-compatible object storage)
|
||||
|
||||
**Storage**
|
||||
- `shared_preferences` (state, queues, cross-isolate coordination) ·
|
||||
`path_provider` · DigitalOcean Spaces + CDN (media)
|
||||
|
||||
**UI**
|
||||
- `flutter_screenutil` · `fl_chart` · `lottie` · `shimmer` · `confetti` ·
|
||||
`scratcher` · `flutter_slidable` · `webview_flutter` · Proxima Nova
|
||||
|
||||
**Device**
|
||||
- `permission_handler` · `image_picker` · `flutter_tts` · `audioplayers` ·
|
||||
`vibration` · `sms_autofill` · `url_launcher` · `floating` (PiP)
|
||||
|
||||
**Build**
|
||||
- Gradle (Groovy) · compileSdk 36 · NDK 28.1.13356709 · R8 minify + resource
|
||||
shrink · arm64-v8a only · custom release-artifact naming task
|
||||
|
||||
**Backend (consumed, not in this repo)**
|
||||
- REST across two hosts — a primary API and a separate write/queue host · MQTT
|
||||
broker on a VPS · versioned endpoints (v1/v2/v3) · `dev`/`live` switched by one
|
||||
constant
|
||||
|
||||
---
|
||||
|
||||
## What's Genuinely Hard Here
|
||||
|
||||
Ranked by how much real thought each needed.
|
||||
|
||||
**1. Cross-isolate odometer coordination.** Two Dart isolates with no shared
|
||||
memory, both holding a GPS stream, both able to increment the same paid counter,
|
||||
either able to be frozen by the OS without notice. The heartbeat-lease over
|
||||
`SharedPreferences` solves it in about 15 lines — but arriving at that solution
|
||||
means understanding Flutter's isolate model, Android's process lifecycle, and why
|
||||
the obvious fixes (a flag, a mutex, a port) all fail across a process boundary.
|
||||
|
||||
**2. Trustworthy distance from an untrustworthy sensor.** Not "apply a Kalman
|
||||
filter" — the filter is the easy half. The hard half is the *asymmetry*: discard
|
||||
distance but move the anchor on a jump; discard the sample but hold the anchor on
|
||||
jitter. Both directions have to be right or the odometer drifts in one direction,
|
||||
and that direction is money. The velocity-scaled jump threshold, so the same guard
|
||||
works at 3 s and 60 s cadences, is the same kind of thinking.
|
||||
|
||||
**3. Correctly diagnosing the Maps bill.** The valuable engineering was the
|
||||
analysis, not the code. "Maps SDK is free; Directions is billed; the multi-stop
|
||||
screen was making N calls per screen-open; the key couldn't be restricted because
|
||||
the calls were web-service calls" — that chain of reasoning is what turned an
|
||||
unbounded vendor migration into a well-scoped one that landed. Plenty of teams
|
||||
would have swapped rendering engines and wondered why the bill barely moved.
|
||||
|
||||
**4. Four-layer background survival.** Anyone can start a foreground service.
|
||||
Making shift-end fire from a *killed process* means: knowing
|
||||
`setExactAndAllowWhileIdle` is the only AlarmManager call that pierces Doze;
|
||||
knowing a broadcast receiver shouldn't boot a Flutter engine; knowing Flutter's
|
||||
`SharedPreferences` keys carry a `flutter.` prefix readable from Kotlin; and
|
||||
knowing you need a `PARTIAL_WAKE_LOCK` or the device sleeps mid-HTTP-request. Each
|
||||
of those is a scar.
|
||||
|
||||
**5. Flicker-free declarative annotations over an imperative native map.**
|
||||
MapLibre's controller is imperative — `addSymbol`, `updateSymbol`, `removeSymbol`
|
||||
— and non-re-entrant. Wrapping that in a declarative Flutter widget requires a
|
||||
keyed diff, sync coalescing, and an equality contract that excludes callbacks. The
|
||||
`onTap`-excluded `==` is a subtle, correct call that most implementations get wrong
|
||||
and then blame on "MapLibre being flickery."
|
||||
|
||||
**6. Overnight shift arithmetic.** Deceptively small. Bare `HH:mm:ss` with no date
|
||||
makes "is the shift over?" ambiguous, and the answer inverts for shifts crossing
|
||||
midnight. Getting it wrong means riders auto-clocked-out at 10 PM mid-delivery, or
|
||||
never clocked out at all.
|
||||
|
||||
**7. Graceful degradation as a design stance.** The four-tier distance cascade,
|
||||
the four-tier start-time fallback, the fail-open geofence next to the fail-closed
|
||||
coordinate validator — these aren't defensive-programming reflexes. Someone
|
||||
decided, per code path, *which* wrong answer is least harmful, and the policies
|
||||
differ where they should. That's the mark of software written by someone who had to
|
||||
answer for its failures in the field.
|
||||
|
||||
---
|
||||
|
||||
## Security & Sensitivity Flags
|
||||
|
||||
**⚠️ Credentials in source — do not include any of this in a portfolio, and rotate
|
||||
before publishing the repo.** All read directly, not inferred:
|
||||
|
||||
| Location | Issue |
|
||||
| --- | --- |
|
||||
| `lib/controllers/deliveries_controller.dart:75-76` | DigitalOcean Spaces **access key + secret key** hardcoded. These ship inside the APK and are recoverable with `strings`. They grant object-storage writes. |
|
||||
| `lib/views/helpers/constants/mqtt_constants.dart` | Broker **IP, username, and password** hardcoded. Port 1883 is **unencrypted MQTT** — credentials and every rider's live location cross the network in plaintext. |
|
||||
| `lib/helpers/http_overrides.dart` | `badCertificateCallback` returns `true` unconditionally, installed globally in `main()` **and** in the background isolate. This disables TLS certificate validation app-wide, making every API call MITM-able. |
|
||||
| Repo root & `android/` | `nearlerider-keystore.jks`, `release-key.jks`, `android/key.properties`, `upload_certificate.pem` are **tracked in git**. The signing keystore plus its password file is the set that lets someone publish as you. |
|
||||
| git history | The previous Google Maps API key remains in history and in every already-shipped APK. It needs revoking in Google Cloud Console regardless of the migration. |
|
||||
|
||||
**Commercially sensitive — describe generically, don't quote:**
|
||||
|
||||
- The compensation formula and its rate key, bonus-point rules, and the 3-hour
|
||||
skip-penalty window
|
||||
- Geofence radius policy
|
||||
- Backend host topology and endpoint paths (two distinct hosts, versioned APIs)
|
||||
- MQTT topic schema and broker address
|
||||
|
||||
**Inference vs. reading.** Everything above is read from source except: the backend
|
||||
implementation (only its HTTP surface is visible from here), the server-side `step`
|
||||
assignment algorithm (the app consumes it, doesn't compute it), and the actual
|
||||
production billing figures for the old Google setup (the pricing model is
|
||||
documented, the invoice isn't in the repo).
|
||||
|
||||
**Also worth knowing for accuracy:** the app is Android-only in practice —
|
||||
`abiFilters "arm64-v8a"`, and the iOS directory has no Podfile. The root
|
||||
`README.md` is still the unmodified Flutter template.
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -1,209 +1,209 @@
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
import 'dart:math' show Random;
|
||||
|
||||
import 'package:get/get.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:image_picker/image_picker.dart';
|
||||
import 'package:minio/io.dart';
|
||||
import 'package:minio/minio.dart';
|
||||
import 'package:nearle/Models/supportticket/support_ticket.dart';
|
||||
|
||||
// Minimal local stub for DigitalOcean Spaces client to avoid undefined name errors.
|
||||
// Replace this with a real package or implementation for production uploads.
|
||||
class dospace {
|
||||
static DOSpaceClient DOSpace({
|
||||
required String region,
|
||||
required String accessKey,
|
||||
required String secretKey,
|
||||
}) =>
|
||||
DOSpaceClient(region: region, accessKey: accessKey, secretKey: secretKey);
|
||||
|
||||
static final ACL = _ACL();
|
||||
}
|
||||
|
||||
class _ACL {
|
||||
final String publicRead = 'public-read';
|
||||
}
|
||||
|
||||
class DOSpaceClient {
|
||||
final String region;
|
||||
final String accessKey;
|
||||
final String secretKey;
|
||||
|
||||
DOSpaceClient({
|
||||
required this.region,
|
||||
required this.accessKey,
|
||||
required this.secretKey,
|
||||
});
|
||||
|
||||
Future<void> putObject({
|
||||
required String bucketName,
|
||||
required String objectName,
|
||||
required File file,
|
||||
required String acl,
|
||||
required String contentType,
|
||||
}) async {
|
||||
// No-op stub: implement actual upload logic here or use a proper package.
|
||||
await Future<void>.value();
|
||||
}
|
||||
}
|
||||
|
||||
class SupportTicketController extends GetxController {
|
||||
final RxList<SupportTicketModel> tickets = <SupportTicketModel>[].obs;
|
||||
final RxBool isLoading = true.obs;
|
||||
final RxString errorMessage = ''.obs;
|
||||
final RxBool isSubmitting = false.obs;
|
||||
|
||||
@override
|
||||
void onInit() {
|
||||
super.onInit();
|
||||
fetchTickets();
|
||||
}
|
||||
|
||||
// ---------- FETCH TICKETS ----------
|
||||
Future<void> fetchTickets() async {
|
||||
try {
|
||||
isLoading(true);
|
||||
errorMessage('');
|
||||
|
||||
const userId = 1242;
|
||||
final url = Uri.parse(
|
||||
'https://jupiter.nearle.app/live/api/v1/partners/getridersupport/?userid=$userId');
|
||||
|
||||
final response = await http.get(url, headers: {
|
||||
'Accept': 'application/json',
|
||||
});
|
||||
|
||||
if (response.statusCode != 200) {
|
||||
throw Exception('Server error: ${response.statusCode}');
|
||||
}
|
||||
|
||||
final Map<String, dynamic> jsonResponse = json.decode(response.body);
|
||||
if (jsonResponse['status'] != true) {
|
||||
throw Exception(jsonResponse['message'] ?? 'Unknown error');
|
||||
}
|
||||
|
||||
final List<dynamic> data = jsonResponse['data'];
|
||||
tickets.assignAll(data.map((e) => SupportTicketModel.fromJson(e)).toList());
|
||||
} catch (e) {
|
||||
errorMessage(e.toString());
|
||||
} finally {
|
||||
isLoading(false);
|
||||
}
|
||||
}
|
||||
|
||||
// ---------- UPLOAD IMAGE TO DO SPACES ----------
|
||||
Future<String?> uploadImageToDOSpaces(File imageFile, int userId) async {
|
||||
try {
|
||||
final rng = Random();
|
||||
const String region = "sgp1";
|
||||
const String accessKey = "DO00NQER7N2FRYZAB2HR";
|
||||
const String secretKey = "nMDewX25IBEu1FM5dakK+v28/WbW3TzBAwq913+dxP0";
|
||||
const String bucketName = "nearle";
|
||||
const String folderName = "support";
|
||||
|
||||
// File name
|
||||
final String fileName = 'ticket-${rng.nextInt(10000)}-$userId.jpg';
|
||||
|
||||
// Object path inside the bucket
|
||||
final String objectPath = "$folderName/$fileName";
|
||||
|
||||
// CDN URL you want
|
||||
final String cdnUrl = "https://images.nearle.app/$objectPath";
|
||||
|
||||
// Initialize Minio
|
||||
final minio = Minio(
|
||||
endPoint: "$region.digitaloceanspaces.com",
|
||||
accessKey: accessKey,
|
||||
secretKey: secretKey,
|
||||
region: region,
|
||||
useSSL: true,
|
||||
);
|
||||
|
||||
print("Uploading: $objectPath");
|
||||
|
||||
// Upload to DO Spaces
|
||||
await minio.fPutObject(
|
||||
bucketName,
|
||||
objectPath,
|
||||
imageFile.path,
|
||||
metadata: {
|
||||
"Content-Type": "image/jpeg",
|
||||
"x-amz-acl": "public-read",
|
||||
},
|
||||
);
|
||||
|
||||
print("Uploaded Successfully: $cdnUrl");
|
||||
return cdnUrl;
|
||||
} catch (e) {
|
||||
print("Upload error: $e");
|
||||
Get.snackbar("Error", "Image upload failed.");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// ---------- CREATE TICKET ----------
|
||||
// ---------- CREATE TICKET ----------
|
||||
Future<bool> createTicket({
|
||||
required int userid,
|
||||
required String category,
|
||||
required String priority,
|
||||
required String subject,
|
||||
required String issue,
|
||||
List<XFile>? attachments,
|
||||
}) async {
|
||||
try {
|
||||
isSubmitting(true);
|
||||
String imageUrl = "";
|
||||
|
||||
// Upload first image if attached
|
||||
if (attachments != null && attachments.isNotEmpty) {
|
||||
final XFile xFile = attachments.first;
|
||||
final File file = File(xFile.path);
|
||||
final uploadedUrl = await uploadImageToDOSpaces(file, userid);
|
||||
|
||||
// Only assign if a valid URL (short length)
|
||||
if (uploadedUrl != null && uploadedUrl.length < 200) {
|
||||
imageUrl = uploadedUrl;
|
||||
} else {
|
||||
print("⚠️ Skipping image URL because it’s too long or invalid.");
|
||||
}
|
||||
}
|
||||
|
||||
// Create ticket request body
|
||||
final Map<String, dynamic> payload = {
|
||||
'userid': userid,
|
||||
'category': category,
|
||||
'priority': priority,
|
||||
'subject': subject,
|
||||
'issue': issue,
|
||||
'image': imageUrl, // ✅ Always short string or empty
|
||||
};
|
||||
|
||||
final response = await http.post(
|
||||
Uri.parse('https://jupiter.nearle.app/live/api/v1/partners/createridersupport/'),
|
||||
headers: {'Accept': 'application/json', 'Content-Type': 'application/json'},
|
||||
body: jsonEncode(payload),
|
||||
);
|
||||
|
||||
if (response.statusCode != 200) {
|
||||
throw Exception('Ticket creation failed: ${response.statusCode}');
|
||||
}
|
||||
|
||||
final jsonResponse = jsonDecode(response.body);
|
||||
if (jsonResponse['status'] != true) {
|
||||
throw Exception(jsonResponse['message'] ?? 'Unknown error');
|
||||
}
|
||||
|
||||
await fetchTickets(); // Refresh list after success
|
||||
return true;
|
||||
} catch (e) {
|
||||
errorMessage(e.toString());
|
||||
return false;
|
||||
} finally {
|
||||
isSubmitting(false);
|
||||
}
|
||||
}
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
import 'dart:math' show Random;
|
||||
|
||||
import 'package:get/get.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:image_picker/image_picker.dart';
|
||||
import 'package:minio/io.dart';
|
||||
import 'package:minio/minio.dart';
|
||||
import 'package:nearle/Models/supportticket/support_ticket.dart';
|
||||
|
||||
// Minimal local stub for DigitalOcean Spaces client to avoid undefined name errors.
|
||||
// Replace this with a real package or implementation for production uploads.
|
||||
class dospace {
|
||||
static DOSpaceClient DOSpace({
|
||||
required String region,
|
||||
required String accessKey,
|
||||
required String secretKey,
|
||||
}) =>
|
||||
DOSpaceClient(region: region, accessKey: accessKey, secretKey: secretKey);
|
||||
|
||||
static final ACL = _ACL();
|
||||
}
|
||||
|
||||
class _ACL {
|
||||
final String publicRead = 'public-read';
|
||||
}
|
||||
|
||||
class DOSpaceClient {
|
||||
final String region;
|
||||
final String accessKey;
|
||||
final String secretKey;
|
||||
|
||||
DOSpaceClient({
|
||||
required this.region,
|
||||
required this.accessKey,
|
||||
required this.secretKey,
|
||||
});
|
||||
|
||||
Future<void> putObject({
|
||||
required String bucketName,
|
||||
required String objectName,
|
||||
required File file,
|
||||
required String acl,
|
||||
required String contentType,
|
||||
}) async {
|
||||
// No-op stub: implement actual upload logic here or use a proper package.
|
||||
await Future<void>.value();
|
||||
}
|
||||
}
|
||||
|
||||
class SupportTicketController extends GetxController {
|
||||
final RxList<SupportTicketModel> tickets = <SupportTicketModel>[].obs;
|
||||
final RxBool isLoading = true.obs;
|
||||
final RxString errorMessage = ''.obs;
|
||||
final RxBool isSubmitting = false.obs;
|
||||
|
||||
@override
|
||||
void onInit() {
|
||||
super.onInit();
|
||||
fetchTickets();
|
||||
}
|
||||
|
||||
// ---------- FETCH TICKETS ----------
|
||||
Future<void> fetchTickets() async {
|
||||
try {
|
||||
isLoading(true);
|
||||
errorMessage('');
|
||||
|
||||
const userId = 1242;
|
||||
final url = Uri.parse(
|
||||
'https://jupiter.nearle.app/live/api/v1/partners/getridersupport/?userid=$userId');
|
||||
|
||||
final response = await http.get(url, headers: {
|
||||
'Accept': 'application/json',
|
||||
});
|
||||
|
||||
if (response.statusCode != 200) {
|
||||
throw Exception('Server error: ${response.statusCode}');
|
||||
}
|
||||
|
||||
final Map<String, dynamic> jsonResponse = json.decode(response.body);
|
||||
if (jsonResponse['status'] != true) {
|
||||
throw Exception(jsonResponse['message'] ?? 'Unknown error');
|
||||
}
|
||||
|
||||
final List<dynamic> data = jsonResponse['data'];
|
||||
tickets.assignAll(data.map((e) => SupportTicketModel.fromJson(e)).toList());
|
||||
} catch (e) {
|
||||
errorMessage(e.toString());
|
||||
} finally {
|
||||
isLoading(false);
|
||||
}
|
||||
}
|
||||
|
||||
// ---------- UPLOAD IMAGE TO DO SPACES ----------
|
||||
Future<String?> uploadImageToDOSpaces(File imageFile, int userId) async {
|
||||
try {
|
||||
final rng = Random();
|
||||
const String region = "sgp1";
|
||||
const String accessKey = "DO00NQER7N2FRYZAB2HR";
|
||||
const String secretKey = "nMDewX25IBEu1FM5dakK+v28/WbW3TzBAwq913+dxP0";
|
||||
const String bucketName = "nearle";
|
||||
const String folderName = "support";
|
||||
|
||||
// File name
|
||||
final String fileName = 'ticket-${rng.nextInt(10000)}-$userId.jpg';
|
||||
|
||||
// Object path inside the bucket
|
||||
final String objectPath = "$folderName/$fileName";
|
||||
|
||||
// CDN URL you want
|
||||
final String cdnUrl = "https://images.nearle.app/$objectPath";
|
||||
|
||||
// Initialize Minio
|
||||
final minio = Minio(
|
||||
endPoint: "$region.digitaloceanspaces.com",
|
||||
accessKey: accessKey,
|
||||
secretKey: secretKey,
|
||||
region: region,
|
||||
useSSL: true,
|
||||
);
|
||||
|
||||
print("Uploading: $objectPath");
|
||||
|
||||
// Upload to DO Spaces
|
||||
await minio.fPutObject(
|
||||
bucketName,
|
||||
objectPath,
|
||||
imageFile.path,
|
||||
metadata: {
|
||||
"Content-Type": "image/jpeg",
|
||||
"x-amz-acl": "public-read",
|
||||
},
|
||||
);
|
||||
|
||||
print("Uploaded Successfully: $cdnUrl");
|
||||
return cdnUrl;
|
||||
} catch (e) {
|
||||
print("Upload error: $e");
|
||||
Get.snackbar("Error", "Image upload failed.");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// ---------- CREATE TICKET ----------
|
||||
// ---------- CREATE TICKET ----------
|
||||
Future<bool> createTicket({
|
||||
required int userid,
|
||||
required String category,
|
||||
required String priority,
|
||||
required String subject,
|
||||
required String issue,
|
||||
List<XFile>? attachments,
|
||||
}) async {
|
||||
try {
|
||||
isSubmitting(true);
|
||||
String imageUrl = "";
|
||||
|
||||
// Upload first image if attached
|
||||
if (attachments != null && attachments.isNotEmpty) {
|
||||
final XFile xFile = attachments.first;
|
||||
final File file = File(xFile.path);
|
||||
final uploadedUrl = await uploadImageToDOSpaces(file, userid);
|
||||
|
||||
// Only assign if a valid URL (short length)
|
||||
if (uploadedUrl != null && uploadedUrl.length < 200) {
|
||||
imageUrl = uploadedUrl;
|
||||
} else {
|
||||
print("⚠️ Skipping image URL because it's too long or invalid.");
|
||||
}
|
||||
}
|
||||
|
||||
// Create ticket request body
|
||||
final Map<String, dynamic> payload = {
|
||||
'userid': userid,
|
||||
'category': category,
|
||||
'priority': priority,
|
||||
'subject': subject,
|
||||
'issue': issue,
|
||||
'image': imageUrl, // ✅ Always short string or empty
|
||||
};
|
||||
|
||||
final response = await http.post(
|
||||
Uri.parse('https://jupiter.nearle.app/live/api/v1/partners/createridersupport/'),
|
||||
headers: {'Accept': 'application/json', 'Content-Type': 'application/json'},
|
||||
body: jsonEncode(payload),
|
||||
);
|
||||
|
||||
if (response.statusCode != 200) {
|
||||
throw Exception('Ticket creation failed: ${response.statusCode}');
|
||||
}
|
||||
|
||||
final jsonResponse = jsonDecode(response.body);
|
||||
if (jsonResponse['status'] != true) {
|
||||
throw Exception(jsonResponse['message'] ?? 'Unknown error');
|
||||
}
|
||||
|
||||
await fetchTickets(); // Refresh list after success
|
||||
return true;
|
||||
} catch (e) {
|
||||
errorMessage(e.toString());
|
||||
return false;
|
||||
} finally {
|
||||
isSubmitting(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
44
lib/services/maps/map_config.dart
Normal file
44
lib/services/maps/map_config.dart
Normal file
@@ -0,0 +1,44 @@
|
||||
/// Map and routing endpoints.
|
||||
///
|
||||
/// Everything here is open source and keyless:
|
||||
///
|
||||
/// * **Tiles** — OpenFreeMap (<https://openfreemap.org>), OpenStreetMap data
|
||||
/// built with Planetiler and served as MapLibre vector tiles. No API key, no
|
||||
/// registration, no request limits, MIT licensed. MapLibre adds the required
|
||||
/// attribution automatically.
|
||||
/// * **Routing** — OSRM (<https://project-osrm.org>), the Open Source Routing
|
||||
/// Machine. Also keyless.
|
||||
///
|
||||
/// Both are overridable at build time so production can point at self-hosted
|
||||
/// instances without a code change:
|
||||
///
|
||||
/// flutter build apk \
|
||||
/// --dart-define=OSRM_BASE_URL=https://osrm.internal.nearle.in \
|
||||
/// --dart-define=MAP_STYLE_URL=https://tiles.internal.nearle.in/styles/liberty
|
||||
class MapConfig {
|
||||
const MapConfig._();
|
||||
|
||||
/// MapLibre style document. Passed straight to the map — no key to inject,
|
||||
/// so unlike a commercial provider there is nothing to patch or proxy.
|
||||
///
|
||||
/// OpenFreeMap styles: `liberty` (detailed), `bright`, `positron` (muted).
|
||||
static const String styleUrl = String.fromEnvironment(
|
||||
'MAP_STYLE_URL',
|
||||
defaultValue: 'https://tiles.openfreemap.org/styles/liberty',
|
||||
);
|
||||
|
||||
/// Base URL of an OSRM instance, without a trailing slash.
|
||||
///
|
||||
/// The default is the FOSSGIS community server. It is free and needs no key,
|
||||
/// but it is donation-funded infrastructure with a fair-use policy — fine for
|
||||
/// development, NOT appropriate for a production delivery fleet. Point this
|
||||
/// at your own OSRM before shipping; see `deploy/osrm/README.md`.
|
||||
static const String osrmBaseUrl = String.fromEnvironment(
|
||||
'OSRM_BASE_URL',
|
||||
defaultValue: 'https://routing.openstreetmap.de/routed-car',
|
||||
);
|
||||
|
||||
/// True when routing points somewhere other than the shared community server.
|
||||
static bool get usingOwnRoutingServer =>
|
||||
!osrmBaseUrl.contains('routing.openstreetmap.de');
|
||||
}
|
||||
242
lib/services/maps/map_marker_icons.dart
Normal file
242
lib/services/maps/map_marker_icons.dart
Normal file
@@ -0,0 +1,242 @@
|
||||
import 'dart:typed_data';
|
||||
import 'dart:ui' as ui;
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
/// A marker bitmap plus a stable identity for it.
|
||||
///
|
||||
/// MapLibre registers images on the style by name, so every distinct bitmap
|
||||
/// needs a key that is equal whenever the pixels are equal.
|
||||
@immutable
|
||||
class MarkerIcon {
|
||||
const MarkerIcon(this.key, this.bytes);
|
||||
|
||||
final String key;
|
||||
final Uint8List bytes;
|
||||
|
||||
@override
|
||||
bool operator ==(Object other) => other is MarkerIcon && other.key == key;
|
||||
|
||||
@override
|
||||
int get hashCode => key.hashCode;
|
||||
}
|
||||
|
||||
/// Marker bitmaps drawn at runtime with a canvas.
|
||||
///
|
||||
/// MapLibre has no equivalent of Google's `BitmapDescriptor.defaultMarker*`,
|
||||
/// so the pins the app used to get for free are drawn here instead. Everything
|
||||
/// is generated in-process — no new image assets, no network fetch.
|
||||
class MapMarkerIcons {
|
||||
const MapMarkerIcons._();
|
||||
|
||||
static final Map<String, MarkerIcon> _cache = {};
|
||||
|
||||
/// Classic teardrop pin, anchored at its tip.
|
||||
static Future<MarkerIcon> pin(Color color) =>
|
||||
_memo('pin_${color.toARGB32()}', () => _drawPin(color));
|
||||
|
||||
/// Filled circle with a white ring — used for the rider's own position.
|
||||
static Future<MarkerIcon> dot(Color color) =>
|
||||
_memo('dot_${color.toARGB32()}', () => _drawDot(color));
|
||||
|
||||
/// Numbered step marker for the multi-stop route screen.
|
||||
static Future<MarkerIcon> numberedCircle(
|
||||
int number, {
|
||||
required Color primaryColor,
|
||||
bool isSkipped = false,
|
||||
bool isNext = false,
|
||||
}) => _memo(
|
||||
'step_${number}_${primaryColor.toARGB32()}_${isSkipped}_$isNext',
|
||||
() => _drawNumberedCircle(
|
||||
number,
|
||||
primaryColor: primaryColor,
|
||||
isSkipped: isSkipped,
|
||||
isNext: isNext,
|
||||
),
|
||||
);
|
||||
|
||||
static Future<MarkerIcon> _memo(
|
||||
String key,
|
||||
Future<Uint8List> Function() build,
|
||||
) async {
|
||||
final cached = _cache[key];
|
||||
if (cached != null) return cached;
|
||||
return _cache[key] = MarkerIcon(key, await build());
|
||||
}
|
||||
|
||||
static Future<Uint8List> _toBytes(
|
||||
ui.PictureRecorder recorder,
|
||||
int width,
|
||||
int height,
|
||||
) async {
|
||||
final image = await recorder.endRecording().toImage(width, height);
|
||||
final data = await image.toByteData(format: ui.ImageByteFormat.png);
|
||||
image.dispose();
|
||||
return data!.buffer.asUint8List();
|
||||
}
|
||||
|
||||
static Future<Uint8List> _drawPin(Color color) async {
|
||||
const double width = 72;
|
||||
const double height = 96;
|
||||
const double radius = 27;
|
||||
const center = Offset(width / 2, radius + 4);
|
||||
|
||||
final recorder = ui.PictureRecorder();
|
||||
final canvas = Canvas(recorder);
|
||||
|
||||
final body = Paint()
|
||||
..color = color
|
||||
..isAntiAlias = true;
|
||||
final outline = Paint()
|
||||
..color = Colors.white
|
||||
..style = PaintingStyle.stroke
|
||||
..strokeWidth = 4
|
||||
..isAntiAlias = true;
|
||||
|
||||
// Tail: a curve from the circle's lower flanks down to the anchor point.
|
||||
final tail = Path()
|
||||
..moveTo(center.dx - radius + 4, center.dy + radius - 9)
|
||||
..quadraticBezierTo(
|
||||
center.dx - 6,
|
||||
height - 14,
|
||||
center.dx,
|
||||
height - 4,
|
||||
)
|
||||
..quadraticBezierTo(
|
||||
center.dx + 6,
|
||||
height - 14,
|
||||
center.dx + radius - 4,
|
||||
center.dy + radius - 9,
|
||||
)
|
||||
..close();
|
||||
|
||||
canvas.drawPath(tail, body);
|
||||
canvas.drawCircle(center, radius, body);
|
||||
canvas.drawCircle(center, radius, outline);
|
||||
canvas.drawCircle(center, 9, Paint()..color = Colors.white);
|
||||
|
||||
return _toBytes(recorder, width.toInt(), height.toInt());
|
||||
}
|
||||
|
||||
static Future<Uint8List> _drawDot(Color color) async {
|
||||
const double size = 48;
|
||||
const center = Offset(size / 2, size / 2);
|
||||
|
||||
final recorder = ui.PictureRecorder();
|
||||
final canvas = Canvas(recorder);
|
||||
|
||||
canvas.drawCircle(
|
||||
center,
|
||||
20,
|
||||
Paint()
|
||||
..color = color.withValues(alpha: 0.25)
|
||||
..isAntiAlias = true,
|
||||
);
|
||||
canvas.drawCircle(
|
||||
center,
|
||||
12,
|
||||
Paint()
|
||||
..color = Colors.white
|
||||
..isAntiAlias = true,
|
||||
);
|
||||
canvas.drawCircle(
|
||||
center,
|
||||
9,
|
||||
Paint()
|
||||
..color = color
|
||||
..isAntiAlias = true,
|
||||
);
|
||||
|
||||
return _toBytes(recorder, size.toInt(), size.toInt());
|
||||
}
|
||||
|
||||
/// Preserves the exact look of the previous Google Maps step marker.
|
||||
static Future<Uint8List> _drawNumberedCircle(
|
||||
int number, {
|
||||
required Color primaryColor,
|
||||
required bool isSkipped,
|
||||
required bool isNext,
|
||||
}) async {
|
||||
const double size = 70;
|
||||
final recorder = ui.PictureRecorder();
|
||||
final canvas = Canvas(recorder);
|
||||
const center = Offset(size / 2, size / 2);
|
||||
|
||||
final paint = Paint()..color = isSkipped ? Colors.orange : primaryColor;
|
||||
canvas.drawCircle(center, 15, paint);
|
||||
|
||||
final border = Paint()
|
||||
..color = isSkipped ? Colors.orange.shade900 : Colors.white
|
||||
..style = PaintingStyle.stroke
|
||||
..strokeWidth = isSkipped ? 4 : 3;
|
||||
canvas.drawCircle(center, 15, border);
|
||||
|
||||
final textPainter = TextPainter(
|
||||
text: TextSpan(
|
||||
text: number.toString(),
|
||||
style: const TextStyle(
|
||||
fontSize: 19,
|
||||
color: Colors.white,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
textDirection: TextDirection.ltr,
|
||||
);
|
||||
textPainter.layout();
|
||||
textPainter.paint(
|
||||
canvas,
|
||||
Offset(
|
||||
center.dx - textPainter.width / 2,
|
||||
center.dy - textPainter.height / 2,
|
||||
),
|
||||
);
|
||||
|
||||
if (isNext) {
|
||||
final signPaint = Paint()
|
||||
..color = Colors.green
|
||||
..style = PaintingStyle.fill;
|
||||
final signRect = RRect.fromRectAndRadius(
|
||||
Rect.fromCenter(center: const Offset(size / 2, 8), width: 45, height: 30),
|
||||
const Radius.circular(4),
|
||||
);
|
||||
canvas.drawRRect(signRect, signPaint);
|
||||
|
||||
final signBorder = Paint()
|
||||
..color = Colors.white
|
||||
..style = PaintingStyle.stroke
|
||||
..strokeWidth = 1.5;
|
||||
canvas.drawRRect(signRect, signBorder);
|
||||
|
||||
final nextTextPainter = TextPainter(
|
||||
text: const TextSpan(
|
||||
text: 'NEXT',
|
||||
style: TextStyle(
|
||||
fontSize: 12,
|
||||
color: Colors.white,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
textDirection: TextDirection.ltr,
|
||||
);
|
||||
nextTextPainter.layout();
|
||||
nextTextPainter.paint(
|
||||
canvas,
|
||||
Offset(size / 2 - nextTextPainter.width / 2, 4),
|
||||
);
|
||||
|
||||
final arrowPath = Path()
|
||||
..moveTo(size / 2, 18)
|
||||
..lineTo(size / 2 - 4, 24)
|
||||
..lineTo(size / 2 + 4, 24)
|
||||
..close();
|
||||
canvas.drawPath(
|
||||
arrowPath,
|
||||
Paint()
|
||||
..color = Colors.white
|
||||
..style = PaintingStyle.fill,
|
||||
);
|
||||
}
|
||||
|
||||
return _toBytes(recorder, size.toInt(), size.toInt());
|
||||
}
|
||||
}
|
||||
374
lib/services/maps/nearle_map.dart
Normal file
374
lib/services/maps/nearle_map.dart
Normal file
@@ -0,0 +1,374 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/foundation.dart' show listEquals;
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:maplibre_gl/maplibre_gl.dart';
|
||||
|
||||
import 'map_config.dart';
|
||||
import 'map_marker_icons.dart';
|
||||
|
||||
export 'package:maplibre_gl/maplibre_gl.dart'
|
||||
show LatLng, LatLngBounds, CameraPosition, CameraUpdate, MyLocationTrackingMode;
|
||||
|
||||
/// Where a marker bitmap sits relative to its coordinate.
|
||||
enum MarkerAnchor { center, bottom }
|
||||
|
||||
@immutable
|
||||
class NearleMapMarker {
|
||||
const NearleMapMarker({
|
||||
required this.id,
|
||||
required this.position,
|
||||
required this.icon,
|
||||
this.anchor = MarkerAnchor.bottom,
|
||||
this.size = 1.0,
|
||||
this.onTap,
|
||||
});
|
||||
|
||||
final String id;
|
||||
final LatLng position;
|
||||
final MarkerIcon icon;
|
||||
final MarkerAnchor anchor;
|
||||
final double size;
|
||||
final VoidCallback? onTap;
|
||||
|
||||
/// Identity for diffing. Deliberately excludes [onTap] — a new closure on
|
||||
/// every rebuild must not force the marker to be torn down and re-added.
|
||||
@override
|
||||
bool operator ==(Object other) =>
|
||||
other is NearleMapMarker &&
|
||||
other.id == id &&
|
||||
other.position.latitude == position.latitude &&
|
||||
other.position.longitude == position.longitude &&
|
||||
other.icon == icon &&
|
||||
other.anchor == anchor &&
|
||||
other.size == size;
|
||||
|
||||
@override
|
||||
int get hashCode =>
|
||||
Object.hash(id, position.latitude, position.longitude, icon, anchor, size);
|
||||
}
|
||||
|
||||
@immutable
|
||||
class NearleMapPolyline {
|
||||
const NearleMapPolyline({
|
||||
required this.id,
|
||||
required this.points,
|
||||
required this.color,
|
||||
this.width = 5.0,
|
||||
});
|
||||
|
||||
final String id;
|
||||
final List<LatLng> points;
|
||||
final Color color;
|
||||
final double width;
|
||||
|
||||
@override
|
||||
bool operator ==(Object other) =>
|
||||
other is NearleMapPolyline &&
|
||||
other.id == id &&
|
||||
other.color == color &&
|
||||
other.width == width &&
|
||||
other.points.length == points.length &&
|
||||
(points.isEmpty ||
|
||||
(other.points.first.latitude == points.first.latitude &&
|
||||
other.points.first.longitude == points.first.longitude &&
|
||||
other.points.last.latitude == points.last.latitude &&
|
||||
other.points.last.longitude == points.last.longitude));
|
||||
|
||||
@override
|
||||
int get hashCode => Object.hash(id, color, width, points.length);
|
||||
}
|
||||
|
||||
/// Thin, crash-tolerant facade over [MapLibreMapController].
|
||||
class NearleMapController {
|
||||
NearleMapController._(this._raw, this._isAlive);
|
||||
|
||||
final MapLibreMapController _raw;
|
||||
final bool Function() _isAlive;
|
||||
|
||||
/// Escape hatch for anything this facade does not wrap.
|
||||
MapLibreMapController get raw => _raw;
|
||||
|
||||
/// Frames [bounds] with uniform [padding] in logical pixels.
|
||||
///
|
||||
/// Degenerate bounds (a single point, or a perfectly horizontal/vertical
|
||||
/// pair) make the native fit routine fail, so they are nudged outward first.
|
||||
Future<void> fitBounds(LatLngBounds bounds, {double padding = 80}) async {
|
||||
if (!_isAlive()) return;
|
||||
const double minSpan = 0.0015; // ~165 m
|
||||
|
||||
var south = bounds.southwest.latitude;
|
||||
var north = bounds.northeast.latitude;
|
||||
var west = bounds.southwest.longitude;
|
||||
var east = bounds.northeast.longitude;
|
||||
|
||||
if (north - south < minSpan) {
|
||||
final mid = (north + south) / 2;
|
||||
south = mid - minSpan / 2;
|
||||
north = mid + minSpan / 2;
|
||||
}
|
||||
if (east - west < minSpan) {
|
||||
final mid = (east + west) / 2;
|
||||
west = mid - minSpan / 2;
|
||||
east = mid + minSpan / 2;
|
||||
}
|
||||
|
||||
await _guard(
|
||||
() => _raw.animateCamera(
|
||||
CameraUpdate.newLatLngBounds(
|
||||
LatLngBounds(
|
||||
southwest: LatLng(south, west),
|
||||
northeast: LatLng(north, east),
|
||||
),
|
||||
left: padding,
|
||||
top: padding,
|
||||
right: padding,
|
||||
bottom: padding,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> animateCamera(CameraUpdate update) =>
|
||||
_guard(() => _raw.animateCamera(update));
|
||||
|
||||
Future<void> moveCamera(CameraUpdate update) =>
|
||||
_guard(() => _raw.moveCamera(update));
|
||||
|
||||
Future<void> _guard(Future<void> Function() action) async {
|
||||
if (!_isAlive()) return;
|
||||
try {
|
||||
await action();
|
||||
} catch (e) {
|
||||
debugPrint('[NEARLE_MAP] Camera command skipped: $e');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The app's single map surface, backed by MapLibre + OpenStreetMap vector
|
||||
/// tiles. No API key is involved anywhere in this widget.
|
||||
///
|
||||
/// Markers and polylines are declarative: pass new lists and the widget diffs
|
||||
/// them against what is on the map, updating only what actually changed. That
|
||||
/// matters on the navigation screen, where the rider marker moves every few
|
||||
/// metres and a clear-and-re-add would flicker.
|
||||
class NearleMap extends StatefulWidget {
|
||||
const NearleMap({
|
||||
super.key,
|
||||
required this.initialCameraPosition,
|
||||
this.markers = const [],
|
||||
this.polylines = const [],
|
||||
this.myLocationEnabled = false,
|
||||
this.trackingMode = MyLocationTrackingMode.none,
|
||||
this.compassEnabled = false,
|
||||
this.zoomGesturesEnabled = true,
|
||||
this.scrollGesturesEnabled = true,
|
||||
this.rotateGesturesEnabled = true,
|
||||
this.tiltGesturesEnabled = true,
|
||||
this.onMapReady,
|
||||
});
|
||||
|
||||
final CameraPosition initialCameraPosition;
|
||||
final List<NearleMapMarker> markers;
|
||||
final List<NearleMapPolyline> polylines;
|
||||
final bool myLocationEnabled;
|
||||
final MyLocationTrackingMode trackingMode;
|
||||
final bool compassEnabled;
|
||||
final bool zoomGesturesEnabled;
|
||||
final bool scrollGesturesEnabled;
|
||||
final bool rotateGesturesEnabled;
|
||||
final bool tiltGesturesEnabled;
|
||||
final void Function(NearleMapController controller)? onMapReady;
|
||||
|
||||
@override
|
||||
State<NearleMap> createState() => _NearleMapState();
|
||||
}
|
||||
|
||||
class _NearleMapState extends State<NearleMap> {
|
||||
MapLibreMapController? _controller;
|
||||
NearleMapController? _facade;
|
||||
|
||||
bool _styleLoaded = false;
|
||||
bool _disposed = false;
|
||||
|
||||
final Set<String> _registeredIcons = {};
|
||||
final Map<String, Symbol> _symbols = {};
|
||||
final Map<String, NearleMapMarker> _markerById = {};
|
||||
final Map<String, Line> _lines = {};
|
||||
final Map<String, NearleMapPolyline> _polylineById = {};
|
||||
|
||||
bool _syncing = false;
|
||||
bool _syncQueued = false;
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_disposed = true;
|
||||
_controller?.onSymbolTapped.remove(_onSymbolTapped);
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
bool _isAlive() => !_disposed && mounted && _styleLoaded;
|
||||
|
||||
Future<void> _onStyleLoaded() async {
|
||||
final controller = _controller;
|
||||
if (controller == null || _disposed) return;
|
||||
|
||||
_styleLoaded = true;
|
||||
// Delivery pins overlap constantly in dense areas; without this MapLibre
|
||||
// silently hides the collided ones.
|
||||
try {
|
||||
await controller.setSymbolIconAllowOverlap(true);
|
||||
await controller.setSymbolIconIgnorePlacement(true);
|
||||
} catch (e) {
|
||||
debugPrint('[NEARLE_MAP] Overlap config skipped: $e');
|
||||
}
|
||||
|
||||
await _sync();
|
||||
|
||||
final facade = _facade;
|
||||
if (facade != null && !_disposed) widget.onMapReady?.call(facade);
|
||||
}
|
||||
|
||||
@override
|
||||
void didUpdateWidget(covariant NearleMap oldWidget) {
|
||||
super.didUpdateWidget(oldWidget);
|
||||
final markersChanged = !listEquals(oldWidget.markers, widget.markers);
|
||||
final linesChanged = !listEquals(oldWidget.polylines, widget.polylines);
|
||||
if (markersChanged || linesChanged) _sync();
|
||||
}
|
||||
|
||||
/// Reconciles the widget's markers/polylines with what is on the map.
|
||||
Future<void> _sync() async {
|
||||
final controller = _controller;
|
||||
if (controller == null || !_styleLoaded || _disposed) return;
|
||||
|
||||
// Native annotation calls are not re-entrant; coalesce overlapping syncs.
|
||||
if (_syncing) {
|
||||
_syncQueued = true;
|
||||
return;
|
||||
}
|
||||
_syncing = true;
|
||||
|
||||
try {
|
||||
await _syncPolylines(controller);
|
||||
await _syncMarkers(controller);
|
||||
} catch (e) {
|
||||
debugPrint('[NEARLE_MAP] Annotation sync failed: $e');
|
||||
} finally {
|
||||
_syncing = false;
|
||||
if (_syncQueued && !_disposed) {
|
||||
_syncQueued = false;
|
||||
unawaited(_sync());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _syncPolylines(MapLibreMapController controller) async {
|
||||
final desired = {for (final p in widget.polylines) p.id: p};
|
||||
|
||||
for (final id in _lines.keys.toList()) {
|
||||
if (desired.containsKey(id)) continue;
|
||||
final line = _lines.remove(id);
|
||||
_polylineById.remove(id);
|
||||
if (line != null) await controller.removeLine(line);
|
||||
}
|
||||
|
||||
for (final polyline in widget.polylines) {
|
||||
if (polyline.points.length < 2) continue;
|
||||
final existing = _lines[polyline.id];
|
||||
final options = LineOptions(
|
||||
geometry: polyline.points,
|
||||
lineColor: _hex(polyline.color),
|
||||
lineWidth: polyline.width,
|
||||
lineOpacity: 1.0,
|
||||
lineJoin: 'round',
|
||||
);
|
||||
|
||||
if (existing == null) {
|
||||
_lines[polyline.id] = await controller.addLine(options);
|
||||
} else if (_polylineById[polyline.id] != polyline) {
|
||||
await controller.updateLine(existing, options);
|
||||
}
|
||||
_polylineById[polyline.id] = polyline;
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _syncMarkers(MapLibreMapController controller) async {
|
||||
for (final marker in widget.markers) {
|
||||
if (_registeredIcons.add(marker.icon.key)) {
|
||||
try {
|
||||
await controller.addImage(marker.icon.key, marker.icon.bytes);
|
||||
} catch (e) {
|
||||
_registeredIcons.remove(marker.icon.key);
|
||||
debugPrint('[NEARLE_MAP] Icon registration failed: $e');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
final desired = {for (final m in widget.markers) m.id: m};
|
||||
|
||||
for (final id in _symbols.keys.toList()) {
|
||||
if (desired.containsKey(id)) continue;
|
||||
final symbol = _symbols.remove(id);
|
||||
_markerById.remove(id);
|
||||
if (symbol != null) await controller.removeSymbol(symbol);
|
||||
}
|
||||
|
||||
for (final marker in widget.markers) {
|
||||
final options = SymbolOptions(
|
||||
geometry: marker.position,
|
||||
iconImage: marker.icon.key,
|
||||
iconSize: marker.size,
|
||||
iconAnchor: marker.anchor == MarkerAnchor.bottom ? 'bottom' : 'center',
|
||||
);
|
||||
|
||||
final existing = _symbols[marker.id];
|
||||
if (existing == null) {
|
||||
_symbols[marker.id] = await controller.addSymbol(options, {
|
||||
'nearleId': marker.id,
|
||||
});
|
||||
} else if (_markerById[marker.id] != marker) {
|
||||
await controller.updateSymbol(existing, options);
|
||||
}
|
||||
_markerById[marker.id] = marker;
|
||||
}
|
||||
}
|
||||
|
||||
void _onSymbolTapped(Symbol symbol) {
|
||||
final id = symbol.data?['nearleId'];
|
||||
if (id is! String) return;
|
||||
// Read the tap handler from the current widget, not the cached marker, so
|
||||
// it always closes over fresh state.
|
||||
for (final marker in widget.markers) {
|
||||
if (marker.id == id) {
|
||||
marker.onTap?.call();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static String _hex(Color color) =>
|
||||
'#${color.toARGB32().toRadixString(16).padLeft(8, '0').substring(2)}';
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MapLibreMap(
|
||||
styleString: MapConfig.styleUrl,
|
||||
initialCameraPosition: widget.initialCameraPosition,
|
||||
myLocationEnabled: widget.myLocationEnabled,
|
||||
myLocationTrackingMode: widget.trackingMode,
|
||||
compassEnabled: widget.compassEnabled,
|
||||
zoomGesturesEnabled: widget.zoomGesturesEnabled,
|
||||
scrollGesturesEnabled: widget.scrollGesturesEnabled,
|
||||
rotateGesturesEnabled: widget.rotateGesturesEnabled,
|
||||
tiltGesturesEnabled: widget.tiltGesturesEnabled,
|
||||
trackCameraPosition: true,
|
||||
onMapCreated: (controller) {
|
||||
_controller = controller;
|
||||
_facade = NearleMapController._(controller, _isAlive);
|
||||
controller.onSymbolTapped.add(_onSymbolTapped);
|
||||
},
|
||||
onStyleLoadedCallback: _onStyleLoaded,
|
||||
);
|
||||
}
|
||||
}
|
||||
332
lib/services/maps/routing_service.dart
Normal file
332
lib/services/maps/routing_service.dart
Normal file
@@ -0,0 +1,332 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:maplibre_gl/maplibre_gl.dart';
|
||||
|
||||
import 'map_config.dart';
|
||||
|
||||
/// A road route returned by the routing backend.
|
||||
class RouteResult {
|
||||
const RouteResult({
|
||||
required this.points,
|
||||
required this.distanceMeters,
|
||||
required this.durationSeconds,
|
||||
});
|
||||
|
||||
/// Decoded geometry of the route, ready to draw as a polyline.
|
||||
final List<LatLng> points;
|
||||
|
||||
/// Total road distance in metres.
|
||||
final double distanceMeters;
|
||||
|
||||
/// Total travel time in seconds.
|
||||
final double durationSeconds;
|
||||
|
||||
double get distanceKm => distanceMeters / 1000.0;
|
||||
|
||||
bool get isEmpty => points.isEmpty && distanceMeters == 0;
|
||||
}
|
||||
|
||||
/// Road routing via OSRM — the Open Source Routing Machine.
|
||||
///
|
||||
/// Replaces the Google Directions API (and `flutter_polyline_points`). OSRM is
|
||||
/// keyless and self-hostable, so there is no credential to ship and no
|
||||
/// per-request billing.
|
||||
///
|
||||
/// Request shape:
|
||||
/// GET {base}/route/v1/driving/{lon},{lat};{lon},{lat}?overview=full&geometries=polyline
|
||||
///
|
||||
/// Note OSRM takes coordinates as **lon,lat** — the reverse of most APIs.
|
||||
class RoutingService {
|
||||
const RoutingService._();
|
||||
|
||||
static const Duration _timeout = Duration(seconds: 10);
|
||||
static const Duration _cacheTtl = Duration(minutes: 30);
|
||||
static const int _maxCacheEntries = 120;
|
||||
|
||||
static final Map<String, _CacheEntry> _cache = {};
|
||||
|
||||
/// Computes a driving route. [waypoints] are visited in order between
|
||||
/// [origin] and [destination] — OSRM routes through all of them in a single
|
||||
/// request, so a multi-stop route costs one call, not one per leg.
|
||||
///
|
||||
/// Returns null when the service is unreachable or finds no route, so callers
|
||||
/// can fall back to a straight line.
|
||||
static Future<RouteResult?> getRoute({
|
||||
required LatLng origin,
|
||||
required LatLng destination,
|
||||
List<LatLng> waypoints = const [],
|
||||
String profile = 'driving',
|
||||
}) async {
|
||||
if (!_isValid(origin) || !_isValid(destination)) {
|
||||
debugPrint('[ROUTING] Invalid coordinates, skipping request.');
|
||||
return null;
|
||||
}
|
||||
|
||||
final cacheKey = _cacheKey(origin, destination, waypoints, profile);
|
||||
final cached = _cache[cacheKey];
|
||||
if (cached != null && !cached.isExpired) return cached.result;
|
||||
|
||||
// OSRM wants lon,lat pairs joined by ';' in the path itself.
|
||||
final coordinates = [
|
||||
origin,
|
||||
...waypoints,
|
||||
destination,
|
||||
].map((p) => '${p.longitude},${p.latitude}').join(';');
|
||||
|
||||
final uri = Uri.parse(
|
||||
'${MapConfig.osrmBaseUrl}/route/v1/$profile/$coordinates',
|
||||
).replace(
|
||||
queryParameters: const {
|
||||
'overview': 'full',
|
||||
'geometries': 'polyline',
|
||||
'alternatives': 'false',
|
||||
'steps': 'false',
|
||||
},
|
||||
);
|
||||
|
||||
try {
|
||||
final response = await http.get(uri).timeout(_timeout);
|
||||
|
||||
if (response.statusCode != 200) {
|
||||
debugPrint('[ROUTING] HTTP ${response.statusCode}');
|
||||
return null;
|
||||
}
|
||||
|
||||
final result = _parse(json.decode(response.body));
|
||||
if (result == null || result.isEmpty) {
|
||||
debugPrint('[ROUTING] No route in response.');
|
||||
return null;
|
||||
}
|
||||
|
||||
_store(cacheKey, result);
|
||||
return result;
|
||||
} catch (e) {
|
||||
debugPrint('[ROUTING] Request failed: $e');
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// Convenience wrapper for callers that only need road distance.
|
||||
static Future<double?> getRouteDistanceKm({
|
||||
required LatLng origin,
|
||||
required LatLng destination,
|
||||
}) async {
|
||||
final route = await getRoute(origin: origin, destination: destination);
|
||||
return route?.distanceKm;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
// Response parsing
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
/// Parses an OSRM `route` response. Written tolerantly so a self-hosted
|
||||
/// build, a proxy, or a drop-in alternative (Valhalla's OSRM-compatible
|
||||
/// endpoint, GraphHopper's) does not require code changes: numeric fields are
|
||||
/// accepted as plain numbers or as `{"value": n}` objects, and the geometry
|
||||
/// is looked for under several key names.
|
||||
static RouteResult? _parse(dynamic body) {
|
||||
if (body is! Map) return null;
|
||||
|
||||
final code = body['code'];
|
||||
if (code is String && code != 'Ok') {
|
||||
debugPrint('[ROUTING] Service returned code: $code');
|
||||
return null;
|
||||
}
|
||||
|
||||
final routes = body['routes'];
|
||||
if (routes is! List || routes.isEmpty) return null;
|
||||
|
||||
final route = routes.first;
|
||||
if (route is! Map) return null;
|
||||
|
||||
var distance = _num(route['distance']);
|
||||
var duration = _num(route['duration']);
|
||||
|
||||
// Fall back to summing legs when totals are absent.
|
||||
if (distance == 0 || duration == 0) {
|
||||
final legs = route['legs'];
|
||||
if (legs is List) {
|
||||
var legDistance = 0.0;
|
||||
var legDuration = 0.0;
|
||||
for (final leg in legs) {
|
||||
if (leg is! Map) continue;
|
||||
legDistance += _num(leg['distance']);
|
||||
legDuration += _num(leg['duration']);
|
||||
}
|
||||
if (distance == 0) distance = legDistance;
|
||||
if (duration == 0) duration = legDuration;
|
||||
}
|
||||
}
|
||||
|
||||
final points = _extractGeometry(route);
|
||||
if (points.isEmpty && distance == 0) return null;
|
||||
|
||||
return RouteResult(
|
||||
points: points,
|
||||
distanceMeters: distance,
|
||||
durationSeconds: duration,
|
||||
);
|
||||
}
|
||||
|
||||
static List<LatLng> _extractGeometry(Map route) {
|
||||
for (final key in const ['geometry', 'overview_polyline', 'polyline']) {
|
||||
final value = route[key];
|
||||
if (value is String && value.isNotEmpty) {
|
||||
final decoded = decodePolyline(value);
|
||||
if (decoded.isNotEmpty) return decoded;
|
||||
}
|
||||
// Encoded polyline nested as {"points": "<encoded>"}.
|
||||
if (value is Map && value['points'] is String) {
|
||||
final decoded = decodePolyline(value['points'] as String);
|
||||
if (decoded.isNotEmpty) return decoded;
|
||||
}
|
||||
// GeoJSON LineString, if the caller ever requests geometries=geojson.
|
||||
if (value is Map && value['coordinates'] is List) {
|
||||
final coords = value['coordinates'] as List;
|
||||
final points = <LatLng>[];
|
||||
for (final c in coords) {
|
||||
if (c is List && c.length >= 2) {
|
||||
points.add(LatLng(_num(c[1]), _num(c[0])));
|
||||
}
|
||||
}
|
||||
if (points.isNotEmpty) return points;
|
||||
}
|
||||
}
|
||||
|
||||
// Last resort: stitch the per-step geometries together.
|
||||
final points = <LatLng>[];
|
||||
final legs = route['legs'];
|
||||
if (legs is List) {
|
||||
for (final leg in legs) {
|
||||
if (leg is! Map) continue;
|
||||
final steps = leg['steps'];
|
||||
if (steps is! List) continue;
|
||||
for (final step in steps) {
|
||||
if (step is! Map) continue;
|
||||
final geometry = step['geometry'] ?? step['polyline'];
|
||||
final encoded = geometry is Map ? geometry['points'] : geometry;
|
||||
if (encoded is String && encoded.isNotEmpty) {
|
||||
points.addAll(decodePolyline(encoded));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return points;
|
||||
}
|
||||
|
||||
static double _num(dynamic value) {
|
||||
if (value == null) return 0.0;
|
||||
if (value is num) return value.toDouble();
|
||||
if (value is Map) return _num(value['value']);
|
||||
return double.tryParse(value.toString()) ?? 0.0;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
// Polyline codec (Google's Encoded Polyline Algorithm)
|
||||
//
|
||||
// OSRM's default `geometries=polyline` uses precision 5. Its `polyline6`
|
||||
// option uses precision 6 — pass `precision: 6` if you switch to it.
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
static List<LatLng> decodePolyline(String encoded, {int precision = 5}) {
|
||||
final points = <LatLng>[];
|
||||
final factor = _pow10(precision);
|
||||
var index = 0;
|
||||
var lat = 0;
|
||||
var lng = 0;
|
||||
|
||||
while (index < encoded.length) {
|
||||
final dLat = _decodeValue(encoded, index);
|
||||
if (dLat == null) break;
|
||||
index = dLat.nextIndex;
|
||||
lat += dLat.value;
|
||||
|
||||
final dLng = _decodeValue(encoded, index);
|
||||
if (dLng == null) break;
|
||||
index = dLng.nextIndex;
|
||||
lng += dLng.value;
|
||||
|
||||
points.add(LatLng(lat / factor, lng / factor));
|
||||
}
|
||||
return points;
|
||||
}
|
||||
|
||||
static _Delta? _decodeValue(String encoded, int index) {
|
||||
var shift = 0;
|
||||
var result = 0;
|
||||
int byte;
|
||||
do {
|
||||
if (index >= encoded.length) return null;
|
||||
byte = encoded.codeUnitAt(index++) - 63;
|
||||
result |= (byte & 0x1f) << shift;
|
||||
shift += 5;
|
||||
} while (byte >= 0x20);
|
||||
final value = (result & 1) != 0 ? ~(result >> 1) : (result >> 1);
|
||||
return _Delta(value, index);
|
||||
}
|
||||
|
||||
static double _pow10(int exponent) {
|
||||
var value = 1.0;
|
||||
for (var i = 0; i < exponent; i++) {
|
||||
value *= 10;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
// Cache
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
static bool _isValid(LatLng p) =>
|
||||
p.latitude != 0 &&
|
||||
p.longitude != 0 &&
|
||||
p.latitude.abs() <= 90 &&
|
||||
p.longitude.abs() <= 180;
|
||||
|
||||
static String _cacheKey(
|
||||
LatLng origin,
|
||||
LatLng destination,
|
||||
List<LatLng> waypoints,
|
||||
String profile,
|
||||
) {
|
||||
// ~11 m resolution: identical pickup/drop pairs reuse one request.
|
||||
String round(LatLng p) =>
|
||||
'${p.latitude.toStringAsFixed(4)},${p.longitude.toStringAsFixed(4)}';
|
||||
return [
|
||||
profile,
|
||||
round(origin),
|
||||
round(destination),
|
||||
...waypoints.map(round),
|
||||
].join('>');
|
||||
}
|
||||
|
||||
static void _store(String key, RouteResult result) {
|
||||
if (_cache.length >= _maxCacheEntries) {
|
||||
final oldest = _cache.entries.reduce(
|
||||
(a, b) => a.value.storedAt.isBefore(b.value.storedAt) ? a : b,
|
||||
);
|
||||
_cache.remove(oldest.key);
|
||||
}
|
||||
_cache[key] = _CacheEntry(result, DateTime.now());
|
||||
}
|
||||
|
||||
@visibleForTesting
|
||||
static void clearCache() => _cache.clear();
|
||||
}
|
||||
|
||||
class _Delta {
|
||||
const _Delta(this.value, this.nextIndex);
|
||||
final int value;
|
||||
final int nextIndex;
|
||||
}
|
||||
|
||||
class _CacheEntry {
|
||||
const _CacheEntry(this.result, this.storedAt);
|
||||
final RouteResult result;
|
||||
final DateTime storedAt;
|
||||
|
||||
bool get isExpired =>
|
||||
DateTime.now().difference(storedAt) > RoutingService._cacheTtl;
|
||||
}
|
||||
@@ -5,12 +5,13 @@ import 'dart:async';
|
||||
import 'dart:convert';
|
||||
import 'dart:ui' as ui;
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_polyline_points/flutter_polyline_points.dart';
|
||||
import 'package:flutter_slidable/flutter_slidable.dart';
|
||||
import 'package:geolocator/geolocator.dart';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:google_maps_flutter/google_maps_flutter.dart';
|
||||
import 'package:lottie/lottie.dart' hide Marker;
|
||||
import 'package:nearle/services/maps/map_marker_icons.dart';
|
||||
import 'package:nearle/services/maps/nearle_map.dart';
|
||||
import 'package:nearle/services/maps/routing_service.dart';
|
||||
import 'package:nearle/views/helpers/constants/Colorconstants.dart';
|
||||
import 'package:nearle/views/helpers/constants/Font_constant.dart';
|
||||
import 'package:slider_button_lite/feature/presentation/slider_button/slider.dart';
|
||||
|
||||
@@ -16,24 +16,18 @@ class _DeliveryMapScreen extends StatefulWidget {
|
||||
}
|
||||
|
||||
class _DeliveryMapScreenState extends State<_DeliveryMapScreen> {
|
||||
GoogleMapController? _mapController;
|
||||
NearleMapController? _mapController;
|
||||
late final LatLng _pickupLocation;
|
||||
late final LatLng _dropLocation;
|
||||
final Set<Marker> _markers = {};
|
||||
final Set<Polyline> _polylines = {};
|
||||
late final PolylinePoints _polylinePoints;
|
||||
bool _isLoadingRoute = true;
|
||||
List<NearleMapMarker> _markers = const [];
|
||||
List<NearleMapPolyline> _polylines = const [];
|
||||
bool _isNavigating = false; // Prevent multiple clicks
|
||||
|
||||
static const String _googleApiKey = 'AIzaSyBhkGfnq27sN0wV5y_S-M2KojpFTk_by-Q';
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_polylinePoints = PolylinePoints(apiKey: _googleApiKey);
|
||||
_resolveLocationsFromDelivery();
|
||||
_setMarkers();
|
||||
_isLoadingRoute = false;
|
||||
_createRealRoute();
|
||||
}
|
||||
|
||||
@@ -73,113 +67,70 @@ class _DeliveryMapScreenState extends State<_DeliveryMapScreen> {
|
||||
_dropLocation = drop;
|
||||
}
|
||||
|
||||
void _setMarkers() {
|
||||
_markers.addAll([
|
||||
Marker(
|
||||
markerId: const MarkerId('pickup'),
|
||||
position: _pickupLocation,
|
||||
infoWindow: const InfoWindow(title: 'Pickup Location'),
|
||||
icon: BitmapDescriptor.defaultMarkerWithHue(BitmapDescriptor.hueRed),
|
||||
),
|
||||
Marker(
|
||||
markerId: const MarkerId('drop'),
|
||||
position: _dropLocation,
|
||||
infoWindow: const InfoWindow(title: 'Drop Location'),
|
||||
icon: BitmapDescriptor.defaultMarkerWithHue(BitmapDescriptor.hueGreen),
|
||||
),
|
||||
]);
|
||||
Future<void> _setMarkers() async {
|
||||
final pickupIcon = await MapMarkerIcons.pin(Colors.red);
|
||||
final dropIcon = await MapMarkerIcons.pin(Colors.green);
|
||||
if (!mounted) return;
|
||||
setState(() {
|
||||
_markers = [
|
||||
NearleMapMarker(
|
||||
id: 'pickup',
|
||||
position: _pickupLocation,
|
||||
icon: pickupIcon,
|
||||
),
|
||||
NearleMapMarker(id: 'drop', position: _dropLocation, icon: dropIcon),
|
||||
];
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> _createRealRoute() async {
|
||||
try {
|
||||
final request = PolylineRequest(
|
||||
origin: PointLatLng(
|
||||
_pickupLocation.latitude,
|
||||
_pickupLocation.longitude,
|
||||
),
|
||||
destination: PointLatLng(
|
||||
_dropLocation.latitude,
|
||||
_dropLocation.longitude,
|
||||
),
|
||||
mode: TravelMode.driving,
|
||||
);
|
||||
final route = await RoutingService.getRoute(
|
||||
origin: _pickupLocation,
|
||||
destination: _dropLocation,
|
||||
);
|
||||
|
||||
final result = await _polylinePoints.getRouteBetweenCoordinates(
|
||||
request: request,
|
||||
);
|
||||
|
||||
if (result.points.isNotEmpty) {
|
||||
final routePoints = result.points
|
||||
.map((e) => LatLng(e.latitude, e.longitude))
|
||||
.toList();
|
||||
|
||||
if (mounted) {
|
||||
setState(() {
|
||||
_polylines.add(
|
||||
Polyline(
|
||||
polylineId: const PolylineId('real_route'),
|
||||
color: ColorConstants.primaryColor,
|
||||
width: 6,
|
||||
points: routePoints,
|
||||
),
|
||||
);
|
||||
_isLoadingRoute = false;
|
||||
});
|
||||
|
||||
_fitMapToRoute();
|
||||
}
|
||||
} else {
|
||||
debugPrint('[MAP_PREVIEW] No route found');
|
||||
if (mounted) {
|
||||
setState(() => _isLoadingRoute = false);
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
debugPrint('[MAP_PREVIEW] Error creating route: $e');
|
||||
if (mounted) {
|
||||
setState(() => _isLoadingRoute = false);
|
||||
}
|
||||
}
|
||||
}
|
||||
Future<void> _fitMapToRoute() async {
|
||||
if (!mounted) return;
|
||||
if (_mapController == null) return;
|
||||
|
||||
// Check if controller is still alive (important!)
|
||||
try {
|
||||
await _mapController!.getVisibleRegion();
|
||||
} catch (e) {
|
||||
debugPrint("❌ Map controller is dead. Skip animateCamera.");
|
||||
return;
|
||||
}
|
||||
|
||||
final bounds = LatLngBounds(
|
||||
southwest: LatLng(
|
||||
math.min(_pickupLocation.latitude, _dropLocation.latitude),
|
||||
math.min(_pickupLocation.longitude, _dropLocation.longitude),
|
||||
),
|
||||
northeast: LatLng(
|
||||
math.max(_pickupLocation.latitude, _dropLocation.latitude),
|
||||
math.max(_pickupLocation.longitude, _dropLocation.longitude),
|
||||
),
|
||||
);
|
||||
|
||||
// Try animate safely
|
||||
for (int i = 0; i < 10; i++) {
|
||||
if (!mounted) return;
|
||||
|
||||
try {
|
||||
await _mapController!.animateCamera(
|
||||
CameraUpdate.newLatLngBounds(bounds, 80),
|
||||
);
|
||||
return;
|
||||
} catch (e) {
|
||||
await Future.delayed(const Duration(milliseconds: 150));
|
||||
}
|
||||
// Fall back to a straight line so the screen still shows the connection
|
||||
// when routing is unavailable.
|
||||
final points = (route != null && route.points.isNotEmpty)
|
||||
? route.points
|
||||
: <LatLng>[_pickupLocation, _dropLocation];
|
||||
|
||||
if (route == null) debugPrint('[MAP_PREVIEW] No route found');
|
||||
|
||||
setState(() {
|
||||
_polylines = [
|
||||
NearleMapPolyline(
|
||||
id: 'real_route',
|
||||
points: points,
|
||||
color: ColorConstants.primaryColor,
|
||||
width: 6,
|
||||
),
|
||||
];
|
||||
});
|
||||
|
||||
_fitMapToRoute();
|
||||
}
|
||||
|
||||
debugPrint("❌ animateCamera failed after retries (map probably disposed)");
|
||||
}
|
||||
Future<void> _fitMapToRoute() async {
|
||||
final controller = _mapController;
|
||||
if (!mounted || controller == null) return;
|
||||
|
||||
await controller.fitBounds(
|
||||
LatLngBounds(
|
||||
southwest: LatLng(
|
||||
math.min(_pickupLocation.latitude, _dropLocation.latitude),
|
||||
math.min(_pickupLocation.longitude, _dropLocation.longitude),
|
||||
),
|
||||
northeast: LatLng(
|
||||
math.max(_pickupLocation.latitude, _dropLocation.latitude),
|
||||
math.max(_pickupLocation.longitude, _dropLocation.longitude),
|
||||
),
|
||||
),
|
||||
padding: 80,
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -188,27 +139,20 @@ Future<void> _fitMapToRoute() async {
|
||||
return Scaffold(
|
||||
body: Stack(
|
||||
children: [
|
||||
SizedBox.expand(
|
||||
child: GoogleMap(
|
||||
initialCameraPosition: CameraPosition(
|
||||
target: _pickupLocation,
|
||||
zoom: 14.5,
|
||||
),
|
||||
onMapCreated: (controller) {
|
||||
_mapController = controller;
|
||||
|
||||
Future.delayed(const Duration(milliseconds: 500), () {
|
||||
if (!_isLoadingRoute && _polylines.isNotEmpty) {
|
||||
_fitMapToRoute();
|
||||
}
|
||||
});
|
||||
},
|
||||
markers: _markers,
|
||||
polylines: _polylines,
|
||||
zoomControlsEnabled: false,
|
||||
myLocationButtonEnabled: false,
|
||||
),
|
||||
),
|
||||
SizedBox.expand(
|
||||
child: NearleMap(
|
||||
initialCameraPosition: CameraPosition(
|
||||
target: _pickupLocation,
|
||||
zoom: 14.5,
|
||||
),
|
||||
markers: _markers,
|
||||
polylines: _polylines,
|
||||
onMapReady: (controller) {
|
||||
_mapController = controller;
|
||||
_fitMapToRoute();
|
||||
},
|
||||
),
|
||||
),
|
||||
Positioned(
|
||||
top: 50,
|
||||
left: 16,
|
||||
|
||||
@@ -24,15 +24,11 @@ class MultiCustomerMapScreen extends StatefulWidget {
|
||||
}
|
||||
|
||||
class _MultiCustomerMapScreenState extends State<MultiCustomerMapScreen> {
|
||||
GoogleMapController? mapController;
|
||||
Set<Marker> markers = {};
|
||||
Set<Polyline> polylines = {};
|
||||
NearleMapController? mapController;
|
||||
List<NearleMapMarker> markers = const [];
|
||||
List<NearleMapPolyline> polylines = const [];
|
||||
LatLng? currentLocation;
|
||||
bool _isLoading = true;
|
||||
bool _mapReady = false;
|
||||
final PolylinePoints _polylinePoints = PolylinePoints(
|
||||
apiKey: 'AIzaSyBhkGfnq27sN0wV5y_S-M2KojpFTk_by-Q',
|
||||
);
|
||||
final Map<String, Map<String, dynamic>> _deliveryMap = {};
|
||||
|
||||
@override
|
||||
@@ -199,7 +195,7 @@ class _MultiCustomerMapScreenState extends State<MultiCustomerMapScreen> {
|
||||
}
|
||||
|
||||
Future<void> _createDeliveryMarkers() async {
|
||||
Set<Marker> tempMarkers = {};
|
||||
final List<NearleMapMarker> tempMarkers = [];
|
||||
List<_StepPoint> stepPoints = [];
|
||||
_deliveryMap.clear();
|
||||
|
||||
@@ -231,8 +227,6 @@ class _MultiCustomerMapScreenState extends State<MultiCustomerMapScreen> {
|
||||
);
|
||||
if (lat == 0 || lon == 0) continue;
|
||||
|
||||
final customerName = (delivery['deliverycustomer'] ?? 'Customer ${i + 1}')
|
||||
.toString();
|
||||
final orderId = (delivery['orderid'] ?? '').toString();
|
||||
final status = (delivery['orderstatus']?.toString().toLowerCase() ?? '')
|
||||
.trim();
|
||||
@@ -242,23 +236,19 @@ class _MultiCustomerMapScreenState extends State<MultiCustomerMapScreen> {
|
||||
// Store delivery data for dialog
|
||||
_deliveryMap['delivery_$orderId'] = delivery;
|
||||
|
||||
final icon = await _createCircularMarkerBitmap(
|
||||
final icon = await MapMarkerIcons.numberedCircle(
|
||||
displayStep,
|
||||
primaryColor: ColorConstants.primaryColor,
|
||||
isSkipped: isSkipped,
|
||||
isNext: isNext,
|
||||
);
|
||||
|
||||
tempMarkers.add(
|
||||
Marker(
|
||||
markerId: MarkerId('delivery_$orderId'),
|
||||
NearleMapMarker(
|
||||
id: 'delivery_$orderId',
|
||||
position: LatLng(lat, lon),
|
||||
icon: icon,
|
||||
infoWindow: InfoWindow(
|
||||
title: isSkipped
|
||||
? 'Step $displayStep: $customerName (SKIPPED)'
|
||||
: 'Step $displayStep: $customerName',
|
||||
snippet: 'Order #$orderId',
|
||||
),
|
||||
anchor: MarkerAnchor.center,
|
||||
onTap: () {
|
||||
_showCustomerDetailsSheet(delivery, displayStep);
|
||||
},
|
||||
@@ -270,13 +260,11 @@ class _MultiCustomerMapScreenState extends State<MultiCustomerMapScreen> {
|
||||
|
||||
if (currentLocation != null) {
|
||||
tempMarkers.add(
|
||||
Marker(
|
||||
markerId: const MarkerId('current_location'),
|
||||
NearleMapMarker(
|
||||
id: 'current_location',
|
||||
position: currentLocation!,
|
||||
icon: BitmapDescriptor.defaultMarkerWithHue(
|
||||
BitmapDescriptor.hueAzure,
|
||||
),
|
||||
infoWindow: const InfoWindow(title: 'You are here'),
|
||||
icon: await MapMarkerIcons.dot(Colors.blueAccent),
|
||||
anchor: MarkerAnchor.center,
|
||||
),
|
||||
);
|
||||
stepPoints.insert(0, _StepPoint(0, currentLocation!));
|
||||
@@ -284,67 +272,7 @@ class _MultiCustomerMapScreenState extends State<MultiCustomerMapScreen> {
|
||||
|
||||
stepPoints.sort((a, b) => a.step.compareTo(b.step));
|
||||
|
||||
Set<Polyline> newPolylines = {};
|
||||
int segmentIndex = 0;
|
||||
|
||||
for (int i = 0; i < stepPoints.length - 1; i++) {
|
||||
final start = stepPoints[i].position;
|
||||
final end = stepPoints[i + 1].position;
|
||||
|
||||
try {
|
||||
// ignore: deprecated_member_use
|
||||
final request = PolylineRequest(
|
||||
origin: PointLatLng(start.latitude, start.longitude),
|
||||
destination: PointLatLng(end.latitude, end.longitude),
|
||||
mode: TravelMode.driving,
|
||||
);
|
||||
|
||||
final result = await _polylinePoints.getRouteBetweenCoordinates(
|
||||
request: request,
|
||||
);
|
||||
|
||||
if (result.points.isNotEmpty) {
|
||||
final routePoints = result.points
|
||||
.map((p) => LatLng(p.latitude, p.longitude))
|
||||
.toList();
|
||||
|
||||
newPolylines.add(
|
||||
Polyline(
|
||||
polylineId: PolylineId('segment_$segmentIndex'),
|
||||
points: routePoints,
|
||||
width: 6,
|
||||
color: Colors.blue,
|
||||
startCap: Cap.roundCap,
|
||||
endCap: Cap.roundCap,
|
||||
jointType: JointType.round,
|
||||
geodesic: true,
|
||||
),
|
||||
);
|
||||
segmentIndex++;
|
||||
} else {
|
||||
newPolylines.add(
|
||||
Polyline(
|
||||
polylineId: PolylineId('segment_fallback_$segmentIndex'),
|
||||
points: [start, end],
|
||||
width: 4,
|
||||
color: Colors.blue.shade200,
|
||||
),
|
||||
);
|
||||
segmentIndex++;
|
||||
}
|
||||
} catch (e) {
|
||||
debugPrint('[CUSTOMER_MAP] Directions error for segment $i: $e');
|
||||
newPolylines.add(
|
||||
Polyline(
|
||||
polylineId: PolylineId('segment_error_$segmentIndex'),
|
||||
points: [start, end],
|
||||
width: 4,
|
||||
color: Colors.blue.shade200,
|
||||
),
|
||||
);
|
||||
segmentIndex++;
|
||||
}
|
||||
}
|
||||
final newPolylines = await _buildRoutePolylines(stepPoints);
|
||||
|
||||
if (mounted) {
|
||||
setState(() {
|
||||
@@ -357,8 +285,50 @@ class _MultiCustomerMapScreenState extends State<MultiCustomerMapScreen> {
|
||||
_fitBoundsToMarkersAndPolylines();
|
||||
}
|
||||
|
||||
/// Builds the route through every stop in one Directions call.
|
||||
///
|
||||
/// This used to issue one request per consecutive pair of stops, so opening
|
||||
/// this screen with N stops cost N billable requests. OSRM takes every stop
|
||||
/// as a coordinate in one request, so the whole route is a single call.
|
||||
Future<List<NearleMapPolyline>> _buildRoutePolylines(
|
||||
List<_StepPoint> stepPoints,
|
||||
) async {
|
||||
if (stepPoints.length < 2) return const [];
|
||||
|
||||
final route = await RoutingService.getRoute(
|
||||
origin: stepPoints.first.position,
|
||||
destination: stepPoints.last.position,
|
||||
waypoints: stepPoints
|
||||
.sublist(1, stepPoints.length - 1)
|
||||
.map((s) => s.position)
|
||||
.toList(),
|
||||
);
|
||||
|
||||
if (route != null && route.points.isNotEmpty) {
|
||||
return [
|
||||
NearleMapPolyline(
|
||||
id: 'route',
|
||||
points: route.points,
|
||||
width: 6,
|
||||
color: Colors.blue,
|
||||
),
|
||||
];
|
||||
}
|
||||
|
||||
debugPrint('[CUSTOMER_MAP] Routing unavailable, drawing direct segments');
|
||||
return [
|
||||
NearleMapPolyline(
|
||||
id: 'route_fallback',
|
||||
points: stepPoints.map((s) => s.position).toList(),
|
||||
width: 4,
|
||||
color: Colors.blue.shade200,
|
||||
),
|
||||
];
|
||||
}
|
||||
|
||||
Future<void> _fitBoundsToMarkersAndPolylines() async {
|
||||
if (mapController == null) return;
|
||||
final controller = mapController;
|
||||
if (controller == null) return;
|
||||
|
||||
double minLat = double.infinity;
|
||||
double maxLat = -double.infinity;
|
||||
@@ -367,8 +337,7 @@ class _MultiCustomerMapScreenState extends State<MultiCustomerMapScreen> {
|
||||
|
||||
bool hasPoint = false;
|
||||
|
||||
for (final m in markers) {
|
||||
final pos = m.position;
|
||||
void include(LatLng pos) {
|
||||
minLat = math.min(minLat, pos.latitude);
|
||||
maxLat = math.max(maxLat, pos.latitude);
|
||||
minLng = math.min(minLng, pos.longitude);
|
||||
@@ -376,137 +345,24 @@ class _MultiCustomerMapScreenState extends State<MultiCustomerMapScreen> {
|
||||
hasPoint = true;
|
||||
}
|
||||
|
||||
for (final m in markers) {
|
||||
include(m.position);
|
||||
}
|
||||
for (final poly in polylines) {
|
||||
for (final pos in poly.points) {
|
||||
minLat = math.min(minLat, pos.latitude);
|
||||
maxLat = math.max(maxLat, pos.latitude);
|
||||
minLng = math.min(minLng, pos.longitude);
|
||||
maxLng = math.max(maxLng, pos.longitude);
|
||||
hasPoint = true;
|
||||
include(pos);
|
||||
}
|
||||
}
|
||||
|
||||
if (!hasPoint) return;
|
||||
|
||||
final bounds = LatLngBounds(
|
||||
southwest: LatLng(minLat, minLng),
|
||||
northeast: LatLng(maxLat, maxLng),
|
||||
);
|
||||
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) async {
|
||||
try {
|
||||
await mapController!.animateCamera(
|
||||
CameraUpdate.newLatLngBounds(bounds, 80),
|
||||
);
|
||||
} catch (e) {
|
||||
Future.delayed(const Duration(milliseconds: 300), () async {
|
||||
try {
|
||||
await mapController!.animateCamera(
|
||||
CameraUpdate.newLatLngBounds(bounds, 80),
|
||||
);
|
||||
} catch (e) {
|
||||
debugPrint('[CUSTOMER_MAP] Retry failed: $e');
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Future<BitmapDescriptor> _createCircularMarkerBitmap(
|
||||
int number, {
|
||||
bool isSkipped = false,
|
||||
bool isNext = false,
|
||||
}) async {
|
||||
const double size = 70;
|
||||
final pictureRecorder = ui.PictureRecorder();
|
||||
final canvas = Canvas(pictureRecorder);
|
||||
final center = Offset(size / 2, size / 2);
|
||||
|
||||
final paint = Paint()
|
||||
..color = isSkipped ? Colors.orange : ColorConstants.primaryColor;
|
||||
canvas.drawCircle(center, 15, paint);
|
||||
|
||||
final border = Paint()
|
||||
..color = isSkipped ? Colors.orange.shade900 : Colors.white
|
||||
..style = PaintingStyle.stroke
|
||||
..strokeWidth = isSkipped ? 4 : 3;
|
||||
canvas.drawCircle(center, 15, border);
|
||||
|
||||
final textPainter = TextPainter(
|
||||
text: TextSpan(
|
||||
text: number.toString(),
|
||||
style: const TextStyle(
|
||||
fontSize: 19,
|
||||
color: Colors.white,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
await controller.fitBounds(
|
||||
LatLngBounds(
|
||||
southwest: LatLng(minLat, minLng),
|
||||
northeast: LatLng(maxLat, maxLng),
|
||||
),
|
||||
textDirection: TextDirection.ltr,
|
||||
padding: 80,
|
||||
);
|
||||
textPainter.layout();
|
||||
textPainter.paint(
|
||||
canvas,
|
||||
Offset(
|
||||
center.dx - textPainter.width / 2,
|
||||
center.dy - textPainter.height / 2,
|
||||
),
|
||||
);
|
||||
|
||||
// Draw "NEXT" indicator (road sign with down arrow) on top
|
||||
if (isNext) {
|
||||
// Draw road sign background (rectangle)
|
||||
final signPaint = Paint()
|
||||
..color = Colors.green
|
||||
..style = PaintingStyle.fill;
|
||||
final signRect = RRect.fromRectAndRadius(
|
||||
Rect.fromCenter(center: Offset(size / 2, 8), width: 45, height: 30),
|
||||
const Radius.circular(4),
|
||||
);
|
||||
canvas.drawRRect(signRect, signPaint);
|
||||
|
||||
// Draw border
|
||||
final signBorder = Paint()
|
||||
..color = Colors.white
|
||||
..style = PaintingStyle.stroke
|
||||
..strokeWidth = 1.5;
|
||||
canvas.drawRRect(signRect, signBorder);
|
||||
|
||||
// Draw "NEXT" text
|
||||
final nextTextPainter = TextPainter(
|
||||
text: const TextSpan(
|
||||
text: 'NEXT',
|
||||
style: TextStyle(
|
||||
fontSize: 12,
|
||||
color: Colors.white,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
textDirection: TextDirection.ltr,
|
||||
);
|
||||
nextTextPainter.layout();
|
||||
nextTextPainter.paint(
|
||||
canvas,
|
||||
Offset(size / 2 - nextTextPainter.width / 2, 4),
|
||||
);
|
||||
|
||||
// Draw down arrow
|
||||
final arrowPath = Path();
|
||||
arrowPath.moveTo(size / 2, 18);
|
||||
arrowPath.lineTo(size / 2 - 4, 24);
|
||||
arrowPath.lineTo(size / 2 + 4, 24);
|
||||
arrowPath.close();
|
||||
final arrowPaint = Paint()
|
||||
..color = Colors.white
|
||||
..style = PaintingStyle.fill;
|
||||
canvas.drawPath(arrowPath, arrowPaint);
|
||||
}
|
||||
|
||||
final img = await pictureRecorder.endRecording().toImage(
|
||||
size.toInt(),
|
||||
size.toInt(),
|
||||
);
|
||||
final data = await img.toByteData(format: ui.ImageByteFormat.png);
|
||||
return BitmapDescriptor.bytes(data!.buffer.asUint8List());
|
||||
}
|
||||
|
||||
void _showCustomerDetailsSheet(
|
||||
@@ -872,21 +728,14 @@ class _MultiCustomerMapScreenState extends State<MultiCustomerMapScreen> {
|
||||
),
|
||||
body: _isLoading
|
||||
? const Center(child: CircularProgressIndicator())
|
||||
: GoogleMap(
|
||||
: NearleMap(
|
||||
initialCameraPosition: initialCameraPosition,
|
||||
markers: markers,
|
||||
polylines: polylines,
|
||||
myLocationEnabled: true,
|
||||
myLocationButtonEnabled: true,
|
||||
onMapCreated: (controller) {
|
||||
onMapReady: (controller) {
|
||||
mapController = controller;
|
||||
_mapReady = true;
|
||||
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
Future.delayed(const Duration(milliseconds: 150), () {
|
||||
if (_mapReady) _fitBoundsToMarkersAndPolylines();
|
||||
});
|
||||
});
|
||||
_fitBoundsToMarkersAndPolylines();
|
||||
},
|
||||
),
|
||||
);
|
||||
|
||||
@@ -19,12 +19,15 @@ class _RiderNavigationScreen extends StatefulWidget {
|
||||
|
||||
class _RiderNavigationScreenState extends State<_RiderNavigationScreen>
|
||||
with WidgetsBindingObserver {
|
||||
GoogleMapController? mapController;
|
||||
NearleMapController? mapController;
|
||||
bool _mapReady = false;
|
||||
bool _isDisposed = false;
|
||||
Position? _currentPosition;
|
||||
StreamSubscription<Position>? _positionStream;
|
||||
final Set<Marker> _markers = {};
|
||||
List<NearleMapMarker> _markers = const [];
|
||||
MarkerIcon? _riderIcon;
|
||||
MarkerIcon? _dropIcon;
|
||||
LatLng? _lastRiderPosition;
|
||||
late final LatLng dropLocation;
|
||||
bool _isInitializing = true;
|
||||
bool _hasOpenedGoogleMaps = false;
|
||||
@@ -46,6 +49,7 @@ class _RiderNavigationScreenState extends State<_RiderNavigationScreen>
|
||||
super.initState();
|
||||
WidgetsBinding.instance.addObserver(this);
|
||||
_resolveDropFromDelivery();
|
||||
_loadMarkerIcons();
|
||||
_initializeMap();
|
||||
_initEtaForNav();
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
@@ -214,8 +218,8 @@ class _RiderNavigationScreenState extends State<_RiderNavigationScreen>
|
||||
if (lastPos != null && mounted) {
|
||||
setState(() {
|
||||
_currentPosition = lastPos;
|
||||
_addMarkers(LatLng(lastPos.latitude, lastPos.longitude));
|
||||
});
|
||||
_addMarkers(LatLng(lastPos.latitude, lastPos.longitude));
|
||||
_startLiveTracking();
|
||||
}
|
||||
});
|
||||
@@ -338,8 +342,8 @@ class _RiderNavigationScreenState extends State<_RiderNavigationScreen>
|
||||
if (mounted) {
|
||||
setState(() {
|
||||
_currentPosition = position;
|
||||
_addMarkers(LatLng(position.latitude, position.longitude));
|
||||
});
|
||||
_addMarkers(LatLng(position.latitude, position.longitude));
|
||||
|
||||
final distance = Geolocator.distanceBetween(
|
||||
position.latitude,
|
||||
@@ -362,27 +366,36 @@ class _RiderNavigationScreenState extends State<_RiderNavigationScreen>
|
||||
});
|
||||
}
|
||||
|
||||
/// Marker bitmaps are drawn on a canvas, so they arrive asynchronously.
|
||||
/// Once they land, rebuild whatever markers are already pending.
|
||||
Future<void> _loadMarkerIcons() async {
|
||||
final rider = await MapMarkerIcons.dot(Colors.blueAccent);
|
||||
final drop = await MapMarkerIcons.pin(Colors.red);
|
||||
if (!mounted || _isDisposed) return;
|
||||
setState(() {
|
||||
_riderIcon = rider;
|
||||
_dropIcon = drop;
|
||||
_addMarkers(_lastRiderPosition ?? dropLocation);
|
||||
});
|
||||
}
|
||||
|
||||
/// Mutates [_markers] only — callers own the [setState] so this can be
|
||||
/// invoked from inside an existing one without nesting.
|
||||
void _addMarkers(LatLng riderPos) {
|
||||
_markers
|
||||
..clear()
|
||||
..add(
|
||||
Marker(
|
||||
markerId: const MarkerId('rider'),
|
||||
position: riderPos,
|
||||
icon: BitmapDescriptor.defaultMarkerWithHue(
|
||||
BitmapDescriptor.hueAzure,
|
||||
),
|
||||
infoWindow: const InfoWindow(title: 'You (Rider)'),
|
||||
),
|
||||
)
|
||||
..add(
|
||||
Marker(
|
||||
markerId: const MarkerId('drop'),
|
||||
position: dropLocation,
|
||||
icon: BitmapDescriptor.defaultMarkerWithHue(BitmapDescriptor.hueRed),
|
||||
infoWindow: const InfoWindow(title: 'Drop Location'),
|
||||
),
|
||||
);
|
||||
_lastRiderPosition = riderPos;
|
||||
final riderIcon = _riderIcon;
|
||||
final dropIcon = _dropIcon;
|
||||
if (riderIcon == null || dropIcon == null) return;
|
||||
|
||||
_markers = [
|
||||
NearleMapMarker(
|
||||
id: 'rider',
|
||||
position: riderPos,
|
||||
icon: riderIcon,
|
||||
anchor: MarkerAnchor.center,
|
||||
),
|
||||
NearleMapMarker(id: 'drop', position: dropLocation, icon: dropIcon),
|
||||
];
|
||||
}
|
||||
|
||||
Future<void> _attemptAutoOpenNavigation({bool fromTimer = false}) async {
|
||||
@@ -511,9 +524,8 @@ class _RiderNavigationScreenState extends State<_RiderNavigationScreen>
|
||||
return Scaffold(
|
||||
body: Stack(
|
||||
children: [
|
||||
GoogleMap(
|
||||
mapType: MapType.normal,
|
||||
onMapCreated: (controller) {
|
||||
NearleMap(
|
||||
onMapReady: (controller) {
|
||||
mapController = controller;
|
||||
_mapReady = true;
|
||||
},
|
||||
@@ -527,10 +539,7 @@ class _RiderNavigationScreenState extends State<_RiderNavigationScreen>
|
||||
),
|
||||
markers: _markers,
|
||||
myLocationEnabled: true,
|
||||
myLocationButtonEnabled: false,
|
||||
trafficEnabled: true,
|
||||
compassEnabled: true,
|
||||
buildingsEnabled: true,
|
||||
tiltGesturesEnabled: true,
|
||||
),
|
||||
if (!_hasOpenedGoogleMaps)
|
||||
@@ -994,11 +1003,7 @@ class _RiderNavigationScreenState extends State<_RiderNavigationScreen>
|
||||
}
|
||||
|
||||
void _safeAnimateCamera(CameraUpdate update) {
|
||||
if (!_mapReady || _isDisposed || mapController == null) return;
|
||||
try {
|
||||
mapController?.animateCamera(update);
|
||||
} catch (e) {
|
||||
debugPrint('[NAVIGATION] animateCamera skipped: $e');
|
||||
}
|
||||
if (!_mapReady || _isDisposed) return;
|
||||
mapController?.animateCamera(update);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'dart:convert';
|
||||
import 'package:flutter_polyline_points/flutter_polyline_points.dart';
|
||||
import 'dart:async';
|
||||
import 'package:image_picker/image_picker.dart';
|
||||
import 'dart:io';
|
||||
@@ -22,8 +21,10 @@ import 'package:nearle/controllers/delivery.dart';
|
||||
import 'package:nearle/controllers/deliveries_controller.dart';
|
||||
import 'package:geolocator/geolocator.dart';
|
||||
import 'package:slide_to_submit_button/slide_to_submit_button.dart';
|
||||
import 'package:google_maps_flutter/google_maps_flutter.dart';
|
||||
import 'package:geocoding/geocoding.dart';
|
||||
import 'package:nearle/services/maps/map_marker_icons.dart';
|
||||
import 'package:nearle/services/maps/nearle_map.dart';
|
||||
import 'package:nearle/services/maps/routing_service.dart';
|
||||
import 'package:url_launcher/url_launcher.dart';
|
||||
import 'package:floating/floating.dart';
|
||||
import 'dart:math' as math;
|
||||
@@ -2624,7 +2625,6 @@ void _showmapDetailsSheet(
|
||||
double pickupLat = parseD(pickupLatStr);
|
||||
double pickupLng = parseD(pickupLngStr);
|
||||
bool hasPickup = pickupLat != 0 && pickupLng != 0;
|
||||
final Completer<GoogleMapController> mapController = Completer();
|
||||
|
||||
double riderLat = 0.0;
|
||||
double riderLng = 0.0;
|
||||
@@ -2661,36 +2661,46 @@ void _showmapDetailsSheet(
|
||||
|
||||
final bool hasAny = hasRiderLocation && hasPickup;
|
||||
|
||||
final List<LatLng> polylineCoords = [];
|
||||
final Set<Polyline> polylines = {};
|
||||
|
||||
const String googleAPIKey = "AIzaSyBhkGfnq27sN0wV5y_S-M2KojpFTk_by-Q";
|
||||
final List<NearleMapPolyline> polylines = [];
|
||||
|
||||
if (hasRiderLocation && hasPickup) {
|
||||
final PolylinePoints polylinePoints = PolylinePoints(apiKey: googleAPIKey);
|
||||
final PolylineResult result = await polylinePoints
|
||||
.getRouteBetweenCoordinates(
|
||||
request: PolylineRequest(
|
||||
origin: PointLatLng(riderLat, riderLng),
|
||||
destination: PointLatLng(pickupLat, pickupLng),
|
||||
mode: TravelMode.driving,
|
||||
),
|
||||
);
|
||||
if (result.points.isNotEmpty) {
|
||||
for (var point in result.points) {
|
||||
polylineCoords.add(LatLng(point.latitude, point.longitude));
|
||||
}
|
||||
final route = await RoutingService.getRoute(
|
||||
origin: LatLng(riderLat, riderLng),
|
||||
destination: LatLng(pickupLat, pickupLng),
|
||||
);
|
||||
if (route != null && route.points.isNotEmpty) {
|
||||
polylines.add(
|
||||
Polyline(
|
||||
polylineId: const PolylineId("route"),
|
||||
NearleMapPolyline(
|
||||
id: 'route',
|
||||
points: route.points,
|
||||
color: Colors.blueAccent,
|
||||
width: 5,
|
||||
points: polylineCoords,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
final List<NearleMapMarker> mapMarkers = [];
|
||||
if (hasRiderLocation) {
|
||||
mapMarkers.add(
|
||||
NearleMapMarker(
|
||||
id: 'rider',
|
||||
position: LatLng(riderLat, riderLng),
|
||||
icon: await MapMarkerIcons.dot(Colors.blueAccent),
|
||||
anchor: MarkerAnchor.center,
|
||||
),
|
||||
);
|
||||
}
|
||||
if (hasPickup) {
|
||||
mapMarkers.add(
|
||||
NearleMapMarker(
|
||||
id: 'pickup',
|
||||
position: LatLng(pickupLat, pickupLng),
|
||||
icon: await MapMarkerIcons.pin(Colors.red),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
// ⭐ STEP 3 — Close loader
|
||||
Navigator.pop(context);
|
||||
|
||||
@@ -2848,7 +2858,7 @@ void _showmapDetailsSheet(
|
||||
decoration: BoxDecoration(
|
||||
border: Border.all(color: Colors.grey.shade200),
|
||||
),
|
||||
child: GoogleMap(
|
||||
child: NearleMap(
|
||||
initialCameraPosition: CameraPosition(
|
||||
target: hasRiderLocation && hasPickup
|
||||
? LatLng(
|
||||
@@ -2862,52 +2872,22 @@ void _showmapDetailsSheet(
|
||||
: const LatLng(0, 0),
|
||||
zoom: hasRiderLocation && hasPickup ? 13 : 15,
|
||||
),
|
||||
markers: {
|
||||
if (hasRiderLocation)
|
||||
Marker(
|
||||
markerId: const MarkerId("rider"),
|
||||
position: LatLng(riderLat, riderLng),
|
||||
infoWindow: const InfoWindow(
|
||||
title: "Your Location",
|
||||
),
|
||||
icon: BitmapDescriptor.defaultMarkerWithHue(
|
||||
BitmapDescriptor.hueAzure,
|
||||
),
|
||||
),
|
||||
if (hasPickup)
|
||||
Marker(
|
||||
markerId: const MarkerId("pickup"),
|
||||
position: LatLng(pickupLat, pickupLng),
|
||||
infoWindow: const InfoWindow(
|
||||
title: "Pickup Location",
|
||||
),
|
||||
icon: BitmapDescriptor.defaultMarkerWithHue(
|
||||
BitmapDescriptor.hueRed,
|
||||
),
|
||||
),
|
||||
},
|
||||
markers: mapMarkers,
|
||||
polylines: polylines,
|
||||
zoomGesturesEnabled: true,
|
||||
scrollGesturesEnabled: true,
|
||||
tiltGesturesEnabled: true,
|
||||
rotateGesturesEnabled: true,
|
||||
zoomControlsEnabled: false,
|
||||
myLocationButtonEnabled: false,
|
||||
onMapCreated: (GoogleMapController controller) async {
|
||||
mapController.complete(controller);
|
||||
onMapReady: (controller) async {
|
||||
if (hasRiderLocation && hasPickup) {
|
||||
final bounds = LatLngBounds(
|
||||
southwest: LatLng(
|
||||
riderLat < pickupLat ? riderLat : pickupLat,
|
||||
riderLng < pickupLng ? riderLng : pickupLng,
|
||||
await controller.fitBounds(
|
||||
LatLngBounds(
|
||||
southwest: LatLng(
|
||||
riderLat < pickupLat ? riderLat : pickupLat,
|
||||
riderLng < pickupLng ? riderLng : pickupLng,
|
||||
),
|
||||
northeast: LatLng(
|
||||
riderLat > pickupLat ? riderLat : pickupLat,
|
||||
riderLng > pickupLng ? riderLng : pickupLng,
|
||||
),
|
||||
),
|
||||
northeast: LatLng(
|
||||
riderLat > pickupLat ? riderLat : pickupLat,
|
||||
riderLng > pickupLng ? riderLng : pickupLng,
|
||||
),
|
||||
);
|
||||
await controller.animateCamera(
|
||||
CameraUpdate.newLatLngBounds(bounds, 80),
|
||||
padding: 80,
|
||||
);
|
||||
}
|
||||
},
|
||||
|
||||
@@ -45,7 +45,7 @@ class HelpCenter extends StatelessWidget {
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
Text(
|
||||
"We make sure your delivery experience is smooth and clear. Whether you’re on your first trip or your hundredth, we’ve got your back. Browse through frequently asked questions or reach out directly if you need further help.",
|
||||
"We make sure your delivery experience is smooth and clear. Whether you're on your first trip or your hundredth, we've got your back. Browse through frequently asked questions or reach out directly if you need further help.",
|
||||
style: TextStyle(fontSize: 18,color: Colors.grey.shade700, height: 1.4, fontFamily: FontConstants.fontFamily),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
|
||||
100
pubspec.lock
100
pubspec.lock
@@ -590,14 +590,6 @@ packages:
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "2.0.33"
|
||||
flutter_polyline_points:
|
||||
dependency: "direct main"
|
||||
description:
|
||||
name: flutter_polyline_points
|
||||
sha256: c775fe59fbcf1f925d611c039555c7f58ed6d9411747b7a2915bbd9c5e730a51
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "3.1.0"
|
||||
flutter_screenutil:
|
||||
dependency: "direct main"
|
||||
description:
|
||||
@@ -752,54 +744,6 @@ packages:
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "6.3.3"
|
||||
google_maps:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: google_maps
|
||||
sha256: "5d410c32112d7c6eb7858d359275b2aa04778eed3e36c745aeae905fb2fa6468"
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "8.2.0"
|
||||
google_maps_flutter:
|
||||
dependency: "direct main"
|
||||
description:
|
||||
name: google_maps_flutter
|
||||
sha256: "819985697596a42e1054b5feb2f407ba1ac92262e02844a40168e742b9f36dca"
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "2.14.0"
|
||||
google_maps_flutter_android:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: google_maps_flutter_android
|
||||
sha256: "3835f6ae5e8b8d4d454d913575069513c9f216e088b87aa5c18cb3610951c6b4"
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "2.18.6"
|
||||
google_maps_flutter_ios:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: google_maps_flutter_ios
|
||||
sha256: "115b03c2e637e74d084a78c0e1faf42884fcd8e65d1a9ce58d909ca5493afa32"
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "2.15.7"
|
||||
google_maps_flutter_platform_interface:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: google_maps_flutter_platform_interface
|
||||
sha256: e8b1232419fcdd35c1fdafff96843f5a40238480365599d8ca661dde96d283dd
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "2.14.1"
|
||||
google_maps_flutter_web:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: google_maps_flutter_web
|
||||
sha256: d416602944e1859f3cbbaa53e34785c223fa0a11eddb34a913c964c5cbb5d8cf
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "0.5.14+3"
|
||||
gsettings:
|
||||
dependency: transitive
|
||||
description:
|
||||
@@ -836,10 +780,10 @@ packages:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: image
|
||||
sha256: "4e973fcf4caae1a4be2fa0a13157aa38a8f9cb049db6529aa00b4d71abc4d928"
|
||||
sha256: f9881ff4998044947ec38d098bc7c8316ae1186fa786eddffdb867b9bc94dfce
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "4.5.4"
|
||||
version: "4.8.0"
|
||||
image_picker:
|
||||
dependency: "direct main"
|
||||
description:
|
||||
@@ -968,6 +912,30 @@ packages:
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "3.3.2"
|
||||
maplibre_gl:
|
||||
dependency: "direct main"
|
||||
description:
|
||||
name: maplibre_gl
|
||||
sha256: b676f124a2fcf88c4dafedc7b462155e6396d61ce7af46354b4de11b45c9812f
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "0.26.2"
|
||||
maplibre_gl_platform_interface:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: maplibre_gl_platform_interface
|
||||
sha256: "1f0ca8a99f03fa9434618ee21f4e42dd615830a7dd973e632b11c73ffec993a8"
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "0.26.2"
|
||||
maplibre_gl_web:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: maplibre_gl_web
|
||||
sha256: bbf022f29ceef26d73f63e584819fbd02fbaf4eb1facf5234206c78936cf8f1c
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "0.26.2"
|
||||
matcher:
|
||||
dependency: transitive
|
||||
description:
|
||||
@@ -1216,14 +1184,6 @@ packages:
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "0.0.2"
|
||||
sanitize_html:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: sanitize_html
|
||||
sha256: "12669c4a913688a26555323fb9cec373d8f9fbe091f2d01c40c723b33caa8989"
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "2.1.0"
|
||||
scratcher:
|
||||
dependency: "direct main"
|
||||
description:
|
||||
@@ -1357,14 +1317,6 @@ packages:
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "2.1.4"
|
||||
stream_transform:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: stream_transform
|
||||
sha256: ad47125e588cfd37a9a7f86c7d6356dde8dfe89d071d293f80ca9e9273a33871
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "2.1.1"
|
||||
string_scanner:
|
||||
dependency: transitive
|
||||
description:
|
||||
|
||||
@@ -2,7 +2,7 @@ name: nearle
|
||||
description: Nearle Rider - Smarter Deliveries, Powered by AI
|
||||
publish_to: "none"
|
||||
|
||||
version: 1.2.28+145
|
||||
version: 1.3.0+148
|
||||
|
||||
|
||||
environment:
|
||||
@@ -32,12 +32,11 @@ dependencies:
|
||||
slide_to_submit_button: ^1.0.3
|
||||
flutter_tts: ^4.0.2
|
||||
vibration: ^3.1.4
|
||||
google_maps_flutter: ^2.13.1
|
||||
maplibre_gl: ^0.26.2
|
||||
contained_tab_bar_view: ^0.8.0
|
||||
tab_indicator_styler: ^2.0.0
|
||||
geocoding: ^4.0.0
|
||||
fl_chart: ^1.1.1
|
||||
flutter_polyline_points: ^3.1.0
|
||||
battery_plus: ^7.0.0
|
||||
provider: ^6.1.5+1
|
||||
slider_button_lite: ^1.0.1
|
||||
|
||||
175
test/maps_smoke_test.dart
Normal file
175
test/maps_smoke_test.dart
Normal file
@@ -0,0 +1,175 @@
|
||||
// Smoke test for the map/routing stack.
|
||||
//
|
||||
// This deliberately hits the LIVE endpoints the app uses, because the point is
|
||||
// to verify the integration end to end — not to test mocks. It therefore needs
|
||||
// network access and is not suitable for offline CI.
|
||||
//
|
||||
// flutter test test/maps_smoke_test.dart
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:nearle/services/maps/map_config.dart';
|
||||
import 'package:nearle/services/maps/map_marker_icons.dart';
|
||||
import 'package:nearle/services/maps/routing_service.dart';
|
||||
import 'package:maplibre_gl/maplibre_gl.dart' show LatLng;
|
||||
|
||||
void main() {
|
||||
// The binding is needed to rasterise marker icons, but it also installs an
|
||||
// HttpOverrides that stubs every request with a 400. Clear it so the LIVE
|
||||
// groups below actually reach the network.
|
||||
TestWidgetsFlutterBinding.ensureInitialized();
|
||||
HttpOverrides.global = null;
|
||||
|
||||
// Two real points in Coimbatore, where Nearle operates.
|
||||
const origin = LatLng(11.0168, 76.9558);
|
||||
const destination = LatLng(10.9984, 76.9776);
|
||||
|
||||
group('polyline decoder (offline)', () {
|
||||
test('decodes a known Google-encoded polyline', () {
|
||||
// Canonical example from Google's encoded-polyline spec:
|
||||
// (38.5,-120.2) (40.7,-120.95) (43.252,-126.453)
|
||||
final points = RoutingService.decodePolyline('_p~iF~ps|U_ulLnnqC_mqNvxq`@');
|
||||
|
||||
expect(points.length, 3);
|
||||
expect(points[0].latitude, closeTo(38.5, 0.00001));
|
||||
expect(points[0].longitude, closeTo(-120.2, 0.00001));
|
||||
expect(points[2].latitude, closeTo(43.252, 0.00001));
|
||||
expect(points[2].longitude, closeTo(-126.453, 0.00001));
|
||||
});
|
||||
|
||||
test('returns empty for garbage rather than throwing', () {
|
||||
expect(RoutingService.decodePolyline(''), isEmpty);
|
||||
expect(() => RoutingService.decodePolyline('!!!'), returnsNormally);
|
||||
});
|
||||
});
|
||||
|
||||
group('marker icons (offline)', () {
|
||||
test('renders PNG bytes with a stable cache key', () async {
|
||||
final red = await MapMarkerIcons.pin(Colors.red);
|
||||
final redAgain = await MapMarkerIcons.pin(Colors.red);
|
||||
final green = await MapMarkerIcons.pin(Colors.green);
|
||||
final dot = await MapMarkerIcons.dot(Colors.blueAccent);
|
||||
final step = await MapMarkerIcons.numberedCircle(
|
||||
3,
|
||||
primaryColor: Colors.deepPurple,
|
||||
);
|
||||
|
||||
// PNG magic number.
|
||||
for (final icon in [red, green, dot, step]) {
|
||||
expect(icon.bytes.length, greaterThan(100));
|
||||
expect(icon.bytes.sublist(0, 4), [0x89, 0x50, 0x4E, 0x47]);
|
||||
}
|
||||
|
||||
expect(red.key, redAgain.key, reason: 'same colour must reuse the key');
|
||||
expect(identical(red.bytes, redAgain.bytes), isTrue,
|
||||
reason: 'second call must come from cache');
|
||||
expect(red.key, isNot(green.key));
|
||||
expect(step.key, contains('3'));
|
||||
});
|
||||
});
|
||||
|
||||
group('LIVE tile style', () {
|
||||
test('style document is fetchable and MapLibre-shaped', () async {
|
||||
final response = await http
|
||||
.get(Uri.parse(MapConfig.styleUrl))
|
||||
.timeout(const Duration(seconds: 30));
|
||||
|
||||
expect(response.statusCode, 200, reason: MapConfig.styleUrl);
|
||||
|
||||
final style = json.decode(response.body) as Map<String, dynamic>;
|
||||
expect(style['version'], 8, reason: 'MapLibre style spec version');
|
||||
expect(style['sources'], isA<Map>());
|
||||
expect((style['layers'] as List), isNotEmpty);
|
||||
|
||||
// No credential should be needed anywhere in the document.
|
||||
expect(
|
||||
response.body.contains('api_key='),
|
||||
isFalse,
|
||||
reason: 'tile style must not require a key',
|
||||
);
|
||||
}, timeout: const Timeout(Duration(seconds: 60)));
|
||||
});
|
||||
|
||||
group('LIVE routing', () {
|
||||
setUp(RoutingService.clearCache);
|
||||
|
||||
test('returns a plausible road route', () async {
|
||||
final route = await RoutingService.getRoute(
|
||||
origin: origin,
|
||||
destination: destination,
|
||||
);
|
||||
|
||||
expect(route, isNotNull, reason: 'no route from ${MapConfig.osrmBaseUrl}');
|
||||
route!;
|
||||
|
||||
// ~2.5 km straight line; the road route must be longer but not absurd.
|
||||
expect(route.distanceMeters, greaterThan(2000));
|
||||
expect(route.distanceMeters, lessThan(15000));
|
||||
expect(route.durationSeconds, greaterThan(0));
|
||||
|
||||
// Geometry must actually be decoded, not a two-point fallback.
|
||||
expect(route.points.length, greaterThan(10));
|
||||
|
||||
// Route must start and end near the requested points, which also proves
|
||||
// the lon,lat ordering in the request is right — swapped coordinates
|
||||
// would land in the Indian Ocean.
|
||||
expect(route.points.first.latitude, closeTo(origin.latitude, 0.02));
|
||||
expect(route.points.first.longitude, closeTo(origin.longitude, 0.02));
|
||||
expect(route.points.last.latitude, closeTo(destination.latitude, 0.02));
|
||||
expect(route.points.last.longitude, closeTo(destination.longitude, 0.02));
|
||||
}, timeout: const Timeout(Duration(seconds: 60)));
|
||||
|
||||
test('multi-stop route is one call and visits every waypoint', () async {
|
||||
const waypoint = LatLng(11.0075, 76.9660);
|
||||
|
||||
final route = await RoutingService.getRoute(
|
||||
origin: origin,
|
||||
destination: destination,
|
||||
waypoints: const [waypoint],
|
||||
);
|
||||
|
||||
expect(route, isNotNull);
|
||||
route!;
|
||||
expect(route.points.length, greaterThan(10));
|
||||
|
||||
// The path should pass near the waypoint.
|
||||
final passesNear = route.points.any(
|
||||
(p) =>
|
||||
(p.latitude - waypoint.latitude).abs() < 0.01 &&
|
||||
(p.longitude - waypoint.longitude).abs() < 0.01,
|
||||
);
|
||||
expect(passesNear, isTrue, reason: 'route ignored the waypoint');
|
||||
}, timeout: const Timeout(Duration(seconds: 60)));
|
||||
|
||||
test('second identical request is served from cache', () async {
|
||||
final first = await RoutingService.getRoute(
|
||||
origin: origin,
|
||||
destination: destination,
|
||||
);
|
||||
expect(first, isNotNull);
|
||||
|
||||
final watch = Stopwatch()..start();
|
||||
final second = await RoutingService.getRoute(
|
||||
origin: origin,
|
||||
destination: destination,
|
||||
);
|
||||
watch.stop();
|
||||
|
||||
expect(identical(first, second), isTrue,
|
||||
reason: 'cache must return the same instance');
|
||||
expect(watch.elapsedMilliseconds, lessThan(50),
|
||||
reason: 'cache hit should not touch the network');
|
||||
}, timeout: const Timeout(Duration(seconds: 60)));
|
||||
|
||||
test('invalid coordinates fail closed instead of throwing', () async {
|
||||
final route = await RoutingService.getRoute(
|
||||
origin: const LatLng(0, 0),
|
||||
destination: destination,
|
||||
);
|
||||
expect(route, isNull);
|
||||
});
|
||||
});
|
||||
}
|
||||
132
tool/compare_route_distances.py
Executable file
132
tool/compare_route_distances.py
Executable file
@@ -0,0 +1,132 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Compare OSRM road distances against previously-recorded (Google) distances.
|
||||
|
||||
Routing distance feeds `riderkms` -> `ridercharges`, so a systematic difference
|
||||
between the old and new routing backend changes what riders get paid. Run this
|
||||
against a sample of completed deliveries BEFORE rolling out.
|
||||
|
||||
Input CSV (header required), one row per completed delivery:
|
||||
|
||||
origin_lat,origin_lng,dest_lat,dest_lng,recorded_km
|
||||
11.0168,76.9558,10.9984,76.9776,4.6
|
||||
11.0510,76.9910,11.0168,76.9558,6.1
|
||||
|
||||
Usage:
|
||||
|
||||
python3 tool/compare_route_distances.py deliveries.csv
|
||||
python3 tool/compare_route_distances.py deliveries.csv \\
|
||||
--osrm https://osrm.your-domain.in
|
||||
"""
|
||||
|
||||
import argparse
|
||||
import csv
|
||||
import json
|
||||
import statistics
|
||||
import subprocess
|
||||
import sys
|
||||
import urllib.parse
|
||||
import urllib.request
|
||||
|
||||
DEFAULT_OSRM = "https://routing.openstreetmap.de/routed-car"
|
||||
USER_AGENT = "nearle-distance-check"
|
||||
|
||||
|
||||
def _fetch(url, timeout):
|
||||
"""GET a URL, falling back to curl when Python's SSL stack is too old.
|
||||
|
||||
Some systems ship a Python linked against an OpenSSL that fails the
|
||||
handshake against modern servers; curl on the same box usually works.
|
||||
"""
|
||||
try:
|
||||
req = urllib.request.Request(url, headers={"User-Agent": USER_AGENT})
|
||||
with urllib.request.urlopen(req, timeout=timeout) as resp:
|
||||
return resp.read().decode()
|
||||
except Exception: # noqa: BLE001
|
||||
result = subprocess.run(
|
||||
["curl", "-sS", "--max-time", str(timeout), "-A", USER_AGENT, url],
|
||||
capture_output=True, text=True,
|
||||
)
|
||||
if result.returncode != 0:
|
||||
raise RuntimeError(result.stderr.strip() or "curl failed") from None
|
||||
return result.stdout
|
||||
|
||||
|
||||
def osrm_km(base, o_lat, o_lng, d_lat, d_lng, timeout=25):
|
||||
"""Road distance in km, or None. Note OSRM takes lon,lat."""
|
||||
url = (
|
||||
f"{base}/route/v1/driving/"
|
||||
f"{o_lng},{o_lat};{d_lng},{d_lat}"
|
||||
f"?{urllib.parse.urlencode({'overview': 'false'})}"
|
||||
)
|
||||
data = json.loads(_fetch(url, timeout))
|
||||
if data.get("code") != "Ok" or not data.get("routes"):
|
||||
return None
|
||||
return data["routes"][0]["distance"] / 1000.0
|
||||
|
||||
|
||||
def main():
|
||||
ap = argparse.ArgumentParser(description=__doc__,
|
||||
formatter_class=argparse.RawDescriptionHelpFormatter)
|
||||
ap.add_argument("csv_path", help="CSV of completed deliveries")
|
||||
ap.add_argument("--osrm", default=DEFAULT_OSRM,
|
||||
help=f"OSRM base URL (default: {DEFAULT_OSRM})")
|
||||
args = ap.parse_args()
|
||||
|
||||
deltas, failures = [], 0
|
||||
|
||||
print(f"{'recorded':>10} {'osrm':>10} {'delta':>10} {'pct':>8}")
|
||||
print("-" * 42)
|
||||
|
||||
with open(args.csv_path, newline="") as fh:
|
||||
for row in csv.DictReader(fh):
|
||||
try:
|
||||
recorded = float(row["recorded_km"])
|
||||
km = osrm_km(
|
||||
args.osrm,
|
||||
float(row["origin_lat"]), float(row["origin_lng"]),
|
||||
float(row["dest_lat"]), float(row["dest_lng"]),
|
||||
)
|
||||
except Exception as exc: # noqa: BLE001
|
||||
print(f" ERROR: {exc}", file=sys.stderr)
|
||||
failures += 1
|
||||
continue
|
||||
|
||||
if km is None:
|
||||
print(" no route found", file=sys.stderr)
|
||||
failures += 1
|
||||
continue
|
||||
|
||||
delta = km - recorded
|
||||
pct = (delta / recorded * 100) if recorded else 0.0
|
||||
deltas.append(pct)
|
||||
print(f"{recorded:9.2f}k {km:9.2f}k {delta:+9.2f}k {pct:+7.1f}%")
|
||||
|
||||
if not deltas:
|
||||
print("\nNo comparable rows.", file=sys.stderr)
|
||||
return 1
|
||||
|
||||
mean = statistics.fmean(deltas)
|
||||
median = statistics.median(deltas)
|
||||
print("-" * 42)
|
||||
print(f"rows compared : {len(deltas)} (failed: {failures})")
|
||||
print(f"mean delta : {mean:+.1f}%")
|
||||
print(f"median delta : {median:+.1f}%")
|
||||
if len(deltas) > 1:
|
||||
print(f"std dev : {statistics.stdev(deltas):.1f}%")
|
||||
|
||||
print()
|
||||
if abs(median) < 3:
|
||||
print("OK — no meaningful bias. Rider pay is unaffected.")
|
||||
elif abs(median) < 8:
|
||||
print("MILD BIAS — acceptable, but tell finance the average trip "
|
||||
f"distance shifts {median:+.1f}%.")
|
||||
else:
|
||||
print(f"SIGNIFICANT BIAS ({median:+.1f}%) — investigate before rollout. "
|
||||
"Usually means missing OSM roads in your service area; consider "
|
||||
"contributing the missing roads to OpenStreetMap, which fixes it "
|
||||
"permanently and for everyone.")
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
Reference in New Issue
Block a user