Skip to content

Fix SharedPreferences key/type mismatches and upgrade safety#10

Closed
timbortnik wants to merge 1 commit intomainfrom
fix/shared-preferences-keys
Closed

Fix SharedPreferences key/type mismatches and upgrade safety#10
timbortnik wants to merge 1 commit intomainfrom
fix/shared-preferences-keys

Conversation

@timbortnik
Copy link
Copy Markdown
Owner

Summary

  • Key mismatch: Kotlin now reads saved_* (user intent) instead of cached_* with
    fallback to legacy cached_* Float keys for upgrade compatibility
  • Type mismatch: Correctly decodes home_widget doubles via getLong() +
    Double.fromBits() instead of getFloat()
  • Race condition: Discards stale weather fetch if user changed location mid-request
  • GPS persistence: _loadWeather now persists GPS coordinates to HomeWidget so background
    refresh (AlarmManager, WorkManager) has coordinates to use
  • Migration: location_sourcesaved_location_source, removes redundant use_gps key,
    cleans both SharedPreferences and HomeWidget stores
  • City clearing: saveLocation() removes stale city when called without one (e.g.
    switching from manual to GPS)
  • Dead code: Removes unused getSavedLocationFromWidget()

Upgrade safety

  • Dart migration in main.dart handles key renames (idempotent, runs on app open)
  • Kotlin fallback in getLocation() bridges GPS users who haven't opened the app post-upgrade
  • saveLocation() skips redundant writes when nothing changed

Note

This is a significant refactor for what were mostly latent issues (the old cached_* Float
round-trip worked in practice). The main real-world fixes are the 0,0 coordinate bug and the
race condition. Consider whether the complexity is warranted.

Test plan

  • make analyze — no issues
  • make test — all Dart + Kotlin tests pass
  • Install on device with existing widget, verify background refresh works without opening
    app
  • Open app, verify GPS coordinates persisted and widget refreshes
  • Switch from manual to GPS, verify stale city cleared
  • Fresh install, verify widget works after first app launch

🤖 Generated with Claude Code

- Read saved_* (Long, user intent) instead of cached_* (Float) in
  WeatherFetcher, with fallback to cached_* for upgrade compatibility
- Decode home_widget doubles correctly: getLong() + Double.fromBits()
- Persist GPS coordinates in _loadWeather for background refresh
- Add race condition check: discard stale fetch if location changed
- Migrate location_source → saved_location_source, remove use_gps
- Clear stale city when saveLocation called without city
- Skip redundant writes when coordinates unchanged
- Remove dead getSavedLocationFromWidget() method
- Document SharedPreferences keys and upgrade safety pattern

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@timbortnik
Copy link
Copy Markdown
Owner Author

Will revisit later

@timbortnik timbortnik closed this Apr 14, 2026
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