Skip to content

feat(metro): locate nearest station across systems - #114

Open
steventeng2022 wants to merge 11 commits into
YetAnotherBusDeveloper:mainfrom
steventeng2022:codex/nearest-metro-station
Open

steventeng2022 wants to merge 11 commits into
YetAnotherBusDeveloper:mainfrom
steventeng2022:codex/nearest-metro-station

Conversation

@steventeng2022

Copy link
Copy Markdown
Contributor

Adds a Find nearest station / 定位最近的車站 button to the metro dashboard. For example, a rider in Kaohsiung with Taipei Metro selected can locate once to switch to the nearest station's system and serving line, open its map, and load its arrival information.

  • Compare stations across all metro systems using the existing cached repository and shared location resolver. Calculate distances locally and request location only after a tap.
  • Resolve line membership from station-of-line, including stations whose line_id is empty. Ignore invalid coordinates and unavailable lines; prefer the current line at an interchange when distances tie.
  • Keep usable results when a system fails and disclose the incomplete coverage. Preserve the current selection on failure, and discard late results after a manual selection or tab change.
  • Include English and Traditional Chinese strings, generated localizations, README documentation, and unit/widget regression coverage.

Closes #55.

Validation with Flutter 3.47.6 / Dart 3.13.5:

  • 26 focused tests passed across nearest_metro_station_test.dart, metro_nearest_station_test.dart, user_location_test.dart, metro_direction_test.dart, and metro_direction_selector_test.dart.
  • Static analysis of all changed Dart files: no issues.
  • Formatting and git diff --check: passed.

The repository-wide analyzer reports six pre-existing unawaited_return_in_try_block warnings in app_controller.dart, app_update_service.dart, and bus_repository.dart with this newer local SDK. The repository's CI uses Flutter 3.41.6; no dependency or SDK versions are changed by this PR. Physical-device GPS and the full test suite were not exercised locally.

@Av1anJay Av1anJay left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code review: changes requested

  • lib/screens/metro_dashboard_screen.dart:262-263 — Incomplete system data is reported as complete. A system is counted as available whenever its three requests return successfully, even if its stations or station-of-line list is empty, or all its stations have invalid coordinates / no matching line. For example, if KRTC returns HTTP 200 with an empty station list while TRTC has data, a user in Kaohsiung will be told that a Taipei station is the nearest with no incomplete-coverage warning. _getJsonList also converts a non-list HTTP 200 response to an empty list, so this can occur without an exception. Detect systems without usable candidate stations as incomplete, and add a widget regression test with one unusable and one usable system.

Checks: GitHub CI Verify passed (Flutter analyze, full Flutter tests, web route test, Node runtime tests); Android, iOS, Linux, macOS, Windows, Wear OS and Web builds passed. Locally, git diff --check passed and the Node runtime tests passed on Node 22.23.4 (3 tests). Flutter SDK is not installed locally, so Flutter checks were not rerun here. Inspected live metro API responses and reproduced the incomplete-coverage condition with an empty KRTC station list.

@Av1anJay Av1anJay left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code review: changes requested

  • P2 — First web lookup can remain stuck after location permission is granted. The new metro lookup invokes the existing shared location resolver, whose web permission request can wait for a first position before reaching the resolver's six-second position timeout. See the inline comment for the failure sequence and the pinned web implementation.

Previous review: The reported wholly unusable-system scenario is fixed: systems without a selectable station now count as unavailable, and the new KRTC-empty/TRTC-usable widget test checks the partial-coverage warning (original review).

Checks and limits: Exact-head CI ran flutter analyze successfully and flutter test (704 passed); its separate web test only runs route_path_points_test. Metro widget tests inject a fake resolver and use the Windows target. The web location issue is a static analysis of the pinned plugin, not an executed browser/GPS test; Flutter was unavailable locally. git diff --check passed.

