Skip to content

Fix GPS fallback: reuse last GPS location instead of Berlin - #22

Merged
timbortnik merged 1 commit into
mainfrom
fix/gps-fallback-last-location
Jun 13, 2026
Merged

Fix GPS fallback: reuse last GPS location instead of Berlin#22
timbortnik merged 1 commit into
mainfrom
fix/gps-fallback-last-location

Conversation

@timbortnik

@timbortnik timbortnik commented Jun 13, 2026

Copy link
Copy Markdown
Owner

Problem

When GPS is unavailable, Meteograph falls back to Berlin even when a valid GPS
location was previously acquired. This shows up after the app has been dormant
(Android 11+ auto-revokes location permission for unused apps) or after a reboot
when a cold GPS fix times out.

Root cause is in the foreground path (LocationService._getGpsLocation):
every "no fresh fix" branch — disabled location services, non-granted permission,
or a one-shot/last-known timeout — jumped straight to the Berlin default without
checking for a previously-resolved GPS location.

That Berlin result then propagates and poisons the widget cache:
home_screen.dart _loadWeatherNativeSvgService.fetchWeather(Berlin)
overwrites the native cached_latitude/cached_longitude (the same
SharedPreferences file the widget reads) and cacheLocationInfo overwrites the
cached city/source. The native background widget refresh then keeps rendering
Berlin.

Fix

  • On every successful GPS resolution (_toLocationData, covering both the
    one-shot fix and the last-known fallback), persist coordinates + city to new
    keys last_gps_latitude / last_gps_longitude / last_gps_city.
  • All three "no fresh fix" branches now route through a new _lastGpsOrFallback()
    helper, which returns the stored last-GPS location (tagged LocationSource.gps)
    and only falls back to Berlin when no GPS location was ever stored (genuine
    first run).
  • Kept separate from the existing saved_* keys (manual-location-only, which
    force use_gps=false), so GPS and manual modes don't clobber each other.

The change is entirely Dart-side; the native cache self-heals on the next good fix.

Tests

Added a LocationService GPS fallback group covering:

  • persisting the last GPS location on a successful fix
  • reusing it when permission is revoked, when services are disabled, and when a
    fresh fix times out (none fall back to Berlin)
  • still falling back to Berlin when no GPS location was ever stored

make analyze clean; all 120 Dart tests pass. No Kotlin changed.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Improvements
    • Location service now remembers your last known GPS position and reuses it when fresh location data is unavailable due to disabled services, permission changes, or signal loss. The default location is used only as a final fallback when no previous GPS data exists.

When GPS mode was active but a fresh fix couldn't be obtained — revoked
permission (Android 11+ auto-revokes for unused apps), disabled location
services, or a cold-start timeout with no last-known position — the
foreground path jumped straight to the Berlin default. That result flowed
through `_loadWeather` → `NativeSvgService.fetchWeather`, overwriting the
native `cached_latitude`/`cached_longitude` (and cached city/source) that
the background widget refresh reads, so the widget got stuck on Berlin.

Now every successful GPS resolution persists its coordinates + city to
new `last_gps_*` keys, and all "no fresh fix" branches reuse that stored
location (tagged as GPS) before falling back to Berlin. Berlin is only
used when no GPS location was ever stored (genuine first run). Kept
separate from the manual `saved_*` keys so the two modes don't clobber
each other. Fix is Dart-side; the native cache self-heals on next fix.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Jun 13, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 73561d12-6752-4143-b9a8-2d1b40c82c4c

📥 Commits

Reviewing files that changed from the base of the PR and between d42e0ed and a21e8e0.

📒 Files selected for processing (2)
  • lib/services/location_service.dart
  • test/location_service_test.dart

📝 Walkthrough

Walkthrough

LocationService now caches successfully resolved GPS locations and reuses them during service unavailability or permission denial, falling back to Berlin only when no prior GPS fix exists. Implementation adds widget-store keys, helper methods, and fallback routing in _getGpsLocation(). Tests verify persistence and fallback scenarios with an enhanced mock infrastructure.

Changes

GPS Location Fallback Persistence

Layer / File(s) Summary
GPS fallback storage keys and helper methods
lib/services/location_service.dart
Adds widget-store keys for last GPS latitude, longitude, and city; implements _saveLastGpsLocation() and _getLastGpsLocation() to persist and retrieve cached GPS fixes.
GPS fallback routing in _getGpsLocation()
lib/services/location_service.dart
Updates _getGpsLocation() to route service-disabled, permission-denied, and timeout cases through _lastGpsOrFallback() instead of directly returning Berlin; persists successful GPS fixes after city resolution.
Test mock infrastructure for GPS fallback testing
test/location_service_test.dart
Refactors MethodChannel mock handler to switch-based dispatcher with shared mock state variables; updates setUp() to reset GPS-related mock state.
GPS fallback test group
test/location_service_test.dart
Adds comprehensive test cases verifying GPS persistence across permission revocation, service disablement, and timeout scenarios, with fallback to Berlin when no GPS has been stored.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title directly and clearly describes the main change: reusing the last GPS location instead of defaulting to Berlin when a fresh GPS fix is unavailable, which is the core fix implemented across both modified files.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/gps-fallback-last-location

Comment @coderabbitai help to get the list of available commands and usage tips.

@timbortnik
timbortnik merged commit 0a896d7 into main Jun 13, 2026
5 checks passed
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.

1 participant