Files
Alby96andClaude Opus 5 06ab816ebe Add saved places and optional device location
A saved place is a named point the user keeps: it frames the map now, and once
the worker publishes station data it is what the nearest-station readings and,
later, the rain notifications will hang off. Free points rather than stations,
because people think in terms of home and work, not in terms of which weather
station happens to represent them.

Everything stays on the device. Places live in SharedPreferences under a
versioned key, and there is no account to attach them to and no server that
would accept them. That is what keeps the Data safety declaration able to say
no location is collected, and it must survive the notification work: the device
will subscribe to the topic for the cell containing a place, so the link
between a person and a place never leaves their phone.

Location is coarse only, and that took enforcing. geolocator declares
ACCESS_FINE_LOCATION in its own manifest and the merger pulls it in, so the
system dialog offered "Precise" despite the app asking for nothing of the sort;
the manifest now removes it with tools:node="remove", and the dialog reads
"approximate location" with no choice offered. Requests also go through the
platform LocationManager rather than the Play Services fused provider, which
prompts about Location Accuracy and, when declined, returns no fix at all —
an absurd outcome for an app that only ever wanted an approximate one, and one
that also tied location to Play Services being present.

The prominent disclosure comes before the system dialog, as Play requires, and
is repeated in Settings so someone who already answered can still read what the
permission is for. Declining leaves the app fully usable.

Two more defects found by running it and by a test:

- MapLibreMap leaves cameraPosition null unless trackCameraPosition is set, so
  "save the map centre" silently saved the region default rather than what the
  user was looking at.
- Place ids came straight from the microsecond clock, so two places saved in
  the same microsecond shared an id and rename, remove and the duplicate-name
  check all acted on the wrong one. A test caught it on a fast machine.

Saving refuses points outside the region rather than accepting them: a place in
Rome would look like it worked and then show nothing forever.

Also corrects CLAUDE.md, which still said ARPA states no licence, and records
the ARPA realtime API there with the property that governs how it may be used —
it lags about 4.5 hours, so it is an observation archive and must never sit
next to 5-minute radar looking current.

Verified: analyze clean, 158 tests passing, and on the emulator the disclosure
precedes the system dialog, the dialog asks only for approximate location, a
place survives restart and reinstall, and tapping one moves the map onto it.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-10 19:39:03 +02:00

143 lines
4.8 KiB
Dart

import 'package:flutter/foundation.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:geolocator/geolocator.dart';
import '../../core/region/geo.dart';
/// What stands between the app and a position fix.
enum LocationAvailability {
/// Ready to locate.
granted,
/// Location is switched off on the device. The user has to enable it in
/// system settings; asking for permission would not help.
serviceDisabled,
/// Not granted yet, but askable.
denied,
/// Refused permanently. Only system settings can undo it, so the UI must send
/// the user there rather than asking again into the void.
deniedForever,
}
/// Reads the device position.
///
/// Behind an interface so the permission dance can be tested without a device
/// and without the plugin's static methods.
abstract interface class LocationService {
Future<LocationAvailability> availability();
/// Asks for permission if it has not been granted yet.
Future<LocationAvailability> request();
/// A single coarse fix, or null if it could not be obtained.
Future<GeoPoint?> currentPoint();
}
class GeolocatorLocationService implements LocationService {
const GeolocatorLocationService();
/// Deliberately coarse.
///
/// This app centres a map and picks the nearest weather station, and stations
/// are kilometres apart. Street-level precision would buy nothing and would
/// mean asking for a more invasive permission, so the app declares only
/// `ACCESS_COARSE_LOCATION` and asks for low accuracy to match.
///
/// `forceLocationManager` uses the platform's own LocationManager instead of
/// the Google Play Services fused provider. The fused provider triggers a
/// "your device will need to use Location Accuracy" prompt, and declining it
/// yields **no fix at all** — an absurd outcome for an app that only wants an
/// approximate one. It also drops the Play Services dependency, so location
/// works on devices that do not have them.
static LocationSettings get _settings {
if (defaultTargetPlatform == TargetPlatform.android) {
return AndroidSettings(
accuracy: LocationAccuracy.low,
forceLocationManager: true,
timeLimit: const Duration(seconds: 20),
);
}
return const LocationSettings(
accuracy: LocationAccuracy.low,
timeLimit: Duration(seconds: 20),
);
}
@override
Future<LocationAvailability> availability() async {
if (!await Geolocator.isLocationServiceEnabled()) {
return LocationAvailability.serviceDisabled;
}
return _map(await Geolocator.checkPermission());
}
@override
Future<LocationAvailability> request() async {
if (!await Geolocator.isLocationServiceEnabled()) {
return LocationAvailability.serviceDisabled;
}
final current = await Geolocator.checkPermission();
if (current == LocationPermission.denied) {
return _map(await Geolocator.requestPermission());
}
return _map(current);
}
@override
Future<GeoPoint?> currentPoint() async {
if (await availability() != LocationAvailability.granted) return null;
try {
final position = await Geolocator.getCurrentPosition(
locationSettings: _settings,
);
return GeoPoint(position.longitude, position.latitude);
} on Object {
// A fresh fix can take seconds indoors, or time out entirely. For
// centring a map a slightly stale position is worth far more than
// nothing, so fall back to whatever the system already knows.
try {
final last = await Geolocator.getLastKnownPosition();
if (last == null) return null;
return GeoPoint(last.longitude, last.latitude);
} on Object {
return null;
}
}
}
static LocationAvailability _map(LocationPermission permission) =>
switch (permission) {
LocationPermission.always ||
LocationPermission.whileInUse => LocationAvailability.granted,
LocationPermission.deniedForever => LocationAvailability.deniedForever,
LocationPermission.denied ||
LocationPermission.unableToDetermine => LocationAvailability.denied,
};
}
final locationServiceProvider = Provider<LocationService>(
(ref) => const GeolocatorLocationService(),
);
/// Whether the user has seen the prominent disclosure in this session.
///
/// Google Play requires the explanation to come **before** the system dialog.
/// Session-scoped rather than persisted: showing it again after an app restart
/// costs one tap, and a user who forgot what they agreed to deserves to be
/// reminded.
class LocationDisclosureShown extends Notifier<bool> {
@override
bool build() => false;
void markShown() => state = true;
}
final locationDisclosureShownProvider =
NotifierProvider<LocationDisclosureShown, bool>(
LocationDisclosureShown.new,
);