Comment thread lib/screens/metro_dashboard_screen.dart Resolved
On web the browser prompt maps to LocationPermission.denied, and
requestPermission() waits for a fix with a one-day timeout. Skip it on web,
let the browser enforce the time limit after the prompt is answered, and
report a position error as a failed fix rather than a permission refusal.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@steventeng2022

Copy link
Copy Markdown
Contributor Author

Thanks for the review — the web first-position issue is addressed in 55e9a9b:

  • On web, resolveUserPosition no longer calls requestPermission() (the browser's prompt state maps to denied, and that call waits on a fix with a one-day default timeout). It calls getCurrentPosition directly, which shows the prompt.
  • The 6s limit is passed to the browser, which only starts it after the prompt is answered, so a user still deciding isn't timed out, while a granted-but-failed fix is now bounded. No Dart-side timeout on web, since that would expire an open prompt.
  • A refused prompt (PermissionDeniedException) is reported as a permission failure; any other error is a failed fix (last known position, else the 'cannot get location' message) rather than a permission denial.
  • Added three web tests in user_location_test.dart (direct call with time limit passed through, refused prompt, failed fix).

Not run locally (no Flutter here) and not tested in a real browser — same static-analysis caveat as the review; relying on CI and a manual web check.

🤖 Generated with Claude Code

@Av1anJay Av1anJay left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code review: changes requested

The previous first-web-lookup issue is still open: this revision bypasses the plugin's one-day permission request, but the replacement passes the requested timeout through a pinned web plugin that interprets its units incorrectly. The same change also causes a separate first-refusal regression in the rail picker and YouBike. See both inline findings below. The earlier wholly unusable-system finding remains fixed (original review); the remaining timeout issue follows up the previous review.

Checks and limits: Exact-head CI ran flutter analyze successfully and flutter test (707 passed). Its Chrome test runs only test/route_path_points_test.dart, not web location. git diff --check passed. The findings are based on static analysis of the app and pinned plugin; no real browser/GPS test or local Flutter test was run.

Comment thread lib/core/user_location.dart Resolved
Comment thread lib/core/user_location.dart Resolved
…-refusal state

geolocator_web 4.1.3 passes the timeout to the browser in microseconds where
milliseconds are expected, so the 6s limit became ~100 minutes. On web, stop
passing it and start a Dart-side limit once the permission is no longer
pending, so an unanswered prompt never times out. Report the permission state
the browser settled on after a refusal so blocked sites are deniedForever and
dismissed prompts stay retryable. Add resolver and rail picker tests.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@steventeng2022

Copy link
Copy Markdown
Contributor Author

Thanks — both findings from the latest review are addressed in b33f0fa:

  • Timeout units: on web the plugin timeout is no longer passed at all (geolocator_web 4.1.3 hands microseconds to the browser as milliseconds). A Dart-side limit starts once checkPermission() is no longer denied (polled while the prompt is pending; immediate for already-granted sites, so they keep the six-second bound). An unanswered prompt is never timed out; a granted prompt with no fix fails after the limit.
  • First refusal: after a PermissionDeniedException the resolver re-reads the permission, so a blocked site reports deniedForever (rail picker and YouBike show the permission message) and a merely dismissed prompt stays retryable.
  • Tests: resolver tests for no plugin timeout, pending prompt not timing out, granted-no-fix timing out, already-granted keeping its limit, and refusal in both settled states; plus a rail picker widget test running the real resolver through a first web refusal. I did not add a YouBike widget test — it uses the same deniedForever branch, but it isn't covered separately.

Not run locally (no Flutter here) and not tested in a real browser; relying on CI.

🤖 Generated with Claude Code

@Av1anJay Av1anJay left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code review: changes requested

The earlier web-location timing issue is still open for browsers without the Permissions API: an unanswered location prompt can expire after six seconds. See the inline finding. This is a remaining variant of the issue in the previous review, not a second independent timeout finding.

Prior findings: The microseconds/milliseconds timeout path is addressed for queryable, granted permissions by the Dart deadline. The previously reported first-refusal case is addressed when the browser reports the site as blocked: the resolver now rechecks the state, and the rail picker test covers that path. My earlier wording implied every first refusal behaved that way; I could not establish that broader claim for retryable dismissals as a regression, so I am not repeating it as an open blocker. The wholly unusable-system coverage warning remains fixed (original review).

Checks and limits: Exact-head CI ran flutter analyze successfully and flutter test (711 passed). Its Chrome test runs only test/route_path_points_test.dart, not browser location. git diff --check passed. The remaining finding is based on static analysis of the app, pinned plugin, and published browser compatibility data; no real browser/GPS or local Flutter test was run.

Comment thread lib/core/user_location.dart Resolved
… is unknown

Browsers without the Permissions API (Safari 15) report unableToDetermine, which
was treated as granted and started the six-second limit while the user was still
answering the prompt. Unknown now waits up to a longer cap instead. Tests observe
the real pending-to-granted transition and the unknown state.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@steventeng2022

Copy link
Copy Markdown
Contributor Author

Thanks — addressed in 6e02474:

  • unableToDetermine is no longer treated as granted. Only whileInUse/always start the six-second limit immediately; denied (prompt) is polled and the limit starts when it flips to granted.
  • With no readable state (no Permissions API), the answer can't be observed, so the limit can't be anchored to it. That case now waits up to a separate 90s cap rather than six seconds: a slow answer still works, but the request can't hang for the plugin's one-day default. The trade-off is that a granted-but-no-fix case there takes up to 90s to report failure.
  • Tests: the pending-to-granted test now answers the prompt after the resolver has been polling for longer than the limit and asserts the limit ran from the answer; new tests for the unknown state (slow answer succeeds; never-resolving request still ends at the cap).

Not run locally and not tested in a real browser (e.g. Safari 15); relying on CI.

🤖 Generated with Claude Code

steventeng2022 and others added 2 commits November 26, 2026 23:40
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…and test tolerance

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

@Av1anJay Av1anJay left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code review: changes requested

The unknown-permission branch improves the previous six-second prompt cutoff to 90 seconds, but the original timing issue remains in a narrower form and the same fallback now extends an already-granted, stalled fix from six to 90 seconds in existing shared-resolver callers. See the consolidated inline finding and previous review. I saw the author's explanation of this trade-off in the reply.

Earlier findings: The wholly unusable-system coverage warning remains fixed. The earlier plugin time-unit mismatch is avoided by omitting its timeout; blocked-site refusal remains fixed. My earlier broader refusal claim was not established for other dismissal cases as a new regression, so I am not repeating it.

Checks and limits: Exact-head CI ran flutter analyze successfully and flutter test (713 passed). Its Chrome test runs only test/route_path_points_test.dart (2 passed), not web location. git diff --check passed. The finding is static analysis of the app and pinned plugin; no real Safari/GPS or local Flutter test was run. Other build jobs were still running at last check.

} catch (_) {}
});
} else {
startDeadline(unknownLimit);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 — Unknown permission trades a prompt cutoff for a 90-second stall in existing screens. On Safari/iOS Safari 15 the pinned geolocator_web returns unableToDetermine when the Permissions API is absent, including when the site was already granted permission. If the browser then neither delivers a fix nor errors, this branch now waits 90 seconds rather than the base resolver's six-second Dart timeout. In particular, YouBikeScreen._initLocation waits for the resolver before loading the default area's stations, so the user sees the locating state for up to 90 seconds instead of reaching the fallback after six. If the user is still answering the prompt after 90 seconds, the same timer also ends the lookup and ignores a later grant; that is a narrower remaining variant of the previous finding, not a second issue.

Please preserve a prompt-aware acquisition bound without imposing the long wait on already-granted, no-fix requests. One avenue to evaluate is a web-specific browser timeout adapter with the plugin's unit conversion corrected: the Geolocation specification requests permission before starting position acquisition, but browser behavior needs validation. The new tests use a fake platform; this scenario is source-based and has not been run on a real Safari/GPS device.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature] 捷運分頁:定位最近的車站

3 participants