Makes the coarse area human, per feedback that a typed code is jargon. - CoarseLocationProvider interface + geolocator-backed impl (BSD-3): low accuracy only, returns null on any denial/error (degrades quietly). Behind an interface so it's fakeable and the plugin stays at the composition root. - Config sheet: a 'Use my approximate location' button (shown only when a provider is wired) fills the area from the device position, reduced to a 5-char geohash via commons_core's Geohash — a precise fix never leaves the device. 'Couldn't get your location' on failure. - Threaded via TaneApp(location); main wires GeolocatorCoarseLocation. - Platform: ACCESS_COARSE_LOCATION (Android) + NSLocationWhenInUseUsageDescription (iOS), both scoped to the coarse-area use. - i18n en/es/pt. Widget test fills the area from a fake location. 8 tests green.
38 lines
1.5 KiB
Dart
38 lines
1.5 KiB
Dart
import 'package:geolocator/geolocator.dart';
|
|
|
|
/// Supplies the device's *approximate* location (or null when unavailable or
|
|
/// denied). Behind an interface so the UI is fakeable in tests and the concrete
|
|
/// platform plugin stays at the composition root. The caller immediately reduces
|
|
/// the result to a low-precision geohash — a precise fix never leaves the device.
|
|
abstract interface class CoarseLocationProvider {
|
|
Future<({double lat, double lon})?> currentCoarseLatLon();
|
|
}
|
|
|
|
/// [geolocator]-backed implementation. Requests low accuracy only, and returns
|
|
/// null on any denial/error rather than throwing, so the UI can degrade quietly.
|
|
class GeolocatorCoarseLocation implements CoarseLocationProvider {
|
|
const GeolocatorCoarseLocation();
|
|
|
|
@override
|
|
Future<({double lat, double lon})?> currentCoarseLatLon() async {
|
|
try {
|
|
if (!await Geolocator.isLocationServiceEnabled()) return null;
|
|
var permission = await Geolocator.checkPermission();
|
|
if (permission == LocationPermission.denied) {
|
|
permission = await Geolocator.requestPermission();
|
|
}
|
|
if (permission == LocationPermission.denied ||
|
|
permission == LocationPermission.deniedForever) {
|
|
return null;
|
|
}
|
|
final position = await Geolocator.getCurrentPosition(
|
|
locationSettings: const LocationSettings(
|
|
accuracy: LocationAccuracy.low,
|
|
),
|
|
);
|
|
return (lat: position.latitude, lon: position.longitude);
|
|
} catch (_) {
|
|
return null; // service off, plugin unsupported (desktop), timeout, …
|
|
}
|
|
}
|
|
}
|